sqlUtilities.dos 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. def get_nav_table_description(entity_type) {
  68. tmp_universe = table(100:0,
  69. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col', 'nav_col', 'prefix'],
  70. [STRING, STRING, STRING, STRING, STRING, STRING]);
  71. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  72. INSERT INTO tmp_universe VALUES (
  73. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  74. ['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'],
  75. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'index_id', 'index_id', 'index_id', 'factor_id', 'portfolio_id'],
  76. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
  77. ['nav', 'nav', 'nav', 'nav', 'close', 'index_value', 'cumulative_nav', 'factor_value', 'cumulative_nav'],
  78. ['HF', 'MF', 'CF', 'EQ', 'IN', 'IN', 'CI', 'FA', '']);
  79. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  80. }
  81. /*
  82. * 根据不同类型的主体返回其业绩表的表名、字段名和ID前两位特征字符
  83. */
  84. def get_performance_table_description(entity_type) {
  85. tmp_universe = table(100:0,
  86. ['type', 'table_name', 'sec_id_col', 'cumulative_nav_col'],
  87. [STRING, STRING, STRING, STRING]);
  88. // 分别对应:私募,公募,私有基金,股票,市场指数,图译指数,私有指数,图译因子,组合
  89. INSERT INTO tmp_universe VALUES (
  90. ['HF', 'MF', 'CF', 'EQ', 'MI', 'FI', 'CI', 'FA', 'PF'],
  91. ['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'],
  92. ['fund_id', 'fund_id', 'fund_id', 'sec_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'],
  93. ['cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'cumulative_nav', 'factor_value', 'cumulative_nav'] );
  94. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  95. }
  96. /*
  97. * 根据不同类型的主体返回其杂项指标的表名、字段名
  98. *
  99. * Example: get_indicator_table_description('HF');
  100. */
  101. def get_indicator_table_description(entity_type) {
  102. tmp_universe = table(100:0,
  103. ['type', 'table_name', 'sec_id_col'],
  104. [STRING, STRING, STRING]);
  105. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  106. INSERT INTO tmp_universe VALUES (
  107. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  108. ['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'],
  109. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  110. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  111. }
  112. /*
  113. * 根据不同类型的主体返回其风险指标的表名、字段名
  114. *
  115. * Example: get_risk_stats_table_description('HF');
  116. */
  117. def get_risk_stats_table_description(entity_type) {
  118. tmp_universe = table(100:0,
  119. ['type', 'table_name', 'sec_id_col'],
  120. [STRING, STRING, STRING]);
  121. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  122. INSERT INTO tmp_universe VALUES (
  123. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  124. ['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'],
  125. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  126. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  127. }
  128. /*
  129. * 根据不同类型的主体返回其风险调整收益指标的表名、字段名
  130. *
  131. * Example: get_riskadjret_stats_table_description('HF');
  132. */
  133. def get_riskadjret_stats_table_description(entity_type) {
  134. tmp_universe = table(100:0,
  135. ['type', 'table_name', 'sec_id_col'],
  136. [STRING, STRING, STRING]);
  137. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  138. INSERT INTO tmp_universe VALUES (
  139. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  140. ['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'],
  141. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  142. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  143. }
  144. /*
  145. * 根据不同类型的主体返回其杂项指标的表名、字段名
  146. *
  147. * Example: get_capture_style_table_description('MF');
  148. */
  149. def get_capture_style_table_description(entity_type) {
  150. tmp_universe = table(100:0,
  151. ['type', 'table_name', 'sec_id_col'],
  152. [STRING, STRING, STRING]);
  153. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  154. INSERT INTO tmp_universe VALUES (
  155. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  156. ['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'],
  157. ['fund_id', 'fund_id', 'fund_id', 'fund_id', 'fund_id', 'index_id', 'factor_id', 'portfolio_id'] );
  158. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  159. }
  160. /*
  161. * 根据不同类型的主体返回其BFI指标的表名、字段名
  162. *
  163. * Example: get_risk_stats_table_description('HF');
  164. */
  165. def get_bfi_indicator_table_description(entity_type) {
  166. tmp_universe = table(100:0,
  167. ['type', 'table_name', 'sec_id_col'],
  168. [STRING, STRING, STRING]);
  169. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合
  170. INSERT INTO tmp_universe VALUES (
  171. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF'],
  172. ['mfdb.fund_ty_bfi_bm_indicator', 'mfdb.fund_ty_bfi_bm_indicator', NULL, NULL, NULL, NULL, NULL, 'pfdb.pf_portfolio_ty_bfi_bm_indicator'],
  173. ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id'] );
  174. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  175. }
  176. /*
  177. * 根据不同类型的主体返回其有效BFI因子的表名、字段名
  178. *
  179. * Example: get_bfi_by_category_group_table_description('HF');
  180. */
  181. def get_bfi_by_category_group_table_description(entity_type) {
  182. tmp_universe = table(100:0,
  183. ['type', 'table_name', 'sec_id_col'],
  184. [STRING, STRING, STRING]);
  185. // 分别对应:私募,公募,私有基金,市场指数,图译指数,私有指数,图译因子,组合, 经理
  186. INSERT INTO tmp_universe VALUES (
  187. ['HF', 'MF', 'CF', 'MI', 'FI', 'CI', 'FA', 'PF', 'MG'],
  188. ['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'],
  189. ['fund_id', 'fund_id', NULL, NULL, NULL, NULL, NULL, 'portfolio_id', 'mamanger_id'] );
  190. return (SELECT * FROM tmp_universe u WHERE u.type = entity_type);
  191. }
  192. /*
  193. * Annulized multiple
  194. */
  195. def get_annulization_multiple(freq) {
  196. ret = 1;
  197. if (freq == 'd') {
  198. ret = 252; // We have differences here between Java and DolphinDB, Java uses 365.25 days
  199. } else if (freq == 'w') {
  200. ret = 52;
  201. } else if (freq == 'm') {
  202. ret = 12;
  203. } else if (freq == 'q') {
  204. ret = 4;
  205. } else if (freq == 's') {
  206. ret = 2;
  207. } else if (freq == 'a') {
  208. ret = 1;
  209. }
  210. return ret;
  211. }