|
| 1 | +from sqlalchemy import exc |
| 2 | +from sqlalchemy import util |
| 3 | + |
1 | 4 | from .base import IRISDialect |
2 | 5 |
|
3 | 6 |
|
| 7 | +def _parse_version_number(server_version): |
| 8 | + server_version = str(server_version).split(" ")[0].split(".") |
| 9 | + return tuple([int("".join(filter(str.isdigit, v)) or 0) for v in server_version]) |
| 10 | + |
| 11 | + |
4 | 12 | class IRISDialect_emb(IRISDialect): |
5 | 13 | driver = "emb" |
6 | 14 |
|
7 | 15 | embedded = True |
8 | 16 |
|
9 | 17 | supports_statement_cache = True |
10 | 18 |
|
| 19 | + insert_returning = False |
| 20 | + insert_executemany_returning = False |
| 21 | + insert_executemany_returning_sort_by_parameter_order = False |
| 22 | + |
| 23 | + _isolation_lookup = set( |
| 24 | + [ |
| 25 | + "READ UNCOMMITTED", |
| 26 | + "READ COMMITTED", |
| 27 | + "REPEATABLE READ", |
| 28 | + "SERIALIZABLE", |
| 29 | + ] |
| 30 | + ) |
| 31 | + |
11 | 32 | def _get_option(self, connection, option): |
12 | | - return connection.iris.cls("%SYSTEM.SQL.Util").GetOption(option) |
| 33 | + import iris |
| 34 | + |
| 35 | + return iris.cls("%SYSTEM.SQL.Util").GetOption(option) |
13 | 36 |
|
14 | 37 | def _set_option(self, connection, option, value): |
15 | | - return connection.iris.cls("%SYSTEM.SQL.Util").SetOption(option) |
| 38 | + import iris |
| 39 | + |
| 40 | + return iris.cls("%SYSTEM.SQL.Util").SetOption(option, value) |
16 | 41 |
|
17 | 42 | @classmethod |
18 | 43 | def import_dbapi(cls): |
19 | | - import intersystems_iris.dbapi._DBAPI as dbapi |
| 44 | + import iris |
| 45 | + |
| 46 | + return iris.dbapi |
| 47 | + |
| 48 | + def create_connect_args(self, url): |
| 49 | + if url.port or url.username or url.password: |
| 50 | + raise exc.ArgumentError( |
| 51 | + "iris+emb:// URLs are local-only; use iris:// or iris+intersystems:// " |
| 52 | + "for host, port, username, or password connections" |
| 53 | + ) |
| 54 | + |
| 55 | + if url.host and url.database: |
| 56 | + raise exc.ArgumentError( |
| 57 | + "iris+emb:// URLs accept the namespace as either " |
| 58 | + "iris+emb://NAMESPACE or iris+emb:///NAMESPACE, not both" |
| 59 | + ) |
| 60 | + |
| 61 | + supported_query_args = {"path"} |
| 62 | + unsupported_query_args = set(url.query).difference(supported_query_args) |
| 63 | + if unsupported_query_args: |
| 64 | + raise exc.ArgumentError( |
| 65 | + "Unsupported iris+emb:// query argument(s): " |
| 66 | + + ", ".join(sorted(unsupported_query_args)) |
| 67 | + ) |
| 68 | + |
| 69 | + opts = { |
| 70 | + "mode": "embedded", |
| 71 | + "namespace": url.host or url.database or "USER", |
| 72 | + } |
| 73 | + path = url.query.get("path") |
| 74 | + if path is not None: |
| 75 | + if isinstance(path, tuple): |
| 76 | + path = path[-1] |
| 77 | + opts["path"] = path |
20 | 78 |
|
21 | | - return dbapi |
| 79 | + return ([], opts) |
22 | 80 |
|
23 | 81 | def _get_server_version_info(self, connection): |
24 | | - server_version = connection._dbapi_connection.iris.system.Version.GetNumber() |
25 | | - server_version = server_version.split(".") |
26 | | - return tuple([int("".join(filter(str.isdigit, v))) for v in server_version]) |
| 82 | + import iris |
| 83 | + |
| 84 | + version_api = getattr(getattr(iris, "system", None), "Version", None) |
| 85 | + get_number = getattr(version_api, "GetNumber", None) |
| 86 | + if callable(get_number): |
| 87 | + return _parse_version_number(get_number()) |
| 88 | + |
| 89 | + return _parse_version_number(iris.cls("%SYSTEM.Version").GetNumber()) |
| 90 | + |
| 91 | + def on_connect(self): |
| 92 | + def on_connect(conn): |
| 93 | + try: |
| 94 | + with conn.cursor() as cursor: |
| 95 | + cursor.execute( |
| 96 | + "select vector_cosine(to_vector('1'), to_vector('1'))" |
| 97 | + ) |
| 98 | + cursor.execute("select to_vector('1')") |
| 99 | + cursor.fetchone() |
| 100 | + self.supports_vectors = True |
| 101 | + except Exception: |
| 102 | + self.supports_vectors = False |
| 103 | + |
| 104 | + try: |
| 105 | + with conn.cursor() as cursor: |
| 106 | + cursor.execute("SELECT TOP 1 Name FROM %Dictionary.PropertyDefinition") |
| 107 | + cursor.fetchone() |
| 108 | + self._dictionary_access = True |
| 109 | + except Exception: |
| 110 | + self._dictionary_access = False |
| 111 | + |
| 112 | + if not self._dictionary_access: |
| 113 | + util.warn( |
| 114 | + """ |
| 115 | +There are no access to %Dictionary, may be required for some advanced features, |
| 116 | + such as Calculated fields, and include columns in indexes |
| 117 | + """.replace( |
| 118 | + "\n", "" |
| 119 | + ) |
| 120 | + ) |
| 121 | + |
| 122 | + return on_connect |
| 123 | + |
| 124 | + def get_isolation_level(self, connection): |
| 125 | + if getattr(connection, "autocommit", False): |
| 126 | + return "AUTOCOMMIT" |
| 127 | + |
| 128 | + isolation_level = getattr(connection, "isolation_level", None) |
| 129 | + if isolation_level: |
| 130 | + return isolation_level.upper() |
| 131 | + |
| 132 | + return "READ COMMITTED" |
| 133 | + |
| 134 | + def set_isolation_level(self, connection, level_str): |
| 135 | + if level_str == "AUTOCOMMIT": |
| 136 | + connection.autocommit = True |
| 137 | + else: |
| 138 | + connection.autocommit = False |
| 139 | + connection.isolation_level = level_str |
| 140 | + |
| 141 | + def do_execute(self, cursor, query, params, context=None): |
| 142 | + if query.endswith(";"): |
| 143 | + query = query[:-1] |
| 144 | + self._debug(query, params) |
| 145 | + cursor.execute(query, params) |
| 146 | + |
| 147 | + def do_executemany(self, cursor, query, params, context=None): |
| 148 | + if query.endswith(";"): |
| 149 | + query = query[:-1] |
| 150 | + self._debug(query, params, True) |
| 151 | + cursor.executemany(query, params) |
27 | 152 |
|
28 | 153 |
|
29 | 154 | dialect = IRISDialect_emb |
0 commit comments