Skip to content

Commit

Permalink
Upgrade to the latest sqlcipher.
Browse files Browse the repository at this point in the history
  • Loading branch information
xeodou committed May 23, 2018
2 parents cc62571 + a72efd6 commit 04d43c0
Show file tree
Hide file tree
Showing 35 changed files with 35,704 additions and 13,643 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
*.db
*.exe
*.dll
*.o
14 changes: 10 additions & 4 deletions .travis.yml
@@ -1,13 +1,19 @@
language: go
sudo: required
dist: trusty
env:
- GOTAGS=
- GOTAGS=libsqlite3
- GOTAGS=trace
- GOTAGS=vtable
go:
- 1.5
- 1.6
- tip
- 1.7.x
- 1.8.x
- 1.9.x
- master
before_install:
- go get github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover
script:
- $HOME/gopath/bin/goveralls -repotoken 3qJVUE0iQwqnCbmNcDsjYu1nh4J4KIFXx
- go test -v . -tags "libsqlite3"
- go test -race -v . -tags "$GOTAGS"
27 changes: 21 additions & 6 deletions README.md
Expand Up @@ -6,10 +6,10 @@ SQLCipher driver conforming to the built-in database/sql interface and using the


which is
`3.8.8.3 2015-02-25 13:29:11 9d6c1880fb75660bbabd693175579529785f8a6b`
`3.20.1`

Working with sqlcipher version which is
`3.8.6 2014-08-15 11:46:33 9491ba7d738528f168657adb43a198238abde19e`
`3.4.2`

