跳到主要内容

sql_conf

简介

使用sqlite进行配置加载和打包 注意事项: table_name不可重复,相当于配置表的表名 数组需要先加载到缓存后才可以通过下标get 支持服务器使用, 服务器启动前载入db配置,并且将所有配置缓存起来 TODO: 记录table拆分到db的映射,提供一个拆分配置

组件接口

sql_conf.gs

函数原型函数作用
bool setup(map para = )初始化sql_conf
string get_default_db_name()获取默认连接的db
void load_db(string db_path, bool is_default = true)加载数据库
void unload_db(string db_name = nil)卸载数据库
object get_db_conn(string db_name = nil)获取加载了的数据库
mixed get(string table_name, mixed key, bool merge_dbase = true, bool cache = true, string db_name = nil)读取一行配置
mixed get_table(string table_name, bool merge_dbase = true, bool cache = true, string db_name = nil)读取整张表,默认会缓存到内存方便下次读取
bool is_table_exist(string table_name)指定表是否打进了sqlite中
void clear_cache()清空缓存
void clear_table_cache(string table_name)清空指定表缓存
void create_db_from_etc(string db_out_path = nil, bool clear_db = true, function restore_func = nil, string postfix = "*", function filter_func = nil)清空并重新根据配置生成db,目前打包配置都放到一个db中
void create_db_from_paths(array paths, string db_out_path = nil, bool clear_db = true, function restore_func = nil)将制定路径的配置打进一个db中
void create_table(string db_path, string table_name, mixed datas, string origin_path)向数据库中覆盖创建一个table
map get_all_table_create_records(object conn)获取所有表创建时的时间和原始路径等信息
map get_table_create_record(string table_name)获取指定表创建时的时间和原始路径等信息

样例

public void main()
{
// 示例先自行构建db,实际该步骤应放在打包阶段
build_sql_conf();

// 是否自动根据etc目录创建db,会执行脏数据检查,只有改动配置表更新
bool rebuild_sqlite = false;
// 初始化sql_conf
sql_conf.setup({"build":rebuild_sqlite});

// 所有从etc读取配置表的操作,setup后会被sql_conf劫持,优先尝试从db读取
write("get row data:\n", etc.get_table_value("sample_table", 10001), "\n");

write("get table datas:\n", etc.__get_table("sample_table"), "\n");
}

public void build_sql_conf()
{
// 项目可根据自己需求处理打包逻辑,以下为几种示例打包
// 0.将etc下所有配置打包到一个db中,读取方式使用etc默认的NormalTableData
sql_conf.create_db_from_etc();

// 1. 将etc下指定目录配置打包到指定db,读取方式使用etc默认的NormalTableData
// sql_conf.create_db_from_paths("/etc/", "/etc/__SQL_CONF__.db");

// 2. 将etc下指定目录配置打包到指定db,自定义重载
// sql_conf.create_db_from_paths("/etc/", "/etc/__SQL_CONF__.db", true, restore_file_data);

// 3. 已有数据,将指定配置表打入指定db中
map sample_data = {
10001 : {"id":10001, "key_0" : 10, "key_1":11},
10002 : {"id":10002, "key_0" : 110, "key_1":111}
};
sql_conf.create_table("/etc/__SQL_CONF__.db", "sample_table", sample_data, "etc/sample_data.xlsx");
}


mixed restore_file_data(string file_path)
{
// 示例读取,实际可根据需求自定义加载方式
string table_name = directory.pure_name(file_path);
mixed data = etc.__get_table(table_name);
return data;
}

main();