hkuserver continue

This commit is contained in:
fasiondog 2021-04-08 00:53:37 +08:00
parent 0d3b6a04a6
commit cb7ad690cf
6 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,82 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-04-08
* Author: fasiondog
*/
#include "DBUpgrade.h"
#include "../../debug.h"
namespace hku {
/*
*
*/
void HKU_API DBUpgrade(const DBConnectPtr &driver, const char *module_name,
const std::vector<string> &upgrade_scripts, int start_version,
const char *create_script) {
HKU_TRACE("check {} database version ...", module_name);
// 如果模块版本表不存在,则创建该表
if (!driver->tableExist("module_version")) {
driver->exec(
"CREATE TABLE `module_version` (`id` INTEGER PRIMARY KEY AUTOINCREMENT,`module` TEXT, "
"`version` INTEGER NOT NULL);");
}
// 尝试获取模板数据库版本
int version = 0;
try {
version = driver->queryInt(format(
"select `version` from `module_version` where module=\"{}\" limit 1", module_name));
} catch (...) {
// Do noting
}
// 模块数据库版本为0不存在模块数据库如果指定了数据库创建脚本则创建数据库否则直接返回
if (0 == version) {
if (!create_script) {
return;
}
// 创建数据库并将模块数据库版本设为1
driver->exec(create_script);
driver->exec(format("INSERT INTO `module_version` (module, version) VALUES (\"{}\", 1);",
module_name));
version = 1;
}
// 缺失中间版本的升级脚本
if (version < start_version - 1) {
HKU_ERROR("THe {} database is too old, can't upgrade!", module_name);
return;
}
size_t upgrade_scripts_count = upgrade_scripts.size();
// 不存在升级脚本,直接返回
if (0 == upgrade_scripts_count) {
return;
}
// 需要被升级到的最终版本
int to_version = start_version + upgrade_scripts_count - 1;
HKU_TRACE("current {} database version: {}", module_name, version);
if (version >= to_version) {
// YH_TRACE("current version greater the upgrade version, ignored!");
return;
}
int start_index = start_version - version - 1;
HKU_TRACE("update {} database ...", module_name);
for (int i = start_index; i < upgrade_scripts_count; i++) {
driver->exec(upgrade_scripts[i]);
}
driver->exec(format("UPDATE module_version SET `version`={} where `module`=\"{}\"", to_version,
module_name));
}
} // namespace hku

View File

@ -0,0 +1,27 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-04-08
* Author: fasiondog
*/
#pragma once
#include "DBConnectBase.h"
namespace hku {
/**
*
* @param driver
* @param module_name
* @param upgrade_scripts
* @param start_version
* @param create_script 使
* @ingroup DataDriver
*/
void HKU_API DBUpgrade(const DBConnectPtr &driver, const char *module_name,
const std::vector<string> &upgrade_scripts, int start_version = 2,
const char *create_script = nullptr);
} // namespace hku

View File

@ -0,0 +1,14 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-04-08
* Author: fasiondog
*/
#pragma once
namespace hku {
void init_trade_db();
} // namespace hku

View File

@ -0,0 +1,21 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-04-08
* Author: fasiondog
*/
namespace hku {
const char *g_sqlite_create_db{
R"(
CREATE TABLE "td_version" (
"id" INTEGER NOT NULL UNIQUE,
"module" TEXT NOT NULL,
"version" INTEGER NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT)
);
)"};
}

View File

@ -0,0 +1,14 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-04-08
* Author: fasiondog
*/
#pragma once
namespace hku {
extern const char *g_sqlite_create_db;
}

Binary file not shown.