diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-05-25 14:20:23 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-05-25 14:20:23 +0000 |
commit | bb79e2e871d0a4585164c1a6ed626d96d0231975 (patch) | |
tree | 6d457ba6c36c408b45db24ec3c29e147fe7504ff /lib/sqlalchemy/databases/mysql.py | |
parent | 4fc3a0648699c2b441251ba4e1d37a9107bd1986 (diff) | |
download | sqlalchemy-bb79e2e871d0a4585164c1a6ed626d96d0231975.tar.gz |
merged 0.2 branch into trunk; 0.1 now in sqlalchemy/branches/rel_0_1
Diffstat (limited to 'lib/sqlalchemy/databases/mysql.py')
-rw-r--r-- | lib/sqlalchemy/databases/mysql.py | 87 |
1 files changed, 40 insertions, 47 deletions
diff --git a/lib/sqlalchemy/databases/mysql.py b/lib/sqlalchemy/databases/mysql.py index 60435f220..0a480ec11 100644 --- a/lib/sqlalchemy/databases/mysql.py +++ b/lib/sqlalchemy/databases/mysql.py @@ -6,14 +6,11 @@ import sys, StringIO, string, types, re, datetime -import sqlalchemy.sql as sql -import sqlalchemy.engine as engine -import sqlalchemy.schema as schema -import sqlalchemy.ansisql as ansisql +from sqlalchemy import sql,engine,schema,ansisql +from sqlalchemy.engine import default import sqlalchemy.types as sqltypes -from sqlalchemy import * import sqlalchemy.databases.information_schema as ischema -from sqlalchemy.exceptions import * +import sqlalchemy.exceptions as exceptions try: import MySQLdb as mysql @@ -26,7 +23,7 @@ class MSNumeric(sqltypes.Numeric): class MSDouble(sqltypes.Numeric): def __init__(self, precision = None, length = None): if (precision is None and length is not None) or (precision is not None and length is None): - raise ArgumentError("You must specify both precision and length or omit both altogether.") + raise exceptions.ArgumentError("You must specify both precision and length or omit both altogether.") super(MSDouble, self).__init__(precision, length) def get_col_spec(self): if self.precision is not None and self.length is not None: @@ -56,7 +53,7 @@ class MSDate(sqltypes.Date): class MSTime(sqltypes.Time): def get_col_spec(self): return "TIME" - def convert_result_value(self, value, engine): + def convert_result_value(self, value, dialect): # convert from a timedelta value if value is not None: return datetime.time(value.seconds/60/60, value.seconds/60%60, value.seconds - (value.seconds/60*60)) @@ -129,72 +126,66 @@ def descriptor(): return {'name':'mysql', 'description':'MySQL', 'arguments':[ - ('user',"Database Username",None), - ('passwd',"Database Password",None), - ('db',"Database Name",None), + ('username',"Database Username",None), + ('password',"Database Password",None), + ('database',"Database Name",None), ('host',"Hostname", None), ]} -class MySQLEngine(ansisql.ANSISQLEngine): - def __init__(self, opts, module = None, **params): + +class MySQLExecutionContext(default.DefaultExecutionContext): + def post_exec(self, engine, proxy, compiled, parameters, **kwargs): + if getattr(compiled, "isinsert", False): + self._last_inserted_ids = [proxy().lastrowid] + +class MySQLDialect(ansisql.ANSIDialect): + def __init__(self, module = None, **kwargs): if module is None: self.module = mysql - self.opts = self._translate_connect_args(('host', 'db', 'user', 'passwd'), opts) - ansisql.ANSISQLEngine.__init__(self, **params) + ansisql.ANSIDialect.__init__(self, **kwargs) + + def create_connect_args(self, url): + opts = url.translate_connect_args(['host', 'db', 'user', 'passwd', 'port']) + return [[], opts] - def connect_args(self): - return [[], self.opts] + def create_execution_context(self): + return MySQLExecutionContext(self) def type_descriptor(self, typeobj): return sqltypes.adapt_type(typeobj, colspecs) - def last_inserted_ids(self): - return self.context.last_inserted_ids def supports_sane_rowcount(self): return False def compiler(self, statement, bindparams, **kwargs): - return MySQLCompiler(statement, bindparams, engine=self, **kwargs) + return MySQLCompiler(self, statement, bindparams, **kwargs) - def schemagenerator(self, **params): - return MySQLSchemaGenerator(self, **params) + def schemagenerator(self, *args, **kwargs): + return MySQLSchemaGenerator(*args, **kwargs) - def schemadropper(self, **params): - return MySQLSchemaDropper(self, **params) + def schemadropper(self, *args, **kwargs): + return MySQLSchemaDropper(*args, **kwargs) def get_default_schema_name(self): if not hasattr(self, '_default_schema_name'): self._default_schema_name = text("select database()", self).scalar() return self._default_schema_name - - def last_inserted_ids(self): - return self.context.last_inserted_ids - - def post_exec(self, proxy, compiled, parameters, **kwargs): - if getattr(compiled, "isinsert", False): - self.context.last_inserted_ids = [proxy().lastrowid] - # executemany just runs normally, since we arent using rowcount at all with mysql -# def _executemany(self, c, statement, parameters): - # """we need accurate rowcounts for updates, inserts and deletes. mysql is *also* is not nice enough - # to produce this correctly for an executemany, so we do our own executemany here.""" - # rowcount = 0 - # for param in parameters: - # c.execute(statement, param) - # rowcount += c.rowcount - # self.context.rowcount = rowcount - def dbapi(self): return self.module - def reflecttable(self, table): + def has_table(self, connection, table_name): + cursor = connection.execute("show table status like '" + table_name + "'") + return bool( not not cursor.rowcount ) + + def reflecttable(self, connection, table): # to use information_schema: #ischema.reflecttable(self, table, ischema_names, use_mysql=True) - tabletype, foreignkeyD = self.moretableinfo(table=table) + tabletype, foreignkeyD = self.moretableinfo(connection, table=table) table.kwargs['mysql_engine'] = tabletype - c = self.execute("describe " + table.name, {}) + c = connection.execute("describe " + table.name, {}) while True: row = c.fetchone() if row is None: @@ -224,7 +215,7 @@ class MySQLEngine(ansisql.ANSISQLEngine): default=default ))) - def moretableinfo(self, table): + def moretableinfo(self, connection, table): """Return (tabletype, {colname:foreignkey,...}) execute(SHOW CREATE TABLE child) => CREATE TABLE `child` ( @@ -233,7 +224,7 @@ class MySQLEngine(ansisql.ANSISQLEngine): KEY `par_ind` (`parent_id`), CONSTRAINT `child_ibfk_1` FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`) ON DELETE CASCADE\n) TYPE=InnoDB """ - c = self.execute("SHOW CREATE TABLE " + table.name, {}) + c = connection.execute("SHOW CREATE TABLE " + table.name, {}) desc = c.fetchone()[1].strip() tabletype = '' lastparen = re.search(r'\)[^\)]*\Z', desc) @@ -277,7 +268,7 @@ class MySQLSchemaGenerator(ansisql.ANSISchemaGenerator): if column.primary_key: if not override_pk: colspec += " PRIMARY KEY" - if not column.foreign_key and first_pk and isinstance(column.type, types.Integer): + if not column.foreign_key and first_pk and isinstance(column.type, sqltypes.Integer): colspec += " AUTO_INCREMENT" if column.foreign_key: colspec += ", FOREIGN KEY (%s) REFERENCES %s(%s)" % (column.name, column.foreign_key.column.table.name, column.foreign_key.column.name) @@ -294,3 +285,5 @@ class MySQLSchemaDropper(ansisql.ANSISchemaDropper): def visit_index(self, index): self.append("\nDROP INDEX " + index.name + " ON " + index.table.name) self.execute() + +dialect = MySQLDialect
\ No newline at end of file |