Skip to content

Commit 3ca9a74

Browse files
Added binary fields support
1 parent 9e592f7 commit 3ca9a74

File tree

2 files changed

+94
-36
lines changed

2 files changed

+94
-36
lines changed

internal/getters/binary.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package getters
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
7+
"github.com/icrowley/fake"
8+
)
9+
10+
// RandomBinary getter
11+
type RandomBinary struct {
12+
name string
13+
maxSize int64
14+
allowNull bool
15+
}
16+
17+
func (r *RandomBinary) Value() interface{} {
18+
if r.allowNull && rand.Int63n(100) < nilFrequency {
19+
return nil
20+
}
21+
var s string
22+
maxSize := uint64(r.maxSize)
23+
if maxSize == 0 {
24+
maxSize = uint64(rand.Int63n(100))
25+
}
26+
27+
if maxSize <= 10 {
28+
s = fake.FirstName()
29+
} else if maxSize < 30 {
30+
s = fake.FullName()
31+
} else {
32+
s = fake.Sentence()
33+
}
34+
if len(s) > int(maxSize) {
35+
s = s[:int(maxSize)]
36+
}
37+
return s
38+
}
39+
40+
func (r *RandomBinary) String() string {
41+
v := r.Value()
42+
if v == nil {
43+
return NULL
44+
}
45+
return v.(string)
46+
}
47+
48+
func (r *RandomBinary) Quote() string {
49+
v := r.Value()
50+
if v == nil {
51+
return NULL
52+
}
53+
return fmt.Sprintf("%q", v)
54+
}
55+
56+
func NewRandomBinary(name string, maxSize int64, allowNull bool) *RandomBinary {
57+
return &RandomBinary{name, maxSize, allowNull}
58+
}

tableparser/tableparser.go

+36-36
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ type Table struct {
4747

4848
// Index holds the basic index information
4949
type Index struct {
50-
Name string
51-
Fields []string
52-
Unique bool
53-
Visible bool
54-
Expression string // MySQL 8.0.16+
50+
Name string
51+
Fields []string
52+
Unique bool
53+
Visible bool
54+
Expression string // MySQL 8.0.16+
5555
}
5656

5757
// IndexField holds raw index information as defined in INFORMATION_SCHEMA table
@@ -68,7 +68,7 @@ type IndexField struct {
6868
Comment string
6969
IndexComment string
7070
NonUnique bool
71-
Visible string // MySQL 8.0+
71+
Visible string // MySQL 8.0+
7272
Expression sql.NullString // MySQL 8.0.16+
7373
}
7474

@@ -213,35 +213,35 @@ func (t *Table) parse() error {
213213
}
214214

215215
func makeScanRecipients(f *Field, allowNull *string, cols []string) []interface{} {
216-
fields := []interface{}{
217-
&f.TableCatalog,
218-
&f.TableSchema,
219-
&f.TableName,
220-
&f.ColumnName,
221-
&f.OrdinalPosition,
222-
&f.ColumnDefault,
223-
&allowNull,
224-
&f.DataType,
225-
&f.CharacterMaximumLength,
226-
&f.CharacterOctetLength,
227-
&f.NumericPrecision,
228-
&f.NumericScale,
229-
}
230-
231-
if len(cols) > 19 { // MySQL 5.5 does not have "DATETIME_PRECISION" field
232-
fields = append(fields, &f.DatetimePrecision)
233-
}
234-
235-
fields = append(fields, &f.CharacterSetName, &f.CollationName, &f.ColumnType, &f.ColumnKey, &f.Extra, &f.Privileges, &f.ColumnComment)
236-
237-
if len(cols) > 20 && cols[20] == "GENERATION_EXPRESSION" { // MySQL 5.7+ "GENERATION_EXPRESSION" field
238-
fields = append(fields, &f.GenerationExpression)
239-
}
240-
if len(cols) > 21 && cols[21] == "SRS_ID" { // MySQL 8.0+ "SRS ID" field
241-
fields = append(fields, &f.SrsID)
242-
}
243-
244-
return fields
216+
fields := []interface{}{
217+
&f.TableCatalog,
218+
&f.TableSchema,
219+
&f.TableName,
220+
&f.ColumnName,
221+
&f.OrdinalPosition,
222+
&f.ColumnDefault,
223+
&allowNull,
224+
&f.DataType,
225+
&f.CharacterMaximumLength,
226+
&f.CharacterOctetLength,
227+
&f.NumericPrecision,
228+
&f.NumericScale,
229+
}
230+
231+
if len(cols) > 19 { // MySQL 5.5 does not have "DATETIME_PRECISION" field
232+
fields = append(fields, &f.DatetimePrecision)
233+
}
234+
235+
fields = append(fields, &f.CharacterSetName, &f.CollationName, &f.ColumnType, &f.ColumnKey, &f.Extra, &f.Privileges, &f.ColumnComment)
236+
237+
if len(cols) > 20 && cols[20] == "GENERATION_EXPRESSION" { // MySQL 5.7+ "GENERATION_EXPRESSION" field
238+
fields = append(fields, &f.GenerationExpression)
239+
}
240+
if len(cols) > 21 && cols[21] == "SRS_ID" { // MySQL 8.0+ "SRS ID" field
241+
fields = append(fields, &f.SrsID)
242+
}
243+
244+
return fields
245245
}
246246

247247
// FieldNames returns an string array with the table's field names
@@ -264,7 +264,7 @@ func getIndexes(db *sql.DB, schema, tableName string) (map[string]Index, error)
264264

265265
for rows.Next() {
266266
var i IndexField
267-
var table, string
267+
var table, visible string
268268
fields := []interface{}{&table, &i.NonUnique, &i.KeyName, &i.SeqInIndex,
269269
&i.ColumnName, &i.Collation, &i.Cardinality, &i.SubPart,
270270
&i.Packed, &i.Null, &i.IndexType, &i.Comment, &i.IndexComment,

0 commit comments

Comments
 (0)