It's wrapper with
* [go-sqlite3](https://github.com/mattn/go-sqlite3) sqlite3 driver for go that using database/sql.
Expand Down Expand Up @@ -43,7 +43,7 @@ This package can be installed with the go get command:
_go-sqlcipher_ is *cgo* package.
If you want to build your app using go-sqlcipher, you need gcc.
However, if you install _go-sqlcipher_ with `go install github.com/xeodou/go-sqlcipher`, you don't need gcc to build your app anymore.

Documentation
-------------

Expand Down Expand Up @@ -73,6 +73,8 @@ Here is some help from go-sqlite3 project.

Use `go build --tags "icu"`

Available extensions: `json1`, `fts5`, `icu`

* Can't build go-sqlite3 on windows 64bit.

> Probably, you are using go 1.0, go1.0 has a problem when it comes to compiling/linking on windows 64bit.
Expand All @@ -81,16 +83,29 @@ Here is some help from go-sqlite3 project.
* Getting insert error while query is opened.

> You can pass some arguments into the connection string, for example, a URI.
> See: https://github.com/mattn/go-sqlite3/issues/39
> See: [#39](https://github.com/mattn/go-sqlite3/issues/39)
* Do you want to cross compile? mingw on Linux or Mac?

> See: https://github.com/mattn/go-sqlite3/issues/106
> See: [#106](https://github.com/mattn/go-sqlite3/issues/106)
> See also: http://www.limitlessfx.com/cross-compile-golang-app-for-windows-from-linux.html
* Want to get time.Time with current locale

Use `loc=auto` in SQLite3 filename schema like `file:foo.db?loc=auto`.
Use `_loc=auto` in SQLite3 filename schema like `file:foo.db?_loc=auto`.

* Can I use this in multiple routines concurrently?

Yes for readonly. But, No for writable. See [#50](https://github.com/mattn/go-sqlite3/issues/50), [#51](https://github.com/mattn/go-sqlite3/issues/51), [#209](https://github.com/mattn/go-sqlite3/issues/209), [#274](https://github.com/mattn/go-sqlite3/issues/274).

* Why is it racy if I use a `sql.Open("sqlite3", ":memory:")` database?

Each connection to :memory: opens a brand new in-memory sql database, so if
the stdlib's sql engine happens to open another connection and you've only
specified ":memory:", that connection will see a brand new database. A
workaround is to use "file::memory:?mode=memory&cache=shared". Every
connection to this string will point to the same in-memory database. See
[#204](https://github.com/mattn/go-sqlite3/issues/204) for more info.

* Print some waring messages like `warning: 'RAND_add' is deprecated: first deprecated in OS X 10.7`

Expand Down
2 changes: 2 additions & 0 deletions _example/encrypto/encrypto.go
Expand Up @@ -9,6 +9,7 @@ package main
import (
"database/sql"
"fmt"

_ "github.com/xeodou/go-sqlcipher"
)

Expand All @@ -24,6 +25,7 @@ func main() {
if err != nil {
fmt.Println(err)
}

c := "CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY, `name` char, `password` chart, UNIQUE(`name`));"
_, err = db.Exec(c)
if err != nil {
Expand Down
29 changes: 18 additions & 11 deletions _example/hook/hook.go
Expand Up @@ -2,9 +2,10 @@ package main

import (
"database/sql"
"github.com/mattn/go-sqlite3"
"log"
"os"

"github.com/mattn/go-sqlite3"
)

func main() {
Expand All @@ -13,41 +14,47 @@ func main() {
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
sqlite3conn = append(sqlite3conn, conn)
conn.RegisterUpdateHook(func(op int, db string, table string, rowid int64) {
switch op {
case sqlite3.SQLITE_INSERT:
log.Println("Notified of insert on db", db, "table", table, "rowid", rowid)
}
})
return nil
},
})
os.Remove("./foo.db")
os.Remove("./bar.db")

destDb, err := sql.Open("sqlite3_with_hook_example", "./foo.db")
srcDb, err := sql.Open("sqlite3_with_hook_example", "./foo.db")
if err != nil {
log.Fatal(err)
}
defer destDb.Close()
destDb.Ping()
defer srcDb.Close()
srcDb.Ping()

_, err = destDb.Exec("create table foo(id int, value text)")
_, err = srcDb.Exec("create table foo(id int, value text)")
if err != nil {
log.Fatal(err)
}
_, err = destDb.Exec("insert into foo values(1, 'foo')")
_, err = srcDb.Exec("insert into foo values(1, 'foo')")
if err != nil {
log.Fatal(err)
}
_, err = destDb.Exec("insert into foo values(2, 'bar')")
_, err = srcDb.Exec("insert into foo values(2, 'bar')")
if err != nil {
log.Fatal(err)
}
_, err = destDb.Query("select * from foo")
_, err = srcDb.Query("select * from foo")
if err != nil {
log.Fatal(err)
}
srcDb, err := sql.Open("sqlite3_with_hook_example", "./bar.db")
destDb, err := sql.Open("sqlite3_with_hook_example", "./bar.db")
if err != nil {
log.Fatal(err)
}
defer srcDb.Close()
srcDb.Ping()
defer destDb.Close()
destDb.Ping()

bk, err := sqlite3conn[1].Backup("main", sqlite3conn[0], "main")
if err != nil {
Expand Down
113 changes: 113 additions & 0 deletions _example/limit/limit.go
@@ -0,0 +1,113 @@
package main

import (
"database/sql"
"fmt"
"log"
"os"
"strings"

"github.com/mattn/go-sqlite3"
)

func createBulkInsertQuery(n int, start int) (query string, args []interface{}) {
values := make([]string, n)
args = make([]interface{}, n*2)
pos := 0
for i := 0; i < n; i++ {
values[i] = "(?, ?)"
args[pos] = start + i
args[pos+1] = fmt.Sprintf("こんにちわ世界%03d", i)
pos += 2
}
query = fmt.Sprintf(
"insert into foo(id, name) values %s",
strings.Join(values, ", "),
)
return
}

func bukInsert(db *sql.DB, query string, args []interface{}) (err error) {
stmt, err := db.Prepare(query)
if err != nil {
return
}

_, err = stmt.Exec(args...)
if err != nil {
return
}

return
}

func main() {
var sqlite3conn *sqlite3.SQLiteConn
sql.Register("sqlite3_with_limit", &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
sqlite3conn = conn
return nil
},
})

os.Remove("./foo.db")
db, err := sql.Open("sqlite3_with_limit", "./foo.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()

sqlStmt := `
create table foo (id integer not null primary key, name text);
delete from foo;
`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return
}

if sqlite3conn == nil {
log.Fatal("not set sqlite3 connection")
}

limitVariableNumber := sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
log.Printf("default SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber)

num := 400
query, args := createBulkInsertQuery(num, 0)
err = bukInsert(db, query, args)
if err != nil {
log.Fatal(err)
}

smallLimitVariableNumber := 100
sqlite3conn.SetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, smallLimitVariableNumber)

limitVariableNumber = sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
log.Printf("updated SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber)

query, args = createBulkInsertQuery(num, num)
err = bukInsert(db, query, args)
if err != nil {
if err != nil {
log.Printf("expect failed since SQLITE_LIMIT_VARIABLE_NUMBER is too small: %v", err)
}
}

bigLimitVariableNumber := 999999
sqlite3conn.SetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, bigLimitVariableNumber)
limitVariableNumber = sqlite3conn.GetLimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
log.Printf("set SQLITE_LIMIT_VARIABLE_NUMBER: %d", bigLimitVariableNumber)
log.Printf("updated SQLITE_LIMIT_VARIABLE_NUMBER: %d", limitVariableNumber)

query, args = createBulkInsertQuery(500, num+num)
err = bukInsert(db, query, args)
if err != nil {
if err != nil {
log.Fatal(err)
}
}

log.Println("no error if SQLITE_LIMIT_VARIABLE_NUMBER > 999")
}
9 changes: 5 additions & 4 deletions _example/mod_vtable/extension.go
Expand Up @@ -3,8 +3,9 @@ package main
import (
"database/sql"
"fmt"
"github.com/mattn/go-sqlite3"
"log"

"github.com/mattn/go-sqlite3"
)

func main() {
Expand All @@ -29,8 +30,8 @@ func main() {
}
defer rows.Close()
for rows.Next() {
var id, full_name, description, html_url string
rows.Scan(&id, &full_name, &description, &html_url)
fmt.Printf("%s: %s\n\t%s\n\t%s\n\n", id, full_name, description, html_url)
var id, fullName, description, htmlURL string
rows.Scan(&id, &fullName, &description, &htmlURL)
fmt.Printf("%s: %s\n\t%s\n\t%s\n\n", id, fullName, description, htmlURL)
}
}

0 comments on commit 04d43c0

Please sign in to comment.