add test util

Related to issue #15
This commit is contained in:
Frank Braun 2020-08-28 18:11:58 +00:00
parent e09215ab7f
commit c9a3a061e2
2 changed files with 88 additions and 0 deletions

43
util/create.go Normal file
View File

@ -0,0 +1,43 @@
package main
import (
"database/sql"
"fmt"
"os"
_ "github.com/mutecomm/go-sqlcipher/v4"
)
func create(dbname, password string) error {
dbnameWithDSN := dbname + fmt.Sprintf("?_pragma_key=%s&_pragma_cipher_page_size=4096", password)
db, err := sql.Open("sqlite3", dbnameWithDSN)
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec("CREATE TABLE t1(a,b);")
if err != nil {
return err
}
_, err = db.Exec("INSERT INTO t1(a,b) values('one for the money', 'two for the show');")
return err
}
func fatal(err error) {
fmt.Fprintf(os.Stderr, "%s: error: %s\n", os.Args[0], err)
os.Exit(1)
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s db_file password\n", os.Args[0])
os.Exit(2)
}
func main() {
if len(os.Args) != 3 {
usage()
}
if err := create(os.Args[1], os.Args[2]); err != nil {
fatal(err)
}
}

45
util/select.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"database/sql"
"fmt"
"os"
_ "github.com/mutecomm/go-sqlcipher/v4"
)
func selectFromDB(dbname, password string) error {
dbnameWithDSN := dbname + fmt.Sprintf("?_pragma_key=%s&_pragma_cipher_page_size=4096", password)
db, err := sql.Open("sqlite3", dbnameWithDSN)
if err != nil {
return err
}
defer db.Close()
var a, b string
row := db.QueryRow("SELECT * FROM t1;")
err = row.Scan(&a, &b)
if err != nil {
return err
}
fmt.Printf("%s, %s\n", a, b)
return nil
}
func fatal(err error) {
fmt.Fprintf(os.Stderr, "%s: error: %s\n", os.Args[0], err)
os.Exit(1)
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s db_file password\n", os.Args[0])
os.Exit(2)
}
func main() {
if len(os.Args) != 3 {
usage()
}
if err := selectFromDB(os.Args[1], os.Args[2]); err != nil {
fatal(err)
}
}