sqlUtilities.dos 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. */
  44. def ids_to_string(ids) {
  45. s_ids = '';
  46. if(ids.isVoid()) return s_ids;
  47. // 输入的 ids 是字符串标量
  48. if (ids.form() == 0) {
  49. s_ids = ids.trim();
  50. // 输入的 ids 是字符串向量
  51. } else if(ids.form() == 1) {
  52. if(ids.type() == 4) // INTEGER
  53. s_ids = ids.concat(",").trim();
  54. else // STRING
  55. s_ids = "'" + ids.concat("','").trim() + "'";
  56. // 缺省返回空
  57. } else {
  58. s_ids = NULL;
  59. }
  60. return s_ids;
  61. }
  62. /*
  63. * 根据不同类型的主体返回其净值表的表名、字段名和ID前两位特征字符
  64. */
  65. def get_nav_table_description(entity_type) {
  66. tmp_universe = table(100:0,
  67. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col', 'nav_col', 'prefix'],
  68. [STRING, STRING, STRING, STRING, STRING, STRING]);
  69. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  70. INSERT INTO tmp_universe VALUES (
  71. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  72. ['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'],
  73. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'index_id', 'index_id', 'index_id', 'factor_id', 'portfolio_id'],
  74. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
  75. ['nav', 'nav', 'nav', 'nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
  76. ['HF', 'MF', 'CF', 'EQ', 'IN', 'IN', 'CI', 'FA', '']);
  77. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  78. }
  79. /*
  80. * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
  81. */
  82. def get_performance_table_description(entity_type) {
  83. tmp_universe = table(100:0,
  84. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col'],
  85. [STRING, STRING, STRING, STRING]);
  86. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  87. INSERT INTO tmp_universe VALUES (
  88. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  89. ['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'],
  90. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'],
  91. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav'] );
  92. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  93. }
  94. /*
  95. * 根据不同类型的主体返回其杂项指标的表名、字段名
  96. *
  97. * Example: get_indicator_table_description('HF');
  98. */
  99. def get_indicator_table_description(entity_type) {
  100. tmp_universe = table(100:0,
  101. ['type', 'table_name', 'sec_id_col'],
  102. [STRING, STRING, STRING]);
  103. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  104. INSERT INTO tmp_universe VALUES (
  105. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  106. ['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'],
  107. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  108. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  109. }
  110. /*
  111. * 根据不同类型的主体返回其风险指标的表名、字段名
  112. *
  113. * Example: get_risk_stats_table_description('HF');
  114. */
  115. def get_risk_stats_table_description(entity_type) {
  116. tmp_universe = table(100:0,
  117. ['type', 'table_name', 'sec_id_col'],
  118. [STRING, STRING, STRING]);
  119. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  120. INSERT INTO tmp_universe VALUES (
  121. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  122. ['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'],
  123. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  124. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  125. }
  126. /*
  127. * 根据不同类型的主体返回其风险调整收益指标的表名、字段名
  128. *
  129. * Example: get_riskadjret_stats_table_description('HF');
  130. */
  131. def get_riskadjret_stats_table_description(entity_type) {
  132. tmp_universe = table(100:0,
  133. ['type', 'table_name', 'sec_id_col'],
  134. [STRING, STRING, STRING]);
  135. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  136. INSERT INTO tmp_universe VALUES (
  137. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  138. ['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'],
  139. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  140. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  141. }
  142. /*
  143. * Annulized multiple
  144. */
  145. def get_annulization_multiple(freq) {
  146. ret = 1;
  147. if (freq == 'd') {
  148. ret = 252; // We have differences here between Java and DolphinDB, Java uses 365.25 days
  149. } else if (freq == 'w') {
  150. ret = 52;
  151. } else if (freq == 'm') {
  152. ret = 12;
  153. } else if (freq == 'q') {
  154. ret = 4;
  155. } else if (freq == 's') {
  156. ret = 2;
  157. } else if (freq == 'a') {
  158. ret = 1;
  159. }
  160. return ret;
  161. }