diff options
-rw-r--r-- | plugins/MirLua/docs/examples/database.lua | 28 | ||||
-rw-r--r-- | plugins/MirLua/src/m_database.cpp | 127 | ||||
-rw-r--r-- | plugins/MirLua/src/mlua.cpp | 1 | ||||
-rw-r--r-- | plugins/MirLua/src/stdafx.h | 4 |
4 files changed, 160 insertions, 0 deletions
diff --git a/plugins/MirLua/docs/examples/database.lua b/plugins/MirLua/docs/examples/database.lua new file mode 100644 index 0000000000..42625a4f64 --- /dev/null +++ b/plugins/MirLua/docs/examples/database.lua @@ -0,0 +1,28 @@ +--- include m_database module +local db = require('m_database') + +--- Save value to database +-- @param hContact The handle of contact (can be NULL) +-- @param module The name of section +-- @param setting The name of setting +-- @return value The value +db.WriteContactSetting(nil, 'MirLua', 'testByte', true) +db.WriteContactSetting(nil, 'MirLua', 'testNum', -2342) +db.WriteContactSetting(nil, 'MirLua', 'testString', "Hello!") + +--- Return value from database +-- @param hContact The handle of contact (can be NULL) +-- @param module The name of section +-- @param setting The name of setting +local str = db.GetContactSetting(nil, 'MirLua', 'testString'); + +-- print string value if bool value is true +if db.GetContactSetting(nil, 'MirLua', 'testByte') then + print(str) + end + +--- Delete value from database +-- @param hContact The handle of contact (can be NULL) +-- @param module The name of section +-- @param setting The name of setting +db.DeleteContactSetting(nil, 'MirLua', 'testString'); diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp new file mode 100644 index 0000000000..6adca91ecc --- /dev/null +++ b/plugins/MirLua/src/m_database.cpp @@ -0,0 +1,127 @@ +#include "stdafx.h"
+
+static int lua_FindFirstContact(lua_State *L)
+{
+ MCONTACT hContact = db_find_first(lua_tostring(L, 1));
+ lua_pushinteger(L, hContact);
+
+ return 1;
+}
+
+static int lua_FindNextContact(lua_State *L)
+{
+ MCONTACT hContact = db_find_next(luaL_checkinteger(L, 1), lua_tostring(L, 2));
+ lua_pushinteger(L, hContact);
+
+ return 1;
+}
+
+static int lua_WriteContactSetting(lua_State *L)
+{
+ MCONTACT hContact = lua_tointeger(L, 1);
+ LPCSTR szModule = luaL_checkstring(L, 2);
+ LPCSTR szSetting = luaL_checkstring(L, 3);
+
+ DBVARIANT dbv = { 0 };
+ int type = lua_type(L, 4);
+ switch (type)
+ {
+ case LUA_TBOOLEAN:
+ dbv.bVal = lua_toboolean(L, 4);
+ dbv.type = DBVT_BYTE;
+ break;
+ case LUA_TNUMBER:
+ dbv.dVal = lua_tonumber(L, 4);
+ dbv.type = DBVT_DWORD;
+ break;
+ case LUA_TSTRING:
+ dbv.pszVal = (char*)lua_tostring(L, 4);
+ dbv.type = DBVT_UTF8;
+ break;
+
+ default:
+ lua_pushinteger(L, hContact);
+ return 1;
+ }
+
+ INT_PTR res = db_set(hContact, szModule, szSetting, &dbv);
+ lua_pushinteger(L, res);
+
+ return 1;
+}
+
+static int lua_GetContactSetting(lua_State *L)
+{
+ MCONTACT hContact = lua_tointeger(L, 1);
+ LPCSTR szModule = luaL_checkstring(L, 2);
+ LPCSTR szSetting = luaL_checkstring(L, 3);
+
+ DBVARIANT dbv;
+ if (db_get(hContact, szModule, szSetting, &dbv))
+ {
+ lua_pushnil(L);
+ return 1;
+ }
+
+
+ switch (dbv.type)
+ {
+ case DBVT_BYTE:
+ lua_pushboolean(L, dbv.bVal);
+ break;
+ case DBVT_WORD:
+ lua_pushinteger(L, dbv.wVal);
+ case DBVT_DWORD:
+ lua_pushnumber(L, dbv.dVal);
+ break;
+ case DBVT_ASCIIZ:
+ lua_pushstring(L, ptrA(mir_utf8encode(dbv.pszVal)));
+ break;
+ case DBVT_UTF8:
+ lua_pushstring(L, dbv.pszVal);
+ break;
+ case DBVT_WCHAR:
+ lua_pushstring(L, ptrA(mir_utf8encodeW(dbv.pwszVal)));
+ break;
+
+ default:
+ db_free(&dbv);
+ lua_pushnil(L);
+ return 1;
+ }
+
+ db_free(&dbv);
+
+ return 1;
+}
+
+static int lua_DeleteContactSetting(lua_State *L)
+{
+ MCONTACT hContact = lua_tointeger(L, 1);
+ LPCSTR szModule = luaL_checkstring(L, 2);
+ LPCSTR szSetting = luaL_checkstring(L, 3);
+
+ INT_PTR res = db_unset(hContact, szModule, szSetting);
+ lua_pushinteger(L, res);
+
+ return 1;
+}
+
+static luaL_Reg databaseApi[] =
+{
+ { "FindFirstContact", lua_FindFirstContact },
+ { "FindNextContact", lua_FindNextContact },
+
+ { "WriteContactSetting", lua_WriteContactSetting },
+ { "GetContactSetting", lua_GetContactSetting },
+ { "DeleteContactSetting", lua_DeleteContactSetting },
+
+ { NULL, NULL }
+};
+
+LUAMOD_API int luaopen_m_database(lua_State *L)
+{
+ luaL_newlib(L, databaseApi);
+
+ return 1;
+}
diff --git a/plugins/MirLua/src/mlua.cpp b/plugins/MirLua/src/mlua.cpp index c600ad058e..2b9ea1fd92 100644 --- a/plugins/MirLua/src/mlua.cpp +++ b/plugins/MirLua/src/mlua.cpp @@ -15,6 +15,7 @@ CMLua::CMLua() luaL_newlib(L, coreLib);
lua_setglobal(L, "m");
+ Preload(MLUA_DATABASE, luaopen_m_database);
Preload(MLUA_ICOLIB, luaopen_m_icolib);
Preload(MLUA_GENMENU, luaopen_m_genmenu);
}
diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index 6d206f7644..f334968fb3 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -8,6 +8,7 @@ #include <m_utils.h>
#include <m_langpack.h>
#include <m_folders.h>
+#include <m_database.h>
#include <m_clist.h>
#include <m_genmenu.h>
#include <m_icolib.h>
@@ -38,6 +39,9 @@ extern HINSTANCE g_hInstance; #define CUSTOM_SCRIPTS_PATHT MIRANDA_USERDATA "\\Scripts"
#endif
+#define MLUA_DATABASE "m_database"
+LUAMOD_API int (luaopen_m_database)(lua_State *L);
+
#define MLUA_ICOLIB "m_icolib"
LUAMOD_API int (luaopen_m_icolib)(lua_State *L);
|