Documentation
¶
Index ¶
- Constants
- Variables
- func EnsureCriterionTypes(schema *avro.RecordSchema, criteria []Criterion) (err error)
- func GetSQLiteTables(db *sql.DB) ([]string, error)
- func GetTables(db *sql.DB, dbName string) ([]string, error)
- func SQLDatabase2AVRO(db *sql.DB, dbName string) ([]avro.RecordSchema, error)
- func SQLEscape(input string) string
- func SQLTable2AVRO(db *sql.DB, dbName, tableName string) (*avro.RecordSchema, error)
- func SQLiteTable2AVRO(db *sql.DB, dbName, tableName string) (*avro.RecordSchema, error)
- func UnderlyingType(union avro.UnionSchema) (avro.Schema, error)
- type Criterion
- func NewCriterionDate(fieldName string, limit *time.Time, order avro.Order) *Criterion
- func NewCriterionDateTime(fieldName string, limit *time.Time, order avro.Order) *Criterion
- func NewCriterionFloat64(fieldName string, limit *float64, order avro.Order) *Criterion
- func NewCriterionFromNative(field *avro.RecordFieldSchema, value interface{}, order avro.Order) (*Criterion, error)
- func NewCriterionFromString(field *avro.RecordFieldSchema, value string, order avro.Order) (*Criterion, error)
- func NewCriterionInt64(fieldName string, limit *int64, order avro.Order) *Criterion
- func NewCriterionString(fieldName string, limit *string, order avro.Order) *Criterion
- func NewCriterionTime(fieldName string, limit *time.Time, order avro.Order) *Criterion
- func Query(cfg QueryConfig) (resultBytes []byte, newCriteria []Criterion, err error)
- type QueryConfig
- type SQLType
Examples ¶
Constants ¶
View Source
const INFORMATION_SCHEMA_NOT_FOUND = "no such table: INFORMATION_SCHEMA.TABLES"
View Source
const SQLITE_TABLE_INFO_QUERY = `` /* 358-byte string literal not displayed */
Variables ¶
View Source
var ( // ErrExpectRecordSchema - ErrExpectRecordSchema = errors.New("ErrExpectRecordSchema") // ErrUnsupportedTypeForCriterion - ErrUnsupportedTypeForCriterion = errors.New("ErrUnsupportedTypeForCriterion") // ErrCriterionUnknownField - ErrCriterionUnknownField = errors.New("ErrCriterionUnknownField") // ErrCannotIgnoreOrder - ErrCannotIgnoreOrder = errors.New("ErrCannotIgnoreOrder") // ErrUnsupportedTypeForSQL - ErrUnsupportedTypeForSQL = errors.New("ErrUnsupportedTypeForSQL") // ErrQueryConfigMissingDB - ErrQueryConfigMissingDB = errors.New("ErrQueryConfigMissingDB") // ErrQueryConfigMissingDBName - ErrQueryConfigMissingDBName = errors.New("ErrQueryConfigMissingDBName") // ErrQueryConfigMissingSchema - ErrQueryConfigMissingSchema = errors.New("ErrQueryConfigMissingSchema") // ErrUnsupportedOutput - query doesn't supprot this output ErrUnsupportedOutput = errors.New("ErrUnsupportedOutput") )
Functions ¶
func EnsureCriterionTypes ¶
func EnsureCriterionTypes(schema *avro.RecordSchema, criteria []Criterion) (err error)
EnsureCriterionTypes - search the given schema to find & set criteria types
func GetSQLiteTables ¶
GetSQLiteTables - returns table names of the given SQLite database
func SQLDatabase2AVRO ¶
SQLDatabase2AVRO - fetch all tables of the given SQL database and translate them to avro schemas
Example ¶
package main import ( "database/sql" "encoding/json" "fmt" "github.com/britt/avro/sqlavro" ) func main() { db, err := sql.Open("mysql", "root@/blog") if err != nil { panic(err) } defer db.Close() _, err = db.Exec( `CREATE TABLE posts( ID INT NOT NULL, title VARCHAR(128) NOT NULL, body LONGBLOB NOT NULL, content_type VARCHAR(128) DEFAULT 'text/markdown; charset=UTF-8', post_date DATETIME NOT NULL, update_date DATETIME, reading_time_minutes DECIMAL(3,1), PRIMARY KEY(ID) )`, ) if err != nil { panic(err) } schemas, err := sqlavro.SQLDatabase2AVRO(db, "blog") if err != nil { panic(err) } schemasBytes, err := json.Marshal(schemas) if err != nil { panic(err) } fmt.Println(string(schemasBytes)) }
Output:
func SQLTable2AVRO ¶
SQLTable2AVRO - translate the given SQL table to AVRO schema
func SQLiteTable2AVRO ¶
SQLiteTable2AVRO - translate the given SQLite table to AVRO schema
func UnderlyingType ¶
func UnderlyingType(union avro.UnionSchema) (avro.Schema, error)
UnderlyingType -
Types ¶
type Criterion ¶
type Criterion struct { FieldName string `json:"field"` RawLimit *json.RawMessage `json:"limit,omitempty"` Order avro.Order `json:"order,omitempty"` // default: Ascending // contains filtered or unexported fields }
Criterion -
func NewCriterionDate ¶
NewCriterionDate -
func NewCriterionDateTime ¶
NewCriterionDateTime -
func NewCriterionFloat64 ¶
NewCriterionFloat64 -
func NewCriterionFromNative ¶
func NewCriterionFromNative(field *avro.RecordFieldSchema, value interface{}, order avro.Order) (*Criterion, error)
NewCriterionFromNative -
func NewCriterionFromString ¶
func NewCriterionFromString(field *avro.RecordFieldSchema, value string, order avro.Order) (*Criterion, error)
NewCriterionFromString -
func NewCriterionInt64 ¶
NewCriterionInt64 -
func NewCriterionString ¶
NewCriterionString -
func NewCriterionTime ¶
NewCriterionTime -
func Query ¶
func Query(cfg QueryConfig) (resultBytes []byte, newCriteria []Criterion, err error)
Query -
Example ¶
package main import ( "database/sql" "fmt" "io/ioutil" "time" "github.com/britt/avro" "github.com/britt/avro/sqlavro" ) func main() { db, err := sql.Open("mysql", "root@/blog") if err != nil { panic(err) } defer db.Close() _, err = db.Exec( `CREATE TABLE posts( ID INT NOT NULL, title VARCHAR(128) NOT NULL, body LONGBLOB NOT NULL, content_type VARCHAR(128) DEFAULT 'text/markdown; charset=UTF-8', post_date DATETIME NOT NULL, update_date DATETIME, reading_time_minutes DECIMAL(3,1), PRIMARY KEY(ID) )`, ) if err != nil { panic(err) } _, err = db.Exec( // statement `INSERT INTO posts(ID,title,body,content_type,post_date,update_date,reading_time_minutes) VALUES (?,?,?,?,?,?,?)`, // values 42, "lorem ispum", []byte(`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.`), "text/markdown; charset=UTF-8", "2009-04-10 00:00:00", "2009-04-10 00:00:00", "4.2", ) if err != nil { panic(err) } schema, err := sqlavro.SQLTable2AVRO(db, "blog", "posts") if err != nil { panic(err) } limit := 1000 order := avro.Ascending from, err := time.Parse("2006-02-01 15:04:05", "2009-04-10 00:00:00") if err != nil { panic(err) } avroBytes, updatedCriteria, err := sqlavro.Query(sqlavro.QueryConfig{ DB: db, DBName: "blog", Schema: schema, Limit: limit, Criteria: []sqlavro.Criterion{ *sqlavro.NewCriterionDateTime("post_date", &from, order), }, Output: "avro", }) if err != nil { panic(err) } err = ioutil.WriteFile("/tmp/blog_posts.avro", avroBytes, 0644) if err != nil { panic(err) } fmt.Println(updatedCriteria) }
Output:
type QueryConfig ¶
type QueryConfig struct { // DB - Required SQL connection pool used to access the database. DB *sql.DB // DBName - Required name of the database to select DBName string // Schema - Required avro Record Schema matching the table to query data from. Schema *avro.RecordSchema // Limit - Optional limit in the number of record to be retrieved. // 0(no limit) is used as default if not set Limit int // Criteria - Optional list of criterion to retreve data from. Criteria []Criterion // Compression - Optional name of the compression codec used to compress blocks // "null", "deflate" and snappy are accepted. // If the value is empty, it is assumed to be "null" Compression string // Output - define the desired format for the output // AVRO and CSV are supported // if not set, then AVRO is the default choice Output string // Separator - if you use CSV output format then // you might want to set the separator. // Default value is ';' Separator rune }
QueryConfig -
func (*QueryConfig) Verify ¶
func (qc *QueryConfig) Verify() error
Verify and ensure the config is valid
type SQLType ¶
type SQLType string
SQLType -
const ( // Char - Char SQLType = "char" // NChar - NChar SQLType = "nchar" // VarChar - VarChar SQLType = "varchar" // NVarChar - NVarChar SQLType = "nvarchar" // Text - Text SQLType = "text" // TinyText - TinyText SQLType = "tinytext" // MediumText - MediumText SQLType = "mdeiumtext" // LongText - LongText SQLType = "longtext" // Blob - Blob SQLType = "blob" // MediumBlob - MediumBlob SQLType = "mediumblob" // LongBlob - LongBlob SQLType = "longblob" // Enum - Enum SQLType = "enum" // Set - Set SQLType = "set" // TinyInt - TinyInt SQLType = "tinyint" // SmallInt - SmallInt SQLType = "smallint" // MediumInt - MediumInt SQLType = "mediumint" // Int - Int SQLType = "int" // BigInt - BigInt SQLType = "bigint" // Float - Float SQLType = "float" // Double - Double SQLType = "double" // Decimal - Decimal SQLType = "decimal" // Date - Date SQLType = "date" // DateTime - DateTime SQLType = "datetime" // Timestamp - Timestamp SQLType = "timestamp" // Time - Time SQLType = "time" // Year - Year SQLType = "year" // Bit - Bit SQLType = "bit" // SQLDateTimeFormat - SQLDateTimeFormat = "2006-01-02 15:04:05" // SQLDateFormat - SQLDateFormat = "2006-01-02" // SQLTimeFormat - SQLTimeFormat = "15:04:05" )
Source Files
¶
- column.go
- criterion.go
- criterion.type.go
- crtierion.factory.go
- database.go
- errors.go
- formatstr.go
- injection.go
- query.go
- query.render.column.go
- query.render.go
- query.result.avro.go
- query.result.csv.go
- query.row.native.go
- query.row.native.notnull.go
- query.row.native.nullable.go
- query.row.string.go
- query.row.string.notnull.go
- query.row.string.nullable.go
- sqltypes.go
- table.go
Click to show internal directories.
Click to hide internal directories.