def set_up_database(self, dbpath): self.dbpath = dbpath settings.DATABASES['mt'] = { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': abspath(dbpath), } # Make sure we've used the SQLite connection. from django.db import connections connections['mt'].cursor().execute('SELECT 1') # Enable our own converter for "text" columns, in case there are some really old posts still in Latin-1 (like I have). from django.db.backends.sqlite3.base import Database Database.register_converter('text', self.convert_text)
def run_importers(importer_classes, sqlite_file, include_deps=False): """ importer_classes: (list) References to the importer classes to run. sqlite_file: (str) Path to the SQLite file to import from. """ # Create the SQLite connection object. conn = sqlite3.connect(sqlite_file) # Ignore characters that can't be decoded from UTF-8. conn.text_factory = lambda x: unicode(x, "utf-8", "ignore") conn.row_factory = sqlite3.Row if include_deps: add_dependencies(importer_classes) ordered_importers = order_importers(importer_classes) # Timestamp of when the imports started. time_started = datetime.datetime.now() # Carry out the imports in order. for importer_class in ordered_importers: importer = importer_class() importer.prep_and_run_importer(conn) # Print the total time, for the ricers. time_elapsed = datetime.datetime.now() - time_started hours = time_elapsed.seconds / 3600 minutes = (time_elapsed.seconds % 3600) / 60 seconds = (time_elapsed.seconds % 3600) % 60 print "Import completed in %0.2d:%0.2d:%0.2d" % ( hours, minutes, seconds )
def run_importers(importer_classes, include_deps=False): """ importer_classes: (list) References to the importer classes to run. """ # Create the SQLite connection object. conn = sqlite3.connect(settings.EVE_CCP_DUMP_SQLITE_DB) conn.row_factory = sqlite3.Row if include_deps: add_dependencies(importer_classes) ordered_importers = order_importers(importer_classes) # Timestamp of when the imports started. time_started = datetime.datetime.now() # Carry out the imports in order. for importer_class in ordered_importers: importer = importer_class() importer.prep_and_run_importer(conn) # Print the total time, for the ricers. time_elapsed = datetime.datetime.now() - time_started hours = time_elapsed.seconds / 3600 minutes = (time_elapsed.seconds % 3600) / 60 seconds = (time_elapsed.seconds % 3600) % 60 print "Import completed in %0.2d:%0.2d:%0.2d" % ( hours, minutes, seconds )
def _cursor(self): if self.connection is None: ## The following is the same as in django.db.backends.sqlite3.base ## settings_dict = self.settings_dict if not settings_dict['NAME']: raise ImproperlyConfigured( "Please fill out the database NAME in the settings module before using the database." ) kwargs = { 'database': settings_dict['NAME'], 'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES, } kwargs.update(settings_dict['OPTIONS']) self.connection = Database.connect(**kwargs) # Register extract, date_trunc, and regexp functions. self.connection.create_function("django_extract", 2, _sqlite_extract) self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) self.connection.create_function("regexp", 2, _sqlite_regexp) self.connection.create_function("django_format_dtdelta", 5, _sqlite_format_dtdelta) connection_created.send(sender=self.__class__, connection=self) ## From here on, customized for GeoDjango ## # Enabling extension loading on the SQLite connection. try: self.connection.enable_load_extension(True) except AttributeError: raise ImproperlyConfigured( 'The pysqlite library does not support C extension loading. ' 'Both SQLite and pysqlite must be configured to allow ' 'the loading of extensions to use SpatiaLite.') # Loading the SpatiaLite library extension on the connection, and returning # the created cursor. cur = self.connection.cursor(factory=SQLiteCursorWrapper) try: cur.execute("SELECT load_extension(%s)", (self.spatialite_lib, )) except Exception, msg: raise ImproperlyConfigured( 'Unable to load the SpatiaLite library extension ' '"%s" because: %s' % (self.spatialite_lib, msg)) return cur
def _cursor(self): if self.connection is None: ## The following is the same as in django.db.backends.sqlite3.base ## settings_dict = self.settings_dict if not settings_dict['NAME']: raise ImproperlyConfigured("Please fill out the database NAME in the settings module before using the database.") kwargs = { 'database': settings_dict['NAME'], 'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES, } kwargs.update(settings_dict['OPTIONS']) self.connection = Database.connect(**kwargs) # Register extract, date_trunc, and regexp functions. self.connection.create_function("django_extract", 2, _sqlite_extract) self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) self.connection.create_function("regexp", 2, _sqlite_regexp) self.connection.create_function("django_format_dtdelta", 5, _sqlite_format_dtdelta) connection_created.send(sender=self.__class__, connection=self) ## From here on, customized for GeoDjango ## # Enabling extension loading on the SQLite connection. try: self.connection.enable_load_extension(True) except AttributeError: raise ImproperlyConfigured('The pysqlite library does not support C extension loading. ' 'Both SQLite and pysqlite must be configured to allow ' 'the loading of extensions to use SpatiaLite.' ) # Loading the SpatiaLite library extension on the connection, and returning # the created cursor. cur = self.connection.cursor(factory=SQLiteCursorWrapper) try: cur.execute("SELECT load_extension(%s)", (self.spatialite_lib,)) except Exception, msg: raise ImproperlyConfigured('Unable to load the SpatiaLite library extension ' '"%s" because: %s' % (self.spatialite_lib, msg)) return cur
for k in self._keys: yield (k, getattr(self, k).is_set) def get_label(self, flag): if isinstance(flag, string_types): flag = self._keys.index(flag) if isinstance(flag, Bit): flag = flag.number return self._labels[flag] from django.core.exceptions import ImproperlyConfigured # We need to register adapters in order to prevent # "ProgrammingError: can't adapt type" try: from django.db.backends.sqlite3.base import Database Database.register_adapter(Bit, lambda x: int(x)) Database.register_adapter(BitHandler, lambda x: int(x)) except ImproperlyConfigured: pass try: from django.db.backends.postgresql.base import Database Database.extensions.register_adapter( Bit, lambda x: Database.extensions.AsIs(int(x))) Database.extensions.register_adapter( BitHandler, lambda x: Database.extensions.AsIs(int(x))) except ImproperlyConfigured: pass
def wrapper(*a, **kw): converter = Database.converters.pop('DATETIME') Database.register_converter("datetime", null_converter) res = func(*a, **kw) Database.register_converter("DATETIME", converter) return res
def iteritems(self): for k in self._keys: yield (k, getattr(self, k).is_set) def get_label(self, flag): if isinstance(flag, string_types): flag = self._keys.index(flag) if isinstance(flag, Bit): flag = flag.number return self._labels[flag] if django.VERSION[:2] >= (1, 8): from django.core.exceptions import ImproperlyConfigured # We need to register adapters in Django 1.8 in order to prevent # "ProgrammingError: can't adapt type" try: from django.db.backends.sqlite3.base import Database Database.register_adapter(Bit, lambda x: int(x)) Database.register_adapter(BitHandler, lambda x: int(x)) except ImproperlyConfigured: pass try: from django.db.backends.postgresql_psycopg2.base import Database Database.extensions.register_adapter(Bit, lambda x: Database.extensions.AsIs(int(x))) Database.extensions.register_adapter(BitHandler, lambda x: Database.extensions.AsIs(int(x))) except ImproperlyConfigured: pass