github.com/gocaveman/caveman@v0.0.0-20191211162744-0ddf99dbdf6e/ddl/create-table-stmt.go (about) 1 package ddl 2 3 // By convention, the struct members are public and end with "Value", so we 4 // can use the name without any suffix as a builder method, e.g. 5 // IfNotExistsValue is set with the IfNotExists() method. We make an exception 6 // for lists of things like PrimaryKeys, since the method name is PrimaryKey() 7 8 type CreateTableStmt struct { 9 *Builder 10 11 NameValue string 12 13 IfNotExistsValue bool // TODO: consider removing this, not sure if it's really needed 14 15 Columns DataTypeDefPtrList 16 17 PrimaryKeys []string 18 19 ForeignKeys []*CreateTableFKDef 20 } 21 22 type CreateTableFKDef struct { 23 ColumnValue string 24 OtherTableValue string 25 OtherColumnValue string 26 } 27 28 type CreateTableColDef struct { 29 *CreateTableStmt 30 *DataTypeDef 31 } 32 33 func (s *CreateTableStmt) IsStmt() {} 34 35 func (s *CreateTableStmt) IfNotExists() *CreateTableStmt { 36 s.IfNotExistsValue = true 37 return s 38 } 39 40 func (s *CreateTableStmt) Column(name string, dataType DataType) *CreateTableColDef { 41 dtd := &DataTypeDef{NameValue: name, DataTypeValue: dataType} 42 s.Columns = append(s.Columns, dtd) 43 return &CreateTableColDef{ 44 CreateTableStmt: s, 45 DataTypeDef: dtd, 46 } 47 } 48 49 func (s *CreateTableStmt) ColumnCustom(name, customSQL string) *CreateTableColDef { 50 dtd := &DataTypeDef{NameValue: name, DataTypeValue: Custom, CustomSQLValue: customSQL} 51 s.Columns = append(s.Columns, dtd) 52 return &CreateTableColDef{ 53 CreateTableStmt: s, 54 DataTypeDef: dtd, 55 } 56 } 57 58 func (def *CreateTableColDef) Null() *CreateTableColDef { 59 def.DataTypeDef.NullValue = true 60 return def 61 } 62 63 func (def *CreateTableColDef) Default(value interface{}) *CreateTableColDef { 64 def.DataTypeDef.DefaultValue = value 65 return def 66 } 67 68 func (def *CreateTableColDef) Length(length int) *CreateTableColDef { 69 def.DataTypeDef.LengthValue = length 70 return def 71 } 72 73 func (def *CreateTableColDef) CaseSensitive() *CreateTableColDef { 74 def.DataTypeDef.CaseSensitiveValue = true 75 return def 76 } 77 78 func (def *CreateTableColDef) ForiegnKey(otherTable, otherColumn string) *CreateTableColDef { 79 def.CreateTableStmt.ForeignKeys = append(def.CreateTableStmt.ForeignKeys, &CreateTableFKDef{ 80 ColumnValue: def.CreateTableStmt.NameValue, 81 OtherTableValue: otherTable, 82 OtherColumnValue: otherColumn, 83 }) 84 return def 85 } 86 87 func (def *CreateTableColDef) PrimaryKey() *CreateTableColDef { 88 def.CreateTableStmt.PrimaryKeys = append(def.CreateTableStmt.PrimaryKeys, def.DataTypeDef.NameValue) 89 return def 90 } 91 92 type DropTableStmt struct { 93 *Builder 94 95 NameValue string 96 } 97 98 func (s *DropTableStmt) IsStmt() {}