Skip to content

Commit

Permalink
update pymysql version
Browse files Browse the repository at this point in the history
  • Loading branch information
zxyle committed Apr 5, 2022
1 parent 5f8b217 commit 1cdf858
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
25 changes: 13 additions & 12 deletions crudlib/mysql.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import re
from urllib.parse import unquote
from typing import List, Dict

import pymysql
from pymysql.cursors import DictCursor

from crudlib.base import DataBase
from crudlib.config import DEFAULT_MYSQL_URI
Expand All @@ -15,15 +16,15 @@ def __init__(self, uri=None, debug=False):
self.connection = pymysql.connect(host=u.host,
port=u.port,
user=u.user,
password=unquote(u.password),
password=u.password,
db=u.db,
charset=u.params.get('charset', 'utf8mb4'),
cursorclass=pymysql.cursors.DictCursor)
self._open = False
self.cursor = None
self._debug = debug

def insert_one(self, tb, doc):
def insert_one(self, tb: str, doc: dict):
"""
Insert a record operation
:param tb: mysql table name
Expand All @@ -41,7 +42,7 @@ def insert_one(self, tb, doc):
sql = f"INSERT INTO `{tb}` ({fields_sql}) VALUES ({values_sql});"
return self.execute(sql, values)

def insert_many(self, tb, doc_list):
def insert_many(self, tb: str, doc_list: List[Dict]):
"""
Insert multi records operation
:param tb: mysql table name
Expand All @@ -58,7 +59,7 @@ def _get_cursor(self):
self._open = True
return self.cursor

def execute(self, sql, data=None):
def execute(self, sql: str, data=None):
"""
execute sql operation
:param sql: SQL statement
Expand All @@ -72,7 +73,7 @@ def execute(self, sql, data=None):
# TODO fetchall fetchone fetchmany
return cursor.fetchall()

def create_db(self, db_name=""):
def create_db(self, db_name: str = ""):
"""Create Database operation"""
if not db_name:
raise ValueError("db name not allowed to be empty.")
Expand All @@ -82,13 +83,13 @@ def create_db(self, db_name=""):
self.execute(sql)
self._debug_info("create database: `{}` success.".format(db_name))

def drop_db(self, db_name=""):
def drop_db(self, db_name: str = ""):
"""Drop database operation"""
sql = f"DROP DATABASE IF EXISTS {db_name};"
self.execute(sql)
self._debug_info("drop database: `{}` success.".format(db_name))

def create_tb(self, tb=""):
def create_tb(self, tb: str = ""):
"""Create table operation"""
sql = f"""
CREATE TABLE IF NOT EXISTS `{tb}`(
Expand All @@ -102,13 +103,13 @@ def create_tb(self, tb=""):
self.execute(sql)
self._debug_info("create table: `{}` success.".format(tb))

def drop_tb(self, tb=""):
def drop_tb(self, tb: str = ""):
"""Drop table operation"""
sql = f"DROP TABLE IF EXISTS {tb};"
self.execute(sql)
self._debug_info("drop table: `{}` success.".format(tb))

def query(self, tb, condition=None):
def query(self, tb: str, condition=None):
"""Select records operation"""
condition = condition or {}
condition_sql, values = self._where(condition)
Expand All @@ -118,7 +119,7 @@ def query(self, tb, condition=None):
results = self.execute(sql, values)
return results

def update(self, tb, doc, condition):
def update(self, tb: str, doc, condition):
"""
Update records operation
:param tb:
Expand All @@ -138,7 +139,7 @@ def update(self, tb, doc, condition):
self.execute(sql, values + v)
self._debug_info("update success.")

def delete(self, tb, condition=None):
def delete(self, tb: str, condition=None):
"""Delete records operation"""
condition = condition or {}
condition_sql, values = self._where(condition)
Expand Down
2 changes: 1 addition & 1 deletion crudlib/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def user(self):

@property
def password(self):
return self.handle.password
return unquote(self.handle.password)

@property
def host(self):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pymysql==0.9.3
pymysql==1.0.2
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"Intended Audience :: Developers",
"Operating System :: OS Independent",
"Topic :: Utilities",
"Topic :: Internet",
"Topic :: Database",
"Topic :: Software Development :: Libraries :: Python Modules",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
Expand Down

0 comments on commit 1cdf858

Please sign in to comment.