promote database init func and fix 2dcruve export error

This commit is contained in:
yyc12345 2022-08-08 21:55:50 +08:00
parent 0b39fd6480
commit a3d6c0f04f
3 changed files with 75 additions and 56 deletions

View File

@ -36,8 +36,27 @@ namespace SSMaterializer {
#pragma region universal sqlite database init and free
SSMaterializerDatabase::SSMaterializerDatabase(const char* file) :
SSMaterializerDatabase::SSMaterializerDatabase() :
mStmtCache(), mDb(NULL) {
;
}
SSMaterializerDatabase::~SSMaterializerDatabase() {
;
}
sqlite3_stmt* SSMaterializerDatabase::CreateStmt(const char* str_stmt) {
int result;
sqlite3_stmt* stmt = NULL;
result = sqlite3_prepare_v2(mDb, str_stmt, -1, &stmt, NULL);
if (result != SQLITE_OK) return NULL;
// append new one
mStmtCache.push_back(stmt);
return stmt;
}
void SSMaterializerDatabase::FakeConstructor(const char* file) {
// open mDb
int result;
result = sqlite3_open(file, &mDb);
@ -47,6 +66,13 @@ namespace SSMaterializer {
result = sqlite3_exec(mDb, "PRAGMA synchronous = OFF;", NULL, NULL, NULL);
if (result != SQLITE_OK) goto fail;
// run init
if (!Init()) goto fail;
//start job
result = sqlite3_exec(mDb, "begin;", NULL, NULL, NULL);
if (result != SQLITE_OK) goto fail;
return;
fail:
sqlite3_close(mDb);
@ -54,10 +80,17 @@ namespace SSMaterializer {
mDb = NULL;
}
SSMaterializerDatabase::~SSMaterializerDatabase() {
void SSMaterializerDatabase::FakeDeconstructor() {
if (mDb == NULL) return;
int result;
//commit job
result = sqlite3_exec(mDb, "commit;", NULL, NULL, NULL);
if (result != SQLITE_OK) goto fail;
// run extra stuff
if (!Finalize()) goto fail;
//free all cached stmts and commit job
for (auto it = mStmtCache.begin(); it != mStmtCache.end(); it++) {
if (*it != NULL) {
@ -72,15 +105,12 @@ namespace SSMaterializer {
mDb = NULL;
}
sqlite3_stmt* SSMaterializerDatabase::CreateStmt(const char* str_stmt) {
int result;
sqlite3_stmt* stmt = NULL;
result = sqlite3_prepare_v2(mDb, str_stmt, -1, &stmt, NULL);
if (result != SQLITE_OK) return NULL;
BOOL SSMaterializerDatabase::Init() {
return TRUE;
}
// append new one
mStmtCache.push_back(stmt);
return stmt;
BOOL SSMaterializerDatabase::Finalize() {
return TRUE;
}
#pragma endregion
@ -88,53 +118,21 @@ namespace SSMaterializer {
#pragma region sub-database constructor, deconstructor and help functions
DocumentDatabase::DocumentDatabase(const char* file, CKParameterManager* paramManager) :
SSMaterializerDatabase(file), mUniqueAttr(), mUniqueObj(), mDbHelper(paramManager) {
if (!Init()) goto fail;
//start job
int result = sqlite3_exec(mDb, "begin;", NULL, NULL, NULL);
if (result != SQLITE_OK) goto fail;
fail:
sqlite3_close(mDb);
mDb = NULL;
SSMaterializerDatabase(), mUniqueAttr(), mUniqueObj(), mDbHelper(paramManager) {
FakeConstructor(file);
}
DocumentDatabase::~DocumentDatabase() {
if (!Finalize()) goto fail;
//commit job
int result = sqlite3_exec(mDb, "commit;", NULL, NULL, NULL);
if (result != SQLITE_OK) goto fail;
fail:
sqlite3_close(mDb);
mDb = NULL;
FakeDeconstructor();
}
EnvironmentDatabase::EnvironmentDatabase(const char* file) :
SSMaterializerDatabase(file), mDbHelper() {
if (!Init()) goto fail;
//start job
int result = sqlite3_exec(mDb, "begin;", NULL, NULL, NULL);
if (result != SQLITE_OK) goto fail;
fail:
sqlite3_close(mDb);
mDb = NULL;
SSMaterializerDatabase(), mDbHelper() {
FakeConstructor(file);
}
EnvironmentDatabase::~EnvironmentDatabase() {
if (!Finalize()) goto fail;
//commit job
int result = sqlite3_exec(mDb, "commit;", NULL, NULL, NULL);
if (result != SQLITE_OK) goto fail;
fail:
sqlite3_close(mDb);
mDb = NULL;
FakeDeconstructor();
}
BOOL DocumentDatabase::is_attr_duplicated(DataStruct::EXPAND_CK_ID parents) {
@ -169,6 +167,9 @@ namespace SSMaterializer {
if (result != SQLITE_OK) { return FALSE; }
BOOL DocumentDatabase::Init() {
// execute parent first
if (!SSMaterializerDatabase::Init()) return FALSE;
int result;
//Init table
@ -202,6 +203,9 @@ if (result != SQLITE_OK) { return FALSE; }
}
BOOL DocumentDatabase::Finalize() {
// execute parent first
if (!SSMaterializerDatabase::Finalize()) return FALSE;
//create index for quick select in SuperScriptDecorator
int result;
@ -225,6 +229,9 @@ if (result != SQLITE_OK) { return FALSE; }
}
BOOL EnvironmentDatabase::Init() {
// execute parent first
if (!SSMaterializerDatabase::Init()) return FALSE;
int result;
//init table
@ -242,6 +249,9 @@ if (result != SQLITE_OK) { return FALSE; }
}
BOOL EnvironmentDatabase::Finalize() {
// execute parent first
if (!SSMaterializerDatabase::Finalize()) return FALSE;
return TRUE;
}

View File

@ -306,11 +306,15 @@ namespace SSMaterializer {
class SSMaterializerDatabase {
public:
SSMaterializerDatabase(const char* file);
SSMaterializerDatabase();
virtual ~SSMaterializerDatabase();
protected:
sqlite3_stmt* CreateStmt(const char* stmt);
void FakeConstructor(const char* file);
void FakeDeconstructor();
virtual BOOL Init();
virtual BOOL Finalize();
sqlite3* mDb;
std::vector<sqlite3_stmt*> mStmtCache;
@ -348,8 +352,8 @@ namespace SSMaterializer {
BOOL is_obj_duplicated(DataStruct::EXPAND_CK_ID parents);
protected:
BOOL Init();
BOOL Finalize();
virtual BOOL Init() override;
virtual BOOL Finalize() override;
std::set<DataStruct::EXPAND_CK_ID> mUniqueAttr;
std::set<DataStruct::EXPAND_CK_ID> mUniqueObj;
@ -368,8 +372,8 @@ namespace SSMaterializer {
void write_variable(DataStruct::dbenv_variable& data);
protected:
BOOL Init();
BOOL Finalize();
virtual BOOL Init() override;
virtual BOOL Finalize() override;
};
}

View File

@ -561,6 +561,7 @@ namespace SSMaterializer {
CKParameter* p = NULL;
CK_CLASSID objcls;
int param_size = 0;
BOOL need_evaluate = FALSE;
for (int row = 0; row < rows; ++row) {
mDb->mDbHelper.array_cell.column = col;
@ -578,11 +579,13 @@ namespace SSMaterializer {
// use class id
objcls = obj->GetClassID();
if (objcls == CKCID_PARAMETER || objcls == CKCID_PARAMETERLOCAL || objcls == CKCID_PARAMETEROUT) {
need_evaluate = objcls == CKCID_PARAMETEROUT;
// CKParameter
p = (CKParameter*)obj;
param_size = p->GetStringValue(NULL, FALSE);
param_size = p->GetStringValue(NULL, need_evaluate);
mDb->mDbHelper.array_cell.showcase.resize(param_size);
p->GetStringValue((char*)mDb->mDbHelper.array_cell.showcase.data(), FALSE);
p->GetStringValue((char*)mDb->mDbHelper.array_cell.showcase.data(), need_evaluate);
mDb->mDbHelper.array_cell.inner_param = p->GetID();
@ -715,7 +718,8 @@ namespace SSMaterializer {
}
if (t == CKPGUID_2DCURVE) {
//CK2dCurve* c;
CK2dCurve* c = (CK2dCurve*)p->GetReadDataPtr(false);
CK2dCurve* c = NULL;
memcpy(&c, p->GetReadDataPtr(false), sizeof(c));
// we need construct a fake json body as our data
static std::string str_2dcurve;
@ -757,6 +761,7 @@ namespace SSMaterializer {
str_2dcurve += '}';
}
str_2dcurve = "]";
DataDictWritter("2dcurve", str_2dcurve.c_str(), mDb, parents);
return;
}