Exemplo n.º 1
0
        /// <summary>
        /// Initializes.
        /// </summary>
        private void Initialize()
        {
            preferencesData = Resources.Load <DataTablePreferences>("DataTablePreferences");
            databasePath    = Path.Combine(Application.persistentDataPath, DataTablesStorageFolderName);

            if (preferencesData && preferencesData.DataTablesStorageLocation == DataTableStorageLocation.StreamingAssetsPath)
            {
                databasePath = Path.Combine(Application.streamingAssetsPath, DataTablesStorageFolderName);
            }

            if (preferencesData && preferencesData.DataTablesStorageLocation == DataTableStorageLocation.ResourcesPath)
            {
                TextAsset binAsset = Resources.Load <TextAsset>(DataTablesStorageFolderName + "/db1");

                if (binAsset)
                {
                    addressMapDBAdapter = new BoxDBAdapter(databasePath, binAsset.bytes);
                }
            }
            else
            {
                addressMapDBAdapter = new BoxDBAdapter(databasePath);
            }

            addressMapDBAdapter.EnsureTable <DataTableAddressMap>(typeof(DataTableAddressMap).Name, DataTableAddressMap.PrimaryKey);
            addressMapDBAdapter.Open();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the object of data table row.
        /// </summary>
        /// <typeparam name="T">The type definition of data table row.</typeparam>
        /// <param name="primaryValue">The primary value.</param>
        /// <returns>The object of type definition.</returns>
        public T GetDataTableRow <T>(object primaryValue) where T : DataTableRow, new()
        {
            DataTableAddressMap addressMap = GetDatabaseAddressMap <T>();
            BoxDBAdapter        dbAdapter  = GetDatabaseBoxAdapter(addressMap);
            T data = default(T);

            if (dbAdapter != null)
            {
                try
                {
                    string tableName = addressMap.Type;
                    dbAdapter.EnsureTable <T>(tableName, addressMap.PrimaryPropertyName);
                    dbAdapter.Open();
                    data = dbAdapter.Select <T>(tableName, primaryValue);
                }
                catch (Exception exception)
                {
                    Debug.LogException(exception);
                }
                finally
                {
                    if (dbAdapter != null)
                    {
                        dbAdapter.Dispose();
                    }
                }
            }

            return(data);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets all data table rows.
        /// </summary>
        /// <typeparam name="T">The type definition of data table row.</typeparam>
        /// <returns>The object array of type definition.</returns>
        public T[] GetAllDataTableRows <T>() where T : DataTableRow, new()
        {
            DataTableAddressMap addressMap = GetDatabaseAddressMap <T>();
            BoxDBAdapter        dbAdapter  = GetDatabaseBoxAdapter(addressMap);
            List <T>            results    = new List <T>();

            if (dbAdapter != null)
            {
                try
                {
                    string tableName = addressMap.Type;
                    dbAdapter.EnsureTable <T>(tableName, addressMap.PrimaryPropertyName);
                    dbAdapter.Open();
                    results = dbAdapter.SelectAll <T>(tableName);
                }
                catch (Exception exception)
                {
                    Debug.LogException(exception);
                }
                finally
                {
                    dbAdapter.Dispose();
                }
            }

            return(results.ToArray());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the data table row count.
        /// </summary>
        /// <typeparam name="T">The type definition of data table row.</typeparam>
        /// <param name="conditions">The conditions.</param>
        /// <param name="multiConditionOperators">The multi condition operators.</param>
        /// <returns>The data table row count.</returns>
        public long GetDataTableRowsCount <T>(List <BoxDBQueryCondition> conditions,
                                              List <BoxDBMultiConditionOperator> multiConditionOperators = null) where T : DataTableRow, new()
        {
            DataTableAddressMap addressMap = GetDatabaseAddressMap <T>();
            BoxDBAdapter        dbAdapter  = GetDatabaseBoxAdapter(addressMap);
            long count = 0;

            if (dbAdapter != null)
            {
                try
                {
                    string tableName = addressMap.Type;
                    dbAdapter.EnsureTable <T>(tableName, addressMap.PrimaryPropertyName);
                    dbAdapter.Open();
                    count = dbAdapter.SelectCount(tableName, conditions, multiConditionOperators);
                }
                catch (Exception exception)
                {
                    Debug.LogException(exception);
                }
                finally
                {
                    dbAdapter.Dispose();
                }
            }

            return(count);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the box database adapter.
        /// </summary>
        /// <returns>The BoxDBAdapter object.</returns>
        private BoxDBAdapter GetBoxDBAdapter()
        {
            BoxDBAdapter db = new BoxDBAdapter(Application.persistentDataPath);

            db.EnsureTable <BoxDBAdapterTestVO>(tableName, "Id");
            db.Open();
            return(db);
        }
Exemplo n.º 6
0
        public void SelectCountTest()
        {
            BoxDBAdapter db    = GetBoxDBAdapter();
            long         count = db.SelectCount(tableName);

            db.Dispose();
            Assert.AreEqual(3, count);
        }
Exemplo n.º 7
0
        public void SelectTest()
        {
            BoxDBAdapter       db = GetBoxDBAdapter();
            BoxDBAdapterTestVO vo = db.Select <BoxDBAdapterTestVO>(tableName, 1);

            Debug.Log(vo.ToString());
            db.Dispose();
            Assert.IsNotNull(vo);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Releases unmanaged and - optionally - managed resources.
 /// </summary>
 /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (addressMapDBAdapter != null)
         {
             addressMapDBAdapter.Dispose();
             addressMapDBAdapter = null;
         }
     }
 }
Exemplo n.º 9
0
        public void SelectAllTest()
        {
            BoxDBAdapter db = GetBoxDBAdapter();
            List <BoxDBAdapterTestVO> list = db.SelectAll <BoxDBAdapterTestVO>(tableName);

            foreach (BoxDBAdapterTestVO vo in list)
            {
                Debug.Log(vo.ToString());
            }

            db.Dispose();
            Assert.AreEqual(3, list.Count);
        }
Exemplo n.º 10
0
        public void InsertTest()
        {
            BoxDBAdapter db = GetBoxDBAdapter();

            BoxDBAdapterTestVO[] voList = new BoxDBAdapterTestVO[3]
            {
                new BoxDBAdapterTestVO((int)db.MakeNewId(), "test1", 1f),
                new BoxDBAdapterTestVO((int)db.MakeNewId(), "测试2", 2f),
                new BoxDBAdapterTestVO((int)db.MakeNewId(), "test3", 3f)
            };
            bool success = db.Insert(tableName, voList);

            db.Dispose();
            Assert.IsTrue(success);
        }
Exemplo n.º 11
0
        public void DeleteTest()
        {
            BoxDBAdapter db = GetBoxDBAdapter();

            BoxDBAdapterTestVO[] voList = new BoxDBAdapterTestVO[]
            {
                new BoxDBAdapterTestVO(9999, "test9999", 9999f)
            };

            db.Insert(tableName, voList);
            bool success = db.Delete(tableName, 9999);

            db.Dispose();
            Assert.IsTrue(success);
        }
Exemplo n.º 12
0
        public void MuiltiConditionsSelectTest()
        {
            BoxDBAdapter db = GetBoxDBAdapter();
            List <BoxDBQueryCondition> list = new List <BoxDBQueryCondition>()
            {
                new BoxDBQueryCondition("Id", 1),
                new BoxDBQueryCondition("Id", 2)
            };

            List <BoxDBAdapterTestVO> result = db.Select <BoxDBAdapterTestVO>(tableName, list,
                                                                              new List <BoxDBMultiConditionOperator>(new BoxDBMultiConditionOperator[1] {
                BoxDBMultiConditionOperator.Or
            }));

            db.Dispose();
            Assert.AreEqual(2f, result[0].Value);
        }
Exemplo n.º 13
0
        public void MuiltiConditionsSelectCountTest()
        {
            BoxDBAdapter db = GetBoxDBAdapter();
            List <BoxDBQueryCondition> list = new List <BoxDBQueryCondition>()
            {
                new BoxDBQueryCondition("Id", 1),
                new BoxDBQueryCondition("Id", 2)
            };

            long count = db.SelectCount(tableName, list,
                                        new List <BoxDBMultiConditionOperator>(new BoxDBMultiConditionOperator[1] {
                BoxDBMultiConditionOperator.Or
            }));

            db.Dispose();
            Assert.AreEqual(2, count);
        }
Exemplo n.º 14
0
        public void UpdateTest()
        {
            BoxDBAdapter db = GetBoxDBAdapter();

            BoxDBAdapterTestVO[] voList = new BoxDBAdapterTestVO[3]
            {
                new BoxDBAdapterTestVO(1, "test1_update", 1f),
                new BoxDBAdapterTestVO(2, "测试2_update", 2f),
                new BoxDBAdapterTestVO(4, "test3_update", 3f)
            };
            db.Update(tableName, voList);

            BoxDBAdapterTestVO vo = db.Select <BoxDBAdapterTestVO>(tableName, 1);

            db.Dispose();

            Assert.AreEqual("test1_update", vo.Name);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Gets the adapter of database.
        /// </summary>
        /// <param name="addressMap">The object of DataTableAddressMap.</param>
        /// <returns>The adapter of database.</returns>
        private BoxDBAdapter GetDatabaseBoxAdapter(DataTableAddressMap addressMap)
        {
            BoxDBAdapter dbAdapter = null;

            if (addressMap != null && addressMap.LocalAddress > 1)
            {
                if (preferencesData.DataTablesStorageLocation == DataTableStorageLocation.ResourcesPath)
                {
                    TextAsset binAsset = Resources.Load <TextAsset>(DataTablesStorageFolderName + "/db" + addressMap.LocalAddress);

                    if (binAsset)
                    {
                        dbAdapter = new BoxDBAdapter(databasePath, binAsset.bytes);
                    }
                }
                else
                {
                    dbAdapter = new BoxDBAdapter(databasePath, addressMap.LocalAddress);
                }
            }

            return(dbAdapter);
        }