Skip to content

Commit

Permalink
[优化]conn
Browse files Browse the repository at this point in the history
  • Loading branch information
longfengpili committed Feb 28, 2024
1 parent f8a2f8f commit 9dbac70
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
16 changes: 9 additions & 7 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: 2023-12-04 11:45:28
# @Last Modified time: 2024-02-28 15:36:43
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -132,12 +132,14 @@ 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)
mysqllogger.info(f'connect {self.__class__.__name__}({self.user}@{self.host}:{self.port}/{self.database})')
if not conn:
self.get_conn()
return conn
if not hasattr(MysqlDB, '_conn'):
with MysqlDB._instance_lock:
if not hasattr(MysqlDB, '_conn'):
conn = pymysql.connect(database=self.database, user=self.user, password=self.password,
host=self.host, port=self.port, charset=self.charset)
mysqllogger.info(f'connect {self.__class__.__name__}({self.user}@{self.host}:{self.port}/{self.database})')
MysqlDB._conn = conn
return MysqlDB._conn

def create(self, tablename, columns, indexes=None, index_part=128, ismultiple_index=True,
partition=None, distribution=None, verbose=0):
Expand Down
14 changes: 8 additions & 6 deletions pydbapi/api/redshift.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-12-04 11:45:42
# @Last Modified time: 2024-02-28 15:37:51
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -71,11 +71,13 @@ def get_instance(cls, *args, **kwargs):
return RedshiftDB._instance

def get_conn(self):
conn = psycopg2.connect(database=self.database, user=self.user, password=self.password, host=self.host, port=self.port)
redlogger.info(f'connect {self.__class__.__name__}({self.user}@{self.host}:{self.port}/{self.database})')
if not conn:
self.get_conn()
return conn
if not hasattr(RedshiftDB, '_conn'):
with RedshiftDB._instance_lock:
if not hasattr(RedshiftDB, '_conn'):
conn = psycopg2.connect(database=self.database, user=self.user, password=self.password, host=self.host, port=self.port)
redlogger.info(f'connect {self.__class__.__name__}({self.user}@{self.host}:{self.port}/{self.database})')
RedshiftDB._conn = conn
return RedshiftDB._conn

def create(self, tablename, columns, indexes=None, verbose=0):
# tablename = f"{self.database}.{tablename}"
Expand Down
11 changes: 10 additions & 1 deletion pydbapi/api/sqlite.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 13:55:40
# @Last Modified time: 2024-02-28 15:38:23
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -71,6 +71,15 @@ def get_conn(self):
self.get_conn()
return conn

def get_conn(self):
if not hasattr(SqliteDB, '_conn'):
with SqliteDB._instance_lock:
if not hasattr(SqliteDB, '_conn'):
conn = sqlite3.connect(database=self.database)
sqlitelogger.info(f'connect {self.__class__.__name__}({self.database})')
SqliteDB._conn = conn
return SqliteDB._conn

def create(self, tablename, columns, indexes=None, verbose=0):
# tablename = f"{self.database}.{tablename}"
sqlcompile = SqliteCompile(tablename)
Expand Down
18 changes: 15 additions & 3 deletions pydbapi/db/base.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 13:55:25
# @Last Modified time: 2024-02-28 15:50:20
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -139,7 +139,7 @@ def execute(self, sql, count=None, ehandling='raise', verbose=0):
finally:
if self.dbtype not in ('trino',):
cur.close()
conn.close()
# conn.close() # 注释掉conn

rows = cur.rowcount
rows = len(results[1:]) if rows == -1 and results else rows
Expand Down Expand Up @@ -206,7 +206,6 @@ def insert(self, tablename, columns, inserttype='value', values=None, chunksize=
def get_columns(self, tablename, verbose=0):
sql = f"pragma table_info('{tablename}');" if self.dbtype == 'sqlite' else f"show columns from {tablename};"
rows, action, results = self.execute(sql, verbose=verbose)
print(action)

_, cols = results[0], results[1:]
nameidx = 1 if self.dbtype == 'sqlite' else 0
Expand Down Expand Up @@ -257,3 +256,16 @@ def add_columns(self, tablename, columns, verbose=0):
sql = sqlcompile.add_column(column.newname, column.coltype)
self.execute(sql, verbose=0)
dblogger.info(f'【{tablename}】add columns succeeded !【{new_columns - old_columns}】')

def alter_column(self, tablename: str, colname: str, newname: str = None, newtype: str = None):
old_columns = ColumnsModel(*self.get_columns(tablename))
alter_col = old_columns.get_column_by_name(colname)

newname = newname or alter_col.newname
newtype = newtype or alter_col.coltype
sqlexpr = f"cast({colname} as {newtype})"
newcol = ColumnModel(newname, newtype, sqlexpr=sqlexpr)

alter_columns = old_columns.alter(colname, newcol)

return old_columns, alter_columns
23 changes: 22 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-02-28 11:47:20
# @Last Modified time: 2024-02-28 15:44:18
# @github: https://github.com/longfengpili


Expand Down Expand Up @@ -141,3 +141,24 @@ def test_select_by_sql(self):
# print(sql)
rows, action, result = self.mysqldb.execute(sql, verbose=3)
print(f"【rows】: {rows}, 【action】: {action}, 【result】: {result}")

def test_log(self):
sql = f'''
-- test1
with test as
(select *
from {self.tablename}
limit 10)
select birthday as time, name as adid, substring(birthday, 1, 10) as dt
from test
'''

rows, action, result = self.mysqldb.execute(sql)
print(rows, action, result)
rows, action, result = self.mysqldb.execute(sql)
print(rows, action, result)
rows, action, result = self.mysqldb.execute(sql)
print(rows, action, result)

0 comments on commit 9dbac70

Please sign in to comment.