Skip to content

Commit

Permalink
[新增]alter table condition
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Feb 29, 2024
1 parent 3152089 commit 1bea6cb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
9 changes: 5 additions & 4 deletions pydbapi/api/mysql.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-02-28 18:21:27
# @Last Modified time: 2024-02-29 18:36:45
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -169,14 +169,15 @@ def loaddata(self, tablename, columns, loadfile, fieldterminated=',', verbose=0)

def alter_table(self, tablename: str, colname: str, newname: str = None, newtype: str = None,
indexes: list = None, index_part: int = 128, ismultiple_index: bool = True,
partition: str = None, distribution: str = None, verbose: int = 0):
partition: str = None, distribution: str = None, conditions: list[str] = None,
verbose: int = 0):

alter_columns = self.alter_column(tablename, colname, newname, newtype)

# create middle table
mtablename = f"{tablename}_middle"
mtablename = f"{tablename}_tmp"
self.create(mtablename, alter_columns, indexes=indexes, index_part=index_part, ismultiple_index=ismultiple_index,
partition=partition, distribution=distribution, verbose=verbose)

# alter table
self.alter_table_base(tablename, mtablename, alter_columns, verbose=verbose)
self.alter_table_base(tablename, mtablename, alter_columns, conditions=conditions, verbose=verbose)
8 changes: 4 additions & 4 deletions pydbapi/api/trino.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-02-28 18:41:09
# @Last Modified time: 2024-02-29 18:34:51
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -151,13 +151,13 @@ def insert(self, tablename, columns, inserttype='value', values=None, chunksize=
return rows, action, result

def alter_table(self, tablename: str, colname: str, newname: str = None, newtype: str = None,
partition: str = 'part_date', verbose: int = 0):
partition: str = 'part_date', conditions: list[str] = None, verbose: int = 0):

alter_columns = self.alter_column(tablename, colname, newname, newtype)

# create middle table
mtablename = f"{tablename}_middle"
mtablename = f"{tablename}_tmp"
self.create(mtablename, alter_columns, partition=partition, verbose=verbose)

# alter table
self.alter_table_base(tablename, mtablename, alter_columns, verbose=verbose)
self.alter_table_base(tablename, mtablename, alter_columns, conditions=conditions, verbose=verbose)
4 changes: 2 additions & 2 deletions pydbapi/conf/logconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @Author: longfengpili
# @Date: 2023-07-26 17:46:27
# @Last Modified by: longfengpili
# @Last Modified time: 2023-12-11 17:30:43
# @Last Modified time: 2024-02-29 17:58:27
# @github: https://github.com/longfengpili


Expand All @@ -12,7 +12,7 @@
import colorlog


AUTO_RULES = ['test_xu'] # 可以自动执行表名(表名包含即可)
AUTO_RULES = ['test_xu', 'tmp'] # 可以自动执行表名(表名包含即可)
REDSHIFT_AUTO_RULES = AUTO_RULES + ['_data_aniland'] # Amazon Redshift 可以自动执行表名(表名包含即可)

# logging settings
Expand Down
31 changes: 18 additions & 13 deletions pydbapi/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
# @Author: longfengpili
# @Date: 2023-06-02 15:27:41
# @Last Modified by: longfengpili
# @Last Modified time: 2024-02-29 10:41:50
# @Last Modified time: 2024-02-29 18:36:12
# @github: https://github.com/longfengpili


import re
import sys
import time
import pandas as pd
from datetime import date

from tqdm.contrib.logging import logging_redirect_tqdm

Expand Down Expand Up @@ -59,7 +57,6 @@ def _execute_step(self, cursor, sql, ehandling='raise'):
try:
cursor.execute(sql)
except Exception as e:
# dblogger.error(e)
if ehandling == 'raise':
raise ValueError(f"【Error】:{e}【Sql】:{sql}")

Expand All @@ -81,7 +78,7 @@ def fetch_query_results(self, cur, count, verbose):
if (verbose == 1 or verbose >= 3) and results:
dblogger.info(f"\n{pd.DataFrame(results, columns=columns)}")
elif verbose and not columns:
dblogger.warning(f"Not Columns, cursor description is {desc}")
dblogger.warning(f"Not results, results: {results}")

if results:
results.insert(0, columns)
Expand Down Expand Up @@ -113,7 +110,8 @@ def execute(self, sql, count=None, ehandling='raise', verbose=0):
rows {[int]} -- [影响的行数]
results {[list]} -- [返回的结果]
'''


results = None
conn = self.get_conn()
cur = conn.cursor()
sqls = self.prepare_sql_statements(sql, verbose)
Expand All @@ -136,8 +134,9 @@ def execute(self, sql, count=None, ehandling='raise', verbose=0):
conn.commit()
except Exception as e:
dblogger.error(e)
conn.rollback()
raise e
if ehandling == 'raise':
conn.rollback()
raise e
finally:
if self.dbtype not in ('trino',):
cur.close()
Expand Down Expand Up @@ -272,20 +271,26 @@ def alter_column(self, tablename: str, colname: str, newname: str = None, newtyp

return alter_columns

def alter_table_base(self, ftablename: str, mtablename: str, alter_columns: ColumnsModel, verbose: int = 0):
def alter_table_base(self, ftablename: str, mtablename: str, alter_columns: ColumnsModel,
conditions: list[str] = None, verbose: int = 0):
import time
from datetime import date
# tablename
today = date.today()
today_str = today.strftime('%Y%m%d')
time_str = time.time_ns()
tablename_backup = f"{ftablename}_{today_str}_{time_str}_{self.user}"
tablename_backup = f"{ftablename}_backup_{today_str}_{time_str}_{self.user}"

# alter ftablename to backup
altersql = f'alter table {ftablename} rename to {tablename_backup};'
self.execute(altersql, verbose=verbose)
self.execute(altersql, ehandling='error', verbose=verbose)

# move data to mtablename
self.insert(mtablename, alter_columns, fromtable=tablename_backup, inserttype='select', verbose=verbose)
conditions = conditions or [None]
for condition in conditions:
self.insert(mtablename, alter_columns, fromtable=tablename_backup, inserttype='select',
condition=condition, verbose=verbose)

# alter mtablename to ftablename
altersql = f'alter table {mtablename} rename to {ftablename};'
self.execute(altersql, verbose=verbose)
self.execute(altersql, ehandling='error', verbose=verbose)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# @Author: longfengpili
# @Date: 2023-06-02 15:27:41
# @Last Modified by: longfengpili
# @Last Modified time: 2024-02-29 17:14:36
# @Last Modified time: 2024-02-29 18:41:27
# @github: https://github.com/longfengpili


import setuptools

VERSION = '0.0.117'
VERSION = '0.0.118'
PROJECT_NAME = 'pydbapi'

with open('README.md', 'r', encoding='utf-8') as f:
Expand Down

0 comments on commit 1bea6cb

Please sign in to comment.