From beacf72c77c536c36cf70d150c970dca13673b80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Mon, 12 Mar 2018 16:26:15 -0400 Subject: [PATCH] first prototype of an actual commandline tool --- cmd/go-sqlite3/main.go | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 cmd/go-sqlite3/main.go diff --git a/cmd/go-sqlite3/main.go b/cmd/go-sqlite3/main.go new file mode 100644 index 0000000..c7412d1 --- /dev/null +++ b/cmd/go-sqlite3/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "database/sql" + "fmt" + _ "github.com/mattn/go-sqlite3" + "log" + "os" +) + +func main() { + if len(os.Args) < 2 { + log.Fatal("not enough arguments") + } + path := os.Args[1] + query := os.Args[2] + db, err := sql.Open("sqlite3", path) + if err != nil { + log.Fatal(err) + } + defer db.Close() + runQuery(db, query) +} + +func runQuery(db *sql.DB, query string) (err error) { + rows, err := db.Query(query) + if err != nil { + log.Println(err) + return err + } + cols, err := rows.ColumnTypes() + if err != nil { + log.Println(err) + return err + } + // http://go-database-sql.org/varcols.html + vals := make([]interface{}, len(cols)) + for i, types := range cols { + vals[i] = new(sql.RawBytes) + fmt.Printf("%-50s\t", types.Name()) + /* debugging... + length, ok := types.Length() + if !ok { + length = 0 + } + fmt.Printf("%s[%s]\t", types.DatabaseTypeName(), length) + */ + } + fmt.Printf("\n") + for rows.Next() { + if err := rows.Scan(vals...); err != nil { + log.Println(err) + return err + } + // Now you can check each element of vals for nil-ness, + // and you can use type introspection and type assertions + // to fetch the column into a typed variable. + for i, _ := range vals { + if vals[i] == nil { + fmt.Printf("%-50s\t", "NULL") + } else { + fmt.Printf("%-50s\t", vals[i]) + } + } + fmt.Printf("\n") + } + if err := rows.Err(); err != nil { + log.Println(err) + } + return err +}