MCPcopy Index your code
hub / github.com/go-dev-frame/sponge / ConvertToSQLByPgFields

Function ConvertToSQLByPgFields

pkg/sql2code/parser/postgresql.go:23–49  ·  view source on GitHub ↗

ConvertToSQLByPgFields convert to mysql table ddl

(tableName string, fields PGFields)

Source from the content-addressed store, hash-verified

21
22// ConvertToSQLByPgFields convert to mysql table ddl
23func ConvertToSQLByPgFields(tableName string, fields PGFields) (string, map[string]string) {
24 fieldStr := ""
25 pgTypeMap := make(map[string]string) // name:type
26 if len(fields) == 0 {
27 return "", pgTypeMap
28 }
29
30 for _, field := range fields {
31 pgTypeMap[field.Name] = getType(field)
32 sqlType := field.getMysqlType()
33 notnullStr := "not null"
34 if !field.Notnull {
35 notnullStr = "null"
36 }
37 comment := strings.ReplaceAll(field.Comment, "'", "\\'")
38 fieldStr += fmt.Sprintf(" `%s` %s %s comment '%s',\n", field.Name, sqlType, notnullStr, comment)
39 }
40
41 primaryField := fields.getPrimaryField()
42 if primaryField != nil {
43 fieldStr += fmt.Sprintf(" PRIMARY KEY (`%s`)\n", primaryField.Name)
44 } else {
45 fieldStr = strings.TrimSuffix(fieldStr, ",\n")
46 }
47 sqlStr := fmt.Sprintf("CREATE TABLE `%s` (\n%s\n);", tableName, fieldStr)
48 return sqlStr, pgTypeMap
49}
50
51// PGField postgresql field
52type PGField struct {

Callers 3

getSQLFunction · 0.92

Calls 3

getTypeFunction · 0.85
getMysqlTypeMethod · 0.45
getPrimaryFieldMethod · 0.45

Tested by 2