diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-04-15 22:04:53 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-04-15 22:04:53 +0000 |
commit | 07cd648f3f34e0dac42304e16e2ecb7d992a2859 (patch) | |
tree | 6c186445ebcc8ab4334c2eaac1ef9ce00a31e8ff /lib/sqlalchemy/databases | |
parent | ad39d4fc1cea4de1ab14341bdfc0e6d833edacfe (diff) | |
download | sqlalchemy-07cd648f3f34e0dac42304e16e2ecb7d992a2859.tar.gz |
- got unicode schemas to work with postgres
- unicode schema with mysql slightly improved, still cant do has_table
- got reflection of unicode schemas working with sqlite, pg, mysql
Diffstat (limited to 'lib/sqlalchemy/databases')
-rw-r--r-- | lib/sqlalchemy/databases/mysql.py | 22 | ||||
-rw-r--r-- | lib/sqlalchemy/databases/postgres.py | 10 | ||||
-rw-r--r-- | lib/sqlalchemy/databases/sqlite.py | 3 |
3 files changed, 22 insertions, 13 deletions
diff --git a/lib/sqlalchemy/databases/mysql.py b/lib/sqlalchemy/databases/mysql.py index 03297cd68..d3a42ccdc 100644 --- a/lib/sqlalchemy/databases/mysql.py +++ b/lib/sqlalchemy/databases/mysql.py @@ -303,7 +303,6 @@ class MySQLDialect(ansisql.ANSIDialect): except: pass opts['client_flag'] = client_flag - return [[], opts] def create_execution_context(self, *args, **kwargs): @@ -331,7 +330,10 @@ class MySQLDialect(ansisql.ANSIDialect): rowcount = cursor.executemany(statement, parameters) if context is not None: context._rowcount = rowcount - + + def supports_unicode_statements(self): + return True + def do_execute(self, cursor, statement, parameters, **kwargs): cursor.execute(statement, parameters) @@ -351,8 +353,11 @@ class MySQLDialect(ansisql.ANSIDialect): return self._default_schema_name def has_table(self, connection, table_name, schema=None): + # TODO: this does not work for table names that contain multibyte characters. + # i have tried dozens of approaches here with no luck. statements like + # DESCRIBE and SHOW CREATE TABLE work better, but they raise an error when + # the table does not exist. cursor = connection.execute("show table status like %s", [table_name]) - print "CURSOR", cursor, "ROWCOUNT", cursor.rowcount, "REAL RC", cursor.cursor.rowcount return bool( not not cursor.rowcount ) def reflecttable(self, connection, table): @@ -362,6 +367,8 @@ class MySQLDialect(ansisql.ANSIDialect): cs = cs.tostring() case_sensitive = int(cs) == 0 + decode_from = connection.execute("show variables like 'character_Set_results'").fetchone()[1] + if not case_sensitive: table.name = table.name.lower() table.metadata.tables[table.name]= table @@ -379,7 +386,9 @@ class MySQLDialect(ansisql.ANSIDialect): found_table = True # these can come back as unicode if use_unicode=1 in the mysql connection - (name, type, nullable, primary_key, default) = (str(row[0]), str(row[1]), row[2] == 'YES', row[3] == 'PRI', row[4]) + (name, type, nullable, primary_key, default) = (row[0], str(row[1]), row[2] == 'YES', row[3] == 'PRI', row[4]) + if not isinstance(name, unicode): + name = name.decode(decode_from) match = re.match(r'(\w+)(\(.*?\))?\s*(\w+)?\s*(\w+)?', type) col_type = match.group(1) @@ -425,10 +434,7 @@ class MySQLDialect(ansisql.ANSIDialect): c = connection.execute("SHOW CREATE TABLE " + table.fullname, {}) desc_fetched = c.fetchone()[1] - # this can come back as unicode if use_unicode=1 in the mysql connection - if type(desc_fetched) is unicode: - desc_fetched = str(desc_fetched) - elif type(desc_fetched) is not str: + if not isinstance(desc_fetched, basestring): # may get array.array object here, depending on version (such as mysql 4.1.14 vs. 4.1.11) desc_fetched = desc_fetched.tostring() desc = desc_fetched.strip() diff --git a/lib/sqlalchemy/databases/postgres.py b/lib/sqlalchemy/databases/postgres.py index 6facde936..a93ba200c 100644 --- a/lib/sqlalchemy/databases/postgres.py +++ b/lib/sqlalchemy/databases/postgres.py @@ -329,9 +329,9 @@ class PGDialect(ansisql.ANSIDialect): def has_table(self, connection, table_name, schema=None): # seems like case gets folded in pg_class... if schema is None: - cursor = connection.execute("""select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=current_schema() and lower(relname)=%(name)s""", {'name':table_name.lower()}); + cursor = connection.execute("""select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=current_schema() and lower(relname)=%(name)s""", {'name':table_name.lower().encode(self.encoding)}); else: - cursor = connection.execute("""select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=%(schema)s and lower(relname)=%(name)s""", {'name':table_name.lower(), 'schema':schema}); + cursor = connection.execute("""select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=%(schema)s and lower(relname)=%(name)s""", {'name':table_name.lower().encode(self.encoding), 'schema':schema}); return bool( not not cursor.rowcount ) def has_sequence(self, connection, sequence_name): @@ -385,7 +385,7 @@ class PGDialect(ansisql.ANSIDialect): ORDER BY a.attnum """ % schema_where_clause - s = sql.text(SQL_COLS) + s = sql.text(SQL_COLS, bindparams=[sql.bindparam('table_name', type=sqltypes.Unicode), sql.bindparam('schema', type=sqltypes.Unicode)], typemap={'attname':sqltypes.Unicode}) c = connection.execute(s, table_name=table.name, schema=table.schema) rows = c.fetchall() @@ -454,7 +454,7 @@ class PGDialect(ansisql.ANSIDialect): AND i.indisprimary = 't') ORDER BY attnum """ - t = sql.text(PK_SQL) + t = sql.text(PK_SQL, typemap={'attname':sqltypes.Unicode}) c = connection.execute(t, table=table_oid) for row in c.fetchall(): pk = row[0] @@ -468,7 +468,7 @@ class PGDialect(ansisql.ANSIDialect): ORDER BY 1 """ - t = sql.text(FK_SQL) + t = sql.text(FK_SQL, typemap={'conname':sqltypes.Unicode, 'condef':sqltypes.Unicode}) c = connection.execute(t, table=table_oid) for conname, condef in c.fetchall(): m = re.search('FOREIGN KEY \((.*?)\) REFERENCES (?:(.*?)\.)?(.*?)\((.*?)\)', condef).groups() diff --git a/lib/sqlalchemy/databases/sqlite.py b/lib/sqlalchemy/databases/sqlite.py index 5140d865e..2b7e28dfb 100644 --- a/lib/sqlalchemy/databases/sqlite.py +++ b/lib/sqlalchemy/databases/sqlite.py @@ -185,6 +185,9 @@ class SQLiteDialect(ansisql.ANSIDialect): def create_execution_context(self, **kwargs): return SQLiteExecutionContext(self, **kwargs) + def supports_unicode_statements(self): + return True + def last_inserted_ids(self): return self.context.last_inserted_ids |