Quellcode durchsuchen

上传文件至 'modules'

Joey vor 1 Woche
Ursprung
Commit
456aa1f335
1 geänderte Dateien mit 34 neuen und 105 gelöschten Zeilen
  1. 34 105
      modules/dataPuller.dos

+ 34 - 105
modules/dataPuller.dos

@@ -1,83 +1,6 @@
 module fundit::dataPuller
- 
-/*
- *  MySQL 连接,使用前应确保 loadPlugin("ODBC")已经被运行过
- *
- *  Create  20240711  使用ODBC连接MySQL数据库                                     Joey
- *  
- */
-def connect_mysql() {
-
-    // 阿里云的mysql被魔改过,当前DolphinDB无法支持MySQL插件,只能用ODBC
-    // loadPlugin("ODBC")
-
-    // 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")
-
-    // 使用Windows的ODBC数据源事先设置号的连接
-    // conn = odbc::connect("Dsn=FunditDB-mfdb")
-    conn = odbc::connect("Dsn=FunditDB-dev-mfdb")
 
-//    t = odbc::query(conn, "SELECT * FROM pfdb.pf_portfolio_nav LIMIT 100")
-
-    return conn
-}
-
-/*
- *  取本地数据库
- *
- *  get_local_database("fundit", "mfdb")
- */
-def get_local_database(server_name, db_name) {
-
-    db = database(directory="D:/Program Files/DolphinDB/server/database/" + server_name + "/" + db_name + "/")
-    
-    return db
-}
-
-/*
- * 读本地dolphindb数据表
- *
- * load_table_from_local("fundit", mfdb.fund_performance")
- */
-def load_table_from_local(server_name, table_name) {
-
-    db = get_local_database(server_name, table_name.split(".")[0])
-    
-    return loadTable(db, table_name.split(".")[1])
-
-}
-
-/*
- * 存数据表到mySQL或本地dolphindb,原数据会被替代!
- *
- * save_table(tb_fund_performance, "mfdb.fund_performance", false)
- */
-
-def save_table(tb, table_name, isToMySQL) {
-
-
-    if(isToMySQL == true) {
-    
-        tb.addColumn("creatorid" "createtime" "updaterid" "updatetime" "isvalid", [INT, DATETIME, INT, DATETIME, INT])
-        
-        UPDATE tb SET creatorid = 888888, createtime = now(), updaterid = null, updatetime = null, isvalid = 1
-    
-        conn = connect_mysql()
-        
-        odbc::execute(conn, "TRUNCATE TABLE " + table_name + "_dolphin")
-        
-        odbc::append(conn, tb, table_name + "_dolphin", false)
-        
-        conn.close()
-    
-    } else {
-    
-        db = get_local_database("fundit", table_name.split(".")[0])
-    
-        saveTable(db, tb, table_name.split(".")[1])
-    }
-    
-}
+use fundit::sqlUtilities
 
 /*
  *  取指数周收益
@@ -429,59 +352,65 @@ def get_fund_list_by_nav_updatetime(fund_ids, updatetime) {
 
 
 /*
- * 存私募基金净值到本地dolphindb
- *
- * save_hedge_fund_nav_to_local(tb_nav)
+ * 取私募基金用于月末 fund_performance 表更新的净值
+ * 
+ * @param fund_ids: 逗号分隔的ID字符串, 每个ID都有''
+ * @param month_end: 月末日期字符串  YYYY-MM
+ * 
+ * 
  */
-def save_hedge_fund_nav_to_local(tb_nav) {
+def get_nav_for_hedge_fund_performance(fund_ids, month_end) {
+   
+    s_query = "CALL pfdb.sp_get_nav_for_fund_performance(" + fund_ids + ", '" + month_end + "', 1);"
+
+    conn = connect_mysql()
 
-    save_table(tb_nav, "mfdb.nav", false)
+    t = odbc::query(conn, s_query)
 
-}
+    conn.close()
+
+    return t
 
+}
 
 /*
- * 存私募基金净值到本地dolphindb
+ * 取组合交易表
+ *
  *
- * get_portfolio_holding_history("166002,364640")
+ * Example: get_portfolio_holding_history("166002,364640")
  */
 def get_portfolio_holding_history(portfolio_ids) {
 
-    s_query = "SELECT portfolio_id, holding_date, fund_id, amount, fund_share, ROUND(amount/fund_share, 6) as nav
+    s_query = "SELECT portfolio_id, holding_date, fund_id, amount, fund_share, ROUND(amount/fund_share, 6) AS nav
                FROM pfdb.pf_portfolio_fund_history
                WHERE portfolio_id IN (" + portfolio_ids + ")
                  AND isvalid = 1
-               ORDER BY portfolio_id, holding_date"
+               ORDER BY portfolio_id, holding_date";
 
-    conn = connect_mysql()
+    conn = connect_mysql();
 
-    t = odbc::query(conn, s_query)
+    t = odbc::query(conn, s_query);
 
-    conn.close()
+    conn.close();
 
-    return t
+    return t;
 
 }
 
 /*
- * 取私募基金用于月末 fund_performance 表更新的净值
- * 
- * @param fund_ids: 逗号分隔的ID字符串, 每个ID都有''
- * @param month_end: 月末日期字符串  YYYY-MM
- * 
+ *  取基金证券从某日期后的所有净值
+ *  @param json_query <JSON>: [{sec_id:xxx, holding_date: yyyy-mm-dd}]
  * 
  */
-def get_nav_for_hedge_fund_performance(fund_ids, month_end) {
-   
-    s_query = "CALL pfdb.sp_get_nav_for_fund_performance(" + fund_ids + ", '" + month_end + "', 1);"
+def get_holding_nav(json_query) {
 
-    conn = connect_mysql()
+    s_query = "CALL pfdb.sp_get_nav_after_date('" + json_query + "')";
 
-    t = odbc::query(conn, s_query)
+    conn = connect_mysql();
 
-    conn.close()
-
-    return t
+    t = odbc::query(conn, s_query);
 
+    conn.close();
 
+    return t;
 }