optional<size_t> TimetableRowTableSync::GetMaxRank( util::RegistryKeyType timetableId ) { DB* db = DBModule::GetDB(); stringstream query; // Content query << "SELECT MAX(" << COL_RANK << ") AS mr " << " FROM " << TABLE.NAME << " WHERE " << COL_TIMETABLE_ID << "=" << timetableId ; try { DBResultSPtr rows = db->execQuery(query.str()); while (rows->next ()) { return rows->getOptionalUnsignedInt("mr"); } return optional<size_t>(); } catch(DBException& e) { throw Exception(e.getMessage()); } }
static void _InitAutoIncrement() { if (!K::TABLE.HAS_AUTO_INCREMENT) return; try { DB* db = DBModule::GetDB(); std::stringstream query; query << "SELECT " << 0x00000000FFFFFFFFLL << " & " << TABLE_COL_ID << " AS maxid FROM " << K::TABLE.NAME << " WHERE " << TABLE_COL_ID << ">=" << encodeUId(0) << " AND " << TABLE_COL_ID << "<=" << encodeUId(0xFFFFFFFF) << " ORDER BY " << TABLE_COL_ID << " DESC LIMIT 1" ; DBResultSPtr result (db->execQuery(query.str())); if (result->next ()) { util::RegistryKeyType maxid = result->getLongLong("maxid"); if (maxid > 0) { _autoIncrementValue = util::decodeObjectId(maxid) + 1; util::Log::GetInstance().debug("Auto-increment of table "+ K::TABLE.NAME +" initialized at "+ boost::lexical_cast<std::string>(_autoIncrementValue)); } } } catch (DBException& e) { util::Log::GetInstance().debug("Table "+ K::TABLE.NAME +" without preceding id.", e); } catch (...) { // Log::GetInstance().debug("Table "+ getTableName() +" without preceding id.", e); } }