123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- module fundit::sqlUtilities
-
- /*
- * MySQL dev server 连接,使用前应确保 loadPlugin("ODBC")已经被运行过
- *
- * Create 20240711 使用ODBC连接MySQL数据库 Joey
- *
- */
- def connect_mysql(user='pf_user') {
- // 阿里云的mysql被魔改过,当前DolphinDB无法支持MySQL插件,只能用ODBC
- // loadPlugin("ODBC")
- // conn = odbc::connect("Driver={MySQL ODBC 9.0 UNICODE Driver};Server=funditdb-dev.mysql.rds.aliyuncs.com;Database=mfdb;User=pf_user;Password=MzBlMDA0OG", "MySQL")
- // 使用Windows的ODBC数据源事先设置号的连接
- // conn = odbc::connect("Dsn=FunditDB-mfdb")
- s = "Dsn=FunditDB-dev-" + user;
- conn = odbc::connect(s);
- // t = odbc::query(conn, "SELECT * FROM pfdb.pf_portfolio_nav LIMIT 100")
- return conn;
- }
- /*
- * 取本地数据库
- *
- * get_local_database("fundit", "mfdb")
- */
- def get_local_database(server_name, db_name) {
- db = database(directory="D:/Program Files/DolphinDB/server/database/" + server_name + "/" + db_name + "/")
-
- return db
- }
- /*
- * 读本地dolphindb数据表
- *
- * load_table_from_local("fundit", mfdb.fund_performance")
- */
- def load_table_from_local(server_name, table_name) {
- db = get_local_database(server_name, table_name.split(".")[0])
-
- return loadTable(db, table_name.split(".")[1])
- }
- /*
- * 未知形态的id转为MySQL需要的的逗号分隔字符串
- *
- * Example: ids_to_string("'a','b','c'");
- * ids_to_string(['a', NULL, 'c']);
- * ids_to_string([1,2,3]);
- * ids_to_string(12);
- * ids_to_string('').isNull();
- */
- def ids_to_string(ids) {
- s_ids = '';
- if(ids.isVoid()) return s_ids;
- // 输入的 ids 是字符串标量
- if (ids.form() == 0) {
- s_ids = (ids$STRING).trim();
- // 输入的 ids 是字符串向量
- } else if(ids.form() == 1) {
- if(ids.type() == 4) // INTEGER
- s_ids = ids.concat(",").trim();
- else // STRING
- s_ids = "'" + ids.concat("','").trim() + "'";
- // 缺省返回空
- } else {
- s_ids = NULL;
- }
- return s_ids;
- }
- /*
- * 根据不同类型的主体返回其净值表的表名、字段名和ID前两位特征字符
- */
- def get_nav_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col', 'nav_col', 'prefix'],
- [STRING, STRING, STRING, STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.nav', 'mfdb.public_nav', 'pfdb.pf_cus_fund_nav', 'mfdb.stock_price', 'mfdb.market_indexes', 'mfdb.indexes_ty_index', 'pfdb.cm_udf_index_nav', 'pfdb.cm_factor_value', 'pfdb.pf_portfolio_nav'],
- ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'index_id', 'index_id', 'index_id', 'factor_id', 'portfolio_id'],
- ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
- ['nav', 'nav', 'nav', 'nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
- ['HF', 'MF', 'CF', 'EQ', 'IN', 'IN', 'CI', 'FA', '']);
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
-
- }
- /*
- * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
- */
- def get_performance_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col'],
- [STRING, STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.fund_performance', 'mfdb.fund_performance', 'pfdb.pf_cus_fund_performance', 'mfdb.stock_performance', 'mfdb.fund_performance', 'mfdb.fund_performance', 'pfdb.cm_udf_index_performance', 'pfdb.cm_factor_performance', 'pfdb.pf_portfolio_performance'],
- ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'],
- ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
-
- }
- /*
- * 根据不同类型的主体返回其杂项指标的表名、字段名
- *
- * Example: get_indicator_table_description('HF');
- */
- def get_indicator_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col'],
- [STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.fund_indicator', 'mfdb.fund_indicator', 'pfdb.pf_cus_fund_indicator', 'mfdb.fund_indicator', 'mfdb.fund_indicator', 'pfdb.cm_udf_index_indicator', 'pfdb.cm_factor_indicator', 'pfdb.pf_portfolio_indicator'],
- ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
- }
- /*
- * 根据不同类型的主体返回其风险指标的表名、字段名
- *
- * Example: get_risk_stats_table_description('HF');
- */
- def get_risk_stats_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col'],
- [STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.fund_risk_stats', 'mfdb.fund_risk_stats', 'pfdb.pf_cus_fund_risk_stats', 'mfdb.fund_risk_stats', 'mfdb.fund_risk_stats', 'pfdb.cm_udf_index_risk_stats', 'pfdb.cm_factor_risk_stats', 'pfdb.pf_portfolio_risk_stats'],
- ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
- }
- /*
- * 根据不同类型的主体返回其风险调整收益指标的表名、字段名
- *
- * Example: get_riskadjret_stats_table_description('HF');
- */
- def get_riskadjret_stats_table_description(entity_type) {
- tmp_universe = table(100:0,
- ['type', 'table_name', 'sec_id_col'],
- [STRING, STRING, STRING]);
- // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
- INSERT INTO tmp_universe VALUES (
- ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
- ['mfdb.fund_riskadjret_stats', 'mfdb.fund_riskadjret_stats', 'pfdb.pf_cus_fund_riskadjret_stats', 'mfdb.fund_riskadjret_stats', 'mfdb.fund_riskadjret_stats', 'pfdb.cm_udf_index_riskadjret_stats', 'pfdb.cm_factor_riskadjret_stats', 'pfdb.pf_portfolio_riskadjret_stats'],
- ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
- return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
- }
- /*
- * Annulized multiple
- */
- def get_annulization_multiple(freq) {
- ret = 1;
-
- if (freq == 'd') {
- ret = 252; // We have differences here between Java and DolphinDB, Java uses 365.25 days
- } else if (freq == 'w') {
- ret = 52;
- } else if (freq == 'm') {
- ret = 12;
- } else if (freq == 'q') {
- ret = 4;
- } else if (freq == 's') {
- ret = 2;
- } else if (freq == 'a') {
- ret = 1;
- }
-
- return ret;
- }
|