MCPcopy
hub / github.com/sqldef/sqldef / buildExportTableDDL

Method buildExportTableDDL

database/postgres/database.go:766–840  ·  view source on GitHub ↗
(components TableDDLComponents)

Source from the content-addressed store, hash-verified

764}
765
766func (d *PostgresDatabase) buildExportTableDDL(components TableDDLComponents) string {
767 var queryBuilder strings.Builder
768 schema, table := splitTableName(components.TableName, components.DefaultSchema)
769 fmt.Fprintf(&queryBuilder, "CREATE TABLE %s.%s (", d.quoteIdentifierIfNeeded(schema), d.quoteIdentifierIfNeeded(table))
770 for i, col := range components.Columns {
771 if i > 0 {
772 fmt.Fprint(&queryBuilder, ",")
773 }
774 fmt.Fprint(&queryBuilder, "\n"+indent)
775 fmt.Fprintf(&queryBuilder, "%s %s", d.quoteIdentifierIfNeeded(col.Name), d.escapeDataTypeName(col.GetDataType()))
776 if !col.Nullable {
777 fmt.Fprint(&queryBuilder, " NOT NULL")
778 }
779 if col.Default != "" && !col.IsAutoIncrement {
780 fmt.Fprintf(&queryBuilder, " DEFAULT %s", col.Default)
781 }
782 if col.IdentityGeneration != "" {
783 fmt.Fprintf(&queryBuilder, " GENERATED %s AS IDENTITY", col.IdentityGeneration)
784 }
785 if col.Check != nil {
786 fmt.Fprintf(&queryBuilder, " CONSTRAINT %s %s", d.quoteIdent(col.Check.name), col.Check.definition)
787 }
788 }
789 if len(components.PrimaryKeyCols) > 0 {
790 fmt.Fprint(&queryBuilder, ",\n"+indent)
791 if components.PrimaryKeyPeriod {
792 lastIdx := len(components.PrimaryKeyCols) - 1
793 var quotedCols []string
794 for i, col := range components.PrimaryKeyCols {
795 quoted := fmt.Sprintf("\"%s\"", col)
796 if i == lastIdx {
797 quoted += " WITHOUT OVERLAPS"
798 }
799 quotedCols = append(quotedCols, quoted)
800 }
801 fmt.Fprintf(&queryBuilder, "CONSTRAINT %s PRIMARY KEY (%s)", d.quoteIdent(components.PrimaryKeyName), strings.Join(quotedCols, ", "))
802 } else {
803 fmt.Fprintf(&queryBuilder, "CONSTRAINT %s PRIMARY KEY (\"%s\")", d.quoteIdent(components.PrimaryKeyName), strings.Join(components.PrimaryKeyCols, "\", \""))
804 }
805 }
806
807 for _, check := range components.CheckConstraints {
808 fmt.Fprint(&queryBuilder, ",\n"+indent)
809 fmt.Fprintf(&queryBuilder, "CONSTRAINT %s %s", d.quoteIdent(check.Name), check.Definition)
810 }
811
812 fmt.Fprintf(&queryBuilder, "\n);\n")
813 for _, v := range components.IndexDefs {
814 fmt.Fprintf(&queryBuilder, "%s;\n", v)
815 }
816 for _, v := range components.ForeignDefs {
817 fmt.Fprintf(&queryBuilder, "%s;\n", v)
818 }
819 for _, v := range components.ExclusionDefs {
820 fmt.Fprintf(&queryBuilder, "%s;\n", v)
821 }
822 for _, v := range components.RLSDefs {
823 fmt.Fprintf(&queryBuilder, "%s;\n", v)

Callers 1

exportTableDDLMethod · 0.95

Calls 7

escapeDataTypeNameMethod · 0.95
quoteIdentMethod · 0.95
CanonicalMapIterFunction · 0.92
GetDataTypeMethod · 0.80
splitTableNameFunction · 0.70
StringMethod · 0.45

Tested by

no test coverage detected