Skip to content

Commit

Permalink
[新增]mysql新增doris处理
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Aug 25, 2021
1 parent 4686f93 commit a36bad7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
15 changes: 11 additions & 4 deletions pydbapi/api/mysql.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @Author: chunyang.xu
# @Email: [email protected]
# @Date: 2020-06-10 14:40:50
# @Last Modified time: 2021-08-11 11:10:48
# @Last Modified time: 2021-08-25 14:46:54
# @github: https://github.com/longfengpili

# !/usr/bin/env python3
Expand Down Expand Up @@ -61,7 +61,7 @@ def create_partition(self, partition):
partition = f"partition by hash (to_days({partition.newname}))"
return partition

def create(self, columns, indexes, index_part=128, ismultiple_index=True, partition=None):
def create(self, columns, indexes, index_part=128, ismultiple_index=True, partition=None, isdoris=False):
'mysql 暂不考虑索引'
sql = self.create_nonindex(columns)

Expand All @@ -77,6 +77,11 @@ def create(self, columns, indexes, index_part=128, ismultiple_index=True, partit
partition = self.create_partition(partition)
sql = sql.replace(';', f'\n{partition};')

if isdoris:
distributed_col = columns[0].newname
distributed = f"distributed by hash({distributed_col})"
sql = sql.replace(';', f'\n{distributed};')

return sql

def dumpsql(self, columns, dumpfile, fromtable=None, condition=None):
Expand All @@ -95,13 +100,14 @@ def loadsql(self, columns, loadfile, intotable=None, fieldterminated=','):
class MysqlDB(DBCommon, DBFileExec):
_instance_lock = threading.Lock()

def __init__(self, host, user, password, database, port=3306, charset="utf8", safe_rule=True):
def __init__(self, host, user, password, database, port=3306, charset="utf8", safe_rule=True, isdoris=False):
self.host = host
self.port = port
self.user = user
self.password = password
self.database = database
self.charset = charset
self.isdoris = isdoris
super(MysqlDB, self).__init__()
self.auto_rules = AUTO_RULES if safe_rule else None

Expand All @@ -115,7 +121,8 @@ def get_instance(cls, *args, **kwargs):
return MysqlDB._instance

def get_conn(self):
conn = pymysql.connect(database=self.database, user=self.user, password=self.password, host=self.host, port=self.port, charset=self.charset)
conn = pymysql.connect(database=self.database, user=self.user, password=self.password,
host=self.host, port=self.port, charset=self.charset)
if not conn:
self.get_conn()
return conn
Expand Down
5 changes: 4 additions & 1 deletion pydbapi/col/colmodel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @Author: chunyang.xu
# @Email: [email protected]
# @Date: 2020-11-30 16:28:21
# @Last Modified time: 2021-08-09 13:46:27
# @Last Modified time: 2021-08-25 14:46:00
# @github: https://github.com/longfengpili

# !/usr/bin/env python3
Expand Down Expand Up @@ -53,6 +53,9 @@ def __init__(self, *columns):
def __repr__(self):
return f"{[column for column in self.columns]}"

def __getitem__(self, key):
return self.columns[key]

@property
def func_cols(self):
func_cols = [col for col in self.columns if col.func]
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @Author: chunyang.xu
# @Email: [email protected]
# @Date: 2020-06-09 16:46:54
# @Last Modified time: 2021-08-11 11:13:22
# @Last Modified time: 2021-08-25 14:36:33
# @github: https://github.com/longfengpili

# !/usr/bin/env python3
Expand All @@ -12,7 +12,7 @@
import shutil
import setuptools

VERSION = '0.0.64'
VERSION = '0.0.65'
PROJECT_NAME = 'pydbapi'

with open('README.md', 'r', encoding='utf-8') as f:
Expand Down
6 changes: 3 additions & 3 deletions tests/mysql/mysql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: chunyang.xu
# @Date: 2021-03-08 14:19:01
# @Last Modified by: chunyang.xu
# @Last Modified time: 2021-08-10 17:20:50
# @Last Modified time: 2021-08-25 14:59:13

import os
import pytest
Expand All @@ -15,8 +15,8 @@
class TestMysql:

def setup_method(self, method):
AdLocal = os.environ.get('ADLOCAL')
AdLocal = json.loads(AdLocal)
AdLocal = os.environ.get('ADLOCAL').lower()
AdLocal = json.loads(AdLocal.replace("'", '"'))
self.mysqldb = MysqlDB(safe_rule=False, **AdLocal)
self.tablename = 'test_xu'
self.id = ColumnModel('id', 'varchar(1024)')
Expand Down

0 comments on commit a36bad7

Please sign in to comment.