Add missing length and real_type fields to cdc_schema.go

The Go version of the CDC schema generator was missing the new `length`
and `real_type` fields.
This commit is contained in:
Markus Mäkelä
2017-07-25 21:08:50 +03:00
parent ba3912ddb4
commit fc81a8097f

View File

@ -15,11 +15,11 @@ import (
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt"
"log" "log"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
"fmt"
) )
import _ "github.com/go-sql-driver/mysql" import _ "github.com/go-sql-driver/mysql"
@ -45,8 +45,10 @@ The "user" and "password" flags are required.
// Avro field // Avro field
type Field struct { type Field struct {
Name string `json:"name"` Name string `json:"name"`
Type string `json:"type"` Type string `json:"type"`
RealType string `json:"real_type"`
Length int `json:"length"`
} }
// Avro schema // Avro schema
@ -68,13 +70,20 @@ func LogObject(obj interface{}) {
} }
var field_re *regexp.Regexp var field_re *regexp.Regexp
var length_re *regexp.Regexp
// Convert the SQL type to the appropriate Avro type // Convert the SQL type to the appropriate Avro type
func (f *Field) ToAvroType() { func (f *Field) ToAvroType() {
orig := f.Type
f.Type = field_re.ReplaceAllString(f.Type, "") f.Type = field_re.ReplaceAllString(f.Type, "")
f.Length = -1
f.RealType = f.Type
switch f.Type { switch f.Type {
case "date", "datetime", "time", "timestamp", "year", "tinytext", "text", case "date", "datetime", "time", "timestamp", "year", "tinytext", "text",
"mediumtext", "longtext", "char", "varchar", "enum", "set": "mediumtext", "longtext", "char", "varchar":
f.Type = "string"
f.Length, _ = strconv.Atoi(length_re.ReplaceAllString(orig, "$1"))
case "enum", "set":
f.Type = "string" f.Type = "string"
case "tinyblob", "blob", "mediumblob", "longblob", "binary", "varbinary": case "tinyblob", "blob", "mediumblob", "longblob", "binary", "varbinary":
f.Type = "bytes" f.Type = "bytes"
@ -127,11 +136,18 @@ func StoreSchema(db *sql.DB, schema, table string) {
func main() { func main() {
var err error var err error
field_re, err = regexp.Compile("[(].*") field_re, err = regexp.Compile("[(].*")
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: ", err)
} }
flag.Usage = PrintUsage; length_re, err = regexp.Compile(".*[(](.*)[)].*")
if err != nil {
log.Fatal("Error: ", err)
}
flag.Usage = PrintUsage
flag.Parse() flag.Parse()
if len(*user) == 0 || len(*passwd) == 0 { if len(*user) == 0 || len(*passwd) == 0 {