platform/shared/rubyJVM/src/com/rho/db/DBAdapter.java in rhodes-1.2.2 vs platform/shared/rubyJVM/src/com/rho/db/DBAdapter.java in rhodes-1.4.0

- old
+ new

@@ -34,10 +34,21 @@ if ( m_Instance == null ) m_Instance = new DBAdapter(RubyRuntime.DatabaseClass); return m_Instance; } + + public void close() + { + try{ + m_dbStorage.close(); + m_dbStorage = null; + }catch(Exception exc) + { + LOG.ERROR("DB close failed.", exc); + } + } public DBAttrManager getAttrMgr() { return m_attrMgr; } @@ -108,11 +119,10 @@ return getInstance().m_dbStorage.createResult(); } public static String makeBlobFolderName()throws Exception{ String fName = RhoClassFactory.createFile().getDirPath("apps/public/db-files"); - return fName; } RubyString[] getColNames(IDBResult rows) { @@ -132,10 +142,12 @@ strDbName = strPath; return strDbName; } + public String getDBPath(){ return getNameNoExt(m_strDBPath); } + private void initFilePaths(String strDBName)throws Exception { if ( strDBName.charAt(0) == '/' || strDBName.charAt(0) == '\\' ) strDBName = strDBName.substring(1); @@ -150,13 +162,14 @@ String strPath = RhoClassFactory.createFile().getDirPath(strDBDir); m_strDBPath = strPath + strDBName.substring(nSlash+1); m_strDBVerPath = strPath + getNameNoExt(strDBName.substring(nSlash+1)) + ".version"; } - private String getSqlScript(){ - //TODO: read script from jar - return "CREATE TABLE client_info ("+ + private String getSqlScript() + { + return RhoConf.getInstance().loadFileFromJar("apps/db/syncdb.schema"); +/* return "CREATE TABLE client_info ("+ "client_id VARCHAR(255) PRIMARY KEY,"+ "token VARCHAR(255) default NULL,"+ "token_sent int default 0,"+ "reset int default 0,"+ "port VARCHAR(10) default NULL,"+ @@ -178,20 +191,22 @@ " update_type varchar(255) default NULL, " + " sent int default 0," + " main_id INTEGER default 0 );"+ "CREATE TABLE sources ("+ //"id INTEGER PRIMARY KEY,"+ - "source_id int PRIMARY KEY,"+ + "source_url VARCHAR(255) PRIMARY KEY," + + "source_id int,"+ "name varchar(255) default NULL,"+ "token varchar(30) default NULL,"+ - "source_url VARCHAR(255) default NULL,"+ + "priority INTEGER," + "session VARCHAR(255) default NULL,"+ "last_updated int default 0,"+ "last_inserted_size int default 0,"+ "last_deleted_size int default 0,"+ "last_sync_duration int default 0,"+ "last_sync_success int default 0," + + "backend_refresh_time int default 0," + "source_attribs varchar default NULL);"+ //"CREATE INDEX by_attrib_obj_utype on object_values (attrib,object,update_type);"+ //"CREATE INDEX by_attrib_utype on object_values (attrib,update_type);"+ //"CREATE INDEX by_src_type ON object_values (source_id, attrib_type, object);"+ "CREATE INDEX by_src_id ON object_values (source_id);"+ @@ -199,10 +214,11 @@ "CREATE INDEX by_src_value ON object_values (attrib, source_id, value);";//+ //"CREATE INDEX by_id ON object_values (id);"; //for delete operation //"CREATE INDEX by_src_object ON object_values (source_id, object);"+ //"CREATE INDEX by_src_up_value ON object_values (source_id, update_type, value);"; //"CREATE INDEX by_type ON object_values (attrib_type)"; + */ } /*private String getSqlScript(){ //TODO: read script from jar return "CREATE TABLE client_info ("+ @@ -407,17 +423,19 @@ String dbNewNameData = dbNewName + ".data"; String dbNameScript = dbName + ".script"; String dbNewNameScript = dbNewName + ".script"; String dbNameJournal = dbName + ".journal"; String dbNewNameJournal = dbNewName + ".journal"; + String dbNewNameProps = dbNewName + ".properties"; //LOG.TRACE("DBAdapter: " + dbNewNameDate + ": " + (fs.exists(dbNewNameData) ? "" : "not ") + "exists"); fs.delete(dbNewNameData); //LOG.TRACE("DBAdapter: " + dbNewNameScript + ": " + (fs.exists(dbNewNameScript) ? "" : "not ") + "exists"); fs.delete(dbNewNameScript); //LOG.TRACE("DBAdapter: " + dbNewNameJournal + ": " + (fs.exists(dbNewNameJournal) ? "" : "not ") + "exists"); fs.delete(dbNewNameJournal); + fs.delete(dbNewNameProps); LOG.TRACE("1. Size of " + dbNameData + ": " + fs.size(dbNameData)); db = RhoClassFactory.createDBStorage(); db.open( dbNewName, getSqlScript() ); @@ -451,10 +469,11 @@ m_dbStorage = null; m_bIsOpen = false; LOG.TRACE("2. Size of " + dbNewNameData + ": " + fs.size(dbNewNameData)); + fs.delete(dbNewNameProps); fs.delete(dbNameJournal); String fName = makeBlobFolderName(); RhoClassFactory.createFile().delete(fName); DBAdapter.makeBlobFolderName(); //Create folder back @@ -506,10 +525,105 @@ m_dbStorage.rollback(); m_bInsideTransaction=false; Unlock(); } + + public void setInitialSyncDB(String fDbName, String fScriptName) + { + IDBStorage db = null; + try{ + db = RhoClassFactory.createDBStorage(); + db.open( fDbName, "" ); + + db.startTransaction(); + + //copy client_info + { + IDBResult res = executeSQL("SELECT * from client_info", null); + String strInsert = ""; + + for ( ; !res.isEnd(); res.next() ) + { + if ( strInsert.length() == 0 ) + strInsert = createInsertStatement(res, "client_info"); + + db.executeSQL(strInsert, res.getCurData(), false ); + } + } + //copy sources + { + IDBResult res = executeSQL("SELECT name, source_url,priority,session from sources", null); + for ( ; !res.isEnd(); res.next() ) + { + String strName = res.getStringByIdx(0); + Object[] values = {res.getStringByIdx(1), new Integer(res.getIntByIdx(2)),res.getStringByIdx(3),strName}; + + db.executeSQL("UPDATE sources SET source_url=?,priority=?,session=? where name=?",values, false); + } + } + + db.commit(); + db.close(); + + m_dbStorage.close(); + m_dbStorage = null; + m_bIsOpen = false; + + String dbName = getNameNoExt(m_strDBPath); + IFileAccess fs = RhoClassFactory.createFileAccess(); + + String dbNameData = dbName + ".data"; + String dbNameScript = dbName + ".script"; + String dbNameJournal = dbName + ".journal"; + String dbNewNameProps = getNameNoExt(fDbName) + ".properties"; + + fs.delete(dbNameJournal); + fs.delete(dbNewNameProps); + + String fName = makeBlobFolderName(); + RhoClassFactory.createFile().delete(fName); + DBAdapter.makeBlobFolderName(); //Create folder back + + fs.renameOverwrite(fDbName, dbNameData); + fs.renameOverwrite(fScriptName, dbNameScript); + + m_dbStorage = RhoClassFactory.createDBStorage(); + m_dbStorage.open(m_strDBPath, getSqlScript() ); + m_bIsOpen = true; + + getAttrMgr().load(this); + + m_dbStorage.setDbCallback(new DBCallback(this)); + + }catch(Exception e) + { + LOG.ERROR("execute failed.", e); + + if ( !m_bIsOpen ) + { + LOG.ERROR("destroy_table error.Try to open old DB."); + try{ + m_dbStorage.open(m_strDBPath, getSqlScript() ); + 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.getMessage()); + } + + throw (e instanceof RubyException ? (RubyException)e : new RubyException(e.getMessage())); + } + + } /* public RubyValue rb_execute(RubyValue v) { RubyArray res = new RubyArray(); try{ @@ -628,14 +742,16 @@ return ObjectFactory.createInteger(0); } private RubyValue rb_close() { try{ - if ( m_dbStorage != null ){ + + //do not close sync db, close it at exit + /*if ( m_dbStorage != null ){ m_dbStorage.close(); m_dbStorage = null; - } + }*/ }catch( Exception e ){ LOG.ERROR("close failed.", e); throw (e instanceof RubyException ? (RubyException)e : new RubyException(e.getMessage())); } @@ -739,10 +855,10 @@ try{ SimpleFile oFile = RhoClassFactory.createFile(); String strFilePath = oFile.getDirPath(""); - strFilePath += url; + strFilePath += url.startsWith("/") ? url.substring(1) : url; oFile.delete(strFilePath); }catch(Exception exc){ LOG.ERROR("DBCallback.OnDeleteFromTable: Error delete file: " + url, exc); }