database-wrapper-mysql 0.1.28__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,92 @@
1
+ Metadata-Version: 2.1
2
+ Name: database_wrapper_mysql
3
+ Version: 0.1.28
4
+ Summary: database_wrapper for MySQL database
5
+ Author-email: Gints Murans <gm@gm.lv>
6
+ License: GNU General Public License v3.0 (GPL-3.0)
7
+ Project-URL: Homepage, https://github.com/gintsmurans/py_database_wrapper
8
+ Project-URL: Documentation, https://github.com/gintsmurans/py_database_wrapper
9
+ Project-URL: Changes, https://github.com/gintsmurans/py_database_wrapper
10
+ Project-URL: Code, https://github.com/gintsmurans/py_database_wrapper
11
+ Project-URL: Issue Tracker, https://github.com/gintsmurans/py_database_wrapper/issues
12
+ Project-URL: Download, https://pypi.org/project/database_wrapper/
13
+ Keywords: database,wrapper,python,mysql,mariadb
14
+ Classifier: Development Status :: 4 - Beta
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
17
+ Classifier: Operating System :: MacOS :: MacOS X
18
+ Classifier: Operating System :: Microsoft :: Windows
19
+ Classifier: Operating System :: POSIX
20
+ Classifier: Programming Language :: Python :: 3
21
+ Classifier: Programming Language :: Python :: 3.8
22
+ Classifier: Programming Language :: Python :: 3.9
23
+ Classifier: Programming Language :: Python :: 3.10
24
+ Classifier: Programming Language :: Python :: 3.11
25
+ Classifier: Programming Language :: Python :: 3.12
26
+ Classifier: Programming Language :: Python :: 3.13
27
+ Classifier: Programming Language :: Python :: Implementation :: CPython
28
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
29
+ Classifier: Topic :: Database
30
+ Classifier: Topic :: Database :: Front-Ends
31
+ Classifier: Topic :: Software Development
32
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
33
+ Requires-Python: >=3.8
34
+ Description-Content-Type: text/markdown
35
+ Requires-Dist: database_wrapper==0.1.28
36
+ Requires-Dist: mysqlclient>=2.2.2
37
+
38
+ # database_wrapper
39
+
40
+ _Part of the `database_wrapper` package._
41
+
42
+ This python package is a database wrapper for [MySQL](https://www.mysql.com/) and [MariaDB](https://mariadb.org/) databases.
43
+
44
+ ## Installation
45
+
46
+ ```bash
47
+ pip install database_wrapper[mysql]
48
+ ```
49
+
50
+ ## Usage
51
+
52
+ ```python
53
+ from database_wrapper_mysql import MySQL, DBWrapperMySQL
54
+
55
+ db = MySQL({
56
+ "hostname": "localhost",
57
+ "port": 3306,
58
+ "username": "root",
59
+ "password": "your_password",
60
+ "database": "my_database"
61
+ })
62
+ db.open()
63
+ dbWrapper = DBWrapperMySQL(db=db)
64
+
65
+ # Simple query
66
+ aModel = MyModel()
67
+ res = await dbWrapper.getByKey(
68
+ aModel,
69
+ "id",
70
+ 3005,
71
+ )
72
+ if res:
73
+ print(f"getByKey: {res.toDict()}")
74
+ else:
75
+ print("No results")
76
+
77
+ # Raw query
78
+ res = await dbWrapper.getAll(
79
+ aModel,
80
+ """
81
+ SELECT t1.*, t2.name AS other_name
82
+ FROM my_table AS t1
83
+ LEFT JOIN other_table AS t2 ON t1.other_id = t2.id
84
+ """
85
+ )
86
+ async for record in res:
87
+ print(f"getAll: {record.toDict()}")
88
+ else:
89
+ print("No results")
90
+
91
+ db.close()
92
+ ```
@@ -0,0 +1,55 @@
1
+ # database_wrapper
2
+
3
+ _Part of the `database_wrapper` package._
4
+
5
+ This python package is a database wrapper for [MySQL](https://www.mysql.com/) and [MariaDB](https://mariadb.org/) databases.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pip install database_wrapper[mysql]
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```python
16
+ from database_wrapper_mysql import MySQL, DBWrapperMySQL
17
+
18
+ db = MySQL({
19
+ "hostname": "localhost",
20
+ "port": 3306,
21
+ "username": "root",
22
+ "password": "your_password",
23
+ "database": "my_database"
24
+ })
25
+ db.open()
26
+ dbWrapper = DBWrapperMySQL(db=db)
27
+
28
+ # Simple query
29
+ aModel = MyModel()
30
+ res = await dbWrapper.getByKey(
31
+ aModel,
32
+ "id",
33
+ 3005,
34
+ )
35
+ if res:
36
+ print(f"getByKey: {res.toDict()}")
37
+ else:
38
+ print("No results")
39
+
40
+ # Raw query
41
+ res = await dbWrapper.getAll(
42
+ aModel,
43
+ """
44
+ SELECT t1.*, t2.name AS other_name
45
+ FROM my_table AS t1
46
+ LEFT JOIN other_table AS t2 ON t1.other_id = t2.id
47
+ """
48
+ )
49
+ async for record in res:
50
+ print(f"getAll: {record.toDict()}")
51
+ else:
52
+ print("No results")
53
+
54
+ db.close()
55
+ ```
@@ -0,0 +1,24 @@
1
+ """
2
+ database_wrapper_mysql package - MySQL database wrapper
3
+
4
+ Part of the database_wrapper package
5
+ """
6
+
7
+ # Copyright 2024 Gints Murans
8
+
9
+ import logging
10
+
11
+ from .db_wrapper_mysql import DBWrapperMysql
12
+ from .connector import MyConfig, MySQL
13
+
14
+ # Set the logger to a quiet default, can be enabled if needed
15
+ logger = logging.getLogger("database_wrapper_mysql")
16
+ if logger.level == logging.NOTSET:
17
+ logger.setLevel(logging.WARNING)
18
+
19
+
20
+ __all__ = [
21
+ "DBWrapperMysql",
22
+ "MyConfig",
23
+ "MySQL",
24
+ ]
@@ -0,0 +1,101 @@
1
+ from typing import Any, NotRequired, TypedDict
2
+
3
+ from MySQLdb.connections import Connection as MySqlConnection
4
+ from MySQLdb.cursors import DictCursor as MySqlDictCursor
5
+
6
+ from database_wrapper import DatabaseBackend
7
+
8
+
9
+ class MyConfig(TypedDict):
10
+ hostname: str
11
+ port: NotRequired[int]
12
+ username: str
13
+ password: str
14
+ database: str
15
+ charset: NotRequired[str]
16
+ collation: NotRequired[str]
17
+ kwargs: NotRequired[dict[str, Any]]
18
+
19
+
20
+ class MySQL(DatabaseBackend):
21
+ """
22
+ MySQL database backend
23
+
24
+ :param config: Configuration for MySQL
25
+ :type config: MyConfig
26
+
27
+ Defaults:
28
+ port = 0 - See comment below
29
+ charset = utf8
30
+ collation = utf8_general_ci
31
+ """
32
+
33
+ config: MyConfig
34
+
35
+ connection: MySqlConnection
36
+ cursor: MySqlDictCursor
37
+
38
+ def open(self):
39
+ # Free resources
40
+ if hasattr(self, "connection") and self.connection:
41
+ self.close()
42
+
43
+ # Set defaults
44
+ if "port" not in self.config or not self.config["port"]:
45
+ self.config["port"] = 0
46
+
47
+ if "charset" not in self.config or not self.config["charset"]:
48
+ self.config["charset"] = "utf8"
49
+
50
+ if "collation" not in self.config or not self.config["collation"]:
51
+ self.config["collation"] = "utf8_general_ci"
52
+
53
+ if "kwargs" not in self.config or not self.config["kwargs"]:
54
+ self.config["kwargs"] = {}
55
+
56
+ self.logger.debug("Connecting to DB")
57
+ self.connection = MySqlConnection(
58
+ host=self.config["hostname"],
59
+ user=self.config["username"],
60
+ passwd=self.config["password"],
61
+ db=self.config["database"],
62
+ # By default, when port is not specified, Python library passes 0 to
63
+ # MySQL C API function mysql_real_connect as port number.
64
+ #
65
+ # At https://dev.mysql.com/doc/c-api/8.0/en/mysql-real-connect.html
66
+ # is written "If port is not 0, the value is used as the port number
67
+ # for the TCP/IP connection."
68
+ #
69
+ # We keep the same behavior not to break services that have port
70
+ # number unspecified.
71
+ port=self.config.get("port", 0),
72
+ connect_timeout=self.connectionTimeout,
73
+ use_unicode=True,
74
+ charset=self.config["charset"],
75
+ collation=self.config["collation"],
76
+ cursorclass=MySqlDictCursor,
77
+ **self.config["kwargs"],
78
+ )
79
+ self.cursor = self.connection.cursor(MySqlDictCursor)
80
+
81
+ def lastInsertId(self) -> int:
82
+ assert self.cursor, "Cursor is not initialized"
83
+ return self.cursor.lastrowid
84
+
85
+ def affectedRows(self) -> int:
86
+ assert self.cursor, "Cursor is not initialized"
87
+ return self.cursor.rowcount
88
+
89
+ def commit(self) -> None:
90
+ """Commit DB queries"""
91
+ assert self.connection, "Connection is not initialized"
92
+
93
+ self.logger.debug("Commit DB queries..")
94
+ self.connection.commit()
95
+
96
+ def rollback(self) -> None:
97
+ """Rollback DB queries"""
98
+ assert self.connection, "Connection is not initialized"
99
+
100
+ self.logger.debug("Rollback DB queries..")
101
+ self.connection.rollback()
@@ -0,0 +1,66 @@
1
+ import logging
2
+ from typing import Any
3
+
4
+ from MySQLdb.connections import Connection as MySqlConnection
5
+ from MySQLdb.cursors import DictCursor as MySqlDictCursor
6
+
7
+ from database_wrapper import DBWrapper
8
+
9
+ from .connector import MySQL
10
+
11
+
12
+ class DBWrapperMysql(DBWrapper):
13
+ """Base model for all RV4 models"""
14
+
15
+ # Override db instance
16
+ db: MySQL
17
+
18
+ #######################
19
+ ### Class lifecycle ###
20
+ #######################
21
+
22
+ # Meta methods
23
+ def __init__(
24
+ self,
25
+ db: MySQL,
26
+ dbConn: MySqlConnection | None = None,
27
+ logger: logging.Logger | None = None,
28
+ ):
29
+ """
30
+ Initializes a new instance of the DBWrapper class.
31
+
32
+ Args:
33
+ db (MySQL): The MySQL object.
34
+ logger (logging.Logger, optional): The logger object. Defaults to None.
35
+ """
36
+ super().__init__(db, dbConn, logger)
37
+
38
+ ######################
39
+ ### Helper methods ###
40
+ ######################
41
+
42
+ def logQuery(
43
+ self,
44
+ cursor: MySqlDictCursor,
45
+ query: Any,
46
+ params: tuple[Any, ...],
47
+ ) -> None:
48
+ """
49
+ Logs the given query and parameters.
50
+
51
+ Args:
52
+ query (Any): The query to log.
53
+ params (tuple[Any, ...]): The parameters to log.
54
+ """
55
+ queryString = cursor.mogrify(query, params)
56
+ self.logger.debug(f"Query: {queryString}")
57
+
58
+ #####################
59
+ ### Query methods ###
60
+ #####################
61
+
62
+ def limitQuery(self, offset: int = 0, limit: int = 100) -> str:
63
+ return f"LIMIT {offset},{limit}"
64
+
65
+ async def createCursor(self, emptyDataClass: Any | None = None) -> MySqlDictCursor:
66
+ return self.db.connection.cursor(MySqlDictCursor)
@@ -0,0 +1,92 @@
1
+ Metadata-Version: 2.1
2
+ Name: database_wrapper_mysql
3
+ Version: 0.1.28
4
+ Summary: database_wrapper for MySQL database
5
+ Author-email: Gints Murans <gm@gm.lv>
6
+ License: GNU General Public License v3.0 (GPL-3.0)
7
+ Project-URL: Homepage, https://github.com/gintsmurans/py_database_wrapper
8
+ Project-URL: Documentation, https://github.com/gintsmurans/py_database_wrapper
9
+ Project-URL: Changes, https://github.com/gintsmurans/py_database_wrapper
10
+ Project-URL: Code, https://github.com/gintsmurans/py_database_wrapper
11
+ Project-URL: Issue Tracker, https://github.com/gintsmurans/py_database_wrapper/issues
12
+ Project-URL: Download, https://pypi.org/project/database_wrapper/
13
+ Keywords: database,wrapper,python,mysql,mariadb
14
+ Classifier: Development Status :: 4 - Beta
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
17
+ Classifier: Operating System :: MacOS :: MacOS X
18
+ Classifier: Operating System :: Microsoft :: Windows
19
+ Classifier: Operating System :: POSIX
20
+ Classifier: Programming Language :: Python :: 3
21
+ Classifier: Programming Language :: Python :: 3.8
22
+ Classifier: Programming Language :: Python :: 3.9
23
+ Classifier: Programming Language :: Python :: 3.10
24
+ Classifier: Programming Language :: Python :: 3.11
25
+ Classifier: Programming Language :: Python :: 3.12
26
+ Classifier: Programming Language :: Python :: 3.13
27
+ Classifier: Programming Language :: Python :: Implementation :: CPython
28
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
29
+ Classifier: Topic :: Database
30
+ Classifier: Topic :: Database :: Front-Ends
31
+ Classifier: Topic :: Software Development
32
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
33
+ Requires-Python: >=3.8
34
+ Description-Content-Type: text/markdown
35
+ Requires-Dist: database_wrapper==0.1.28
36
+ Requires-Dist: mysqlclient>=2.2.2
37
+
38
+ # database_wrapper
39
+
40
+ _Part of the `database_wrapper` package._
41
+
42
+ This python package is a database wrapper for [MySQL](https://www.mysql.com/) and [MariaDB](https://mariadb.org/) databases.
43
+
44
+ ## Installation
45
+
46
+ ```bash
47
+ pip install database_wrapper[mysql]
48
+ ```
49
+
50
+ ## Usage
51
+
52
+ ```python
53
+ from database_wrapper_mysql import MySQL, DBWrapperMySQL
54
+
55
+ db = MySQL({
56
+ "hostname": "localhost",
57
+ "port": 3306,
58
+ "username": "root",
59
+ "password": "your_password",
60
+ "database": "my_database"
61
+ })
62
+ db.open()
63
+ dbWrapper = DBWrapperMySQL(db=db)
64
+
65
+ # Simple query
66
+ aModel = MyModel()
67
+ res = await dbWrapper.getByKey(
68
+ aModel,
69
+ "id",
70
+ 3005,
71
+ )
72
+ if res:
73
+ print(f"getByKey: {res.toDict()}")
74
+ else:
75
+ print("No results")
76
+
77
+ # Raw query
78
+ res = await dbWrapper.getAll(
79
+ aModel,
80
+ """
81
+ SELECT t1.*, t2.name AS other_name
82
+ FROM my_table AS t1
83
+ LEFT JOIN other_table AS t2 ON t1.other_id = t2.id
84
+ """
85
+ )
86
+ async for record in res:
87
+ print(f"getAll: {record.toDict()}")
88
+ else:
89
+ print("No results")
90
+
91
+ db.close()
92
+ ```
@@ -0,0 +1,11 @@
1
+ README.md
2
+ pyproject.toml
3
+ database_wrapper_mysql/__init__.py
4
+ database_wrapper_mysql/connector.py
5
+ database_wrapper_mysql/db_wrapper_mysql.py
6
+ database_wrapper_mysql/py.typed
7
+ database_wrapper_mysql.egg-info/PKG-INFO
8
+ database_wrapper_mysql.egg-info/SOURCES.txt
9
+ database_wrapper_mysql.egg-info/dependency_links.txt
10
+ database_wrapper_mysql.egg-info/requires.txt
11
+ database_wrapper_mysql.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ database_wrapper==0.1.28
2
+ mysqlclient>=2.2.2
@@ -0,0 +1 @@
1
+ database_wrapper_mysql
@@ -0,0 +1,51 @@
1
+ [build-system]
2
+ requires = ["setuptools >= 61.0.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "database_wrapper_mysql"
7
+ version = "0.1.28"
8
+ description = "database_wrapper for MySQL database"
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ license = {text = "GNU General Public License v3.0 (GPL-3.0)"}
12
+ authors = [
13
+ {name = "Gints Murans", email = "gm@gm.lv"}
14
+ ]
15
+ classifiers = [
16
+ "Development Status :: 4 - Beta",
17
+ "Intended Audience :: Developers",
18
+ "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
19
+ "Operating System :: MacOS :: MacOS X",
20
+ "Operating System :: Microsoft :: Windows",
21
+ "Operating System :: POSIX",
22
+ "Programming Language :: Python :: 3",
23
+ "Programming Language :: Python :: 3.8",
24
+ "Programming Language :: Python :: 3.9",
25
+ "Programming Language :: Python :: 3.10",
26
+ "Programming Language :: Python :: 3.11",
27
+ "Programming Language :: Python :: 3.12",
28
+ "Programming Language :: Python :: 3.13",
29
+ "Programming Language :: Python :: Implementation :: CPython",
30
+ "Programming Language :: Python :: Implementation :: PyPy",
31
+ "Topic :: Database",
32
+ "Topic :: Database :: Front-Ends",
33
+ "Topic :: Software Development",
34
+ "Topic :: Software Development :: Libraries :: Python Modules"
35
+ ]
36
+ keywords = ["database", "wrapper", "python", "mysql", "mariadb"]
37
+ dependencies = [
38
+ "database_wrapper == 0.1.28",
39
+ "mysqlclient >= 2.2.2"
40
+ ]
41
+
42
+ [project.urls]
43
+ Homepage = "https://github.com/gintsmurans/py_database_wrapper"
44
+ Documentation = "https://github.com/gintsmurans/py_database_wrapper"
45
+ Changes = "https://github.com/gintsmurans/py_database_wrapper"
46
+ Code = "https://github.com/gintsmurans/py_database_wrapper"
47
+ "Issue Tracker" = "https://github.com/gintsmurans/py_database_wrapper/issues"
48
+ Download = "https://pypi.org/project/database_wrapper/"
49
+
50
+ [tool.setuptools.package-data]
51
+ database_wrapper_mysql = ["py.typed"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+