Skip to content

Commit

Permalink
[优化]insert
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Jun 6, 2024
1 parent c574011 commit e971082
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
27 changes: 22 additions & 5 deletions pydbapi/sql/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: longfengpili
# @Date: 2023-06-02 15:27:41
# @Last Modified by: longfengpili
# @Last Modified time: 2023-11-14 16:14:56
# @Last Modified time: 2024-06-06 15:10:13
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -85,18 +85,35 @@ def _insert_value(self, columns, values):
columns {[ColumnsModel]} -- [列信息]
values {[list]} -- [插入的数据]
'''
def deal_values(values):
def deal_value(dtypes: list, value: list):
_value = []
for d, v in zip(dtypes, value):
if v is None:
v = 'Null'
elif d.startswith('varchar') or d.startswith('str'):
v = f"'{v}'"
elif d in ('date', 'datetime', 'timestamp') or isinstance(v, str):
v = f"'{v}'"
else:
v = f'{v}'

_value.append(v)
_value = '(' + ','.join(_value) + ')'
return _value

def deal_values(columns: ColumnsModel, values: list):
j_values = []
if not values:
raise ValueError(f"{values} is empty !!!")
if not isinstance(values, list):
raise TypeError('values must be a list !')
j_values = [str(tuple(value)) for value in values]

dtypes = [col.coltype for col in columns]
j_values = [deal_value(dtypes, value) for value in values]
j_values = ',\n'.join(j_values)
# j_values = j_values.replace('\'\'', 'null').replace('None', 'null') # 空值替换为null
return j_values

values = deal_values(values)
values = deal_values(columns, values)

if not isinstance(columns, ColumnsModel):
raise TypeError("colums must be a ColumnsModel !")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import setuptools

VERSION = '0.0.121'
VERSION = '0.0.122'
PROJECT_NAME = 'pydbapi'

with open('README.md', 'r', encoding='utf-8') as f:
Expand Down
7 changes: 6 additions & 1 deletion tests/mysql/mysql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: longfengpili
# @Date: 2023-06-02 15:27:41
# @Last Modified by: longfengpili
# @Last Modified time: 2024-03-05 18:21:25
# @Last Modified time: 2024-06-06 15:14:31
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -46,6 +46,11 @@ def test_get_instance(self):
# result = eval(f"mysql4.{i}")
# print(f"【{i}】: {result}")

def test_create_for_drop(self):
indexes = ['id', 'name']
rows, action, result = self.mysqldb.create(self.tablename, self.columns, indexes, partition='birthday')
print(f"【rows】: {rows}, 【action】: {action}, 【result】: {result}")

def test_drop(self):
rows, action, result = self.mysqldb.drop(self.tablename)
print(f"【rows】: {rows}, 【action】: {action}, 【result】: {result}")
Expand Down

0 comments on commit e971082

Please sign in to comment.