Merge pull request #1583 from nanoric/mysql

[Add] 增加对mysql的支持
This commit is contained in:
vn.py 2019-04-11 20:28:53 +08:00 committed by GitHub
commit 8994f525b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 6 deletions

View File

@ -1,4 +1,5 @@
PyQt5<5.12
pyqtgraph
dataclasses; python_version<="3.6"
qdarkstyle
requests

View File

@ -1,13 +1,47 @@
""""""
from peewee import SqliteDatabase, Model, CharField, DateTimeField, FloatField
from peewee import CharField, DateTimeField, FloatField, Model, MySQLDatabase, PostgresqlDatabase, \
SqliteDatabase
from .constant import Exchange, Interval
from .object import BarData, TickData
from .utility import get_file_path
from .setting import SETTINGS
from .utility import resolve_path
DB_NAME = "database.db"
DB = SqliteDatabase(str(get_file_path(DB_NAME)))
def init():
db_settings = SETTINGS['database']
driver = db_settings["driver"]
init_funcs = {
"sqlite": init_sqlite,
"mysql": init_mysql,
"postgresql": init_postgresql,
}
assert driver in init_funcs
del db_settings['driver']
return init_funcs[driver](db_settings)
def init_sqlite(settings: dict):
global DB
database = settings['database']
DB = SqliteDatabase(str(resolve_path(database)))
def init_mysql(settings: dict):
global DB
DB = MySQLDatabase(**settings)
def init_postgresql(settings: dict):
global DB
DB = PostgresqlDatabase(**settings)
init()
class DbBarData(Model):

View File

@ -23,10 +23,17 @@ SETTINGS = {
"email.receiver": "",
"rqdata.username": "",
"rqdata.password": ""
"rqdata.password": "",
"database": {
"driver": "sqlite", # sqlite, mysql, postgresql
"database": "{VNPY_TEMP}/database.db", # for sqlite, use this as filepath
"host": "localhost",
"port": 3306,
"user": "root",
"password": ""
}
}
# Load global setting from json file.
SETTING_FILENAME = "vt_setting.json"
SETTINGS.update(load_json(SETTING_FILENAME))

View File

@ -3,6 +3,7 @@ General utility functions.
"""
import json
import os
from pathlib import Path
from typing import Callable
@ -12,6 +13,12 @@ import talib
from .object import BarData, TickData
def resolve_path(pattern: str):
env = dict(os.environ)
env.update({"VNPY_TEMP": str(TEMP_DIR)})
return pattern.format(**env)
def _get_trader_dir(temp_name: str):
"""
Get path where trader is running in.