Permalink
Browse files

Initial commit (not working with geodjango)

  • Loading branch information...
0 parents commit 16aa7ea5ab5e0b0436e96b26d3f405ed0337ae41 David Cramer committed Nov 30, 2009
Showing with 558 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +7 −0 README.rst
  3. +1 −0 mysql_oursql/__init__.py
  4. +293 −0 mysql_oursql/base.py
  5. +37 −0 mysql_oursql/client.py
  6. +65 −0 mysql_oursql/creation.py
  7. +98 −0 mysql_oursql/introspection.py
  8. +28 −0 mysql_oursql/validation.py
  9. +25 −0 setup.py
4 .gitignore
@@ -0,0 +1,4 @@
+/django_oursql.egg-info
+/build
+/dist
+*.pyc
7 README.rst
@@ -0,0 +1,7 @@
+A simple database backend for Django to utilize the oursql connector.
+
+Install with setuptools or pip::
+
+ pip install django-oursql
+
+For more information about oursql, check the docs: http://packages.python.org/oursql/
1 mysql_oursql/__init__.py
@@ -0,0 +1 @@
+__version__ = (0, 1)
293 mysql_oursql/base.py
@@ -0,0 +1,293 @@
+"""
+MySQL database backend for Django.
+
+Requires oursql: https://launchpad.net/oursql
+"""
+
+import re
+
+try:
+ import oursql as Database
+except ImportError, e:
+ from django.core.exceptions import ImproperlyConfigured
+ raise ImproperlyConfigured("Error loading oursql module: %s" % e)
+
+from oursql import _conversions as django_conversions
+
+from django.db.backends import *
+from django.db.backends.signals import connection_created
+from django.db.backends.mysql.client import DatabaseClient
+from django.db.backends.mysql.creation import DatabaseCreation
+from django.db.backends.mysql.introspection import DatabaseIntrospection
+from django.db.backends.mysql.validation import DatabaseValidation
+from django.utils.safestring import SafeString, SafeUnicode
+
+# Raise exceptions for database warnings if DEBUG is on
+from django.conf import settings
+if settings.DEBUG:
+ from warnings import filterwarnings
+ filterwarnings("error", category=Database.Warning)
+
+DatabaseError = Database.DatabaseError
+IntegrityError = Database.IntegrityError
+
+# TODO: is this still valid for oursql?
+# MySQLdb-1.2.1 returns TIME columns as timedelta -- they are more like
+# timedelta in terms of actual behavior as they are signed and include days --
+# and Django expects time, so we still need to override that. We also need to
+# add special handling for SafeUnicode and SafeString as MySQLdb's type
+# checking is too tight to catch those (see Django ticket #6052).
+# django_conversions = conversions.copy()
+# django_conversions.update({
+# FIELD_TYPE.TIME: util.typecast_time,
+# FIELD_TYPE.DECIMAL: util.typecast_decimal,
+# FIELD_TYPE.NEWDECIMAL: util.typecast_decimal,
+# })
+
+# This should match the numerical portion of the version numbers (we can treat
+# versions like 5.0.24 and 5.0.24a as the same). Based on the list of version
+# at http://dev.mysql.com/doc/refman/4.1/en/news.html and
+# http://dev.mysql.com/doc/refman/5.0/en/news.html .
+server_version_re = re.compile(r'(\d{1,2})\.(\d{1,2})\.(\d{1,2})')
+
+# TODO: is this still valid in oursql?
+# MySQLdb-1.2.1 and newer automatically makes use of SHOW WARNINGS on
+# MySQL-4.1 and newer, so the MysqlDebugWrapper is unnecessary. Since the
+# point is to raise Warnings as exceptions, this can be done with the Python
+# warning module, and this is setup when the connection is created, and the
+# standard util.CursorDebugWrapper can be used. Also, using sql_mode
+# TRADITIONAL will automatically cause most warnings to be treated as errors.
+
+class CursorWrapper(object):
+ """
+ A thin wrapper around oursql's normal cursor class so that we can catch
+ particular exception instances and reraise them with the right types.
+
+ Implemented as a wrapper, rather than a subclass, so that we aren't stuck
+ to the particular underlying representation returned by Connection.cursor().
+ """
+ codes_for_integrityerror = (1048,)
+
+ def __init__(self, cursor):
+ self.cursor = cursor
+
+ def execute(self, query, args=None):
+ try:
+ return self.cursor.execute(query, args)
+ except Database.OperationalError, e:
+ # Map some error codes to IntegrityError, since they seem to be
+ # misclassified and Django would prefer the more logical place.
+ if e[0] in self.codes_for_integrityerror:
+ raise Database.IntegrityError(tuple(e))
+ raise
+
+ def executemany(self, query, args):
+ try:
+ return self.cursor.executemany(query, args)
+ except Database.OperationalError, e:
+ # Map some error codes to IntegrityError, since they seem to be
+ # misclassified and Django would prefer the more logical place.
+ if e[0] in self.codes_for_integrityerror:
+ raise Database.IntegrityError(tuple(e))
+ raise
+
+ def __getattr__(self, attr):
+ if attr in self.__dict__:
+ return self.__dict__[attr]
+ else:
+ return getattr(self.cursor, attr)
+
+ def __iter__(self):
+ return iter(self.cursor)
+
+class DatabaseFeatures(BaseDatabaseFeatures):
+ empty_fetchmany_value = ()
+ update_can_self_select = False
+ allows_group_by_pk = True
+ related_fields_match_type = True
+
+class DatabaseOperations(BaseDatabaseOperations):
+ def date_extract_sql(self, lookup_type, field_name):
+ # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
+ if lookup_type == 'week_day':
+ # DAYOFWEEK() returns an integer, 1-7, Sunday=1.
+ # Note: WEEKDAY() returns 0-6, Monday=0.
+ return "DAYOFWEEK(%s)" % field_name
+ else:
+ return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
+
+ def date_trunc_sql(self, lookup_type, field_name):
+ fields = ['year', 'month', 'day', 'hour', 'minute', 'second']
+ format = ('%%Y-', '%%m', '-%%d', ' %%H:', '%%i', ':%%s') # Use double percents to escape.
+ format_def = ('0000-', '01', '-01', ' 00:', '00', ':00')
+ try:
+ i = fields.index(lookup_type) + 1
+ except ValueError:
+ sql = field_name
+ else:
+ format_str = ''.join([f for f in format[:i]] + [f for f in format_def[i:]])
+ sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str)
+ return sql
+
+ def drop_foreignkey_sql(self):
+ return "DROP FOREIGN KEY"
+
+ def force_no_ordering(self):
+ """
+ "ORDER BY NULL" prevents MySQL from implicitly ordering by grouped
+ columns. If no ordering would otherwise be applied, we don't want any
+ implicit sorting going on.
+ """
+ return ["NULL"]
+
+ def fulltext_search_sql(self, field_name):
+ return 'MATCH (%s) AGAINST (%%s IN BOOLEAN MODE)' % field_name
+
+ def no_limit_value(self):
+ # 2**64 - 1, as recommended by the MySQL documentation
+ return 18446744073709551615L
+
+ def quote_name(self, name):
+ if name.startswith("`") and name.endswith("`"):
+ return name # Quoting once is enough.
+ return "`%s`" % name
+
+ def random_function_sql(self):
+ return 'RAND()'
+
+ def sql_flush(self, style, tables, sequences):
+ # NB: The generated SQL below is specific to MySQL
+ # 'TRUNCATE x;', 'TRUNCATE y;', 'TRUNCATE z;'... style SQL statements
+ # to clear all tables of all data
+ if tables:
+ sql = ['SET FOREIGN_KEY_CHECKS = 0;']
+ for table in tables:
+ sql.append('%s %s;' % (style.SQL_KEYWORD('TRUNCATE'), style.SQL_FIELD(self.quote_name(table))))
+ sql.append('SET FOREIGN_KEY_CHECKS = 1;')
+
+ # 'ALTER TABLE table AUTO_INCREMENT = 1;'... style SQL statements
+ # to reset sequence indices
+ sql.extend(["%s %s %s %s %s;" % \
+ (style.SQL_KEYWORD('ALTER'),
+ style.SQL_KEYWORD('TABLE'),
+ style.SQL_TABLE(self.quote_name(sequence['table'])),
+ style.SQL_KEYWORD('AUTO_INCREMENT'),
+ style.SQL_FIELD('= 1'),
+ ) for sequence in sequences])
+ return sql
+ else:
+ return []
+
+ def value_to_db_datetime(self, value):
+ if value is None:
+ return None
+
+ # MySQL doesn't support tz-aware datetimes
+ if value.tzinfo is not None:
+ raise ValueError("MySQL backend does not support timezone-aware datetimes.")
+
+ # MySQL doesn't support microseconds
+ return unicode(value.replace(microsecond=0))
+
+ def value_to_db_time(self, value):
+ if value is None:
+ return None
+
+ # MySQL doesn't support tz-aware datetimes
+ if value.tzinfo is not None:
+ raise ValueError("MySQL backend does not support timezone-aware datetimes.")
+
+ # MySQL doesn't support microseconds
+ return unicode(value.replace(microsecond=0))
+
+ def year_lookup_bounds(self, value):
+ # Again, no microseconds
+ first = '%s-01-01 00:00:00'
+ second = '%s-12-31 23:59:59.99'
+ return [first % value, second % value]
+
+class DatabaseWrapper(BaseDatabaseWrapper):
+ operators = {
+ 'exact': '= %s',
+ 'iexact': 'LIKE %s',
+ 'contains': 'LIKE BINARY %s',
+ 'icontains': 'LIKE %s',
+ 'regex': 'REGEXP BINARY %s',
+ 'iregex': 'REGEXP %s',
+ 'gt': '> %s',
+ 'gte': '>= %s',
+ 'lt': '< %s',
+ 'lte': '<= %s',
+ 'startswith': 'LIKE BINARY %s',
+ 'endswith': 'LIKE BINARY %s',
+ 'istartswith': 'LIKE %s',
+ 'iendswith': 'LIKE %s',
+ }
+
+ def __init__(self, *args, **kwargs):
+ super(DatabaseWrapper, self).__init__(*args, **kwargs)
+
+ self.server_version = None
+ self.features = DatabaseFeatures()
+ self.ops = DatabaseOperations()
+ self.client = DatabaseClient(self)
+ self.creation = DatabaseCreation(self)
+ self.introspection = DatabaseIntrospection(self)
+ self.validation = DatabaseValidation()
+
+ def _valid_connection(self):
+ if self.connection is not None:
+ try:
+ self.connection.ping()
+ return True
+ except DatabaseError:
+ self.connection.close()
+ self.connection = None
+ return False
+
+ def _cursor(self):
+ if not self._valid_connection():
+ kwargs = {
+ 'conv': django_conversions,
+ 'charset': 'utf8',
+ 'use_unicode': True,
+ }
+ settings_dict = self.settings_dict
+ if settings_dict['DATABASE_USER']:
+ kwargs['user'] = settings_dict['DATABASE_USER']
+ if settings_dict['DATABASE_NAME']:
+ kwargs['db'] = settings_dict['DATABASE_NAME']
+ if settings_dict['DATABASE_PASSWORD']:
+ kwargs['passwd'] = settings_dict['DATABASE_PASSWORD']
+ if settings_dict['DATABASE_HOST'].startswith('/'):
+ kwargs['unix_socket'] = settings_dict['DATABASE_HOST']
+ elif settings_dict['DATABASE_HOST']:
+ kwargs['host'] = settings_dict['DATABASE_HOST']
+ if settings_dict['DATABASE_PORT']:
+ kwargs['port'] = int(settings_dict['DATABASE_PORT'])
+ # We need the number of potentially affected rows after an
+ # "UPDATE", not the number of changed rows.
+ kwargs['found_rows'] = True
+ kwargs.update(settings_dict['DATABASE_OPTIONS'])
+ self.connection = Database.connect(**kwargs)
+ self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
+ self.connection.encoders[SafeString] = self.connection.encoders[str]
+ connection_created.send(sender=self.__class__)
+ cursor = CursorWrapper(self.connection.cursor())
+ return cursor
+
+ def _rollback(self):
+ try:
+ BaseDatabaseWrapper._rollback(self)
+ except Database.NotSupportedError:
+ pass
+
+ def get_server_version(self):
+ if not self.server_version:
+ if not self._valid_connection():
+ self.cursor()
+ m = server_version_re.match(self.connection.get_server_info())
+ if not m:
+ raise Exception('Unable to determine MySQL version from version string %r' % self.connection.get_server_info())
+ self.server_version = tuple([int(x) for x in m.groups()])
+ return self.server_version
37 mysql_oursql/client.py
@@ -0,0 +1,37 @@
+import os
+import sys
+
+from django.db.backends import BaseDatabaseClient
+
+class DatabaseClient(BaseDatabaseClient):
+ executable_name = 'mysql'
+
+ def runshell(self):
+ settings_dict = self.connection.settings_dict
+ args = [self.executable_name]
+ db = settings_dict['DATABASE_OPTIONS'].get('db', settings_dict['DATABASE_NAME'])
+ user = settings_dict['DATABASE_OPTIONS'].get('user', settings_dict['DATABASE_USER'])
+ passwd = settings_dict['DATABASE_OPTIONS'].get('passwd', settings_dict['DATABASE_PASSWORD'])
+ host = settings_dict['DATABASE_OPTIONS'].get('host', settings_dict['DATABASE_HOST'])
+ port = settings_dict['DATABASE_OPTIONS'].get('port', settings_dict['DATABASE_PORT'])
+ defaults_file = settings_dict['DATABASE_OPTIONS'].get('read_default_file')
+ # Seems to be no good way to set sql_mode with CLI.
+
+ if defaults_file:
+ args += ["--defaults-file=%s" % defaults_file]
+ if user:
+ args += ["--user=%s" % user]
+ if passwd:
+ args += ["--password=%s" % passwd]
+ if host:
+ args += ["--host=%s" % host]
+ if port:
+ args += ["--port=%s" % port]
+ if db:
+ args += [db]
+
+ if os.name == 'nt':
+ sys.exit(os.system(" ".join(args)))
+ else:
+ os.execvp(self.executable_name, args)
+
65 mysql_oursql/creation.py
@@ -0,0 +1,65 @@
+from django.conf import settings
+from django.db.backends.creation import BaseDatabaseCreation
+
+class DatabaseCreation(BaseDatabaseCreation):
+ # This dictionary maps Field objects to their associated MySQL column
+ # types, as strings. Column-type strings can contain format strings; they'll
+ # be interpolated against the values of Field.__dict__ before being output.
+ # If a column type is set to None, it won't be included in the output.
+ data_types = {
+ 'AutoField': 'integer AUTO_INCREMENT',
+ 'BooleanField': 'bool',
+ 'CharField': 'varchar(%(max_length)s)',
+ 'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
+ 'DateField': 'date',
+ 'DateTimeField': 'datetime',
+ 'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
+ 'FileField': 'varchar(%(max_length)s)',
+ 'FilePathField': 'varchar(%(max_length)s)',
+ 'FloatField': 'double precision',
+ 'IntegerField': 'integer',
+ 'IPAddressField': 'char(15)',
+ 'NullBooleanField': 'bool',
+ 'OneToOneField': 'integer',
+ 'PositiveIntegerField': 'integer UNSIGNED',
+ 'PositiveSmallIntegerField': 'smallint UNSIGNED',
+ 'SlugField': 'varchar(%(max_length)s)',
+ 'SmallIntegerField': 'smallint',
+ 'TextField': 'longtext',
+ 'TimeField': 'time',
+ }
+
+ def sql_table_creation_suffix(self):
+ suffix = []
+ if settings.TEST_DATABASE_CHARSET:
+ suffix.append('CHARACTER SET %s' % settings.TEST_DATABASE_CHARSET)
+ if settings.TEST_DATABASE_COLLATION:
+ suffix.append('COLLATE %s' % settings.TEST_DATABASE_COLLATION)
+ return ' '.join(suffix)
+
+ def sql_for_inline_foreign_key_references(self, field, known_models, style):
+ "All inline references are pending under MySQL"
+ return [], True
+
+ def sql_for_inline_many_to_many_references(self, model, field, style):
+ from django.db import models
+ opts = model._meta
+ qn = self.connection.ops.quote_name
+
+ table_output = [
+ ' %s %s %s,' %
+ (style.SQL_FIELD(qn(field.m2m_column_name())),
+ style.SQL_COLTYPE(models.ForeignKey(model).db_type()),
+ style.SQL_KEYWORD('NOT NULL')),
+ ' %s %s %s,' %
+ (style.SQL_FIELD(qn(field.m2m_reverse_name())),
+ style.SQL_COLTYPE(models.ForeignKey(field.rel.to).db_type()),
+ style.SQL_KEYWORD('NOT NULL'))
+ ]
+ deferred = [
+ (field.m2m_db_table(), field.m2m_column_name(), opts.db_table,
+ opts.pk.column),
+ (field.m2m_db_table(), field.m2m_reverse_name(),
+ field.rel.to._meta.db_table, field.rel.to._meta.pk.column)
+ ]
+ return table_output, deferred
98 mysql_oursql/introspection.py
@@ -0,0 +1,98 @@
+from django.db.backends import BaseDatabaseIntrospection
+from oursql import ProgrammingError, OperationalError
+from oursql.constants import FIELD_TYPE
+import re
+
+foreign_key_re = re.compile(r"\sCONSTRAINT `[^`]*` FOREIGN KEY \(`([^`]*)`\) REFERENCES `([^`]*)` \(`([^`]*)`\)")
+
+class DatabaseIntrospection(BaseDatabaseIntrospection):
+ data_types_reverse = {
+ FIELD_TYPE.BLOB: 'TextField',
+ FIELD_TYPE.CHAR: 'CharField',
+ FIELD_TYPE.DECIMAL: 'DecimalField',
+ FIELD_TYPE.NEWDECIMAL: 'DecimalField',
+ FIELD_TYPE.DATE: 'DateField',
+ FIELD_TYPE.DATETIME: 'DateTimeField',
+ FIELD_TYPE.DOUBLE: 'FloatField',
+ FIELD_TYPE.FLOAT: 'FloatField',
+ FIELD_TYPE.INT24: 'IntegerField',
+ FIELD_TYPE.LONG: 'IntegerField',
+ FIELD_TYPE.LONGLONG: 'IntegerField',
+ FIELD_TYPE.SHORT: 'IntegerField',
+ FIELD_TYPE.STRING: 'CharField',
+ FIELD_TYPE.TIMESTAMP: 'DateTimeField',
+ FIELD_TYPE.TINY: 'IntegerField',
+ FIELD_TYPE.TINY_BLOB: 'TextField',
+ FIELD_TYPE.MEDIUM_BLOB: 'TextField',
+ FIELD_TYPE.LONG_BLOB: 'TextField',
+ FIELD_TYPE.VAR_STRING: 'CharField',
+ }
+
+ def get_table_list(self, cursor):
+ "Returns a list of table names in the current database."
+ cursor.execute("SHOW TABLES")
+ return [row[0] for row in cursor.fetchall()]
+
+ def get_table_description(self, cursor, table_name):
+ "Returns a description of the table, with the DB-API cursor.description interface."
+ cursor.execute("SELECT * FROM %s LIMIT 1" % self.connection.ops.quote_name(table_name))
+ return cursor.description
+
+ def _name_to_index(self, cursor, table_name):
+ """
+ Returns a dictionary of {field_name: field_index} for the given table.
+ Indexes are 0-based.
+ """
+ return dict([(d[0], i) for i, d in enumerate(self.get_table_description(cursor, table_name))])
+
+ def get_relations(self, cursor, table_name):
+ """
+ Returns a dictionary of {field_index: (field_index_other_table, other_table)}
+ representing all relationships to the given table. Indexes are 0-based.
+ """
+ my_field_dict = self._name_to_index(cursor, table_name)
+ constraints = []
+ relations = {}
+ try:
+ # This should work for MySQL 5.0.
+ cursor.execute("""
+ SELECT column_name, referenced_table_name, referenced_column_name
+ FROM information_schema.key_column_usage
+ WHERE table_name = %s
+ AND table_schema = DATABASE()
+ AND referenced_table_name IS NOT NULL
+ AND referenced_column_name IS NOT NULL""", [table_name])
+ constraints.extend(cursor.fetchall())
+ except (ProgrammingError, OperationalError):
+ # Fall back to "SHOW CREATE TABLE", for previous MySQL versions.
+ # Go through all constraints and save the equal matches.
+ cursor.execute("SHOW CREATE TABLE %s" % self.connection.ops.quote_name(table_name))
+ for row in cursor.fetchall():
+ pos = 0
+ while True:
+ match = foreign_key_re.search(row[1], pos)
+ if match == None:
+ break
+ pos = match.end()
+ constraints.append(match.groups())
+
+ for my_fieldname, other_table, other_field in constraints:
+ other_field_index = self._name_to_index(cursor, other_table)[other_field]
+ my_field_index = my_field_dict[my_fieldname]
+ relations[my_field_index] = (other_field_index, other_table)
+
+ return relations
+
+ def get_indexes(self, cursor, table_name):
+ """
+ Returns a dictionary of fieldname -> infodict for the given table,
+ where each infodict is in the format:
+ {'primary_key': boolean representing whether it's the primary key,
+ 'unique': boolean representing whether it's a unique index}
+ """
+ cursor.execute("SHOW INDEX FROM %s" % self.connection.ops.quote_name(table_name))
+ indexes = {}
+ for row in cursor.fetchall():
+ indexes[row[4]] = {'primary_key': (row[2] == 'PRIMARY'), 'unique': not bool(row[1])}
+ return indexes
+
28 mysql_oursql/validation.py
@@ -0,0 +1,28 @@
+from django.db.backends import BaseDatabaseValidation
+
+class DatabaseValidation(BaseDatabaseValidation):
+ def validate_field(self, errors, opts, f):
+ """
+ There are some field length restrictions for MySQL:
+
+ - Prior to version 5.0.3, character fields could not exceed 255
+ characters in length.
+ - No character (varchar) fields can have a length exceeding 255
+ characters if they have a unique index on them.
+ """
+ from django.db import models
+ from django.db import connection
+ db_version = connection.get_server_version()
+ varchar_fields = (models.CharField, models.CommaSeparatedIntegerField,
+ models.SlugField)
+ if isinstance(f, varchar_fields) and f.max_length > 255:
+ if db_version < (5, 0, 3):
+ msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when you are using a version of MySQL prior to 5.0.3 (you are using %(version)s).'
+ elif f.unique == True:
+ msg = '"%(name)s": %(cls)s cannot have a "max_length" greater than 255 when using "unique=True".'
+ else:
+ msg = None
+
+ if msg:
+ errors.add(opts, msg % {'name': f.name, 'cls': f.__class__.__name__, 'version': '.'.join([str(n) for n in db_version[:3]])})
+
25 setup.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+
+from setuptools import setup, find_packages
+
+setup(
+ name='django-oursql',
+ version='.'.join(map(str, __import__('mysql_oursql').__version__)),
+ author='David Cramer',
+ author_email='dcramer@gmail.com',
+ url='http://github.com/dcramer/django-oursql',
+ install_requires=[
+ 'Django>=1.0',
+ 'oursql',
+ ],
+ description = 'Django database backend for MySQL via oursql.',
+ packages=find_packages(),
+ include_package_data=True,
+ classifiers=[
+ 'Framework :: Django',
+ 'Intended Audience :: Developers',
+ 'Intended Audience :: System Administrators',
+ 'Operating System :: OS Independent',
+ 'Topic :: Software Development'
+ ],
+)

0 comments on commit 16aa7ea

Please sign in to comment.