sqlUtilities.dos 7.9 KB

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