/// <summary>
        /// Evolute the specified tableName with sqls.
        /// </summary>
        /// <param name='tableName'>
        /// Table name.
        /// </param>
        /// <param name='sqls'>
        /// Sqls.
        /// </param>
        public void Evolute(string tableName, List <string> sqls)
        {
            TableVersion version = GetTableVersion(tableName);
            var          db      = this.Database;

            if (RecreateTableIfHashDiffers && version.QueryHash != null)
            {
                string hash;
                if (sqls.Count > version.QueryIndex)
                {
                    hash = GetHash(sqls.GetRange(0, version.QueryIndex + 1));
                }
                else
                {
                    hash = "";
                }
                if (hash != version.QueryHash)
                {
                    Debug.Log(string.Format("Table hash error.OnDB:{0} Passed:{1}", version.QueryHash, hash));
                    DropTable(tableName);
                    version.QueryIndex = -1;
                    version.QueryHash  = null;
                }
            }

            bool updated      = false;
            int  successIndex = -1;
            bool error        = false;

            for (int i = version.QueryIndex + 1; i < sqls.Count; i++)
            {
                Debug.Log(string.Format("Execute query of {0}:{1}", tableName, i));
                updated = true;
                try {
                    db.ExecuteNonQuery(sqls[i]);
                    successIndex = i;
                } catch (SqliteException e) {
                    error = true;
                    Debug.Log("Error SQL:" + sqls[i]);
                    Debug.LogException(e);
                    break;
                }
            }

            if (successIndex >= 0)
            {
                sqls = sqls.GetRange(0, successIndex + 1);
                if (version.QueryHash == null)
                {
                    string hashCode = GetHash(sqls);
                    CreateRecord(tableName, sqls.Count - 1, hashCode);
                }
                else
                {
                    string hashCode = GetHash(sqls);
                    UpdateRecord(tableName, sqls.Count - 1, hashCode);
                }
            }

            if (error)
            {
                Debug.Log("There are some errors.Please fix your sql");
            }
        }
示例#2
0
        TableVersion GetTableVersion(string tableName)
        {
            string sql = @"SELECT * FROM MetaTable WHERE tableName = '" + tableName + "';";
            DataTable dt = Database.ExecuteQuery(sql);

            TableVersion tv = new TableVersion();
            if(dt.Rows.Count > 0){
                tv.TableName = tableName;
                var row = dt.Rows[0];
                tv.QueryIndex = row.GetAsInt("queryIndex");
                tv.QueryHash = row["queryHash"].ToString();
            }else{
                tv.TableName = tableName;
                tv.QueryIndex = -1;
                tv.QueryHash = null;
            }
            return tv;
        }