diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:04 +0000 |
commit | b09c6d56832eb1718c07d74abf3bc6ae3fe4e030 (patch) | |
tree | d2caec2610d4ea887803ec9e9c3cd77136c448ba /dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/hook/hook.go | |
parent | Initial commit. (diff) | |
download | icingadb-b09c6d56832eb1718c07d74abf3bc6ae3fe4e030.tar.xz icingadb-b09c6d56832eb1718c07d74abf3bc6ae3fe4e030.zip |
Adding upstream version 1.1.0.upstream/1.1.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/hook/hook.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/hook/hook.go b/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/hook/hook.go new file mode 100644 index 0000000..6023181 --- /dev/null +++ b/dependencies/pkg/mod/github.com/mattn/go-sqlite3@v1.14.16/_example/hook/hook.go @@ -0,0 +1,78 @@ +package main + +import ( + "database/sql" + "log" + "os" + + "github.com/mattn/go-sqlite3" +) + +func main() { + sqlite3conn := []*sqlite3.SQLiteConn{} + sql.Register("sqlite3_with_hook_example", + &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") + + srcDb, err := sql.Open("sqlite3_with_hook_example", "./foo.db") + if err != nil { + log.Fatal(err) + } + defer srcDb.Close() + srcDb.Ping() + + _, err = srcDb.Exec("create table foo(id int, value text)") + if err != nil { + log.Fatal(err) + } + _, err = srcDb.Exec("insert into foo values(1, 'foo')") + if err != nil { + log.Fatal(err) + } + _, err = srcDb.Exec("insert into foo values(2, 'bar')") + if err != nil { + log.Fatal(err) + } + _, err = srcDb.Query("select * from foo") + if err != nil { + log.Fatal(err) + } + destDb, err := sql.Open("sqlite3_with_hook_example", "./bar.db") + if err != nil { + log.Fatal(err) + } + defer destDb.Close() + destDb.Ping() + + bk, err := sqlite3conn[1].Backup("main", sqlite3conn[0], "main") + if err != nil { + log.Fatal(err) + } + + _, err = bk.Step(-1) + if err != nil { + log.Fatal(err) + } + _, err = destDb.Query("select * from foo") + if err != nil { + log.Fatal(err) + } + _, err = destDb.Exec("insert into foo values(3, 'bar')") + if err != nil { + log.Fatal(err) + } + + bk.Finish() +} |