使用单独的表存储索引,提高插入和删除时的效率

This commit is contained in:
luoliangyi 2022-06-22 20:38:39 +08:00
parent 66a5b61f64
commit ec5a1bff92
2 changed files with 241 additions and 139 deletions

View File

@ -94,7 +94,22 @@ namespace ver_2
ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL);
assert(0 == ret); assert(0 == ret);
ret = sqlite3_exec(m_sqlite, "create table 'table_default' (id integer primary key autoincrement, idx integer, format text, tag text, image blob, thumb blob)", NULL, NULL, NULL); do
{
ret = sqlite3_exec(m_sqlite, "create table 'table_default' (id integer primary key autoincrement, format text, tag text, image blob, thumb blob)", NULL, NULL, NULL);
if (0 != ret)
break;
ret = sqlite3_exec(m_sqlite, "create table 'table_default_idx' (id integer, idx integer)", NULL, NULL, NULL);
if (0 != ret)
break;
ret = sqlite3_exec(m_sqlite, "insert into table_ (name) values ('default')", NULL, NULL, NULL);
if (0 != ret)
break;
} while (0);
if (0 != ret) if (0 != ret)
{ {
ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL);
@ -102,17 +117,8 @@ namespace ver_2
} }
else else
{ {
ret = sqlite3_exec(m_sqlite, "insert into table_ (name) values ('default')", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "commit", NULL, NULL, NULL);
if (0 != ret) assert(0 == ret);
{
ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL);
assert(0 == ret);
}
else
{
ret = sqlite3_exec(m_sqlite, "commit", NULL, NULL, NULL);
assert(0 == ret);
}
} }
} }
@ -120,8 +126,8 @@ namespace ver_2
ret = sqlite3_prepare(m_sqlite, "select name from table_ order by id desc", -1, &stmt, NULL); ret = sqlite3_prepare(m_sqlite, "select name from table_ order by id desc", -1, &stmt, NULL);
assert(0 == ret); assert(0 == ret);
ret = sqlite3_step(stmt); ret = sqlite3_step(stmt);
assert(SQLITE_ROW == ret); if (SQLITE_ROW == ret)
m_currBatchId = (const char*)sqlite3_column_text(stmt, 0); m_currBatchId = (const char*)sqlite3_column_text(stmt, 0);
ret = sqlite3_finalize(stmt); ret = sqlite3_finalize(stmt);
assert(0 == ret); assert(0 == ret);
} }
@ -1367,6 +1373,15 @@ namespace ver_2
int ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL); int ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL);
assert(0 == ret); assert(0 == ret);
sprintf(sql, "drop table 'table_%s_idx'", batchId.c_str());
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret)
{
ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL);
assert(0 == ret);
return -1;
}
sprintf(sql, "drop table 'table_%s'", batchId.c_str()); sprintf(sql, "drop table 'table_%s'", batchId.c_str());
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) if (0 != ret)
@ -1405,7 +1420,16 @@ namespace ver_2
int ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL); int ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL);
assert(0 == ret); assert(0 == ret);
sprintf(sql, "create table 'table_%s' (id integer primary key autoincrement, idx integer, format text, tag text, image blob, thumb blob)", batchId.c_str()); sprintf(sql, "create table 'table_%s' (id integer primary key autoincrement, format text, tag text, image blob, thumb blob)", batchId.c_str());
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret)
{
ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL);
assert(0 == ret);
return -1;
}
sprintf(sql, "create table 'table_%s_idx' (id integer, idx integer)", batchId.c_str());
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) if (0 != ret)
{ {
@ -1472,6 +1496,15 @@ namespace ver_2
return -1; return -1;
} }
sprintf(sql, "alter table 'table_%s_idx' rename to 'table_%s_idx'", batchId.c_str(), newBatchId.c_str());
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret)
{
ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL);
assert(0 == ret);
return -1;
}
sprintf(sql, "update table_ set name = '%s' where name = '%s'", newBatchId.c_str(), batchId.c_str()); sprintf(sql, "update table_ set name = '%s' where name = '%s'", newBatchId.c_str(), batchId.c_str());
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) if (0 != ret)
@ -1546,7 +1579,8 @@ namespace ver_2
sqlite3_stmt* stmt = NULL; sqlite3_stmt* stmt = NULL;
char sql[256]; char sql[256];
sprintf(sql, "select format, tag, thumb from 'table_%s' order by idx asc", m_currBatchId.c_str()); sprintf(sql, "select format, tag, thumb from table_%s join table_%s_idx on table_%s.id == table_%s_idx.id order by table_%s_idx.idx asc",
m_currBatchId.c_str(), m_currBatchId.c_str(), m_currBatchId.c_str(), m_currBatchId.c_str(), m_currBatchId.c_str());
int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL); int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret); assert(0 == ret);
@ -1600,7 +1634,7 @@ namespace ver_2
char** result = NULL; char** result = NULL;
char sql[256]; char sql[256];
sprintf(sql, "select id from 'table_%s'", m_currBatchId.c_str()); sprintf(sql, "select id from 'table_%s_idx'", m_currBatchId.c_str());
int rows, cols; int rows, cols;
int ret = sqlite3_get_table(m_sqlite, sql, &result, &rows, &cols, NULL); int ret = sqlite3_get_table(m_sqlite, sql, &result, &rows, &cols, NULL);
assert(0 == ret); assert(0 == ret);
@ -1621,11 +1655,15 @@ namespace ver_2
if (NULL == m_sqlite) if (NULL == m_sqlite)
return -1; return -1;
int id = GetId(imageIndex);
if (-1 == id)
return -1;
int rc = -1; int rc = -1;
sqlite3_stmt* stmt = NULL; sqlite3_stmt* stmt = NULL;
char sql[256]; char sql[256];
sprintf(sql, "select format, tag, image from 'table_%s' where idx = %d", m_currBatchId.c_str(), imageIndex); sprintf(sql, "select format, tag, image from 'table_%s' where id = '%d'", m_currBatchId.c_str(), id);
int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL); int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret); assert(0 == ret);
ret = sqlite3_step(stmt); ret = sqlite3_step(stmt);
@ -1673,11 +1711,15 @@ namespace ver_2
if (NULL == m_sqlite) if (NULL == m_sqlite)
return -1; return -1;
int id = GetId(imageIndex);
if (-1 == id)
return -1;
int rc = -1; int rc = -1;
sqlite3_stmt* stmt = NULL; sqlite3_stmt* stmt = NULL;
char sql[256]; char sql[256];
sprintf(sql, "select format, image from 'table_%s' where idx = %d", m_currBatchId.c_str(), imageIndex); sprintf(sql, "select format, image from 'table_%s' where id = '%d'", m_currBatchId.c_str(), id);
int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL); int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret); assert(0 == ret);
ret = sqlite3_step(stmt); ret = sqlite3_step(stmt);
@ -1746,42 +1788,56 @@ namespace ver_2
ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL);
assert(0 == ret); assert(0 == ret);
bool ok = true; do
for (int i = 0; i < (int)tables.size(); ++i)
{ {
if (i >= insertPos) sprintf(sql, "insert into 'table_%s' (format, tag, image, thumb) values ('%s', '%s', ?, ?)", m_currBatchId.c_str(),
imgFormat.c_str(), imageTag.c_str());
sqlite3_stmt* stmt = NULL;
ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 1, imgData, (int)imgSize, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 2, thumbData, (int)thumbSize, NULL);
assert(0 == ret);
sqlite3_step(stmt);
ret = sqlite3_finalize(stmt);
if (0 != ret)
{ {
sprintf(sql, "update 'table_%s' set idx = '%d' where id = '%d'", break;
m_currBatchId.c_str(), i + 1, tables[i].id); }
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) bool ok = true;
for (int i = 0; i < (int)tables.size(); ++i)
{
if (i >= insertPos)
{ {
ok = false; sprintf(sql, "update 'table_%s_idx' set idx = '%d' where id = '%d'",
break; m_currBatchId.c_str(), i + 1, tables[i].id);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret)
{
ok = false;
break;
}
} }
} }
}
if (!ok) if (!ok)
{ {
ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL); break;
assert(0 == ret); }
delete[] thumbData;
delete[] imgData; int id = (int)sqlite3_last_insert_rowid(m_sqlite);
return -1; sprintf(sql, "insert into 'table_%s_idx' (id, idx) values ('%d', '%d')", m_currBatchId.c_str(),
} id, insertPos);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret)
{
break;
}
} while (0);
sprintf(sql, "insert into 'table_%s' (idx, format, tag, image, thumb) values ('%d', '%s', '%s', ?, ?)", m_currBatchId.c_str(),
insertPos, imgFormat.c_str(), imageTag.c_str());
sqlite3_stmt* stmt = NULL;
ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 1, imgData, (int)imgSize, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 2, thumbData, (int)thumbSize, NULL);
assert(0 == ret);
sqlite3_step(stmt);
ret = sqlite3_finalize(stmt);
if (0 != ret) if (0 != ret)
{ {
ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL);
@ -1844,42 +1900,56 @@ namespace ver_2
ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL);
assert(0 == ret); assert(0 == ret);
bool ok = true; do
for (int i = 0; i < (int)tables.size(); ++i)
{ {
if (i >= insertPos) sprintf(sql, "insert into 'table_%s' (format, tag, image, thumb) values ('%s', '%s', ?, ?)", m_currBatchId.c_str(),
imgFormat.c_str(), imageTag.c_str());
sqlite3_stmt* stmt = NULL;
ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 1, imgData, (int)imgSize, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 2, thumbData, (int)thumbSize, NULL);
assert(0 == ret);
sqlite3_step(stmt);
ret = sqlite3_finalize(stmt);
if (0 != ret)
{ {
sprintf(sql, "update 'table_%s' set idx = '%d' where id = '%d'", break;
m_currBatchId.c_str(), i + 1, tables[i].id); }
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) bool ok = true;
for (int i = 0; i < (int)tables.size(); ++i)
{
if (i >= insertPos)
{ {
ok = false; sprintf(sql, "update 'table_%s_idx' set idx = '%d' where id = '%d'",
break; m_currBatchId.c_str(), i + 1, tables[i].id);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret)
{
ok = false;
break;
}
} }
} }
}
if (!ok) if (!ok)
{ {
ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL); break;
assert(0 == ret); }
delete[] thumbData;
delete[] imgData; int id = (int)sqlite3_last_insert_rowid(m_sqlite);
return -1; sprintf(sql, "insert into 'table_%s_idx' (id, idx) values ('%d', '%d')", m_currBatchId.c_str(),
} id, insertPos);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret)
{
break;
}
} while (0);
sprintf(sql, "insert into 'table_%s' (idx, format, tag, image, thumb) values ('%d', '%s', '%s', ?, ?)", m_currBatchId.c_str(),
insertPos, imgFormat.c_str(), imageTag.c_str());
sqlite3_stmt* stmt = NULL;
ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 1, imgData, (int)imgSize, NULL);
assert(0 == ret);
ret = sqlite3_bind_blob(stmt, 2, thumbData, (int)thumbSize, NULL);
assert(0 == ret);
sqlite3_step(stmt);
ret = sqlite3_finalize(stmt);
if (0 != ret) if (0 != ret)
{ {
ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL);
@ -1936,8 +2006,9 @@ namespace ver_2
bool ok = true; bool ok = true;
for (int i = 0; i < (int)imageIndexList.size(); ++i) for (int i = 0; i < (int)imageIndexList.size(); ++i)
{ {
sprintf(sql, "update 'table_%s' set tag = '%s' where idx = '%d'", m_currBatchId.c_str(), int id = tables[imageIndexList[i]].id;
imageTagList[i].c_str(), imageIndexList[i]); sprintf(sql, "update 'table_%s' set tag = '%s' where id = '%d'",
m_currBatchId.c_str(), imageTagList[i].c_str(), id);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) if (0 != ret)
{ {
@ -1992,7 +2063,17 @@ namespace ver_2
bool ok = true; bool ok = true;
for (int i = 0; i < (int)imageIndexList.size(); ++i) for (int i = 0; i < (int)imageIndexList.size(); ++i)
{ {
sprintf(sql, "delete from 'table_%s' where idx = '%d'", m_currBatchId.c_str(), imageIndexList[i]); int id = tables[imageIndexList[i]].id;
sprintf(sql, "delete from 'table_%s' where id = '%d'", m_currBatchId.c_str(), id);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret)
{
ok = false;
break;
}
sprintf(sql, "delete from 'table_%s_idx' where id = '%d'", m_currBatchId.c_str(), id);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) if (0 != ret)
{ {
@ -2029,7 +2110,7 @@ namespace ver_2
{ {
if (value > 0) if (value > 0)
{ {
sprintf(sql, "update 'table_%s' set idx = '%d' where id = '%d'", sprintf(sql, "update 'table_%s_idx' set idx = '%d' where id = '%d'",
m_currBatchId.c_str(), i - value, tables[i].id); m_currBatchId.c_str(), i - value, tables[i].id);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) if (0 != ret)
@ -2083,6 +2164,15 @@ namespace ver_2
return -1; return -1;
} }
sprintf(sql, "delete from 'table_%s_idx'", m_currBatchId.c_str());
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret)
{
ret = sqlite3_exec(m_sqlite, "rollback", NULL, NULL, NULL);
assert(0 == ret);
return -1;
}
ret = sqlite3_exec(m_sqlite, "commit", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "commit", NULL, NULL, NULL);
assert(0 == ret); assert(0 == ret);
@ -2132,8 +2222,9 @@ namespace ver_2
ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL);
assert(0 == ret); assert(0 == ret);
sprintf(sql, "update 'table_%s' set format = '%s', image = ?, thumb = ? where idx = '%d'", int id = tables[imageIndex].id;
m_currBatchId.c_str(), imgFormat.c_str(), imageIndex); sprintf(sql, "update 'table_%s' set format = '%s', image = ?, thumb = ? where id = '%d'",
m_currBatchId.c_str(), imgFormat.c_str(), id);
sqlite3_stmt* stmt = NULL; sqlite3_stmt* stmt = NULL;
ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL); ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret); assert(0 == ret);
@ -2203,8 +2294,9 @@ namespace ver_2
ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL);
assert(0 == ret); assert(0 == ret);
sprintf(sql, "update 'table_%s' set format = '%s', image = ?, thumb = ? where idx = '%d'", int id = tables[imageIndex].id;
m_currBatchId.c_str(), imgFormat.c_str(), imageIndex); sprintf(sql, "update 'table_%s' set format = '%s', image = ?, thumb = ? where id = '%d'",
m_currBatchId.c_str(), imgFormat.c_str(), id);
sqlite3_stmt* stmt = NULL; sqlite3_stmt* stmt = NULL;
ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL); ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret); assert(0 == ret);
@ -2249,6 +2341,7 @@ namespace ver_2
std::vector<BatchTableInfo> tables; std::vector<BatchTableInfo> tables;
GetBatchTableInfo(tables); GetBatchTableInfo(tables);
std::vector<BatchTableInfo> tables2 = tables;
if (tables.empty()) if (tables.empty())
return -1; return -1;
@ -2260,7 +2353,7 @@ namespace ver_2
bool indexValid = true; bool indexValid = true;
for (int i = 0; i < (int)imageIndexList.size(); ++i) for (int i = 0; i < (int)imageIndexList.size(); ++i)
{ {
if (imageIndexList[i] >= (int)tables.size()) if (imageIndexList[i] < 0 || imageIndexList[i] >= (int)tables.size())
{ {
indexValid = false; indexValid = false;
break; break;
@ -2270,15 +2363,22 @@ namespace ver_2
if (!indexValid) if (!indexValid)
return -1; return -1;
std::vector<int> idList;
for (int i = 0; i < (int)imageIndexList.size(); ++i)
{
int id = tables[imageIndexList[i]].id;
idList.push_back(id);
}
if ("pos" == mode) if ("pos" == mode)
{ {
int posEx = target; int posEx = target;
for (int i = 0; i < (int)imageIndexList.size(); ++i) for (int i = 0; i < (int)idList.size(); ++i)
{ {
int oldIndex = -1; int oldIndex = -1;
for (int j = 0; j < (int)tables.size(); ++j) for (int j = 0; j < (int)tables.size(); ++j)
{ {
if (imageIndexList[i] == tables[j].idx) if (idList[i] == tables[j].id)
{ {
oldIndex = j; oldIndex = j;
break; break;
@ -2303,12 +2403,12 @@ namespace ver_2
else else
{ {
std::vector<BatchTableInfo> infos; std::vector<BatchTableInfo> infos;
for (int i = 0; i < (int)imageIndexList.size(); ++i) for (int i = 0; i < (int)idList.size(); ++i)
{ {
int oldIndex = -1; int oldIndex = -1;
for (int j = 0; j < (int)tables.size(); ++j) for (int j = 0; j < (int)tables.size(); ++j)
{ {
if (imageIndexList[i] == tables[j].idx) if (idList[i] == tables[j].id)
{ {
oldIndex = j; oldIndex = j;
break; break;
@ -2349,7 +2449,7 @@ namespace ver_2
bool ok = true; bool ok = true;
for (int i = 0; i < (int)tables.size(); ++i) for (int i = 0; i < (int)tables.size(); ++i)
{ {
sprintf(sql, "update 'table_%s' set idx = '%d' where id = '%d'", sprintf(sql, "update 'table_%s_idx' set idx = '%d' where id = '%d'",
m_currBatchId.c_str(), i, tables[i].id); m_currBatchId.c_str(), i, tables[i].id);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) if (0 != ret)
@ -2371,7 +2471,7 @@ namespace ver_2
if (!m_bindFolder.empty()) if (!m_bindFolder.empty())
{ {
ClearBindFolderImageList(tables); ClearBindFolderImageList(tables2);
UpdateBindFolder(); UpdateBindFolder();
} }
@ -2389,7 +2489,8 @@ namespace ver_2
std::vector<BatchTableInfo> tables; std::vector<BatchTableInfo> tables;
GetBatchTableInfo(tables); GetBatchTableInfo(tables);
if (imageIndex1 >= (int)tables.size() || imageIndex2 >= (int)tables.size()) if (imageIndex1 < 0 || imageIndex1 >= (int)tables.size()
|| imageIndex2 < 0 || imageIndex2 >= (int)tables.size())
return -1; return -1;
int ret; int ret;
@ -2398,7 +2499,7 @@ namespace ver_2
ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, "begin", NULL, NULL, NULL);
assert(0 == ret); assert(0 == ret);
sprintf(sql, "update 'table_%s' set idx = '%d' where id = '%d'", sprintf(sql, "update 'table_%s_idx' set idx = '%d' where id = '%d'",
m_currBatchId.c_str(), imageIndex2, tables[imageIndex1].id); m_currBatchId.c_str(), imageIndex2, tables[imageIndex1].id);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) if (0 != ret)
@ -2408,7 +2509,7 @@ namespace ver_2
return -1; return -1;
} }
sprintf(sql, "update 'table_%s' set idx = '%d' where id = '%d'", sprintf(sql, "update 'table_%s_idx' set idx = '%d' where id = '%d'",
m_currBatchId.c_str(), imageIndex1, tables[imageIndex2].id); m_currBatchId.c_str(), imageIndex1, tables[imageIndex2].id);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) if (0 != ret)
@ -2439,6 +2540,7 @@ namespace ver_2
std::vector<BatchTableInfo> tables; std::vector<BatchTableInfo> tables;
GetBatchTableInfo(tables); GetBatchTableInfo(tables);
std::vector<BatchTableInfo> tables2 = tables;
std::list<BatchTableInfo> infos1, infos2; std::list<BatchTableInfo> infos1, infos2;
for (int i = 0; i < (int)tables.size(); ++i) for (int i = 0; i < (int)tables.size(); ++i)
@ -2466,7 +2568,7 @@ namespace ver_2
bool ok = true; bool ok = true;
for (int i = 0; i < (int)tables.size(); ++i) for (int i = 0; i < (int)tables.size(); ++i)
{ {
sprintf(sql, "update 'table_%s' set idx = '%d' where id = '%d'", sprintf(sql, "update 'table_%s_idx' set idx = '%d' where id = '%d'",
m_currBatchId.c_str(), i, tables[i].id); m_currBatchId.c_str(), i, tables[i].id);
ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL); ret = sqlite3_exec(m_sqlite, sql, NULL, NULL, NULL);
if (0 != ret) if (0 != ret)
@ -2488,7 +2590,7 @@ namespace ver_2
if (!m_bindFolder.empty()) if (!m_bindFolder.empty())
{ {
ClearBindFolderImageList(tables); ClearBindFolderImageList(tables2);
UpdateBindFolder(); UpdateBindFolder();
} }
@ -3473,13 +3575,33 @@ namespace ver_2
return ret; return ret;
} }
int ManagerV2::GetId(int idx)
{
int id = -1;
char sql[1024];
sprintf(sql, "select id from 'table_%s_idx' where idx = '%d'", m_currBatchId.c_str(), idx);
sqlite3_stmt* stmt = NULL;
int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret);
ret = sqlite3_step(stmt);
if (SQLITE_ROW == ret)
id = sqlite3_column_int(stmt, 0);
ret = sqlite3_finalize(stmt);
assert(0 == ret);
return id;
}
void ManagerV2::GetBatchTableInfo(std::vector<BatchTableInfo>& tables) void ManagerV2::GetBatchTableInfo(std::vector<BatchTableInfo>& tables)
{ {
tables.clear(); tables.clear();
sqlite3_stmt* stmt = NULL; sqlite3_stmt* stmt = NULL;
char sql[256]; char sql[256];
sprintf(sql, "select id, idx, format from 'table_%s' order by idx asc", m_currBatchId.c_str()); sprintf(sql, "select table_%s.id, table_%s.format from table_%s join table_%s_idx on table_%s.id == table_%s_idx.id order by table_%s_idx.idx asc",
m_currBatchId.c_str(), m_currBatchId.c_str(), m_currBatchId.c_str(), m_currBatchId.c_str(), m_currBatchId.c_str(),
m_currBatchId.c_str(), m_currBatchId.c_str());
int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL); int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret); assert(0 == ret);
@ -3488,8 +3610,7 @@ namespace ver_2
{ {
BatchTableInfo info; BatchTableInfo info;
info.id = sqlite3_column_int(stmt, 0); info.id = sqlite3_column_int(stmt, 0);
info.idx = sqlite3_column_int(stmt, 1); info.format = (const char *)sqlite3_column_text(stmt, 1);
info.format = (const char *)sqlite3_column_text(stmt, 2);
tables.push_back(info); tables.push_back(info);
ret = sqlite3_step(stmt); ret = sqlite3_step(stmt);
@ -3554,8 +3675,10 @@ namespace ver_2
return; return;
sqlite3_stmt* stmt = NULL; sqlite3_stmt* stmt = NULL;
char sql[256]; char sql[1024];
sprintf(sql, "select idx, format, image from 'table_%s'", m_currBatchId.c_str()); sprintf(sql, "select table_%s_idx.idx, table_%s.format, table_%s.image from table_%s join table_%s_idx on table_%s.id == table_%s_idx.id",
m_currBatchId.c_str(), m_currBatchId.c_str(), m_currBatchId.c_str(), m_currBatchId.c_str(), m_currBatchId.c_str(),
m_currBatchId.c_str(), m_currBatchId.c_str());
int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL); int ret = sqlite3_prepare(m_sqlite, sql, -1, &stmt, NULL);
assert(0 == ret); assert(0 == ret);
@ -3591,12 +3714,12 @@ namespace ver_2
for (int i = (int)tables.size() - 1; i >= 0; --i) for (int i = (int)tables.size() - 1; i >= 0; --i)
{ {
if (tables[i].idx >= insertPos) if (i >= insertPos)
{ {
char fileName[256]; char fileName[256];
sprintf(fileName, fmt, m_bindFolder.c_str(), m_bindNameBase + tables[i].idx, tables[i].format.c_str()); sprintf(fileName, fmt, m_bindFolder.c_str(), m_bindNameBase + i, tables[i].format.c_str());
char destName[256]; char destName[256];
sprintf(destName, fmt, m_bindFolder.c_str(), m_bindNameBase + tables[i].idx + 1, tables[i].format.c_str()); sprintf(destName, fmt, m_bindFolder.c_str(), m_bindNameBase + i + 1, tables[i].format.c_str());
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
MoveFileA(fileName, destName); MoveFileA(fileName, destName);
#else #else
@ -3617,16 +3740,7 @@ namespace ver_2
char fmt[24]; char fmt[24];
sprintf(fmt, "%%s%%0%dd.%%s", m_bindNameWidth); sprintf(fmt, "%%s%%0%dd.%%s", m_bindNameWidth);
std::string oldFormat = tables[imageIndex].format;
std::string oldFormat;
for (int i = 0; i < (int)tables.size(); ++i)
{
if (tables[i].idx == imageIndex)
{
oldFormat = tables[i].format;
break;
}
}
char oldFileName[256]; char oldFileName[256];
sprintf(oldFileName, fmt, m_bindFolder.c_str(), m_bindNameBase + imageIndex, oldFormat.c_str()); sprintf(oldFileName, fmt, m_bindFolder.c_str(), m_bindNameBase + imageIndex, oldFormat.c_str());
@ -3646,16 +3760,11 @@ namespace ver_2
for (int i = 0; i < (int)imageIndexList.size(); ++i) for (int i = 0; i < (int)imageIndexList.size(); ++i)
{ {
for (int j = 0; j < (int)tables.size(); ++j) int idx = imageIndexList[i];
{
if (tables[j].idx == imageIndexList[i]) char fileName[256];
{ sprintf(fileName, fmt, m_bindFolder.c_str(), m_bindNameBase + idx, tables[idx].format.c_str());
char fileName[256]; HGBase_DeleteFile(fileName);
sprintf(fileName, fmt, m_bindFolder.c_str(), m_bindNameBase + tables[j].idx, tables[j].format.c_str());
HGBase_DeleteFile(fileName);
break;
}
}
} }
int value = 0; int value = 0;
@ -3664,7 +3773,7 @@ namespace ver_2
bool find = false; bool find = false;
for (int j = 0; j < (int)imageIndexList.size(); ++j) for (int j = 0; j < (int)imageIndexList.size(); ++j)
{ {
if (tables[i].idx == imageIndexList[j]) if (i == imageIndexList[j])
{ {
find = true; find = true;
break; break;
@ -3680,9 +3789,9 @@ namespace ver_2
if (value > 0) if (value > 0)
{ {
char fileName[256]; char fileName[256];
sprintf(fileName, fmt, m_bindFolder.c_str(), m_bindNameBase + tables[i].idx, tables[i].format.c_str()); sprintf(fileName, fmt, m_bindFolder.c_str(), m_bindNameBase + i, tables[i].format.c_str());
char destName[256]; char destName[256];
sprintf(destName, fmt, m_bindFolder.c_str(), m_bindNameBase + tables[i].idx - value, tables[i].format.c_str()); sprintf(destName, fmt, m_bindFolder.c_str(), m_bindNameBase + i - value, tables[i].format.c_str());
#if defined(HG_CMP_MSC) #if defined(HG_CMP_MSC)
MoveFileA(fileName, destName); MoveFileA(fileName, destName);
#else #else
@ -3700,15 +3809,8 @@ namespace ver_2
char fmt[24]; char fmt[24];
sprintf(fmt, "%%s%%0%dd.%%s", m_bindNameWidth); sprintf(fmt, "%%s%%0%dd.%%s", m_bindNameWidth);
std::string format1, format2; std::string format1 = tables[imageIndex1].format;
std::string format2 = tables[imageIndex2].format;
for (int i = 0; i < (int)tables.size(); ++i)
{
if (tables[i].idx == imageIndex1)
format1 = tables[i].format;
else if (tables[i].idx == imageIndex2)
format2 = tables[i].format;
}
char fileName1[256]; char fileName1[256];
sprintf(fileName1, fmt, m_bindFolder.c_str(), m_bindNameBase + imageIndex1, format1.c_str()); sprintf(fileName1, fmt, m_bindFolder.c_str(), m_bindNameBase + imageIndex1, format1.c_str());
@ -3749,7 +3851,7 @@ namespace ver_2
for (int i = 0; i < (int)tables.size(); ++i) for (int i = 0; i < (int)tables.size(); ++i)
{ {
char fileName[256]; char fileName[256];
sprintf(fileName, fmt, m_bindFolder.c_str(), m_bindNameBase + tables[i].idx, tables[i].format.c_str()); sprintf(fileName, fmt, m_bindFolder.c_str(), m_bindNameBase + i, tables[i].format.c_str());
HGBase_DeleteFile(fileName); HGBase_DeleteFile(fileName);
} }
} }

View File

@ -101,7 +101,6 @@ namespace ver_2
struct BatchTableInfo struct BatchTableInfo
{ {
int id; int id;
int idx;
std::string format; std::string format;
}; };
@ -281,6 +280,7 @@ namespace ver_2
static HGByte* LoadThumbFromBase64(const std::string& imageBase64, HGUInt& size); static HGByte* LoadThumbFromBase64(const std::string& imageBase64, HGUInt& size);
static bool SaveToBase64(const HGByte* data, HGUInt size, std::string& base64); static bool SaveToBase64(const HGByte* data, HGUInt size, std::string& base64);
static bool SaveToFile(const HGByte* data, HGUInt size, const std::string &filePath); static bool SaveToFile(const HGByte* data, HGUInt size, const std::string &filePath);
int GetId(int idx);
void GetBatchTableInfo(std::vector<BatchTableInfo>& tables); void GetBatchTableInfo(std::vector<BatchTableInfo>& tables);
void ClearBindFolder(); void ClearBindFolder();
void UpdateBindFolder(); void UpdateBindFolder();