]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Added INET4 and INET6 types for MariaDB
authorAdam Žurek <thejabko@gmail.com>
Fri, 15 Nov 2024 18:12:54 +0000 (13:12 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 19 Nov 2024 14:01:51 +0000 (09:01 -0500)
Added sql types ``INET4`` and ``INET6`` in the MariaDB dialect.

Fixes: #10720
Closes: #12028
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12028
Pull-request-sha: 25f939076eda0a763bc33fb0455d45ef00002110

Change-Id: I2efa53420aa5566f61a19f228cb421116b2e2720
(cherry picked from commit 0f81c14b7cd9ac821205d6c48cf2393447058394)

doc/build/changelog/unreleased_20/10720.rst [new file with mode: 0644]
doc/build/dialects/mysql.rst
lib/sqlalchemy/dialects/mysql/__init__.py
lib/sqlalchemy/dialects/mysql/mariadb.py
test/dialect/mysql/test_types.py

diff --git a/doc/build/changelog/unreleased_20/10720.rst b/doc/build/changelog/unreleased_20/10720.rst
new file mode 100644 (file)
index 0000000..d676a44
--- /dev/null
@@ -0,0 +1,5 @@
+.. change::
+    :tags: usecase, mariadb
+    :ticket: 10720
+
+    Added sql types ``INET4`` and ``INET6`` in the MariaDB dialect.
index a46bf721e21cd3f4f10186a7833bfcd1458d334c..657cd2a41893aee23c574305ed0614860e08e062 100644 (file)
@@ -56,7 +56,14 @@ valid with MySQL are importable from the top level dialect::
         YEAR,
     )
 
-Types which are specific to MySQL, or have MySQL-specific
+In addition to the above types, MariaDB also supports the following::
+
+    from sqlalchemy.dialects.mysql import (
+        INET4,
+        INET6,
+    )
+
+Types which are specific to MySQL or MariaDB, or have specific
 construction arguments, are as follows:
 
 .. note: where :noindex: is used, indicates a type that is not redefined
@@ -117,6 +124,10 @@ construction arguments, are as follows:
     :members: __init__
 
 
+.. autoclass:: INET4
+
+.. autoclass:: INET6
+
 .. autoclass:: INTEGER
     :members: __init__
 
index 60bac87443d47bee0a5e419e5967a66d96b70f4f..05f41cf351234f261b0ac8d15d1c3fd638169791 100644 (file)
@@ -53,7 +53,8 @@ from .base import YEAR
 from .dml import Insert
 from .dml import insert
 from .expression import match
-from ...util import compat
+from .mariadb import INET4
+from .mariadb import INET6
 
 # default dialect
 base.dialect = dialect = mysqldb.dialect
@@ -71,6 +72,8 @@ __all__ = (
     "DOUBLE",
     "ENUM",
     "FLOAT",
+    "INET4",
+    "INET6",
     "INTEGER",
     "INTEGER",
     "JSON",
index 10a05f9cb36cfdfda1d1a1e62cefd4a4b940666d..be7aebeaeb4da74aa7d20b86aee04a15cbfa33b8 100644 (file)
@@ -7,6 +7,34 @@
 # mypy: ignore-errors
 from .base import MariaDBIdentifierPreparer
 from .base import MySQLDialect
+from .base import MySQLTypeCompiler
+from ...sql import sqltypes
+
+
+class INET4(sqltypes.TypeEngine[str]):
+    """INET4 column type for MariaDB
+
+    .. versionadded:: 2.0.37
+    """
+
+    __visit_name__ = "INET4"
+
+
+class INET6(sqltypes.TypeEngine[str]):
+    """INET6 column type for MariaDB
+
+    .. versionadded:: 2.0.37
+    """
+
+    __visit_name__ = "INET6"
+
+
+class MariaDBTypeCompiler(MySQLTypeCompiler):
+    def visit_INET4(self, type_, **kwargs) -> str:
+        return "INET4"
+
+    def visit_INET6(self, type_, **kwargs) -> str:
+        return "INET6"
 
 
 class MariaDBDialect(MySQLDialect):
@@ -14,6 +42,7 @@ class MariaDBDialect(MySQLDialect):
     supports_statement_cache = True
     name = "mariadb"
     preparer = MariaDBIdentifierPreparer
+    type_compiler_cls = MariaDBTypeCompiler
 
 
 def loader(driver):
index c73e82a945b9f1a26fb3160facf59f2f9a2c1c99..2e5033ec571e9e28f79fa6b937940d10d1873a09 100644 (file)
@@ -21,6 +21,7 @@ from sqlalchemy import TypeDecorator
 from sqlalchemy import types as sqltypes
 from sqlalchemy import UnicodeText
 from sqlalchemy.dialects.mysql import base as mysql
+from sqlalchemy.dialects.mysql import mariadb
 from sqlalchemy.testing import assert_raises
 from sqlalchemy.testing import assert_raises_message
 from sqlalchemy.testing import AssertsCompiledSQL
@@ -474,6 +475,17 @@ class TypeCompileTest(fixtures.TestBase, AssertsCompiledSQL):
         self.assert_compile(type_, sql_text)
 
 
+class INETMariadbTest(fixtures.TestBase, AssertsCompiledSQL):
+    __dialect__ = mariadb.MariaDBDialect()
+
+    @testing.combinations(
+        (mariadb.INET4(), "INET4"),
+        (mariadb.INET6(), "INET6"),
+    )
+    def test_mariadb_inet6(self, type_, res):
+        self.assert_compile(type_, res)
+
+
 class TypeRoundTripTest(fixtures.TestBase, AssertsExecutionResults):
     __dialect__ = mysql.dialect()
     __only_on__ = "mysql", "mariadb"