static Database* __database = NULL;
static unsigned int __numDBConnection = 0;
-static void __initDatabase()
+static const char* __getTableName(uid_t uid)
{
- const char* createJobInfoTable =
- "CREATE TABLE IF NOT EXISTS " JOB_INFO_TABLE "(" \
- "uid INTEGER NOT NULL," \
- "jobId INTEGER NOT NULL," \
- "owner TEXT NOT NULL," \
- "jobInfo TEXT NOT NULL," \
- "state TEXT NOT NULL DEFAULT ''," \
- "PRIMARY KEY (uid, jobId)" \
- ");";
-
- __database->execute(createJobInfoTable, NULL);
+ static char tableName[24] = {0};
+
+ g_snprintf(tableName, 24, JOB_INFO_TABLE "_%u", static_cast<unsigned>(uid));
+ return tableName;
+}
+
+static void __initDatabase(uid_t uid)
+{
+ char* query = sqlite3_mprintf(
+ "CREATE TABLE IF NOT EXISTS [%s] (" \
+ "jobId INTEGER NOT NULL PRIMARY KEY," \
+ "owner TEXT NOT NULL," \
+ "jobInfo TEXT NOT NULL," \
+ "state TEXT NOT NULL DEFAULT ''" \
+ ")", __getTableName(uid));
+
+ __database->execute(query, NULL);
+
+ sqlite3_free(query);
}
-static bool __open()
+static bool __open(uid_t uid)
{
if (__numDBConnection == 0) {
__database = new PlatformDatabase(DB_NAME);
_E("DB open failed");
return false;
}
-
- __initDatabase();
}
+ __initDatabase(uid);
+
++__numDBConnection;
return true;
}
JobInfoDatabase::JobInfoDatabase(uid_t uid) :
__uid(uid)
{
- if (!__open()) {
+ if (!__open(uid)) {
throw std::runtime_error("DB open failed");
}
}
void JobInfoDatabase::add(JobInfo* jobInfo, const std::string& owner)
{
char* query = sqlite3_mprintf(
- "INSERT INTO " JOB_INFO_TABLE " (uid, jobId, owner, jobInfo) VALUES (%d, %d, '%s', '%s')",
- static_cast<int>(__uid), jobInfo->getId(), owner.c_str(), jobInfo->serialize().c_str());
+ "INSERT INTO [%s] (jobId, owner, jobInfo) VALUES (%d, '%s', '%s')",
+ __getTableName(__uid), jobInfo->getId(), owner.c_str(), jobInfo->serialize().c_str());
__database->execute(query, NULL);
void JobInfoDatabase::remove(int jobId)
{
char* query = sqlite3_mprintf(
- "DELETE FROM " JOB_INFO_TABLE " WHERE uid = %d AND jobId = %d",
- static_cast<int>(__uid), jobId);
+ "DELETE FROM [%s] WHERE jobId = %d", __getTableName(__uid), jobId);
__database->execute(query, NULL);
void JobInfoDatabase::setStarted(int jobId, bool started)
{
char* query = sqlite3_mprintf(
- "UPDATE " JOB_INFO_TABLE " SET state = '%s' WHERE uid = %d AND jobId = %d",
- started ? ST_STARTED : EMPTY_STR, static_cast<int>(__uid), jobId);
+ "UPDATE [%s] SET state = '%s' WHERE jobId = %d",
+ __getTableName(__uid), started ? ST_STARTED : EMPTY_STR, jobId);
__database->execute(query, NULL);
std::vector<std::pair<std::string, JobInfo*>> jobInfos;
std::vector<std::shared_ptr<Tuple>> queryResult;
- std::string query =
- "SELECT owner, jobInfo, state FROM " JOB_INFO_TABLE " WHERE uid = "
- + std::to_string(__uid);
+ char* query = sqlite3_mprintf(
+ "SELECT owner, jobInfo, state FROM [%s]", __getTableName(__uid));
__database->execute(query, &queryResult);
+ sqlite3_free(query);
+
for (auto& tuple : queryResult) {
const char* owner = NULL;
const char* jobInfoStr = NULL;