add basic read/execute loop

This commit is contained in:
Antoine Beaupré 2018-03-12 16:38:09 -04:00
parent beacf72c77
commit 45937002d9
No known key found for this signature in database
GPG Key ID: 3EA1DDDDB261D97B
1 changed files with 23 additions and 2 deletions

View File

@ -1,25 +1,46 @@
package main package main
import ( import (
"bufio"
"database/sql" "database/sql"
"fmt" "fmt"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"log" "log"
"os" "os"
"strings"
) )
func main() { func main() {
log.Printf("args: %d\n", len(os.Args))
if len(os.Args) < 2 { if len(os.Args) < 2 {
log.Fatal("not enough arguments") log.Fatal("not enough arguments")
} }
path := os.Args[1] path := os.Args[1]
query := os.Args[2]
db, err := sql.Open("sqlite3", path) db, err := sql.Open("sqlite3", path)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer db.Close() defer db.Close()
runQuery(db, query) if len(os.Args) < 3 {
dumbShell(db)
} else {
query := os.Args[2]
runQuery(db, query)
}
}
func dumbShell(db *sql.DB) {
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print("sql> ")
var query string
query, _ = reader.ReadString('\n')
//fmt.Println(query)
query = strings.TrimSpace(query)
if len(query) > 0 {
runQuery(db, query)
}
}
} }
func runQuery(db *sql.DB, query string) (err error) { func runQuery(db *sql.DB, query string) (err error) {