Esempio n. 1
0
    public HotFileDatabaseIO(string key, string filePath)
    {
        //初始化数据连接
        if (mConn == null)
        {
            //Debug.Log(Application.persistentDataPath);
            if (DataBaseName == "")
            {
                DataBaseName = "GameDB.db";
            }
#if UNITY_ANDROID
            string dataPath = Application.persistentDataPath + "/DataFiles";
#else
            string dataPath = System.Environment.CurrentDirectory + "/DataFiles";
#endif
            //Debug.Log("dataPath=" + dataPath);
            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }

#if UNITY_ANDROID
            mConn = new SqliteConnection("URI=file:" + dataPath + "/" + DataBaseName + ";");
#else
            mConn = new SqliteConnection("Data Source = DataFiles/" + DataBaseName + ";");
#endif
            mConn.Open();

            if (mCMD == null)
            {
                mCMD = mConn.CreateCommand();
                mCMD.Parameters.Add(new SqliteParameter());
            }

            mCMD.CommandText = "PRAGMA journal_mode =wal;";//wal模式
            mCMD.ExecuteNonQuery();
        }



        mKey    = key;
        mDBType = GetDBType();
        //创建表

        mCMD.CommandText = string.Format("CREATE TABLE IF NOT EXISTS {0:s}(Val {1:s} KEY DEFAULT {2:s});", mKey, GetDBTypeString(), GetDefaultDBValString());
        mCMD.ExecuteNonQuery();

        //初始化插入一行
        mCMD.CommandText = " SELECT COUNT(*) FROM " + mKey + ";";
        long rowNum = System.Convert.ToInt64(mCMD.ExecuteScalar());

        if (rowNum < 1)
        {
#if ENABLE_MASK
            if (typeof(ValueType) == typeof(int))
            {
                mCMD.CommandText = string.Format("INSERT INTO {0:s} (Val) VALUES ({1})", mKey, 0x7129A9AD);
            }
            else
            {
                mCMD.CommandText = string.Format("INSERT INTO {0:s} DEFAULT VALUES", mKey);
            }
#else
            mCMD.CommandText = string.Format("INSERT INTO {0:s} DEFAULT VALUES", mKey);
#endif

            mCMD.ExecuteNonQuery();
        }
//         if (typeof(ValueType) == typeof(string))
//             mUpdateSQLText = "UPDATE  " + mKey + " SET Val = '?';";
//         else
        mUpdateSQLText = "UPDATE " + mKey + " SET Val = ?;";

        ++SqliteConnRefCount;
        HotFileDBIOReleaser.HotFileDBReg(this);
    }
Esempio n. 2
0
    public HotFileDbFRAMIO(string key, string filePath)
    {
        if (GameMain.Singleton != null)
        {
            mMCU = GameMain.Singleton.ArcIO;
        }
        if (mMCU == null)
        {
            mMCU = INemoControlIOSinglton.Get();
        }
        //?????????????
        if (mConn == null)
        {
            if (DataBaseName == "")
            {
                DataBaseName = "GameDB.db";
            }

            string dataPath = System.Environment.CurrentDirectory + "/DataFiles";
            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }


            mConn = new SqliteConnection("Data Source = DataFiles/" + DataBaseName + ";");
            mConn.Open();

            if (mCMD == null)
            {
                mCMD = mConn.CreateCommand();
                mCMD.Parameters.Add(new SqliteParameter());
            }

            mCMD.CommandText = "PRAGMA journal_mode =wal;";//wal??
            mCMD.ExecuteNonQuery();
        }



        mKey    = key;
        mDBType = GetDBType();
        //bool isInitFRAMByDB = false;
        //?????DB_FARM_ID
        if (mMCU.IsOpen() && DB_ID == System.Guid.Empty)
        {
            using (SqliteCommand cmd = mConn.CreateCommand())
            {
                cmd.CommandText = "CREATE TABLE IF NOT EXISTS _DB_GUID(Val BLOB);";
                //cmd.Parameters.Add("@data", System.Data.DbType.Binary).Value = System.Guid.Empty.ToByteArray();
                cmd.ExecuteNonQuery();
                cmd.CommandText = string.Format(" SELECT * FROM _DB_GUID;");
                SqliteDataReader r = cmd.ExecuteReader();
                r.Read();

                if (r.HasRows)//????????????????
                {
                    //???????????guid?
                    DB_ID = new System.Guid(r.GetValue(0) as byte[]);
                    //???FRAM??guid?
                    byte[] guidFRAMByte = null;
                    if (mMCU.Read_Block(0, 16, out guidFRAMByte))//??FRAM???guid???
                    {
                        System.Guid FRAMEGUID = new System.Guid(guidFRAMByte);
                        if (FRAMEGUID != DB_ID)                                      //FRAM??db??guid?????
                        {
                            mMCU.RequestReadWrite(true, 0, 16, DB_ID.ToByteArray()); //??dbid§Õ??FRAM
                            IsInitFRAMByDB = true;
                        }
                    }
                    r.Close();
                }
                else//?????????guid
                {
                    r.Close();
                    //??????dbGUID
                    DB_ID = System.Guid.NewGuid();
                    byte[] guidByteNew = DB_ID.ToByteArray();
                    //????db
                    cmd.CommandText = "INSERT INTO _DB_GUID VALUES (@data)";
                    cmd.Parameters.Add("@data", System.Data.DbType.Binary).Value = guidByteNew;
                    cmd.ExecuteNonQuery();

                    //?????????guid ????FRAM
                    mMCU.RequestReadWrite(true, 0, 16, guidByteNew);
                    IsInitFRAMByDB = true;
                }
            }
        }


        //??????(4294967295 == 0xffffffff)
        mCMD.CommandText = string.Format("CREATE TABLE IF NOT EXISTS {0:s}(Val {1:s} KEY DEFAULT {2:s},Address INTEGER DEFAULT 4294967295,Length INTEGER DEFAULT {3:d});", mKey, GetDBTypeString(), GetDefaultDBValString(), GetValLength());
        mCMD.ExecuteNonQuery();


        //???????????????
        mCMD.CommandText = string.Format(" SELECT * FROM {0:s};", mKey);
        SqliteDataReader reader = mCMD.ExecuteReader();

        reader.Read();

        //
        //System.Guid.NewGuid();

        if (reader.HasRows)//????????????????
        {
            //????????????????????????
            mAddress    = (uint)reader.GetInt64(1);
            mDataLength = (byte)reader.GetInt32(2);
            if (IsInitFRAMByDB)
            {
                WriteToFRAM(ObjectToVal(reader.GetValue(0)));
            }
            reader.Close();
        }
        else//????????????????,????????
        {
            mAddress    = CurrentUseAddress;
            mDataLength = (byte)GetValLength();
            reader.Close();

            //???????????
            mCMD.CommandText   = string.Format("INSERT INTO {0:s} VALUES({1:s},{2:d},{3:d})", mKey, GetDefaultDBValString(), mAddress, mDataLength);
            CurrentUseAddress += GetValLength();//???????
            mCMD.ExecuteNonQuery();

            //?????farm
            WriteToFRAM(GetDefaultVal());
        }

        mUpdateSQLText = "UPDATE " + mKey + " SET Val = ?;";

        ++SqliteConnRefCount;
        HotFileDBIOReleaser.HotFileDBReg(this);
        HotFileDBFRAM_Updater.Reg(this);
    }