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

Method buildExportTableDDL

database/mssql/database.go:143–269  ·  view source on GitHub ↗
(table string, columns []column, indexDefs []*indexDef, foreignDefs []string, checkDefs []string)

Source from the content-addressed store, hash-verified

141}
142
143func (d *MssqlDatabase) buildExportTableDDL(table string, columns []column, indexDefs []*indexDef, foreignDefs []string, checkDefs []string) string {
144 var queryBuilder strings.Builder
145 fmt.Fprintf(&queryBuilder, "CREATE TABLE %s (", table)
146 for i, col := range columns {
147 if i > 0 {
148 fmt.Fprint(&queryBuilder, ",")
149 }
150 fmt.Fprint(&queryBuilder, "\n"+indent)
151 fmt.Fprintf(&queryBuilder, "%s %s", d.quoteIdentifier(col.Name), col.dataType)
152 if length, ok := col.getLength(); ok {
153 fmt.Fprintf(&queryBuilder, "(%s)", length)
154 }
155 if !col.Nullable {
156 fmt.Fprint(&queryBuilder, " NOT NULL")
157 }
158 if col.DefaultName != "" {
159 fmt.Fprintf(&queryBuilder, " CONSTRAINT %s DEFAULT %s", d.quoteIdentifier(col.DefaultName), col.DefaultVal)
160 }
161 if col.Identity != nil {
162 fmt.Fprintf(&queryBuilder, " IDENTITY(%s,%s)", col.Identity.SeedValue, col.Identity.IncrementValue)
163 if col.Identity.NotForReplication {
164 fmt.Fprint(&queryBuilder, " NOT FOR REPLICATION")
165 }
166 }
167 if col.Check != nil {
168 fmt.Fprintf(&queryBuilder, " CONSTRAINT %s CHECK", d.quoteIdentifier(col.Check.Name))
169 if col.Check.NotForReplication {
170 fmt.Fprint(&queryBuilder, " NOT FOR REPLICATION")
171 }
172 fmt.Fprintf(&queryBuilder, " %s", col.Check.Definition)
173 }
174 }
175
176 // PRIMARY KEY
177 for _, indexDef := range indexDefs {
178 if !indexDef.primary {
179 continue
180 }
181 fmt.Fprint(&queryBuilder, ",\n"+indent)
182 fmt.Fprintf(&queryBuilder, "CONSTRAINT %s PRIMARY KEY", d.quoteIdentifier(indexDef.name))
183
184 if indexDef.indexType == "CLUSTERED" || indexDef.indexType == "NONCLUSTERED" {
185 fmt.Fprintf(&queryBuilder, " %s", indexDef.indexType)
186 }
187 fmt.Fprintf(&queryBuilder, " (%s)", strings.Join(indexDef.columns, ", "))
188 if len(indexDef.options) > 0 {
189 fmt.Fprint(&queryBuilder, " WITH (")
190 for i, option := range indexDef.options {
191 if i > 0 {
192 fmt.Fprint(&queryBuilder, ",")
193 }
194 fmt.Fprintf(&queryBuilder, " %s", fmt.Sprintf("%s = %s", option.name, option.value))
195 }
196 fmt.Fprint(&queryBuilder, " )")
197 }
198 }
199
200 // UNIQUE CONSTRAINTS (that were created as constraints, not indexes)

Callers 2

exportTableDDLMethod · 0.95

Calls 3

quoteIdentifierMethod · 0.95
getLengthMethod · 0.80
StringMethod · 0.45