MCPcopy
hub / github.com/sqlc-dev/sqlc / convertColumnDef

Function convertColumnDef

internal/engine/dolphin/convert.go:240–300  ·  view source on GitHub ↗
(def *pcast.ColumnDef)

Source from the content-addressed store, hash-verified

238}
239
240func convertColumnDef(def *pcast.ColumnDef) *ast.ColumnDef {
241 var vals *ast.List
242 if len(def.Tp.GetElems()) > 0 {
243 vals = &ast.List{}
244 for i := range def.Tp.GetElems() {
245 vals.Items = append(vals.Items, &ast.String{
246 Str: def.Tp.GetElems()[i],
247 })
248 }
249 }
250 comment := ""
251 for _, opt := range def.Options {
252 switch opt.Tp {
253 case pcast.ColumnOptionComment:
254 if value, ok := opt.Expr.(*driver.ValueExpr); ok {
255 comment = value.GetString()
256 }
257 }
258 }
259
260 // Build TypeName with modifiers for proper formatting
261 typeName := &ast.TypeName{Name: types.TypeToStr(def.Tp.GetType(), def.Tp.GetCharset())}
262
263 // Add type modifiers (e.g., length for varchar(255), char(32))
264 // Only for types where length is meaningful and user-specified
265 tp := def.Tp.GetType()
266 flen := def.Tp.GetFlen()
267 needsLength := false
268 switch tp {
269 case mysql.TypeVarchar, mysql.TypeString, mysql.TypeVarString:
270 // VARCHAR(n), CHAR(n) - always need length
271 needsLength = flen >= 0
272 case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
273 // BLOB types - only if user specified length (VARBINARY(n), BINARY(n))
274 // Default blob types don't need length
275 needsLength = false
276 }
277
278 if needsLength {
279 typeName.Typmods = &ast.List{
280 Items: []ast.Node{
281 &ast.Integer{Ival: int64(flen)},
282 },
283 }
284 }
285
286 columnDef := ast.ColumnDef{
287 Colname: def.Name.String(),
288 TypeName: typeName,
289 IsNotNull: isNotNull(def),
290 IsUnsigned: isUnsigned(def),
291 Comment: comment,
292 Vals: vals,
293 }
294 if def.Tp.GetFlen() >= 0 {
295 length := def.Tp.GetFlen()
296 columnDef.Length = &length
297 }

Callers 2

convertAlterTableStmtMethod · 0.70

Calls 4

isUnsignedFunction · 0.85
isNotNullFunction · 0.70
GetTypeMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected