Exemplo n.º 1
0
        public override bool UpdateDatabaseSequence(IDbConnection connection, IDbTransaction trans, ExBackgroundWorker worker)
        {
            synch_worker = worker;

            String sql = SanitaUtility.GetResourceFileStreamText("update_sequence");

            if (!String.IsNullOrEmpty(sql))
            {
                DataTable mDataTable = myBaseDao.DoGetDataTable(connection, trans, sql);
                if (mDataTable != null)
                {
                    //Synch database
                    if (synch_worker != null)
                    {
                        synch_worker.ReportProgress(0, mDataTable.Rows.Count);
                    }
                    int index_sql = 0;
                    foreach (DataRow row in mDataTable.Rows)
                    {
                        if (row["sql_column"] != null)
                        {
                            String sql_update = row["sql_column"].ToString();
                            myBaseDao.DoUpdate(connection, trans, sql_update);
                        }

                        index_sql++;
                        synch_worker.ReportProgress(index_sql, mDataTable.Rows.Count);
                    }
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        public override bool SynchDatabase(IDbConnection connection, IDbTransaction trans, ExBackgroundWorker worker, int number_hosobenhan, bool IsCapNhatTrigger)
        {
            synch_worker = worker;

            //Get database name
            String DatabaseName = myBaseDao.GetDatabaseName();
            String file_name    = "Database\\" + DatabaseName + ".db";

            SanitaLogEx.d(TAG, "[SynchDatabase] Database = [" + DatabaseName + "]");

            //If have no database -> create datbase
            if (!File.Exists("Database\\" + DatabaseName + ".db"))
            {
                SanitaLog.e(TAG, "[SynchDatabase] Database chưa tồn tại");
                CreateDatabase(DatabaseName);
            }

            //Get all base table
            IList <ClassTable> listTable = GetListTable(connection, trans, DatabaseName);

            //Synch database
            if (synch_worker != null)
            {
                synch_worker.ReportProgress(0, listFixTable.Count);
            }
            for (int i = 0; i < listFixTable.Count; i++)
            {
                if (synch_worker != null)
                {
                    synch_worker.ReportProgress(i + 1, "Nâng cấp table '" + listFixTable[i].Table + "...");
                }

                //Convert mysql to sqlite
                for (int j = 0; j < listFixTable[i].listColumn.Count; j++)
                {
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("int(10) unsigned NOT NULL auto_increment", "INTEGER");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("int(10) unsigned", "INTEGER");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("int(10)", "INTEGER");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("int(11)", "INTEGER");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("text CHARACTER SET utf8 COLLATE utf8_unicode_ci", "text");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("longblob", "BLOB");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("double", "REAL");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("Datetime DEFAULT '1-1-1'", "TEXT");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("datetime DEFAULT '1-1-1'", "TEXT");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("Datetime", "TEXT");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("datetime", "TEXT");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP", "TEXT");
                }

                //Tim xem co table ton tai chua
                var foundTable = listTable.FirstOrDefault(p => p.Table.Equals(listFixTable[i].Table, StringComparison.CurrentCultureIgnoreCase));
                if (foundTable == null)
                {
                    //Create table
                    SanitaLogEx.d(TAG, "[SynchDatabase] Create table '" + listFixTable[i].Table + "'");
                    if (CreateTable(connection, trans, DatabaseName, listFixTable[i]) <= -100)
                    {
                        SanitaLogEx.e(TAG, "[SynchDatabase] Create table lỗi");
                        if (synch_worker != null)
                        {
                            synch_worker.ReportProgress(-1, "Tạo table '" + listFixTable[i].Table + "' lỗi !");
                        }
                        return(false);
                    }
                }
                else
                {
                    //Alter table
                    SanitaLogEx.e(TAG, "[SynchDatabase] Alter table '" + listFixTable[i].Table + "'");
                    if (AlterTableTable(connection, trans, DatabaseName, listFixTable[i], foundTable) <= -100)
                    {
                        SanitaLogEx.e(TAG, "[SynchDatabase] Alter table lỗi");
                        if (synch_worker != null)
                        {
                            synch_worker.ReportProgress(-1, "Update table '" + listFixTable[i].Table + "' lỗi !");
                        }
                        return(false);
                    }
                }

                if (synch_worker != null)
                {
                    synch_worker.ReportProgress(i + 1, "Nâng cấp table '" + listFixTable[i].Table + "' OK !");
                }
            }

            return(true);
        }