sqlUtilities.dos 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. * 返回ID的规律(前缀)
  66. *
  67. */
  68. def get_entity_id_info() {
  69. tmp = table(10:0, ['entity_type', 'prefix'], [STRING, STRING]);
  70. // 公募,私募,私有基金,股票,市场指数,图译指数,图译因子,人物,公司
  71. INSERT INTO tmp VALUES (`MF`HF`CF`EQ`MI`FI`CI`FA`PL`CO, `MF`HF`CF`EQ`IN`IN`CI`FA`PL`CO);
  72. return tmp;
  73. }
  74. /*
  75. * 返回标准RBSA的几组指数集
  76. *
  77. */
  78. def get_rbsa_index() {
  79. d = dict(STRING, ANY);
  80. d['AS0000005Q'] = ['IN00000008', 'IN00000077','IN0000007G', 'IN0000009M'];
  81. d['BondType'] = ['IN0000007A', 'IN0000007G','IN0000008J', 'IN000002CM'];
  82. d['Cap3Style'] = ['FA00000WKG', 'FA00000WKH','IN0000007G'];
  83. d['CNI7Style'] = ['IN0000000S', 'IN0000000T','IN0000000U', 'IN0000000V', 'IN0000000W', 'IN0000000X', 'IN0000007G'];
  84. d['CSI11'] = ['IN0000000Y', 'IN0000000Z','IN00000010', 'IN00000011', 'IN00000012', 'IN00000013', 'IN00000014', 'IN00000015', 'IN00000016', 'IN00000017', 'IN0000007G'];
  85. d['CSI5'] = ['FA00000VML', 'FA00000VMM','FA00000VMN', 'FA00000VMO', 'IN0000007G'];
  86. d['Large4Assets'] = ['IN00000008', 'IN00000077','IN0000007G', 'IN0000009M'];
  87. return d;
  88. }
  89. /*
  90. * 根据不同类型的主体返回其净值表的表名、字段名和ID前两位特征字符
  91. */
  92. def get_nav_table_description(entity_type) {
  93. tmp_universe = table(100:0,
  94. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col', 'nav_col', 'prefix'],
  95. [STRING, STRING, STRING, STRING, STRING, STRING]);
  96. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  97. INSERT INTO tmp_universe VALUES (
  98. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  99. ['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'],
  100. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'index_id', 'index_id', 'index_id', 'factor_id', 'portfolio_id'],
  101. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
  102. ['nav', 'nav', 'nav', 'nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
  103. ['HF', 'MF', 'CF', 'EQ', 'IN', 'IN', 'CI', 'FA', '']);
  104. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  105. }
  106. /*
  107. * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
  108. */
  109. def get_performance_table_description(entity_type) {
  110. tmp_universe = table(100:0,
  111. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col'],
  112. [STRING, STRING, STRING, STRING]);
  113. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  114. INSERT INTO tmp_universe VALUES (
  115. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF', 'PL', 'CO'],
  116. ['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'],
  117. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id', 'manager_id', 'company_id'],
  118. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav'] );
  119. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  120. }
  121. /*
  122. * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
  123. */
  124. def get_performance_weekly_table_description(entity_type) {
  125. tmp_universe = table(100:0,
  126. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col'],
  127. [STRING, STRING, STRING, STRING]);
  128. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  129. INSERT INTO tmp_universe VALUES (
  130. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  131. ['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'],
  132. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'],
  133. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav'] );
  134. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  135. }
  136. /*
  137. * 根据不同类型的主体返回其杂项指标的表名、字段名
  138. *
  139. * Example: get_indicator_table_description('HF');
  140. */
  141. def get_indicator_table_description(entity_type) {
  142. tmp_universe = table(100:0,
  143. ['type', 'table_name', 'sec_id_col'],
  144. [STRING, STRING, STRING]);
  145. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  146. INSERT INTO tmp_universe VALUES (
  147. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  148. ['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'],
  149. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  150. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  151. }
  152. /*
  153. * 根据不同类型的主体返回其风险指标的表名、字段名
  154. *
  155. * Example: get_risk_stats_table_description('HF');
  156. */
  157. def get_risk_stats_table_description(entity_type) {
  158. tmp_universe = table(100:0,
  159. ['type', 'table_name', 'sec_id_col'],
  160. [STRING, STRING, STRING]);
  161. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  162. INSERT INTO tmp_universe VALUES (
  163. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  164. ['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'],
  165. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  166. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  167. }
  168. /*
  169. * 根据不同类型的主体返回其风险调整收益指标的表名、字段名
  170. *
  171. * Example: get_riskadjret_stats_table_description('HF');
  172. */
  173. def get_riskadjret_stats_table_description(entity_type) {
  174. tmp_universe = table(100:0,
  175. ['type', 'table_name', 'sec_id_col'],
  176. [STRING, STRING, STRING]);
  177. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  178. INSERT INTO tmp_universe VALUES (
  179. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  180. ['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'],
  181. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  182. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  183. }
  184. /*
  185. * 根据不同类型的主体返回其杂项指标的表名、字段名
  186. *
  187. * Example: get_capture_style_table_description('MF');
  188. */
  189. def get_capture_style_table_description(entity_type) {
  190. tmp_universe = table(100:0,
  191. ['type', 'table_name', 'sec_id_col'],
  192. [STRING, STRING, STRING]);
  193. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  194. INSERT INTO tmp_universe VALUES (
  195. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  196. ['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'],
  197. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  198. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  199. }
  200. /*
  201. * 根据不同类型的主体返回其BFI指标的表名、字段名
  202. *
  203. * Example: get_risk_stats_table_description('HF');
  204. */
  205. def get_bfi_indicator_table_description(entity_type) {
  206. tmp_universe = table(100:0,
  207. ['type', 'table_name', 'sec_id_col'],
  208. [STRING, STRING, STRING]);
  209. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  210. INSERT INTO tmp_universe VALUES (
  211. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  212. ['mfdb.fund_ty_bfi_bm_indicator', 'mfdb.fund_ty_bfi_bm_indicator', NULL, NULL, NULL, NULL, NULL, 'pfdb.pf_portfolio_ty_bfi_bm_indicator'],
  213. ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id'] );
  214. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  215. }
  216. /*
  217. * 根据不同类型的主体返回其有效BFI因子的表名、字段名
  218. *
  219. * Example: get_bfi_by_category_group_table_description('HF');
  220. */
  221. def get_bfi_by_category_group_table_description(entity_type) {
  222. tmp_universe = table(100:0,
  223. ['type', 'table_name', 'sec_id_col'],
  224. [STRING, STRING, STRING]);
  225. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合, 经理
  226. INSERT INTO tmp_universe VALUES (
  227. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF', 'MG'],
  228. ['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'],
  229. ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id', 'mamanger_id'] );
  230. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  231. }
  232. /*
  233. * Annulized multiple
  234. */
  235. def get_annulization_multiple(freq) {
  236. ret = 1;
  237. if (freq == 'd') {
  238. ret = 252; // We have differences here between Java and DolphinDB, Java uses 365.25 days
  239. } else if (freq == 'w') {
  240. ret = 52;
  241. } else if (freq == 'm') {
  242. ret = 12;
  243. } else if (freq == 'q') {
  244. ret = 4;
  245. } else if (freq == 's') {
  246. ret = 2;
  247. } else if (freq == 'a') {
  248. ret = 1;
  249. }
  250. return ret;
  251. }