sqlUtilities.dos 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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'],
  122. [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. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  130. }
  131. /*
  132. * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
  133. */
  134. def get_performance_weekly_table_description(entity_type) {
  135. tmp_universe = table(100:0,
  136. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col'],
  137. [STRING, STRING, STRING, STRING]);
  138. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  139. INSERT INTO tmp_universe VALUES (
  140. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  141. ['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'],
  142. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'],
  143. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav'] );
  144. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  145. }
  146. /*
  147. * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
  148. */
  149. def get_latest_performance_table_description(entity_type) {
  150. tmp_universe = table(100:0,
  151. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col'],
  152. [STRING, STRING, STRING, STRING]);
  153. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  154. INSERT INTO tmp_universe VALUES (
  155. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  156. ['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'],
  157. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'],
  158. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav'] );
  159. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  160. }
  161. /*
  162. * 根据不同类型的主体返回其杂项指标的表名、字段名
  163. *
  164. * Example: get_indicator_table_description('HF');
  165. */
  166. def get_indicator_table_description(entity_type) {
  167. tmp_universe = table(100:0,
  168. ['type', 'table_name', 'sec_id_col'],
  169. [STRING, STRING, STRING]);
  170. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  171. INSERT INTO tmp_universe VALUES (
  172. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  173. ['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'],
  174. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  175. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  176. }
  177. /*
  178. * 根据不同类型的主体返回其风险指标的表名、字段名
  179. *
  180. * Example: get_risk_stats_table_description('HF');
  181. */
  182. def get_risk_stats_table_description(entity_type) {
  183. tmp_universe = table(100:0,
  184. ['type', 'table_name', 'sec_id_col'],
  185. [STRING, STRING, STRING]);
  186. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  187. INSERT INTO tmp_universe VALUES (
  188. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  189. ['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'],
  190. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  191. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  192. }
  193. /*
  194. * 根据不同类型的主体返回其风险调整收益指标的表名、字段名
  195. *
  196. * Example: get_riskadjret_stats_table_description('HF');
  197. */
  198. def get_riskadjret_stats_table_description(entity_type) {
  199. tmp_universe = table(100:0,
  200. ['type', 'table_name', 'sec_id_col'],
  201. [STRING, STRING, STRING]);
  202. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  203. INSERT INTO tmp_universe VALUES (
  204. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  205. ['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'],
  206. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  207. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  208. }
  209. /*
  210. * 根据不同类型的主体返回其杂项指标的表名、字段名
  211. *
  212. * Example: get_capture_style_table_description('MF');
  213. */
  214. def get_capture_style_table_description(entity_type) {
  215. tmp_universe = table(100:0,
  216. ['type', 'table_name', 'sec_id_col'],
  217. [STRING, STRING, STRING]);
  218. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  219. INSERT INTO tmp_universe VALUES (
  220. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  221. ['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'],
  222. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  223. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  224. }
  225. /*
  226. * 根据不同类型的主体返回其BFI指标的表名、字段名
  227. *
  228. * Example: get_risk_stats_table_description('HF');
  229. */
  230. def get_bfi_indicator_table_description(entity_type) {
  231. tmp_universe = table(100:0,
  232. ['type', 'table_name', 'sec_id_col'],
  233. [STRING, STRING, STRING]);
  234. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  235. INSERT INTO tmp_universe VALUES (
  236. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  237. ['mfdb.fund_ty_bfi_bm_indicator', 'mfdb.fund_ty_bfi_bm_indicator', NULL, NULL, NULL, NULL, NULL, 'pfdb.pf_portfolio_ty_bfi_bm_indicator'],
  238. ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id'] );
  239. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  240. }
  241. /*
  242. * 根据不同类型的主体返回其有效BFI因子的表名、字段名
  243. *
  244. * Example: get_bfi_by_category_group_table_description('HF');
  245. */
  246. def get_bfi_by_category_group_table_description(entity_type) {
  247. tmp_universe = table(100:0,
  248. ['type', 'table_name', 'sec_id_col'],
  249. [STRING, STRING, STRING]);
  250. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合, 经理
  251. INSERT INTO tmp_universe VALUES (
  252. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF', 'MG'],
  253. ['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'],
  254. ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id', 'mamanger_id'] );
  255. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  256. }
  257. /*
  258. * Annulized multiple
  259. */
  260. def get_annulization_multiple(freq) {
  261. ret = 1;
  262. if (freq == 'd') {
  263. ret = 252; // We have differences here between Java and DolphinDB, Java uses 365.25 days
  264. } else if (freq == 'w') {
  265. ret = 52;
  266. } else if (freq == 'm') {
  267. ret = 12;
  268. } else if (freq == 'q') {
  269. ret = 4;
  270. } else if (freq == 's') {
  271. ret = 2;
  272. } else if (freq == 'a') {
  273. ret = 1;
  274. }
  275. return ret;
  276. }
  277. /*
  278. * 计算MySQL中常用的 year_week, 周一为一个星期的第一天,相当于 YEARWEEK(date, 1)
  279. *
  280. *
  281. * Example: get_year_week(2024.10.27);
  282. * get_year_week(2024.11.01);
  283. *
  284. */
  285. def get_year_week(date) {
  286. // 当12月31日是周四、五、六时,该周为第52周,所以次年前几天有可能是上一年的第52,53周
  287. return iif(date.weekOfYear() >= 52 && date.monthOfYear() == 1, date.year()-1, date.year())$STRING + (date.weekOfYear()$STRING).lpad(2, "0");
  288. }