コード例 #1
0
 def execute(self, query, args=()):
     if djangoVersion[:2] >= (1, 4) and settings.USE_TZ:
         args = _datetimes_in(args)
     try:
         if args != None:
             query = self.convert_query(query, len(args))
         ret = self.cursor.execute(trace(query), trace(args))
         return ret
     except Database.OperationalError as 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
コード例 #2
0
 def executemany(self, query, args):
     if djangoVersion[:2] >= (1, 4) and settings.USE_TZ:
         args = tuple(_datetimes_in(arg) for arg in args)
     try:
         try:
             len(args)
         except TypeError:
             args = tuple(args)
         if len(args) > 0:
             query = self.convert_query(query, args[0])
             ret = self.cursor.executemany(query)
             return ret
         else:
             return None
     except Database.OperationalError as e:
         # Map some error codes to IntegrityError, since they seem to be
         # misclassified and Django would prefer the more logical place.
         if e.errorcode in self.codes_for_integrityerror:
             raise Database.IntegrityError(e)
         raise
コード例 #3
0
ファイル: base.py プロジェクト: vaquer/sqlany-django
 def execute(self, query, args=()):
     if djangoVersion[:2] >= (1, 4) and settings.USE_TZ:
         args = _datetimes_in(args)
     try:
         if args != None:
             query = self.convert_query(query, len(args))
         ret = self.cursor.execute(trace(query), trace(args))
         return ret
     except Database.OperationalError as e:
         if e.message == 'Connection was terminated':
             from django import db
             try:
                 db.close_old_connections()
             except AttributeError:
                 db.close_connection()
         # Map some error codes to IntegrityError, since they seem to be
         # misclassified and Django would prefer the more logical place.
         if e.errorcode in self.codes_for_integrityerror:
             raise Database.IntegrityError(e)
         raise
コード例 #4
0
    def execute(self, query, args=()):
        global PAUSE

        if 'CREATE TABLE' in query:
            import pdb
            pdb.set_trace()

        if djangoVersion[:2] >= (1, 4) and settings.USE_TZ:
            args = _datetimes_in(args)
        try:
            if args != None:
                query = self.convert_query(query, args)

            # if 'CREATE TABLE "auth_user"' in query:
            #     import pdb
            #     pdb.set_trace()
            #
            # if 'CREATE INDEX "analytics_result_project_id_9b00f933" ON "analytics_result" ("project_id")' == query or PAUSE:
            #     import pdb
            #     pdb.set_trace()
            #     PAUSE = True

            ret = self.cursor.execute(query)
            return ret
        except Database.OperationalError as e:
            if str(e) == 'Connection was terminated':
                from django import db
                try:
                    db.close_old_connections()
                except AttributeError:
                    db.close_connection()

            # Ignore errors from tables already existing
            if e.errorcode == -110:
                return None

            # For Django >= 1.10, do a multi-stage statement to change an index key
            # Q: Should we perform this logic for any version of Django?
            if djangoVersion[:2] >= (
                    1, 10) and e.errorcode in self.codes_for_edit_index:
                # TODO We need to do more work to make the modification of index keys generic.
                # Currently this only handles the case where Django's default auth_user migrations (008) attempt to
                # increase the length of the unique username field to 150 characters. For user defined models, we will
                # need to dynamically look up the index. There should be a way of doing this if Django is consistent
                # with its naming schema for these constraints ("<table_name> UNIQUE (<field_name>)")
                if query != 'ALTER TABLE "auth_user" ALTER "username" varchar(150)':
                    # This is not the expected case, and we don't do generic index keys yet; raise original error
                    raise
                try:
                    self.cursor.execute(
                        'DROP INDEX "auth_user"."auth_user UNIQUE (username)"')
                    ret = self.cursor.execute(
                        query
                    )  # ALTER TABLE "auth_user" ALTER "username" varchar(150)
                    self.cursor.execute(
                        'ALTER TABLE "auth_user" ADD CONSTRAINT "auth_user UNIQUE (username)" UNIQUE ( "username" ASC)'
                    )
                    return ret
                except Database.OperationalError as e:
                    raise Database.OperationalError(
                        'attempted to modify index edit for username, but failed'
                    ) from e

            # Map some error codes to IntegrityError, since they seem to be
            # misclassified and Django would prefer the more logical place.
            if e.errorcode in self.codes_for_integrityerror:
                raise Database.IntegrityError(e)
            raise