Skip to content

Commit eb6ce74

Browse files
author
Sergey Podgornyy
committed
Add more helper functions to the table
1 parent a1b7b09 commit eb6ce74

File tree

2 files changed

+288
-0
lines changed

2 files changed

+288
-0
lines changed

table.go

+80
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,86 @@ func (t *Table) Timestamps() {
8585
})
8686
}
8787

88+
// Int adds int(precision) column to the table
89+
func (t *Table) Int(name string, precision uint16, unsigned bool) {
90+
t.Column(name, Integer{Precision: precision, Unsigned: unsigned})
91+
}
92+
93+
// BigInt adds bigint(precision) column to the table
94+
func (t *Table) BigInt(name string, precision uint16, unsigned bool) {
95+
t.Column(name, Integer{Prefix: "big", Precision: precision, Unsigned: unsigned})
96+
}
97+
98+
// Float adds float(precision,scale) column to the table
99+
func (t *Table) Float(name string, precision uint16, scale uint16) {
100+
t.Column(name, Floatable{Precision: precision, Scale: scale})
101+
}
102+
103+
// FixedFloat is an alias to decimal(precision,scale) column
104+
func (t *Table) FixedFloat(name string, precision uint16, scale uint16) {
105+
t.Decimal(name, precision, scale)
106+
}
107+
108+
// Decimal adds decimal(precision,scale) column to the table
109+
func (t *Table) Decimal(name string, precision uint16, scale uint16) {
110+
t.Column(name, Floatable{Type: "decimal", Precision: precision, Scale: scale})
111+
}
112+
113+
// Varchar adds varchar(precision) column to the table
114+
func (t *Table) Varchar(name string, precision uint16) {
115+
t.Column(name, String{Precision: precision})
116+
}
117+
118+
// Char adds char(precision) column to the table
119+
func (t *Table) Char(name string, precision uint16) {
120+
t.Column(name, String{Fixed: true, Precision: precision})
121+
}
122+
123+
// Text adds text column to the table
124+
func (t *Table) Text(name string, nullable bool) {
125+
t.Column(name, Text{Nullable: nullable})
126+
}
127+
128+
// Blob adds blob column to the table
129+
func (t *Table) Blob(name string, nullable bool) {
130+
t.Column(name, Text{Blob: true, Nullable: nullable})
131+
}
132+
133+
// JSON adds json column to the table
134+
func (t *Table) JSON(name string) {
135+
t.Column(name, JSON{})
136+
}
137+
138+
// Timestamp adds timestamp column to the table
139+
func (t *Table) Timestamp(name string, nullable bool, def string) {
140+
t.Column(name, Timable{Nullable: nullable, Default: def})
141+
}
142+
143+
// Date adds date column to the table
144+
func (t *Table) Date(name string, nullable bool, def string) {
145+
t.Column(name, Timable{Type: "date", Nullable: nullable, Default: def})
146+
}
147+
148+
// Time adds time column to the table
149+
func (t *Table) Time(name string, nullable bool, def string) {
150+
t.Column(name, Timable{Type: "time", Nullable: nullable, Default: def})
151+
}
152+
153+
// Year adds year column to the table
154+
func (t *Table) Year(name string, nullable bool, def string) {
155+
t.Column(name, Timable{Type: "year", Nullable: nullable, Default: def})
156+
}
157+
158+
// Binary adds binary(precision) column to the table
159+
func (t *Table) Binary(name string, precision uint16, nullable bool) {
160+
t.Column(name, Binary{Fixed: true, Precision: precision, Nullable: nullable})
161+
}
162+
163+
// Varbinary adds varbinary(precision) column to the table
164+
func (t *Table) Varbinary(name string, precision uint16, nullable bool) {
165+
t.Column(name, Binary{Precision: precision, Nullable: nullable})
166+
}
167+
88168
// Primary adds primary key
89169
func (t *Table) Primary(columns ...string) {
90170
if len(columns) == 0 {

table_test.go

+208
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,214 @@ func TestTimestampsColumn(t *testing.T) {
109109
assert.Equal(Timable{Type: "timestamp", Default: "CURRENT_TIMESTAMP", OnUpdate: "CURRENT_TIMESTAMP"}, table.columns[1].definition)
110110
}
111111

112+
func TestIntColumn(t *testing.T) {
113+
assert := assert.New(t)
114+
table := Table{}
115+
116+
assert.Nil(table.columns)
117+
118+
table.Int("number", 64, true)
119+
120+
assert.Len(table.columns, 1)
121+
assert.Equal("number", table.columns[0].field)
122+
assert.Equal(Integer{Precision: 64, Unsigned: true}, table.columns[0].definition)
123+
}
124+
125+
func TestBigIntColumn(t *testing.T) {
126+
assert := assert.New(t)
127+
table := Table{}
128+
129+
assert.Nil(table.columns)
130+
131+
table.BigInt("number", 64, true)
132+
133+
assert.Len(table.columns, 1)
134+
assert.Equal("number", table.columns[0].field)
135+
assert.Equal(Integer{Prefix: "big", Precision: 64, Unsigned: true}, table.columns[0].definition)
136+
}
137+
138+
func TestFloatColumn(t *testing.T) {
139+
assert := assert.New(t)
140+
table := Table{}
141+
142+
assert.Nil(table.columns)
143+
144+
table.Float("number", 15, 2)
145+
146+
assert.Len(table.columns, 1)
147+
assert.Equal("number", table.columns[0].field)
148+
assert.Equal(Floatable{Precision: 15, Scale: 2}, table.columns[0].definition)
149+
}
150+
151+
func TestFixedFloatColumn(t *testing.T) {
152+
assert := assert.New(t)
153+
table := Table{}
154+
155+
assert.Nil(table.columns)
156+
157+
table.FixedFloat("number", 15, 2)
158+
159+
assert.Len(table.columns, 1)
160+
assert.Equal("number", table.columns[0].field)
161+
assert.Equal(Floatable{Type: "decimal", Precision: 15, Scale: 2}, table.columns[0].definition)
162+
}
163+
164+
func TestDecimalColumn(t *testing.T) {
165+
assert := assert.New(t)
166+
table := Table{}
167+
168+
assert.Nil(table.columns)
169+
170+
table.Decimal("number", 15, 2)
171+
172+
assert.Len(table.columns, 1)
173+
assert.Equal("number", table.columns[0].field)
174+
assert.Equal(Floatable{Type: "decimal", Precision: 15, Scale: 2}, table.columns[0].definition)
175+
}
176+
177+
func TestVarcharColumn(t *testing.T) {
178+
assert := assert.New(t)
179+
table := Table{}
180+
181+
assert.Nil(table.columns)
182+
183+
table.Varchar("string", 64)
184+
185+
assert.Len(table.columns, 1)
186+
assert.Equal("string", table.columns[0].field)
187+
assert.Equal(String{Precision: 64}, table.columns[0].definition)
188+
}
189+
190+
func TestCharColumn(t *testing.T) {
191+
assert := assert.New(t)
192+
table := Table{}
193+
194+
assert.Nil(table.columns)
195+
196+
table.Char("string", 32)
197+
198+
assert.Len(table.columns, 1)
199+
assert.Equal("string", table.columns[0].field)
200+
assert.Equal(String{Fixed: true, Precision: 32}, table.columns[0].definition)
201+
}
202+
203+
func TestTextColumn(t *testing.T) {
204+
assert := assert.New(t)
205+
table := Table{}
206+
207+
assert.Nil(table.columns)
208+
209+
table.Text("string", true)
210+
211+
assert.Len(table.columns, 1)
212+
assert.Equal("string", table.columns[0].field)
213+
assert.Equal(Text{Nullable: true}, table.columns[0].definition)
214+
}
215+
216+
func TestBlobColumn(t *testing.T) {
217+
assert := assert.New(t)
218+
table := Table{}
219+
220+
assert.Nil(table.columns)
221+
222+
table.Blob("string", true)
223+
224+
assert.Len(table.columns, 1)
225+
assert.Equal("string", table.columns[0].field)
226+
assert.Equal(Text{Blob: true, Nullable: true}, table.columns[0].definition)
227+
}
228+
229+
func TestJsonColumn(t *testing.T) {
230+
assert := assert.New(t)
231+
table := Table{}
232+
233+
assert.Nil(table.columns)
234+
235+
table.JSON("data")
236+
237+
assert.Len(table.columns, 1)
238+
assert.Equal("data", table.columns[0].field)
239+
assert.Equal(JSON{}, table.columns[0].definition)
240+
}
241+
242+
func TestTimestampColumn(t *testing.T) {
243+
assert := assert.New(t)
244+
table := Table{}
245+
246+
assert.Nil(table.columns)
247+
248+
table.Timestamp("date", true, "CURRENT_TIMESTAMP")
249+
250+
assert.Len(table.columns, 1)
251+
assert.Equal("date", table.columns[0].field)
252+
assert.Equal(Timable{Nullable: true, Default: "CURRENT_TIMESTAMP"}, table.columns[0].definition)
253+
}
254+
255+
func TestDateColumn(t *testing.T) {
256+
assert := assert.New(t)
257+
table := Table{}
258+
259+
assert.Nil(table.columns)
260+
261+
table.Date("date", true, "NOW()")
262+
263+
assert.Len(table.columns, 1)
264+
assert.Equal("date", table.columns[0].field)
265+
assert.Equal(Timable{Type: "date", Nullable: true, Default: "NOW()"}, table.columns[0].definition)
266+
}
267+
268+
func TestTimeColumn(t *testing.T) {
269+
assert := assert.New(t)
270+
table := Table{}
271+
272+
assert.Nil(table.columns)
273+
274+
table.Time("time", true, "NOW()")
275+
276+
assert.Len(table.columns, 1)
277+
assert.Equal("time", table.columns[0].field)
278+
assert.Equal(Timable{Type: "time", Nullable: true, Default: "NOW()"}, table.columns[0].definition)
279+
}
280+
281+
func TestYearColumn(t *testing.T) {
282+
assert := assert.New(t)
283+
table := Table{}
284+
285+
assert.Nil(table.columns)
286+
287+
table.Year("year", true, "YEAR(NOW())")
288+
289+
assert.Len(table.columns, 1)
290+
assert.Equal("year", table.columns[0].field)
291+
assert.Equal(Timable{Type: "year", Nullable: true, Default: "YEAR(NOW())"}, table.columns[0].definition)
292+
}
293+
294+
func TestBinaryColumn(t *testing.T) {
295+
assert := assert.New(t)
296+
table := Table{}
297+
298+
assert.Nil(table.columns)
299+
300+
table.Binary("binary", 36, true)
301+
302+
assert.Len(table.columns, 1)
303+
assert.Equal("binary", table.columns[0].field)
304+
assert.Equal(Binary{Fixed: true, Precision: 36, Nullable: true}, table.columns[0].definition)
305+
}
306+
307+
func TestVarbinaryColumn(t *testing.T) {
308+
assert := assert.New(t)
309+
table := Table{}
310+
311+
assert.Nil(table.columns)
312+
313+
table.Varbinary("binary", 36, true)
314+
315+
assert.Len(table.columns, 1)
316+
assert.Equal("binary", table.columns[0].field)
317+
assert.Equal(Binary{Precision: 36, Nullable: true}, table.columns[0].definition)
318+
}
319+
112320
func TestTablePrimaryIndex(t *testing.T) {
113321
t.Run("it skips adding key on empty columns list", func(t *testing.T) {
114322
assert := assert.New(t)

0 commit comments

Comments
 (0)