void IDbResult::bind(DataTable &table) { init(table); while(end() == false) { DataTableRow row = table.addRow(); bind(row); moveNext(); } }
bool CompatibilityManager::razorUpgrade(const String &folder) { try { if(FileSystem::instance()->directoryExists(folder)) { StringList portals; FileSystem::instance()->getDirectories(folder, portals, false); for(StringList::const_iterator i = portals.begin(); i != portals.end(); ++i) { // 000000018F94C9554C8B227CF000C6E30EB281AEAE1A77E7 String name = *i; if(name.length() == 48) { // Portal < Razor std::string oldID = name.to_ascii(); // Options String oldPath = utils::standardisePath(folder + oldID); shared_ptr<PortalOptions> options(OS_NEW PortalOptions()); // Carica le impostazioni del portale if(options->readFromPath(oldPath) == false) return false; shared_ptr<IDbDriver> driver = DatabasesSystem::instance()->getDriver("sqlite"); // Query Database shared_ptr<IDbConnection> connection = driver->createConnection(oldID, driver->createOptions()); connection->open(); String portalName = options->getName(); //String portalDescription = static_cast<String>(options->getOptionValue("description")); NotificationsManager::instance()->notify(_S("Migration of '") + portalName + _S("' to 1.0 series")); PlatformManager::instance()->sleep(500); //ObjectID userID = static_cast<String>(m_portalOptions.getOption(options::users_reference)).to_ascii(); ObjectID userID = static_cast<String>(options->getOptionValue("users.reference")).to_ascii(); //bool monarchic = (options->getPortalID().getPortalType() == portalTypeMonarchic); ID oldPortalID(oldID); uint8 portalType = OS_UINT8_LO(oldPortalID.getHeader().getFirst()); bool monarchic = (portalType == 1); // Calcolo UserID. // Se non lo posso dedurre, uso il primo che trovo loggato. if(userID.empty()) { if(monarchic) { //userID = options->getPortalID().getAdministratorID(); #ifdef OS_NOOBJECTID userID = oldPortalID.getHash(); #else userID = ObjectID::generate(portalObjectTypeUser, oldPortalID.getHash()); #endif } else { String sql = String::EMPTY; sql += _S(" select tp.profile, tu.reference "); sql += _S(" from "); sql += _S(" os_snapshot_profiles tp, "); sql += _S(" os_snapshot_users tu "); sql += _S(" where "); sql += _S(" tp.profile=tu.profile and "); sql += _S(" tu.score=1 and "); sql += _S(" tu.follow=1 and "); sql += _S(" tu.description='Auto valutation'"); shared_ptr<IDbResult> result = connection->query(sql); DataTable table; result->init(table); DataTableRow row = table.addRow(); while(result->end() == false) { result->bind(row); String objectID = static_cast<String>(*row[1]); userID = objectID.to_ascii(); result->moveNext(); break; } } } #ifdef OS_NOOBJECTID PovID povID = userID; #else PovID povID = userID.getHash(); #endif if(userID.empty() == false) { // Clean old snapshot connection->execute(_S("delete from os_snapshot_objects")); connection->execute(_S("delete from os_snapshot_users")); connection->execute(_S("delete from os_snapshot_profiles")); connection->execute(_S("delete from os_discussions_stats")); //connection->execute(_S("delete from os_forums_forum_stats")); //connection->execute(_S("delete from os_forums_section_stats")); connection->execute(_S("delete from os_polls_stats")); connection->execute(_S("delete from os_polls_options_stats")); connection->execute(_S("delete from os_users_stats")); // Old connection->execute(_S("drop table if exists os_forums_forum_stats")); connection->execute(_S("drop table if exists os_forums_section_stats")); // Reset acceptable connection->execute(_S("update os_entries set rank=-2")); // Clean signatures #ifdef OS_NEWSIGNATURE connection->execute(_S("update os_attributes set signature=x'00'")); connection->execute(_S("update os_avatars set signature=x'00'")); connection->execute(_S("update os_calendar_events set signature=x'00'")); connection->execute(_S("update os_files set signature=x'00'")); connection->execute(_S("update os_instances set signature=x'00'")); connection->execute(_S("update os_models set signature=x'00'")); connection->execute(_S("update os_polls set signature=x'00'")); connection->execute(_S("update os_polls_options set signature=x'00'")); connection->execute(_S("update os_polls_votes set signature=x'00'")); connection->execute(_S("update os_posts set signature=x'00'")); connection->execute(_S("update os_private_messages set signature=x'00'")); connection->execute(_S("update os_reputations set signature=x'00'")); connection->execute(_S("update os_sections set signature=x'00'")); connection->execute(_S("update os_tags set signature=x'00'")); connection->execute(_S("update os_texts set signature=x'00'")); connection->execute(_S("update os_users set signature=x'00'")); connection->execute(_S("update os_votes set signature=x'00'")); #endif // identificarli: // select * from os_users where signature=x'00' } connection->close(); if(userID.empty() == false) { String newPortalIDKey = oldID.substr(8); PortalID newPortalID = CryptManager:: instance()->SHA(newPortalIDKey.buffer(), newPortalIDKey.buffer_size()).toHex(); String newPovID = Portal::generatePovID(newPortalID, povID); String newPath = utils::standardisePath(folder + newPovID); options->setPortalID(newPortalID); options->setPovID(povID); options->setDatabaseName(newPovID); FileSystem::instance()->ensureDirectory(newPath); options->writeToPath(newPath); String sqlitePluginID = _S("532E9E0A68EB22E08240965CFA9366DFA6A26A62"); String databaseRootPath = utils::makeFolderPath(Options::instance()->getDataPath(), utils::makeFolderPath(OS_STORE_PATH, sqlitePluginID)); String oldDatabasePath = utils::makeFilePath(databaseRootPath,oldID) + _S(".db"); String newDatabasePath = utils::makeFilePath(databaseRootPath,newPovID) + _S(".db"); FileSystem::instance()->copyFile(oldDatabasePath, newDatabasePath); FileSystem::instance()->remove(oldDatabasePath); FileSystem::instance()->removeDirectory(oldPath); } else { OS_LOG_ERROR("Unable to migrate portal '" + portalName + "', ID:" + oldID + ". Contact our forum, thanks."); } } } } } catch(std::exception &e) { OS_LOG_ERROR(e.what()); return false; } catch(...) { OS_LOG_ERROR(_S("Unknown error during Razor upgrade.'")); return false; } return true; }
bool CompatibilityManager::upgradeContentOML(const shared_ptr<IPortalDatabase> &database, const String &tableName, const String& fieldName, bool xmlEncoded) { NotificationsManager::instance()->notify(_S("Migration (OML texts) of '") + database->getPortal()->getName() + _S("' to 1.0 series")); DataTable result; String sql = _S("select id,") + fieldName + _S(" as oml from ") + tableName; //database->execute(sql,result); shared_ptr<IDbResult> result2 = database->getConnection()->query(sql); result2->init(result); DataTableRow row = result.addRow(); //String patch; //for(uint32 r=0;r<result.rows();r++) while(result2->end() == false) { result2->bind(row); //String id = result.get(r,_S("id")); //String oml = result.get(r,_S("oml")); String id = *row[_S("id")]; String omlOrig = *row[_S("oml")]; String oldOml = omlOrig; // portal=0D2C4E505C1D98B221E222CABE4E167BECB2D669 #ifdef OS_NOOBJECTID omlOrig = RegexManager::instance()->replace(omlOrig, _S("[0-1][0-9]0[0-1]000[0-4]([0-9A-F]{40})"), _S("$1"), false); #else omlOrig = RegexManager::instance()->replace(omlOrig, _S("[0-1][0-9]0[0-1]000[2-4]([0-9A-F]{40})"), _S("$1"), false); #endif String oml = omlOrig; /* bool xmlEncoded = false; if(oml.starts_with(_S("<?xml version=\"1.0\" encoding=\"UTF-16\" standalone=\"no\" ?>"))) { oml = HtmlParser::instance()->decode(omlOrig); xmlEncoded = true; }*/ if(xmlEncoded) oml = HtmlParser::instance()->decode(omlOrig); int nReplace = 0; String::size_type lastPosLink = 0; for(;;) { nReplace++; String::size_type posLink = oml.find(_S("osiris://"),lastPosLink); if(posLink == String::npos) break; String::size_type posEnd = String::npos; { String::size_type posEndC = oml.find(_S("\""), posLink+1); if( (posEndC != String::npos) && ( (posEndC<posEnd) || (posEnd == String::npos) ) ) posEnd = posEndC; } { String::size_type posEndC = oml.find(_S("'"), posLink+1); if( (posEndC != String::npos) && ( (posEndC<posEnd) || (posEnd == String::npos) ) ) posEnd = posEndC; } { String::size_type posEndC = oml.find(_S("["), posLink+1); if( (posEndC != String::npos) && ( (posEndC<posEnd) || (posEnd == String::npos) ) ) posEnd = posEndC; } { String::size_type posEndC = oml.find(_S("]"), posLink+1); if( (posEndC != String::npos) && ( (posEndC<posEnd) || (posEnd == String::npos) ) ) posEnd = posEndC; } String linkEx = oml.substr(posLink); bool valid = true; bool warning = true; if(posEnd == String::npos) valid = false; if(valid) { String link = oml.substr(posLink, posEnd-posLink); if( (link == _S("osiris://")) ) { valid = false; warning = false; } else if( (link == _S("osiris://.")) ) { valid = false; warning = false; } else { OsirisLink osirisLink(link.to_ascii()); if(osirisLink.isValid() == false) valid = false; else { String linkNew = osirisLink.generate(); oml = oml.replace_all(link, linkNew); if(id == _S("FE10596C2B406B19020DE70118C5A36208CE7B06")) { valid = true; } if(xmlEncoded == false) omlOrig = omlOrig.replace_all(link, linkNew); else omlOrig = omlOrig.replace_all(HtmlParser::instance()->encodeEx(link), HtmlParser::instance()->encodeEx(linkNew)); //m_DebugLog += id + _S(" --> ") + link + _S(" --> ") + linkNew + _S("\r\n"); } } } if(valid == false) { linkEx = linkEx.replace_all(_S("\r"),_S(" {\\r} ")); linkEx = linkEx.replace_all(_S("\n"),_S(" {\\n} ")); m_DebugLog += id + _S(" --> ") + linkEx + _S(" --> invalid!\r\n"); lastPosLink = posLink+1; } if(nReplace == 10000) // Something wrong. return false; } // Qui rilevo eventuale parametro 'portal' della 0.x. Lo converto nel nuovo ID. if(omlOrig != oldOml) { String sql = _S("update ") + tableName + _S(" set ") + fieldName + _S("=") + Convert::toSQL(omlOrig) + _S(" where id='") + id + _S("'"); if(database->execute(sql) == false) return false; //patch.append(); } result2->moveNext(); } /* if(patch.empty() == false) if(database->execute(patch) == false) return false; */ return true; }
int SqliteClient::executeSqlQuery(const string& sql, DataTable& table) { Locker locker(&_sqliteDbMutex); #if DEBUG Stopwatch sw("executeSqlQuery", 100); #endif sqlite3_stmt *stmt; int result = sqlite3_prepare_v2(_sqliteDb, sql.c_str(), sql.length(), &stmt, 0); if(result != SQLITE_OK) { printErrorInfo("sqlite3_prepare_v2", sql); return result; } //const char* name = sqlite3_column_table_name(stmt, 0); //table.setName(name != NULL ? name : "temp"); // todo: linke error for sqlite3_column_table_name. table.setName("temp"); #if DEBUG sw.setInfo(Convert::convertStr("executeSqlQuery, the table name is '%s'", table.getName().c_str())); #endif int columnCount = sqlite3_column_count(stmt); for (int i = 0; i < columnCount; i++) { char* nameStr = (char*)sqlite3_column_name(stmt, i); string name; if(nameStr != NULL) { name = nameStr; } else { char temp[32]; sprintf(temp, "tempCol%d", i); name = temp; } const char* typeStr = sqlite3_column_decltype(stmt, i); string type = typeStr != NULL ? typeStr : "int"; DataColumn* column = new DataColumn(name, type); table.addColumn(column); } while(sqlite3_step(stmt) == SQLITE_ROW) { DataRow* row = new DataRow(); for (int i = 0; i < columnCount; i++) { DataColumn* column = table.getColumns()->at(i); DataCell* cell = NULL; Value value; memset(&value, 0, sizeof(value)); ValueTypes type = column->getType(); switch (type) { case Null: break; case Integer: value.nValue = sqlite3_column_int(stmt, i); break; case String: case DateTime: { char* str = (char*)sqlite3_column_text(stmt, i); DataCell::setStringValue(value, str); } break; case Float: value.dValue = sqlite3_column_double(stmt, i); break; default: assert(false); break; } cell = new DataCell(column, value); row->addCell(cell); } table.addRow(row); } result = sqlite3_finalize(stmt); if(result != SQLITE_OK) { printErrorInfo("sqlite3_finalize", sql); return result; } return SQLITE_OK; }