|
| 1 | +import sqlalchemy.types as sqltypes |
| 2 | +from sqlalchemy import util |
| 3 | +from sqlalchemy.dialects.sqlite.base import DATE, DATETIME, SQLiteDialect |
| 4 | +from sqlalchemy.engine.url import URL |
| 5 | +from sqlalchemy.exc import ArgumentError |
| 6 | +from sqlalchemy.pool import NullPool |
| 7 | + |
| 8 | + |
| 9 | +class _SQLite_pysqliteTimeStamp(DATETIME): |
| 10 | + def bind_processor(self, dialect): |
| 11 | + if dialect.native_datetime: |
| 12 | + return None |
| 13 | + else: |
| 14 | + return DATETIME.bind_processor(self, dialect) |
| 15 | + |
| 16 | + def result_processor(self, dialect, coltype): |
| 17 | + if dialect.native_datetime: |
| 18 | + return None |
| 19 | + else: |
| 20 | + return DATETIME.result_processor(self, dialect, coltype) |
| 21 | + |
| 22 | + |
| 23 | +class _SQLite_pysqliteDate(DATE): |
| 24 | + def bind_processor(self, dialect): |
| 25 | + if dialect.native_datetime: |
| 26 | + return None |
| 27 | + else: |
| 28 | + return DATE.bind_processor(self, dialect) |
| 29 | + |
| 30 | + def result_processor(self, dialect, coltype): |
| 31 | + if dialect.native_datetime: |
| 32 | + return None |
| 33 | + else: |
| 34 | + return DATE.result_processor(self, dialect, coltype) |
| 35 | + |
| 36 | + |
| 37 | +class SQLiteCloudDialect(SQLiteDialect): |
| 38 | + default_paramstyle = "qmark" |
| 39 | + supports_statement_cache = False |
| 40 | + |
| 41 | + colspecs = util.update_copy( |
| 42 | + SQLiteDialect.colspecs, |
| 43 | + { |
| 44 | + sqltypes.Date: _SQLite_pysqliteDate, |
| 45 | + sqltypes.TIMESTAMP: _SQLite_pysqliteTimeStamp, |
| 46 | + }, |
| 47 | + ) |
| 48 | + |
| 49 | + driver = "sqlitecloud" |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def dbapi(cls): |
| 53 | + from sqlitecloud import dbapi2 |
| 54 | + |
| 55 | + return dbapi2 |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def get_pool_class(cls, url): |
| 59 | + return NullPool |
| 60 | + |
| 61 | + def _get_server_version_info(self, connection): |
| 62 | + return self.dbapi.sqlite_version_info |
| 63 | + |
| 64 | + def set_isolation_level(self, connection, level): |
| 65 | + if level != "AUTOCOMMIT": |
| 66 | + raise ArgumentError( |
| 67 | + "SQLite Cloud supports only AUTOCOMMIT isolation level." |
| 68 | + ) |
| 69 | + |
| 70 | + if hasattr(connection, "dbapi_connection"): |
| 71 | + dbapi_connection = connection.dbapi_connection |
| 72 | + else: |
| 73 | + dbapi_connection = connection |
| 74 | + |
| 75 | + if level == "AUTOCOMMIT": |
| 76 | + dbapi_connection.isolation_level = None |
| 77 | + else: |
| 78 | + dbapi_connection.isolation_level = "" |
| 79 | + return super(SQLiteCloudDialect, self).set_isolation_level( |
| 80 | + connection, level |
| 81 | + ) |
| 82 | + |
| 83 | + def on_connect(self): |
| 84 | + connect = super(SQLiteCloudDialect, self).on_connect() |
| 85 | + |
| 86 | + fns = [] |
| 87 | + |
| 88 | + if self.isolation_level is not None: |
| 89 | + |
| 90 | + def iso_level(conn): |
| 91 | + self.set_isolation_level(conn, self.isolation_level) |
| 92 | + |
| 93 | + fns.append(iso_level) |
| 94 | + |
| 95 | + def connect(conn): # noqa: F811 |
| 96 | + for fn in fns: |
| 97 | + fn(conn) |
| 98 | + |
| 99 | + return connect |
| 100 | + |
| 101 | + def create_connect_args(self, url: URL): |
| 102 | + if not url.host: |
| 103 | + raise ArgumentError( |
| 104 | + "SQLite Cloud URL is required.\n" |
| 105 | + "Register on https://sqlitecloud.io/ to get your free SQLite Cloud account.\n" |
| 106 | + "Valid SQLite Cloud URL are:\n" |
| 107 | + " sqlite+sqlitecloud:///myuser:mypass@myserver.sqlite.cloud/mydb.sqlite?non_linearizable=true\n" |
| 108 | + " sqlite+sqlitecloud:///myserver.sqlite.cloud/?apikey=mykey1234" |
| 109 | + ) |
| 110 | + |
| 111 | + # TODO: this should be the list of SQLite Cloud Config params |
| 112 | + pysqlite_args = [ |
| 113 | + # ("timeout", float), |
| 114 | + # ("isolation_level", str), |
| 115 | + ("detect_types", int), |
| 116 | + ] |
| 117 | + opts = url.query |
| 118 | + pysqlite_opts = {} |
| 119 | + for key, type_ in pysqlite_args: |
| 120 | + util.coerce_kw_type(opts, key, type_, dest=pysqlite_opts) |
| 121 | + |
| 122 | + # sqlitecloud//... |
| 123 | + url = url.set(drivername="sqlitecloud") |
| 124 | + |
| 125 | + return ([url.render_as_string(hide_password=False)], pysqlite_opts) |
| 126 | + |
| 127 | + def is_disconnect(self, e, connection, cursor): |
| 128 | + return isinstance( |
| 129 | + e, self.dbapi.ProgrammingError |
| 130 | + ) and "Cannot operate on a closed database." in str(e) |
| 131 | + |
| 132 | + |
| 133 | +dialect = SQLiteCloudDialect |
0 commit comments