summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects
diff options
context:
space:
mode:
authorAlex Grönholm <alex.gronholm@nextday.fi>2016-04-11 17:01:42 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2016-06-01 12:57:36 -0400
commita8e7bb8782ca8fd858ac036082104b4ac2991cfc (patch)
treec3b2a3c1e68b364e33feacd91221b405c6ad737f /lib/sqlalchemy/dialects
parent3f55039e7f15efafacc3e8e0fbf0ba38fa612b09 (diff)
downloadsqlalchemy-a8e7bb8782ca8fd858ac036082104b4ac2991cfc.tar.gz
Implemented CHECK constraint reflection for SQLite and PostgreSQL
Co-Authored-By: Mike Bayer <mike_mp@zzzcomputing.com> Change-Id: Ie6cf2d2958d1c567324db9e08fef2d3186e97350 Pull-request: https://bitbucket.org/zzzeek/sqlalchemy/pull-requests/80
Diffstat (limited to 'lib/sqlalchemy/dialects')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py25
-rw-r--r--lib/sqlalchemy/dialects/sqlite/base.py26
2 files changed, 51 insertions, 0 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py
index 9d019b56e..fe3d29450 100644
--- a/lib/sqlalchemy/dialects/postgresql/base.py
+++ b/lib/sqlalchemy/dialects/postgresql/base.py
@@ -2497,6 +2497,31 @@ class PGDialect(default.DefaultDialect):
for name, uc in uniques.items()
]
+ @reflection.cache
+ def get_check_constraints(
+ self, connection, table_name, schema=None, **kw):
+ table_oid = self.get_table_oid(connection, table_name, schema,
+ info_cache=kw.get('info_cache'))
+
+ CHECK_SQL = """
+ SELECT
+ cons.conname as name,
+ cons.consrc as src
+ FROM
+ pg_catalog.pg_constraint cons
+ WHERE
+ cons.conrelid = :table_oid AND
+ cons.contype = 'c'
+ """
+
+ c = connection.execute(sql.text(CHECK_SQL), table_oid=table_oid)
+
+ return [
+ {'name': name,
+ 'sqltext': src[1:-1]}
+ for name, src in c.fetchall()
+ ]
+
def _load_enums(self, connection, schema=None):
schema = schema or self.default_schema_name
if not self.supports_native_enum:
diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py
index ddd869448..2fe10556b 100644
--- a/lib/sqlalchemy/dialects/sqlite/base.py
+++ b/lib/sqlalchemy/dialects/sqlite/base.py
@@ -1474,6 +1474,32 @@ class SQLiteDialect(default.DefaultDialect):
return unique_constraints
@reflection.cache
+ def get_check_constraints(self, connection, table_name,
+ schema=None, **kw):
+ table_data = self._get_table_sql(
+ connection, table_name, schema=schema, **kw)
+ if not table_data:
+ return []
+
+ CHECK_PATTERN = (
+ '(?:CONSTRAINT (\w+) +)?'
+ 'CHECK *\( *(.+) *\),? *'
+ )
+ check_constraints = []
+ # NOTE: we aren't using re.S here because we actually are
+ # taking advantage of each CHECK constraint being all on one
+ # line in the table definition in order to delineate. This
+ # necessarily makes assumptions as to how the CREATE TABLE
+ # was emitted.
+ for match in re.finditer(CHECK_PATTERN, table_data, re.I):
+ check_constraints.append({
+ 'sqltext': match.group(2),
+ 'name': match.group(1)
+ })
+
+ return check_constraints
+
+ @reflection.cache
def get_indexes(self, connection, table_name, schema=None, **kw):
pragma_indexes = self._get_table_pragma(
connection, "index_list", table_name, schema=schema)