Sqlite problem

Discussion in 'Rust Development' started by Rizzok, Mar 4, 2016.

  1. Hi,
    Writing plugin, that working with sqlite db. Some code:
    Code:
           internal class Database
            {
                private readonly Ext.SQLite.Libraries.SQLite _sqLite = Interface.GetMod().GetLibrary<Ext.SQLite.Libraries.SQLite>();
                private Ext.SQLite.Connection con;
               
                public Database(string filename, Plugin p)
                {
                    this.con = _sqLite.OpenDb(filename, p, true);
                    this.CreateTables();
                }            public void Close()
                {
                    _sqLite.CloseDb(con);
                }
                private void CreateTables()
                {
                    this.Exec("CREATE TABLE IF NOT EXISTS blabla...so on", new object[0]);
                    //...
                }
                public int Exec(string query, params object[] args)
                {
                    Sql sql = new Sql(query, args);
                    int result;
                    _sqLite.ExecuteNonQuery(sql, con, i =>
                    {
                        result = i; //??????
                    });
                    return result;
                }
                //other methods  ...
            }
           
            private bool AddFriend(ulong playerId, ulong friendId)
            {
                int num;            Database database = this.db;
                object[] objArray = new object[] { playerId, friendId };
                num = database.Exec("INSERT OR IGNORE INTO friends (id, friend_id) VALUES (@0, @1)", objArray);            if (num <= 0)
                {
                    return false;
                }
                return true;
            }        [ChatCommand("friend")]
            public void cmdFriend(BasePlayer player, string cmd, string[] args)
            {
                // Checks... resolving... checks... and finally:
                done = AddFriend(PlayerID, targetPlayerID);
                // ....
            }
    and code will not work, and even compile, cos database query is called async?
    Any suggestions how I can report to user result of db action? Store somehow Baseplayer and inform him with callback? Or any other nice method?

    And to Oxide devs: why not to make in Oxide.Ext.SQLite.Connection: internal SQLiteConnection Con { get; set; } pulbicaly readable? So we can use standard library functions of System.Data.SQLite to make queries synchronously or commands like: SQLiteCommand.ExecuteScalar?
    [DOUBLEPOST=1457103591,1457030297][/DOUBLEPOST]@Wulf @Mughisi any comment on second part of topic, and maybe first? :)
     
  2. Wulf

    Wulf Community Admin

    If the plugin is not compiling, please provide the log with the errors.

    Everything is wrapped the way it is to prevent security issues, as some of those functions expose System.IO access I believe.
     
  3. @Wulf
    I think, that after connection is opened, you can't do anything other, then query... (maybe I'm wrong)...
    I can't understand, how to write code, using Oxide wrapped SQLite to do query without callbacks? I need returned data from query in same context (same way when I'm using data files)...
    And for compile errors? Look at this http://puu.sh/nxQAO/6096b7ba3d.png Will it compile by oxide compiler, if VS telling me, that result var declared, but not initialized?
     
  4. sqlite is intended to be async and you won't be able to return a value that way, you need to pass your callback and handle the result in there...
    have you checked the sqlite example? SQLite Extension | Oxide
    your expression lambda is the callback, you can also use a method there if you don't want a anonymous callback...

    sync queries won't be added since you would easily create lags on the gameserver.
     
  5. Thanks for reply. I know about delegates, lambda func and checked documentation before ask questions... Do you have statistics, how many plugins using SQLite or MySQL? I feel zero or very near it. SQL makes a lot easier to maintain data, update it and query... 300+ lines of code to save a bunch of player data in files, or 100 or less to manage it in sql... I understand, why MySQL need to be async (network delay, host delay etc)... But SQLite? Where we can make delays in SQLite? Complex query? I think plugin devs can shoot himself in the leg in many other ways (i.e. create lag on server)...
    Nevermind, I'm simply upset, that I can't manage data in way I need... Returning to old good json data files...