sqlUtilities.dos 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. module fundit::sqlUtilities
  2. /*
  3. * MySQL 连接,使用前应确保 loadPlugin("ODBC")已经被运行过
  4. *
  5. * Create 20240711 使用ODBC连接MySQL数据库 Joey
  6. *
  7. */
  8. def connect_mysql() {
  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. conn = odbc::connect("Dsn=FunditDB-dev-mfdb")
  15. // t = odbc::query(conn, "SELECT * FROM pfdb.pf_portfolio_nav LIMIT 100")
  16. return conn
  17. }
  18. /*
  19. * 取本地数据库
  20. *
  21. * get_local_database("fundit", "mfdb")
  22. */
  23. def get_local_database(server_name, db_name) {
  24. db = database(directory="D:/Program Files/DolphinDB/server/database/" + server_name + "/" + db_name + "/")
  25. return db
  26. }
  27. /*
  28. * 读本地dolphindb数据表
  29. *
  30. * load_table_from_local("fundit", mfdb.fund_performance")
  31. */
  32. def load_table_from_local(server_name, table_name) {
  33. db = get_local_database(server_name, table_name.split(".")[0])
  34. return loadTable(db, table_name.split(".")[1])
  35. }
  36. /*
  37. * 存数据表到mySQL或本地dolphindb,原数据会被替代!
  38. *
  39. * save_table(tb_fund_performance, "mfdb.fund_performance", false)
  40. */
  41. def save_table(tb, table_name, isToMySQL) {
  42. if(isToMySQL == true) {
  43. tb.addColumn("creatorid" "createtime" "updaterid" "updatetime" "isvalid", [INT, DATETIME, INT, DATETIME, INT])
  44. UPDATE tb SET creatorid = 888888, createtime = now(), updaterid = null, updatetime = null, isvalid = 1
  45. conn = connect_mysql()
  46. odbc::execute(conn, "TRUNCATE TABLE " + table_name + "_dolphin")
  47. odbc::append(conn, tb, table_name + "_dolphin", false)
  48. conn.close()
  49. } else {
  50. db = get_local_database("fundit", table_name.split(".")[0])
  51. saveTable(db, tb, table_name.split(".")[1])
  52. }
  53. }
  54. /*
  55. * 存私募基金净值到本地dolphindb
  56. *
  57. * save_hedge_fund_nav_to_local(tb_nav)
  58. */
  59. def save_hedge_fund_nav_to_local(tb_nav) {
  60. save_table(tb_nav, "mfdb.nav", false)
  61. }