sqlUtilities.dos 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. module fundit::sqlUtilities
  2. /*
  3. * MySQL dev server 连接,使用前应确保 loadPlugin("ODBC")已经被运行过
  4. *
  5. * Create 20240711 使用ODBC连接MySQL数据库 Joey
  6. *
  7. */
  8. def connect_mysql(user='pf_user') {
  9. // 阿里云的mysql被魔改过,当前DolphinDB无法支持MySQL插件,只能用ODBC
  10. // loadPlugin("ODBC")
  11. // 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")
  12. // 使用Windows的ODBC数据源事先设置号的连接
  13. // conn = odbc::connect("Dsn=FunditDB-mfdb")
  14. s = "Dsn=FunditDB-dev-" + user;
  15. conn = odbc::connect(s);
  16. // t = odbc::query(conn, "SELECT * FROM pfdb.pf_portfolio_nav LIMIT 100")
  17. return conn;
  18. }
  19. /*
  20. * 取本地数据库
  21. *
  22. * get_local_database("fundit", "mfdb")
  23. */
  24. def get_local_database(server_name, db_name) {
  25. db = database(directory="D:/Program Files/DolphinDB/server/database/" + server_name + "/" + db_name + "/")
  26. return db
  27. }
  28. /*
  29. * 读本地dolphindb数据表
  30. *
  31. * load_table_from_local("fundit", mfdb.fund_performance")
  32. */
  33. def load_table_from_local(server_name, table_name) {
  34. db = get_local_database(server_name, table_name.split(".")[0])
  35. return loadTable(db, table_name.split(".")[1])
  36. }
  37. /*
  38. * 未知形态的id转为MySQL需要的的逗号分隔字符串
  39. *
  40. * Example: ids_to_string("'a','b','c'");
  41. * ids_to_string(['a', NULL, 'c']);
  42. * ids_to_string([1,2,3]);
  43. * ids_to_string(12);
  44. * ids_to_string('').isNull();
  45. */
  46. def ids_to_string(ids) {
  47. s_ids = '';
  48. if(ids.isVoid()) return s_ids;
  49. // 输入的 ids 是字符串标量
  50. if (ids.form() == 0) {
  51. s_ids = (ids$STRING).trim();
  52. // 输入的 ids 是字符串向量
  53. } else if(ids.form() == 1) {
  54. if(ids.type() == 4) // INTEGER
  55. s_ids = ids.concat(",").trim();
  56. else // STRING
  57. s_ids = "'" + ids.concat("','").trim() + "'";
  58. // 缺省返回空
  59. } else {
  60. s_ids = NULL;
  61. }
  62. return s_ids;
  63. }
  64. /*
  65. * 【初始化数据专用】 返回初始化数据时的各类常数
  66. *
  67. *
  68. */
  69. def get_ini_data_const() {
  70. d = dict(STRING, ANY);
  71. d['date'] = 1989.01.01;
  72. return d;
  73. }
  74. /*
  75. * 返回ID的规律(前缀)
  76. *
  77. */
  78. def get_entity_id_info() {
  79. tmp = table(10:0, ['entity_type', 'prefix'], [STRING, STRING]);
  80. // 公募,私募,私有基金,股票,市场指数,图译指数,图译因子,人物,公司
  81. INSERT INTO tmp VALUES (`MF`HF`CF`EQ`MI`FI`CI`FA`PL`CO, `MF`HF`CF`EQ`IN`IN`CI`FA`PL`CO);
  82. return tmp;
  83. }
  84. /*
  85. * 返回标准RBSA的几组指数集
  86. *
  87. */
  88. def get_rbsa_index() {
  89. d = dict(STRING, ANY);
  90. d['AS0000005Q'] = ['IN00000008', 'IN00000077','IN0000007G', 'IN0000009M'];
  91. d['BondType'] = ['IN0000007A', 'IN0000007G','IN0000008J', 'IN000002CM'];
  92. d['Cap3Style'] = ['FA00000WKG', 'FA00000WKH','IN0000007G'];
  93. d['CNI7Style'] = ['IN0000000S', 'IN0000000T','IN0000000U', 'IN0000000V', 'IN0000000W', 'IN0000000X', 'IN0000007G'];
  94. d['CSI11'] = ['IN0000000Y', 'IN0000000Z','IN00000010', 'IN00000011', 'IN00000012', 'IN00000013', 'IN00000014', 'IN00000015', 'IN00000016', 'IN00000017', 'IN0000007G'];
  95. d['CSI5'] = ['FA00000VML', 'FA00000VMM','FA00000VMN', 'FA00000VMO', 'IN0000007G'];
  96. d['Large4Assets'] = ['IN00000008', 'IN00000077','IN0000007G', 'IN0000009M'];
  97. return d;
  98. }
  99. /*
  100. * 根据不同类型的主体返回其净值表的表名、字段名和ID前两位特征字符
  101. */
  102. def get_nav_table_description(entity_type) {
  103. tmp_universe = table(100:0,
  104. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col', 'nav_col', 'prefix'],
  105. [STRING, STRING, STRING, STRING, STRING, STRING]);
  106. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  107. INSERT INTO tmp_universe VALUES (
  108. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  109. ['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'],
  110. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'index_id', 'index_id', 'index_id', 'factor_id', 'portfolio_id'],
  111. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
  112. ['nav', 'nav', 'nav', 'nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
  113. ['HF', 'MF', 'CF', 'EQ', 'IN', 'IN', 'CI', 'FA', '']);
  114. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  115. }
  116. /*
  117. * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
  118. */
  119. def get_performance_table_description(entity_type) {
  120. tmp_universe = table(100:0,
  121. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col', 'ret_col', 'prefix'],
  122. [STRING, STRING, STRING, STRING, STRING, STRING]);
  123. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  124. INSERT INTO tmp_universe VALUES (
  125. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF', 'PL', 'CO'],
  126. ['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', 'mfdb.manager_performance', 'mfdb.company_performance'],
  127. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id', 'manager_id', 'company_id'],
  128. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav'],
  129. ['ret_1m', 'ret_1m', 'ret_1m', 'ret_1m', 'ret_1m', 'ret_1m', 'ret_1m', 'ret_1m', 'ret_1m', 'ret_1m', 'ret_1m'],
  130. ['HF', 'MF', 'CF', 'EQ', 'IN', 'IN', 'CI', 'FA', '', 'PL', 'CO']);
  131. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  132. }
  133. /*
  134. * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
  135. */
  136. def get_performance_weekly_table_description(entity_type) {
  137. tmp_universe = table(100:0,
  138. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col', 'ret_col', 'prefix'],
  139. [STRING, STRING, STRING, STRING, STRING, STRING]);
  140. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  141. INSERT INTO tmp_universe VALUES (
  142. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  143. ['mfdb.fund_performance_weekly', 'mfdb.fund_performance_weekly', 'pfdb.pf_cus_fund_performance_weekly', 'mfdb.stock_performance_weekly', 'mfdb.fund_performance_weekly', 'mfdb.fund_performance_weekly', 'pfdb.cm_udf_index_performance_weekly', 'pfdb.cm_factor_performance_weekly', 'pfdb.pf_portfolio_performance_weekly'],
  144. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'],
  145. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
  146. ['ret_1w', 'ret_1w', 'ret_1w', 'ret_1w', 'ret_1w', 'ret_1w', 'ret_1w', 'ret_1w', 'ret_1w'],
  147. ['HF', 'MF', 'CF', 'EQ', 'IN', 'IN', 'CI', 'FA', '']);
  148. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  149. }
  150. /*
  151. * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
  152. */
  153. def get_latest_performance_table_description(entity_type) {
  154. tmp_universe = table(100:0,
  155. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col', 'prefix'],
  156. [STRING, STRING, STRING, STRING, STRING]);
  157. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  158. INSERT INTO tmp_universe VALUES (
  159. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  160. ['mfdb.fund_latest_nav_performance', 'mfdb.fund_latest_nav_performance', 'pfdb.pf_cus_fund_latest_nav_performance', 'mfdb.stock_latest_nav_performance', 'mfdb.fund_latest_nav_performance', 'mfdb.fund_latest_nav_performance', 'pfdb.cm_udf_index_latest_nav_performance', 'pfdb.pf_factor_latest_nav_performance', 'pfdb.pf_portfolio_latest_nav_performance'],
  161. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'],
  162. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
  163. ['HF', 'MF', 'CF', 'EQ', 'IN', 'IN', 'CI', 'FA', '']);
  164. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  165. }
  166. /*
  167. * 根据不同类型的主体返回其杂项指标的表名、字段名
  168. *
  169. * Example: get_indicator_table_description('HF');
  170. */
  171. def get_indicator_table_description(entity_type) {
  172. tmp_universe = table(100:0,
  173. ['type', 'table_name', 'sec_id_col'],
  174. [STRING, STRING, STRING]);
  175. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  176. INSERT INTO tmp_universe VALUES (
  177. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  178. ['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'],
  179. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  180. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  181. }
  182. /*
  183. * 根据不同类型的主体返回其风险指标的表名、字段名
  184. *
  185. * Example: get_risk_stats_table_description('HF');
  186. */
  187. def get_risk_stats_table_description(entity_type) {
  188. tmp_universe = table(100:0,
  189. ['type', 'table_name', 'sec_id_col'],
  190. [STRING, STRING, STRING]);
  191. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  192. INSERT INTO tmp_universe VALUES (
  193. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  194. ['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'],
  195. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  196. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  197. }
  198. /*
  199. * 根据不同类型的主体返回其风险调整收益指标的表名、字段名
  200. *
  201. * Example: get_riskadjret_stats_table_description('HF');
  202. */
  203. def get_riskadjret_stats_table_description(entity_type) {
  204. tmp_universe = table(100:0,
  205. ['type', 'table_name', 'sec_id_col'],
  206. [STRING, STRING, STRING]);
  207. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  208. INSERT INTO tmp_universe VALUES (
  209. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  210. ['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'],
  211. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  212. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  213. }
  214. /*
  215. * 根据不同类型的主体返回其杂项指标的表名、字段名
  216. *
  217. * Example: get_capture_style_table_description('MF');
  218. */
  219. def get_capture_style_table_description(entity_type) {
  220. tmp_universe = table(100:0,
  221. ['type', 'table_name', 'sec_id_col'],
  222. [STRING, STRING, STRING]);
  223. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  224. INSERT INTO tmp_universe VALUES (
  225. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  226. ['mfdb.fund_style_stats', 'mfdb.fund_style_stats', 'pfdb.pf_cus_fund_style_stats', 'mfdb.fund_style_stats', 'mfdb.fund_style_stats', 'pfdb.cm_udf_index_style_stats', 'pfdb.cm_factor_style_stats', 'pfdb.pf_portfolio_style_stats'],
  227. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  228. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  229. }
  230. /*
  231. * 根据不同类型的主体返回其BFI指标的表名、字段名
  232. *
  233. * Example: get_risk_stats_table_description('HF');
  234. */
  235. def get_bfi_indicator_table_description(entity_type) {
  236. tmp_universe = table(100:0,
  237. ['type', 'table_name', 'sec_id_col'],
  238. [STRING, STRING, STRING]);
  239. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  240. INSERT INTO tmp_universe VALUES (
  241. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  242. ['mfdb.fund_ty_bfi_bm_indicator', 'mfdb.fund_ty_bfi_bm_indicator', NULL, NULL, NULL, NULL, NULL, 'pfdb.pf_portfolio_ty_bfi_bm_indicator'],
  243. ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id'] );
  244. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  245. }
  246. /*
  247. * 根据不同类型的主体返回其有效BFI因子的表名、字段名
  248. *
  249. * Example: get_bfi_by_category_group_table_description('HF');
  250. */
  251. def get_bfi_by_category_group_table_description(entity_type) {
  252. tmp_universe = table(100:0,
  253. ['type', 'table_name', 'sec_id_col'],
  254. [STRING, STRING, STRING]);
  255. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合, 经理
  256. INSERT INTO tmp_universe VALUES (
  257. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF', 'MG'],
  258. ['pfdb.pf_fund_factor_bfi_by_category_group', 'pfdb.pf_fund_factor_bfi_by_category_group', NULL, NULL, NULL, NULL, NULL, 'pfdb.pf_portfolio_factor_bfi_by_category_group', 'pf_manager_factor_bfi_by_category_group'],
  259. ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id', 'mamanger_id'] );
  260. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  261. }
  262. /*
  263. * Annulized multiple
  264. */
  265. def get_annulization_multiple(freq) {
  266. ret = 1;
  267. if (freq == 'd') {
  268. ret = 252; // We have differences here between Java and DolphinDB, Java uses 365.25 days
  269. } else if (freq == 'w') {
  270. ret = 52;
  271. } else if (freq == 'm') {
  272. ret = 12;
  273. } else if (freq == 'q') {
  274. ret = 4;
  275. } else if (freq == 's') {
  276. ret = 2;
  277. } else if (freq == 'a') {
  278. ret = 1;
  279. }
  280. return ret;
  281. }
  282. /*
  283. * 计算MySQL中常用的 year_week, 周一为一个星期的第一天,相当于 YEARWEEK(date, 1)
  284. *
  285. *
  286. * Example: get_year_week(2024.10.27);
  287. * get_year_week(2024.11.01);
  288. *
  289. */
  290. def get_year_week(date) {
  291. // 当12月31日是周四、五、六时,该周为第52周,所以次年前几天有可能是上一年的第52,53周
  292. return iif(date.weekOfYear() >= 52 && date.monthOfYear() == 1, date.year()-1, date.year())$STRING + (date.weekOfYear()$STRING).lpad(2, "0");
  293. }