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

View File

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

View File

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