Fixed a few bugs.

Olivier Tremblay 5 years ago
parent
commit
6fc12db5e6
2 changed files with 14 additions and 6 deletions
  1. 0 0
      BUGS
  2. 14 6
      main.go

+ 0 - 0
BUGS


+ 14 - 6
main.go

@@ -38,15 +38,15 @@ func main() {
38 38
 		}
39 39
 	}
40 40
 
41
-	more_rows, err := db.Query("select table_name, column_name, data_type, column_key, is_nullable from information_schema.columns where table_schema = ?", dbName)
41
+	more_rows, err := db.Query("select table_name, column_name, data_type, column_key, is_nullable, extra from information_schema.columns where table_schema = ?", dbName)
42 42
 
43 43
 	if err != nil {
44 44
 		panic(err)
45 45
 	}
46 46
 
47 47
 	for more_rows.Next() {
48
-		var tn, cn, ct, ck, nullable string
49
-		err := more_rows.Scan(&tn, &cn, &ct, &ck, &nullable)
48
+		var tn, cn, ct, ck, nullable, extra string
49
+		err := more_rows.Scan(&tn, &cn, &ct, &ck, &nullable, &extra)
50 50
 		if err != nil {
51 51
 			panic(err)
52 52
 		}
@@ -55,7 +55,7 @@ func main() {
55 55
 
56 56
 		col.Primary = ck == "PRI"
57 57
 		fmt.Println(col, ck)
58
-		if nullable == "NO" {
58
+		if nullable == "NO" && extra != "auto_increment" {
59 59
 			table.Constraints = append(table.Constraints, fmt.Sprintf(`if row.%s == %s {return errors.New("Preconditions failed, %s must be set.")}`, col.CapitalizedColumnName(), col.ColumnNullValue(), col.CapitalizedColumnName()))
60 60
 		}
61 61
 		table.TableColumns[cn] = col
@@ -76,7 +76,7 @@ type tableInfo struct {
76 76
 	Constraints  []string
77 77
 }
78 78
 
79
-func columnNames(cols []tableColumn) []string {
79
+func capitalizedColumnNames(cols []tableColumn) []string {
80 80
 	result := []string{}
81 81
 	for _, c := range cols {
82 82
 		result = append(result, c.CapitalizedColumnName())
@@ -84,6 +84,14 @@ func columnNames(cols []tableColumn) []string {
84 84
 	return result
85 85
 }
86 86
 
87
+func columnNames(cols []tableColumn) []string {
88
+	result := []string{}
89
+	for _, c := range cols {
90
+		result = append(result, c.ColumnName)
91
+	}
92
+	return result
93
+}
94
+
87 95
 func (t tableInfo) QueryFieldNames() string {
88 96
 	return strings.Join(columnNames(t.ColOrder), ",")
89 97
 }
@@ -122,7 +130,7 @@ func (t tableInfo) PrimaryWhere() string {
122 130
 }
123 131
 
124 132
 func (t tableInfo) PrimaryColumnsJoinedByAnd() string {
125
-	return strings.Join(columnNames(t.PrimaryColumns()), "And")
133
+	return strings.Join(capitalizedColumnNames(t.PrimaryColumns()), "And")
126 134
 }
127 135
 
128 136
 func colformat(cols []tableColumn, format string, joinstring string, str1, str2 func(tableColumn) string) string {