mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-12-02 03:48:19 +08:00
hkuserver continue
This commit is contained in:
parent
0d3b6a04a6
commit
cb7ad690cf
82
hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.cpp
Normal file
82
hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.cpp
Normal 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
|
27
hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.h
Normal file
27
hikyuu_cpp/hikyuu/utilities/db_connect/DBUpgrade.h
Normal 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
|
14
hikyuu_cpp/hikyuu_server/service/trade/db/sqlite/initdb.h
Normal file
14
hikyuu_cpp/hikyuu_server/service/trade/db/sqlite/initdb.h
Normal 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
|
@ -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)
|
||||
);
|
||||
|
||||
)"};
|
||||
|
||||
}
|
@ -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.
Loading…
Reference in New Issue
Block a user