void loadAllSources() { if (isNoThreadedMode()) { RhoRuby.loadAllSyncSources(); } else { getNet().pushData(getNet().resolveUrl("/system/loadallsyncsources"), "", null); } m_sources.removeAllElements(); Vector <String> arPartNames = DBAdapter.getDBAllPartitionNames(); for (int i = 0; i < (int)arPartNames.size(); i++) { DBAdapter dbPart = DBAdapter.getDB((String)arPartNames.elementAt(i)); IDBResult res = dbPart.executeSQL("SELECT source_id,sync_type,name from sources ORDER BY sync_priority"); for ( ; !res.isEnd(); res.next()) { String strShouldSync = res.getStringByIdx(1); if (strShouldSync.compareTo("none") == 0) { continue; } String strName = res.getStringByIdx(2); m_sources.addElement(new SyncSource(res.getIntByIdx(0), strName, strShouldSync, dbPart, this)); } } checkSourceAssociations(); }
public override int getLastPollInterval() { try{ long nowTime = (TimeInterval.getCurrentTime().toULong()) / 1000; long latestTimeUpdated = 0; Vector <String> arPartNames = DBAdapter.getDBAllPartitionNames(); for (int i = 0; i < (int)arPartNames.size(); i++) { DBAdapter dbPart = DBAdapter.getDB((String)arPartNames.elementAt(i)); IDBResult res = dbPart.executeSQL("SELECT last_updated from sources"); for ( ; !res.isEnd(); res.next()) { long timeUpdated = res.getLongByIdx(0); if (latestTimeUpdated < timeUpdated) { latestTimeUpdated = timeUpdated; } } } return(latestTimeUpdated > 0 ? (int)(nowTime - latestTimeUpdated) : 0); }catch (Exception exc) { LOG.ERROR("isStartSyncNow failed.", exc); } return(0); }
public static RubyArray Execute(RhoDatabase /*!*/ self, MutableString /*!*/ sqlStatement, Boolean isBatch, RubyArray args) { try { RubyArray retArr = new RubyArray(); if (isBatch) { self.m_db.executeBatchSQL(sqlStatement.ToString()); } else { Object[] values = null; if (args != null && args.Count > 0) { if (args[0] != null && args[0] is RubyArray) { values = ((RubyArray)args[0]).ToArray(); } else { values = args.ToArray(); } } using (IDBResult rows = self.m_db.executeSQL(sqlStatement.ToString(), values, true)) { if (rows != null) { MutableString[] colNames = null; for (; !rows.isEnd(); rows.next()) { IDictionary <object, object> map = new Dictionary <object, object>(); Hash row = new Hash(map); for (int nCol = 0; nCol < rows.getColCount(); nCol++) { if (colNames == null) { colNames = getOrigColNames(rows); } row.Add(colNames[nCol], rows.getRubyValueByIdx(nCol)); } retArr.Add(row); } } } } return(retArr); }catch (Exception exc) { //TODO: throw ruby exception throw exc; } }
public CSqliteCopyResult(IDBResult res) { for( int i = 0; i < res.getColCount(); i++) m_arColumns.addElement(res.getColName(i)); for( ; !res.isEnd(); res.next() ) { m_arRows.addElement(res.getCurData()); } }
public CSqliteCopyResult(IDBResult res) { for (int i = 0; i < res.getColCount(); i++) { m_arColumns.addElement(res.getColName(i)); } for ( ; !res.isEnd(); res.next()) { m_arRows.addElement(res.getCurData()); } }
static void loadAttrs(DBAdapter db, Hashtable <int, Hashtable <String, int> > mapAttrs, String strDBAttr, Hashtable <String, int> mapSrcNames) { mapAttrs.clear(); String strSql = "SELECT source_id,"; strSql += strDBAttr + ",name from sources"; IDBResult res = db.executeSQL(strSql); for ( ; !res.isEnd(); res.next()) { int nSrcID = res.getIntByIdx(0); String strAttribs = res.getStringByIdx(1); if (strAttribs.length() == 0) { continue; } Tokenizer oTokenizer = new Tokenizer(strAttribs, ","); Hashtable <String, int> mapAttr = new Hashtable <String, int>(); String strAttr = ""; while (oTokenizer.hasMoreTokens()) { String tok = oTokenizer.nextToken(); if (tok.length() == 0) { continue; } if (strAttr.length() > 0) { mapAttr.put(strAttr, int.Parse(tok)); strAttr = ""; } else { strAttr = tok; } } mapAttrs.put(nSrcID, mapAttr); if (mapSrcNames != null) { mapSrcNames.put(res.getStringByIdx(2).toUpperCase(), nSrcID); } } }
private void copyTable(String tableName, IDBStorage dbFrom, IDBStorage dbTo) { IDBResult res = dbFrom.executeSQL("SELECT * from " + tableName, null, false, false); String strInsert = ""; for ( ; !res.isEnd(); res.next()) { if (strInsert.length() == 0) { strInsert = createInsertStatement(res, tableName); } dbTo.executeSQL(strInsert, res.getCurData(), false, false); } }
public void applyChangedValues(DBAdapter db) { IDBResult resSrc = db.executeSQL("SELECT DISTINCT(source_id) FROM changed_values"); for ( ; !resSrc.isEnd(); resSrc.next()) { int nSrcID = resSrc.getIntByIdx(0); IDBResult res = db.executeSQL("SELECT source_id,sync_type,name, partition from sources WHERE source_id=?", nSrcID); if (res.isEnd()) { continue; } SyncSource src = new SyncSource(res.getIntByIdx(0), res.getStringByIdx(2), "none", db, this); src.applyChangedValues(); } }
public string[] getAllTableNames() { IDBResult res = executeSQL("SELECT name FROM sqlite_master WHERE type='table'", null, false, false); Vector <Object> arTables = new Vector <Object>(); for (; !res.isEnd(); res.next()) { arTables.addElement(res.getCurData()[0]); } String[] vecTables = new String[arTables.size()]; for (int i = 0; i < arTables.size(); i++) { vecTables[i] = (String)arTables.elementAt(i); } return(vecTables); }
void copyChangedValues(DBAdapter db) { updateAllAttribChanges(); copyTable("changed_values", m_dbStorage, db.m_dbStorage); { Vector <int> arOldSrcs = new Vector <int>(); { IDBResult resSrc = db.executeSQL("SELECT DISTINCT(source_id) FROM changed_values"); for ( ; !resSrc.isEnd(); resSrc.next()) { arOldSrcs.addElement(resSrc.getIntByIdx(0)); } } for (int i = 0; i < arOldSrcs.size(); i++) { int nOldSrcID = arOldSrcs.elementAt(i); IDBResult res = executeSQL("SELECT name from sources WHERE source_id=?", nOldSrcID); if (!res.isEnd()) { String strSrcName = res.getStringByIdx(0); IDBResult res2 = db.executeSQL("SELECT source_id from sources WHERE name=?", strSrcName); if (!res2.isEnd()) { if (nOldSrcID != res2.getIntByIdx(0)) { db.executeSQL("UPDATE changed_values SET source_id=? WHERE source_id=?", res2.getIntByIdx(0), nOldSrcID); } continue; } } //source not exist in new partition, remove this changes db.executeSQL("DELETE FROM changed_values WHERE source_id=?", nOldSrcID); } } }
public void updateAllAttribChanges() { //Check for attrib = object IDBResult res = executeSQL("SELECT object, source_id, update_type " + "FROM changed_values where attrib = 'object' and sent=0"); if (res.isEnd()) { return; } startTransaction(); Vector <String> arObj = new Vector <String>(), arUpdateType = new Vector <String>(); Vector <int> arSrcID = new Vector <int>(); for ( ; !res.isEnd(); res.next()) { arObj.addElement(res.getStringByIdx(0)); arSrcID.addElement(res.getIntByIdx(1)); arUpdateType.addElement(res.getStringByIdx(2)); } for (int i = 0; i < (int)arObj.size(); i++) { IDBResult resSrc = executeSQL("SELECT name, schema FROM sources where source_id=?", arSrcID.elementAt(i)); boolean bSchemaSource = false; String strTableName = "object_values"; if (!resSrc.isEnd()) { bSchemaSource = resSrc.getStringByIdx(1).length() > 0; if (bSchemaSource) { strTableName = resSrc.getStringByIdx(0); } } if (bSchemaSource) { IDBResult res2 = executeSQL("SELECT * FROM " + strTableName + " where object=?", arObj.elementAt(i)); for (int j = 0; j < res2.getColCount(); j++) { String strAttrib = res2.getColName(j); String value = res2.getStringByIdx(j); String attribType = getAttrMgr().isBlobAttr(arSrcID.elementAt(i), strAttrib) ? "blob.file" : ""; executeSQLReportNonUnique("INSERT INTO changed_values (source_id,object,attrib,value,update_type,attrib_type,sent) VALUES(?,?,?,?,?,?,?)", arSrcID.elementAt(i), arObj.elementAt(i), strAttrib, value, arUpdateType.elementAt(i), attribType, 0); } } else { IDBResult res2 = executeSQL("SELECT attrib, value FROM " + strTableName + " where object=? and source_id=?", arObj.elementAt(i), arSrcID.elementAt(i)); for ( ; !res2.isEnd(); res2.next()) { String strAttrib = res2.getStringByIdx(0); String value = res2.getStringByIdx(1); String attribType = getAttrMgr().isBlobAttr(arSrcID.elementAt(i), strAttrib) ? "blob.file" : ""; executeSQLReportNonUnique("INSERT INTO changed_values (source_id,object,attrib,value,update_type,attrib_type,sent) VALUES(?,?,?,?,?,?,?)", arSrcID.elementAt(i), arObj.elementAt(i), strAttrib, value, arUpdateType.elementAt(i), attribType, 0); } } } executeSQL("DELETE FROM changed_values WHERE attrib='object'"); endTransaction(); }
private void processDelete(String tableName, IDBResult rows2Delete, int[] cols) { if (tableName.equalsIgnoreCase("changed_values") || tableName.equalsIgnoreCase("sources") || tableName.equalsIgnoreCase("client_info")) { return; } boolean bProcessTable = tableName.equalsIgnoreCase("object_values"); boolean bSchemaSrc = false; int nSrcID = 0; if (!bProcessTable) { nSrcID = m_db.getAttrMgr().getSrcIDHasBlobsByName(tableName); bProcessTable = nSrcID != 0; bSchemaSrc = bProcessTable; } if (!bProcessTable) { return; } if (!bSchemaSrc && !isChangedCol(cols, 3)) //value { return; } for ( ; !rows2Delete.isEnd(); rows2Delete.next()) { if (!bSchemaSrc) { nSrcID = rows2Delete.getIntByIdx(0); String attrib = rows2Delete.getStringByIdx(1); String value = rows2Delete.getStringByIdx(3); //if (cols == null) //delete // m_db.getAttrMgr().remove(nSrcID, attrib); if (m_db.getAttrMgr().isBlobAttr(nSrcID, attrib)) { processBlobDelete(nSrcID, attrib, value); } } else { Object[] data = rows2Delete.getCurData(); for (int i = 0; i < rows2Delete.getColCount(); i++) { if (!isChangedCol(cols, i)) { continue; } String attrib = rows2Delete.getColName(i); if (!(data[i] is String)) { continue; } String value = (String)data[i]; if (m_db.getAttrMgr().isBlobAttr(nSrcID, attrib)) { processBlobDelete(nSrcID, attrib, value); } } } } }
public void loadBlobAttrs(DBAdapter db) { loadAttrs(db, m_mapBlobAttrs, "blob_attribs", m_mapSrcNames); String strTriggerPrefix = "rhoSchemaTrigger_"; IDBResult res = db.executeSQL("SELECT name FROM sqlite_master WHERE type='trigger'"); Hashtable <String, int> mapTriggers = new Hashtable <String, int>(); for (; !res.isEnd(); res.next()) { String strName = res.getStringByIdx(0); if (!strName.startsWith(strTriggerPrefix)) { continue; } mapTriggers[strName.Substring(strTriggerPrefix.length())] = 0; } foreach (KeyValuePair <int, Hashtable <String, int> > kvpBlobAttrs in m_mapBlobAttrs) { int nSrcID = kvpBlobAttrs.Key; res = db.executeSQL("SELECT name FROM sources WHERE source_id=?", nSrcID); if (res.isEnd()) { continue; } String strName = res.getStringByIdx(0); if (!db.isTableExist(strName)) { continue; } Hashtable <String, int> hashAttribs = kvpBlobAttrs.Value; foreach (KeyValuePair <String, int> kvpHashAttribs in hashAttribs) { String strTriggerName = strName + "_" + kvpHashAttribs.Key; if (!mapTriggers.containsKey(strTriggerName + "_delete")) { String strTrigger = "CREATE TRIGGER " + strTriggerPrefix + strTriggerName + "_delete BEFORE DELETE ON \"" + strName + "\" FOR EACH ROW \r\n" + " BEGIN \r\n" + " SELECT rhoOnDeleteSchemaRecord( OLD." + kvpHashAttribs.Key + ");\r\n" + " END;\r\n" + ";"; db.createTrigger(strTrigger); } else { mapTriggers[strTriggerName + "_delete"] = 1; } if (!mapTriggers.containsKey(strTriggerName + "_update")) { String strTrigger = "CREATE TRIGGER " + strTriggerPrefix + strTriggerName + "_update BEFORE UPDATE ON \"" + strName + "\" FOR EACH ROW\r\n" + " BEGIN \r\n" + " SELECT rhoOnUpdateSchemaRecord( OLD." + kvpHashAttribs.Key + ", NEW." + kvpHashAttribs.Key + ");\r\n" + " END;\r\n" + ";"; db.createTrigger(strTrigger); } else { mapTriggers[strTriggerName + "_update"] = 1; } } } //Remove outdated triggers foreach (KeyValuePair <string, int> kvp in mapTriggers) { if (kvp.Value != 0) { db.dropTrigger(strTriggerPrefix + kvp.Key.ToString()); } } }
private void processDelete(String tableName, IDBResult rows2Delete, int[] cols) { if ( tableName.equalsIgnoreCase("changed_values") || tableName.equalsIgnoreCase("sources") || tableName.equalsIgnoreCase("client_info")) return; boolean bProcessTable = tableName.equalsIgnoreCase("object_values"); boolean bSchemaSrc = false; int nSrcID = 0; if ( !bProcessTable ) { nSrcID = m_db.getAttrMgr().getSrcIDHasBlobsByName(tableName); bProcessTable = nSrcID != 0; bSchemaSrc = bProcessTable; } if ( !bProcessTable) return; if ( !bSchemaSrc && !isChangedCol(cols, 3))//value return; for( ; !rows2Delete.isEnd(); rows2Delete.next() ) { if ( !bSchemaSrc ) { nSrcID = rows2Delete.getIntByIdx(0); String attrib = rows2Delete.getStringByIdx(1); String value = rows2Delete.getStringByIdx(3); //if (cols == null) //delete // m_db.getAttrMgr().remove(nSrcID, attrib); if ( m_db.getAttrMgr().isBlobAttr(nSrcID, attrib) ) processBlobDelete(nSrcID, attrib, value); }else { Object[] data = rows2Delete.getCurData(); for ( int i = 0; i < rows2Delete.getColCount(); i++ ) { if (!isChangedCol(cols, i)) continue; String attrib = rows2Delete.getColName(i); if ( !(data[i] is String ) ) continue; String value = (String)data[i]; if ( m_db.getAttrMgr().isBlobAttr(nSrcID, attrib) ) processBlobDelete(nSrcID, attrib, value); } } } }
//{"source_name":"SampleAdapter","client_id":1,"create":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}}} //{"source_name":"SampleAdapter","client_id":1,"update":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}}} //{"source_name":"SampleAdapter","client_id":1,"delete":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}}} //{"source_name":"SampleAdapter","client_id":1,"delete":{"3":{"brand":"HTC","name":"Fuze","price":"299.99"}},"create":{"1":{"brand":"Apple","name":"iPhone","price":"199.99"}},"update":{"2":{"brand":"Android","name":"G2","price":"99.99"}}} String makePushBody_Ver3(String strUpdateType, boolean isSync) { String strBody = ""; getDB().Lock(); if (isSync) { getDB().updateAllAttribChanges(); } IDBResult res = getDB().executeSQL("SELECT attrib, object, value, attrib_type " + "FROM changed_values where source_id=? and update_type =? and sent<=1 ORDER BY object", getID(), strUpdateType); if (res.isEnd()) { res.close(); getDB().Unlock(); return(strBody); } String strCurObject = ""; boolean bFirst = true; for ( ; !res.isEnd(); res.next()) { String strAttrib = res.getStringByIdx(0); String strObject = res.getStringByIdx(1); String value = res.getStringByIdx(2); String attribType = res.getStringByIdx(3); if (attribType.compareTo("blob.file") == 0) { NetRequest.MultipartItem oItem = new NetRequest.MultipartItem(); oItem.m_strFilePath = RHODESAPP().resolveDBFilesPath(value); oItem.m_strContentType = "application/octet-stream"; oItem.m_strName = strAttrib + "-" + strObject; m_arBlobAttrs.addElement(strAttrib); m_arMultipartItems.addElement(oItem); } if (strBody.length() == 0) { if (!isSync) { strBody += "{"; } else { strBody += "\"" + strUpdateType + "\":{"; } } if (strObject.compareTo(strCurObject) != 0) { if (strCurObject.length() > 0) { if (!bFirst) { strBody += "}"; } strBody += ","; } bFirst = true; strBody += JSONEntry.quoteValue(strObject); strCurObject = strObject; } if (!bFirst) { strBody += ","; } if (strAttrib.length() > 0) { if (bFirst) { strBody += ":{"; } strBody += JSONEntry.quoteValue(strAttrib) + ":" + JSONEntry.quoteValue(value); bFirst = false; } } if (strBody.length() > 0) { if (!bFirst) { strBody += "}"; } strBody += "}"; } if (isSync) { getDB().executeSQL("UPDATE changed_values SET sent=1 WHERE source_id=? and update_type=? and sent=0", getID(), strUpdateType); } getDB().Unlock(); return(strBody); }