static int deleteData(int L) { if (!LuaCommon.CheckAndShowArgsError(L, LConst.String)) return 0; String key = Lua.Lua_tostring(L, 2).ToString(); bool result = RYTDatabase.DeleteData(key, null); Lua.Lua_pushboolean(L, result ? 1 : 0); return 0; }
/// <summary> /// 更新键值,如果不存在则添加,此接口已在5.2.48_B 以后废弃 /// </summary> /// <returns></returns> static int updateDate(int L) { if (!LuaCommon.CheckAndShowArgsError(L, LConst.String, LConst.String)) return 0; string key = Lua.Lua_tostring(L, 2); string value = Lua.Lua_tostring(L, 3); RYTDatabase.UpdateData(key, value,null,null); return 0; }
static int put(int L) { if (!LuaCommon.CheckAndShowArgsError(L, LConst.String, LConst.LuaData)) { return(0); } string key = Lua.Lua_tostring(L, 2); object value; if (Lua.Lua_isstring(L, 3)) { value = Lua.Lua_tostring(L, 3); } else if (Lua.Lua_isbool(L, 3)) { value = Lua.Lua_toboolean(L, 3); } else if (Lua.Lua_isnumber(L, 3)) { value = Lua.Lua_tointeger(L, 3); } else { List <KeyValuePair <string, string> > kvList = new List <KeyValuePair <string, string> >(); Lua.Lua_pushnil(L); while (Lua.Lua_next(L, -2) != 0) { String tvalue = Lua.Lua_tostring(L, -1).ToString(); String tkey = Lua.Lua_tostring(L, -2).ToString(); kvList.Add(new KeyValuePair <string, string>(tkey, tvalue)); Lua.Lua_pop(L, 1); } value = kvList; } bool result = false; // kvy 值为空不添加 if (!string.IsNullOrEmpty(key)) { result = RYTDatabase.PutKVData(key, value); } if (result) { Lua.Lua_pushboolean(L, 1); } else { Lua.Lua_pushboolean(L, 0); } return(1); }
static int close(int L) { if (!LuaCommon.CheckAndShowArgsError(L, LConst.UserData)) return 0; try { SQLiteConnection db = Lua.Lua_topermanentuserdata(L, 2) as SQLiteConnection; RYTDatabase.Close(db.Database); } catch { } return 0; }
static int get(int L) { if (!LuaCommon.CheckAndShowArgsError(L, LConst.String)) { return(0); } string key = Lua.Lua_tostring(L, 2); var value = RYTDatabase.GetKVData(key); if (value != null) { if (value is string) { Lua.Lua_pushstring(L, value.ToString()); } else if (value is double) { Lua.Lua_pushnumber(L, (double)value); } else if (value is Boolean) { int result = (int)value; Lua.Lua_pushboolean(L, result); } else { List <KeyValuePair <string, string> > list = value as List <KeyValuePair <string, string> >; if (list != null && list.Count > 0) { Lua.Lua_newtable(L); for (int i = 0; i < list.Count; i++) { LuaManager.PushValueByType(L, list[i].Key); LuaManager.PushValueByType(L, list[i].Value); Lua.Lua_rawset(L, -3); } } } } else { Lua.Lua_pushnil(L); } return(1); }
static int addData(int L) { if (!LuaCommon.CheckAndShowArgsError(L, LConst.String, LConst.String)) return 0; String key = Lua.Lua_tostring(L, 2); String value = string.Empty; if (!Lua.Lua_isnil(L, 3)) { value = Lua.Lua_tostring(L, 3); } bool result = RYTDatabase.AddData(key, value, null, null); Lua.Lua_pushboolean(L, result ? 1 : 0); return 0; }
static int del(int L) { if (!LuaCommon.CheckAndShowArgsError(L, LConst.String)) { return(0); } string key = Lua.Lua_tostring(L, 2); bool result = RYTDatabase.DelKVData(key); if (result) { Lua.Lua_pushboolean(L, 1); } else { Lua.Lua_pushboolean(L, 0); } return(1); }
// 数据库名必须加.sql static int open(int L) { if (!LuaCommon.CheckAndShowArgsError(L, LConst.String)) return 0; String dbName = Lua.Lua_tostring(L, 2).ToString(); if (!string.IsNullOrEmpty(dbName)) { SQLiteConnection db = RYTDatabase.OpenDB(dbName); Lua.Lua_pushpermanentuserdata(L, db); } else { Lua.Lua_pushnil(L); } return 1; }
static int getData(int L) { if (!LuaCommon.CheckAndShowArgsError(L, LConst.String)) return 0; String key = Lua.Lua_tostring(L, 2).ToString(); var value = RYTDatabase.GetData(key, null, null); if (value != null) { Lua.Lua_pushstring(L, value); } else { Lua.Lua_pushnil(L); } return 1; }
static int exec(int L) { if (!LuaCommon.CheckAndShowArgsError(L, LConst.UserData, LConst.String)) { return(0); } SQLiteConnection db = Lua.Lua_topermanentuserdata(L, 2) as SQLiteConnection; String sql = Lua.Lua_tostring(L, 3).Trim(); try { if (db != null && sql != null) { Action <List <List <KeyValuePair <string, object> > > > action = (list) => { if (sql.StartsWith("select", StringComparison.CurrentCultureIgnoreCase)) { Lua.Lua_newtable(L); if (list != null && list.Count > 0) { for (int i = 0; i < list.Count; i++) { Lua.Lua_pushnumber(L, i + 1); Lua.Lua_newtable(L); foreach (var kv in list[i]) { LuaManager.PushValueByType(L, kv.Key); LuaManager.PushValueByType(L, kv.Value); Lua.Lua_rawset(L, -3); } Lua.Lua_rawset(L, -3); } } } else { Lua.Lua_pushnil(L); } }; RYTDatabase.ExecuteSQL(db.Database, sql, action, null, null); //SQLiteCommand cmd = db.CreateCommand(sql); //if (sql.StartsWith("select", StringComparison.CurrentCultureIgnoreCase)) //{ // List<List<KeyValuePair<string, object>>> list = cmd.ExecuteQueryAndReturnTable(); // if (list.Count > 0) // { // Lua.Lua_newtable(L); // for (int i = 0; i < list.Count; i++) // { // Lua.Lua_pushnumber(L, i + 1); // Lua.Lua_newtable(L); // foreach (var kv in list[i]) // { // LuaManager.PushValueByType(L, lua, kv.Key); // LuaManager.PushValueByType(L, lua, kv.Value); // Lua.Lua_rawset(L, -3); // } // Lua.Lua_rawset(L, -3); // } // return 1; // } //} //else //{ // int result = cmd.ExecuteNonQuery(); // Lua.Lua_pushnil(L); //} } else { Lua.Lua_pushnil(L); } } catch (Exception e) { if (sql.StartsWith("select", StringComparison.CurrentCultureIgnoreCase)) { Lua.Lua_newtable(L); } else { Lua.Lua_pushnil(L); } Debug.WriteLine("DB Exception: " + e.Message); LuaCommon.ShowError(null, e.Message, "DB Exception:"); } return(1); }