Esempio n. 1
0
        public void open(String strPath, String strSqlScript, String strEncryptionInfo)
        {
            try
            {
                String dbURI = RHODESAPP().resolveDBFilesPath(strPath);
                dbURI = CFilePath.removeFirstSlash(dbURI);
                boolean bEncrypted = strEncryptionInfo != null && strEncryptionInfo.length() > 0;
                //DatabaseSecurityOptions dbso = new DatabaseSecurityOptions(bEncrypted);

                boolean bExist = CRhoFile.isFileExist(strPath);

                int res = Sqlite3.sqlite3_open(dbURI, ref m_db);
                if (res != Sqlite3.SQLITE_OK)
                {
                    throw new DBException(res, "Could not open database file: " + strPath);
                }

                res = Sqlite3.sqlite3_exec(m_db, "PRAGMA journal_mode=PERSIST", 0, 0, 0);
                if (res != Sqlite3.SQLITE_OK)
                {
                    Sqlite3.sqlite3_close(m_db);
                    m_db = null;
                    throw new DBException(res, "Cannot set journal mode to PERSIST: " + strPath);
                }

                string[] ar1 = CRhoFile.enumDirectory("db");

                if (!bExist)
                {
                    createSchema(strSqlScript);
                }

                Sqlite3.sqlite3_create_function(m_db, "rhoOnDeleteObjectRecord", 3, Sqlite3.SQLITE_ANY, 0,
                                                DBAdapter.SyncBlob_DeleteCallback, null, null);
                Sqlite3.sqlite3_create_function(m_db, "rhoOnUpdateObjectRecord", 3, Sqlite3.SQLITE_ANY, 0,
                                                DBAdapter.SyncBlob_UpdateCallback, null, null);
                Sqlite3.sqlite3_create_function(m_db, "rhoOnDeleteSchemaRecord", 1, Sqlite3.SQLITE_ANY, 0,
                                                DBAdapter.SyncBlob_DeleteSchemaCallback, null, null);
                Sqlite3.sqlite3_create_function(m_db, "rhoOnUpdateSchemaRecord", 2, Sqlite3.SQLITE_ANY, 0,
                                                DBAdapter.SyncBlob_UpdateSchemaCallback, null, null);

                string[] ar2 = CRhoFile.enumDirectory("db");

                if (m_bPendingTransaction)
                {
                    startTransaction();
                }

                m_bPendingTransaction = false;
            }
            catch (Exception exc)
            {
                throw new DBException(exc);
            }
            finally
            {
                m_nInsideTransaction = 0;
            }
        }
Esempio n. 2
0
        public void rb_destroy_tables(Vector <String> vecIncludes, Vector <String> vecExcludes)
        {
            if (!m_bIsOpen)
            {
                return;
            }

            IDBStorage db = null;

            try{
                String dbNewName = CFilePath.changeBaseName(m_strDBPath, "resetdbtemp.sqlite");

                CRhoFile.deleteFile(dbNewName);
                CRhoFile.deleteFile(dbNewName + "-journal");
                CRhoFile.deleteFile(dbNewName + ".version");

                db = RhoClassFactory.createDBStorage();
                db.open(dbNewName, getSqlScript(), getEncryptionInfo());

                String[] vecTables = m_dbStorage.getAllTableNames();
                //IDBResult res;

                db.startTransaction();

                for (int i = 0; i < vecTables.Length; i++)
                {
                    String tableName = vecTables[i];
                    if (destroyTableName(tableName, vecIncludes, vecExcludes))
                    {
                        continue;
                    }

                    copyTable(tableName, this.m_dbStorage, db);
                }

                db.commit();
                db.close();

                String dbOldName = m_strDBPath;

                m_dbStorage.close();
                m_dbStorage = null;
                m_bIsOpen   = false;

                CRhoFile.deleteFilesInFolder(RHODESAPP().getBlobsDirPath());

                string[] ar1 = CRhoFile.enumDirectory("db");

                CRhoFile.deleteFile(dbOldName);
                CRhoFile.deleteFile(dbOldName + "-journal");
                CRhoFile.renameFile(dbNewName, dbOldName);
                CRhoFile.renameFile(dbNewName + "-journal", dbOldName + "-journal");

                string[] ar2 = CRhoFile.enumDirectory("db");

                m_dbStorage = RhoClassFactory.createDBStorage();
                m_dbStorage.open(m_strDBPath, getSqlScript(), getEncryptionInfo());
                m_bIsOpen = true;

                string[] ar3 = CRhoFile.enumDirectory("db");
                m_dbStorage.setDbCallback(new DBCallback(this));
            }catch (Exception e)
            {
                LOG.ERROR("destroy_table failed.", e);

                if (!m_bIsOpen)
                {
                    LOG.ERROR("destroy_table error.Try to open old DB.");
                    try{
                        m_dbStorage.open(m_strDBPath, getSqlScript(), getEncryptionInfo());
                        m_bIsOpen = true;
                    }catch (Exception exc)
                    {
                        LOG.ERROR("destroy_table open old table failed.", exc);
                    }
                }

                try {
                    if (db != null)
                    {
                        db.close();
                    }
                } catch (DBException e1) {
                    LOG.ERROR("closing of DB caused exception: " + e1.Message);
                }

                throw e;
            }
        }