Esempio n. 1
0
        /*public SQLiteAsyncConnection GetDatabaseAsync() {
         *      return new SQLiteAsyncConnection(dbPath);
         * }*/

        public void OnCreate(SQLiteConnection database)
        {
            SQLConsole.WriteLine("Creating database file...");
            SQLConsole.WriteLine(" - Name: " + Configuration.DATABASE_NAME);
            SQLConsole.WriteLine(" - Version: " + Configuration.DATABASE_VERSION);
            SQLConsole.WriteLine(" - Create: " + Configuration.DATABASE_CREATE);
            if (Configuration.DATABASE_CREATE.Equals("create") || Configuration.DATABASE_CREATE.Equals("drop-create"))
            {
                database.Execute("PRAGMA FOREIGN_KEYS = OFF");                //validateDatabase(database);
                try {
                    List <TypeInfo> types = Reflections.GetTypesFromAssembly(Configuration.DOMAIN_PACKAGE);
                    foreach (TypeInfo type in types)
                    {
                        if (type.IsSubclassOf(typeof(PersistentEntity)))
                        {
                            PersistentEntity entity = (PersistentEntity)Activator.CreateInstance(type.AsType());

                            //String query = (String)typeof(QueryGenerator<PersistentEntity>).GetTypeInfo().MakeGenericType(type.AsType()).GetTypeInfo().GetDeclaredMethod("CreateTableQuery").Invoke(null, new object[] { entity.GetTableData() });
                            //Execute(QueryGenerator<PersistentEntity>.CreateTableQuery((entity.GetTableData<PersistentEntity>()).GetTableMapping()));
                            dynamic tableData = entity.GetTableData();                            //GetType().GetTypeInfo().GetDeclaredMethod("GetTableData").MakeGenericMethod(entity.GetType()).Invoke(entity, null);
                            //Reflections.CastTo();

                            String query = (String)typeof(QueryGenerator <>).GetTypeInfo().MakeGenericType(type.AsType()).GetTypeInfo().GetDeclaredMethod("CreateTableQuery").Invoke(null, new object[] { tableData.GetTableMapping() });
                            //QueryGenerator<PersistentEntity>.CreateTableQuery(tableData);
                            //dynamic tableData = (EntityManager<PersistentEntity>)Convert.ChangeType(entity.GetType().GetTypeInfo().GetDeclaredMethod("GetTableData").MakeGenericMethod(entity.GetType()).Invoke(entity, null), typeof(EntityManager<PersistentEntity>));
                            Execute(query);
                        }
                    }
                } catch (System.IO.FileNotFoundException e) {
                    Debug.WriteLine(e.ToString());
                }
                database.Execute("PRAGMA FOREIGN_KEYS = ON");
            }
        }
Esempio n. 2
0
        public void OnUpgrade(SQLiteConnection database, int oldVersion, int newVersion)
        {
            SQLConsole.WriteLine("Updating database file...");
            SQLConsole.WriteLine(" - Name: " + Configuration.DATABASE_NAME);
            SQLConsole.WriteLine(" - Version: " + oldVersion);

            if (Configuration.DATABASE_CREATE.Equals("drop-create"))
            {
                database.Execute("PRAGMA FOREIGN_KEYS = OFF");
                try {
                    List <TypeInfo> types = Reflections.GetTypesFromAssembly(Configuration.DOMAIN_PACKAGE);
                    foreach (TypeInfo type in types)
                    {
                        if (type.IsSubclassOf(typeof(PersistentEntity)))
                        {
                            PersistentEntity entity = (PersistentEntity)Activator.CreateInstance(type.AsType());

                            //String query = (String)typeof(QueryGenerator<PersistentEntity>).GetTypeInfo().MakeGenericType(type.AsType()).GetTypeInfo().GetDeclaredMethod("CreateTableQuery").Invoke(null, new object[] { entity.GetTableData() });
                            //Execute(QueryGenerator<PersistentEntity>.CreateTableQuery((entity.GetTableData<PersistentEntity>()).GetTableMapping()));
                            dynamic tableData = entity.GetTableData();                            //GetType().GetTypeInfo().GetDeclaredMethod("GetTableData").MakeGenericMethod(entity.GetType()).Invoke(entity, null);
                            //Reflections.CastTo();

                            String query = (String)typeof(QueryGenerator <>).GetTypeInfo().MakeGenericType(type.AsType()).GetTypeInfo().GetDeclaredMethod("DropTableQuery").Invoke(null, new object[] { tableData.GetTableMapping() });
                            //QueryGenerator<PersistentEntity>.CreateTableQuery(tableData);
                            //dynamic tableData = (EntityManager<PersistentEntity>)Convert.ChangeType(entity.GetType().GetTypeInfo().GetDeclaredMethod("GetTableData").MakeGenericMethod(entity.GetType()).Invoke(entity, null), typeof(EntityManager<PersistentEntity>));
                            Execute(query);
                        }
                    }
                } catch (System.IO.FileNotFoundException e) {
                    Debug.WriteLine(e.ToString());
                }
            }
            else if (Configuration.DATABASE_CREATE.Equals("update"))
            {
                database.Execute("PRAGMA FOREIGN_KEYS = OFF");

                /*ArrayList<Table> newTables = getDomainStructure();
                 * ArrayList<Table> oldTables = getDatabaseStructure(database);
                 * for (Table oldTable : oldTables) {
                 *      if (!newTables.contains(oldTable)) {
                 *              String dropTableQuery = "DROP TABLE IF EXISTS " + oldTable.name;
                 *              SQLConsole.Log(dropTableQuery);
                 *              database.execSQL(dropTableQuery);
                 *      } else {
                 *              // TODO: comprobar campos
                 *      }
                 * }
                 * for (Table newTable : newTables) {
                 *      if (!oldTables.contains(newTable)) {
                 *              String createTableQuery = SQLQueryGenerator.getCreateTable(newTable.type);
                 *              SQLConsole.Log(createTableQuery);
                 *              database.execSQL(createTableQuery);
                 *      }
                 * }*/
                database.Execute("PRAGMA FOREIGN_KEYS = ON");

                //validateDatabase(database);
            }

            OnCreate(database);
        }