Exemple #1
0
        static int setListener(int L)
        {
            if (!LuaCommon.CheckAndShowArgsError(L, LConst.UserData, LConst.Function))
            {
                return(1);
            }

            Geolocator watcher = (Geolocator)Lua.Lua_touserdata(L, 2);

            if (watcher != null)
            {
                try
                {
                    if (_GeoChangedRegistry == null)
                    {
                        _GeoChangedRegistry = new List <KeyValuePair <Geolocator, int> >();
                    }

                    int callbackF = Lua.LuaL_ref(L, Lua._LUA_REGISTRYINDEX);
                    if (!_GeoChangedRegistry.Any(k => k.Key.Equals(watcher)))
                    {
                        watcher.PositionChanged += GeoChangedCallback4Lua;
                    }
                    _GeoChangedRegistry.Add(new KeyValuePair <Geolocator, int>(watcher, callbackF));

                    //watcher.Start();
                }
                catch
                {
                    LuaCommon.ShowError(null, "setListener failed @gps", "GPS");
                }
            }

            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);
        }