示例#1
0
    def create_type_sql(cls,
                        connection,
                        style=no_style(),
                        only_if_not_exists=False):
        """Return the appropriate SQL to create the custom type in the
        database.
        """
        answer = []

        # Get the name of the database type.
        db_type_name = cls.db_type(connection)
        qn = connection.ops.quote_name

        # Iterate over the sub-fields and construct the SQL for
        # construction of each of them.
        if not only_if_not_exists or not type_exists(connection, db_type_name):
            subfield_type_defs = []
            for key, field in cls._meta.fields:
                subfield_type_defs.append('    %s %s' % (
                    style.SQL_FIELD(qn(key)),
                    style.SQL_COLTYPE(field.db_type(connection)),
                ))

            # Return the final SQL to create the type.
            sql = []
            sql.append(style.SQL_KEYWORD('CREATE TYPE '))
            sql.append(style.SQL_TABLE(qn(db_type_name)))
            sql.append(style.SQL_KEYWORD(' AS'))
            sql.append(' (\n')
            sql.append(',\n'.join(subfield_type_defs))
            sql.append('\n)')
            answer.append(''.join(sql) + '\n;')

        # Iterate over sub-fields and add any pre-create SQL that
        #   they may require.
        # It's worth noting that if the sub-fields do need pre-create SQL,
        #   it would need to be executed first.
        # Therefore, *prepend* the sub-field SQL if it exists.
        for field_name, field in cls._meta.fields:
            if hasattr(field, 'create_type_sql'):
                answer.insert(
                    0,
                    field.create_type_sql(
                        connection,
                        style,
                        only_if_not_exists=only_if_not_exists))

        # Return the answer.
        return ';\n'.join(answer)
示例#2
0
    def create_type_sql(cls, connection, style=no_style(),
                                         only_if_not_exists=False ):
        """Return the appropriate SQL to create the custom type in the
        database.
        """
        answer = []

        # Get the name of the database type.
        db_type_name = cls.db_type(connection)
        qn = connection.ops.quote_name

        # Iterate over the sub-fields and construct the SQL for
        # construction of each of them.
        if not only_if_not_exists or not type_exists(connection, db_type_name):
            subfield_type_defs = []
            for key, field in cls._meta.fields:
                subfield_type_defs.append(
                    '    %s %s' % (
                        style.SQL_FIELD(qn(key)),
                        style.SQL_COLTYPE(field.db_type(connection)),
                    )
                )

            # Return the final SQL to create the type.
            sql = []
            sql.append(style.SQL_KEYWORD('CREATE TYPE '))
            sql.append(style.SQL_TABLE(qn(db_type_name)))
            sql.append(style.SQL_KEYWORD(' AS'))
            sql.append(' (\n')
            sql.append(',\n'.join(subfield_type_defs))
            sql.append('\n)')
            answer.append(''.join(sql) + '\n;')

        # Iterate over sub-fields and add any pre-create SQL that
        #   they may require.
        # It's worth noting that if the sub-fields do need pre-create SQL,
        #   it would need to be executed first.
        # Therefore, *prepend* the sub-field SQL if it exists.
        for field_name, field in cls._meta.fields:
            if hasattr(field, 'create_type_sql'):
                answer.insert(0, field.create_type_sql(connection, style,
                                    only_if_not_exists=only_if_not_exists))

        # Return the answer.
        return ';\n'.join(answer)
示例#3
0
 def type_exists(cls, connection):
     return type_exists(connection, cls._meta.db_type)
示例#4
0
 def type_exists(cls, connection):
     return type_exists(connection, cls._meta.db_type)