Backup functions support? #104

Closed
jamiefoster opened this Issue Jan 22, 2014 · 17 comments

Comments

Projects
None yet
3 participants

Do you have plans to add backup functions support?

C.sqlite3_backup_init
C.sqlite3_backup_step
C.sqlite3_backup_finish

Here is an example package that supports that:
https://code.google.com/p/go-sqlite/source/browse/go1/sqlite3/backup.go

Any thoughts on this one?

Owner

mattn commented Jan 24, 2014

when using go-sqlite3 as driver of database/sql, how do you thinkg the way to use?

That's a very good question. As I see from the source, the SQLiteConn structure is storing the C SQLITE database handle. The db.Open function returns an SQLiteConn structure. Maybe adding the backup functionality to SQLiteConn would do it?

Later edit: As db.Open is returning an *DB handle, I guess the challenge is how to use the backup functions from that DB handle, right?

Owner

mattn commented Jan 24, 2014

db.Conn is covered up by database/sql package for that doesn't call something of drivers directly.

And in your case, driver.Conn is a SQLiteConn, right? (https://github.com/mattn/go-sqlite3/blob/afb81456924a13f8a8653ac8a8b8d017e8ba37f0/sqlite3.go#L262)

Couldn't we use something like:

db, err := sql.Open("sqlite3", "./foo.db")
mySQLiteConn, ok := db.(*SQLiteConn)
if ok { ... }

to convert db to a *SQLiteConn (https://github.com/mattn/go-sqlite3/blob/afb81456924a13f8a8653ac8a8b8d017e8ba37f0/sqlite3.go#L89)

Then we could use the db field from:

type SQLiteConn struct {
        db *C.sqlite3
}

And pass it to the backup functions like C.sqlite3_backup_init?

Owner

mattn commented Jan 24, 2014

Couldn't we use something like:

No, because driver.Conn is not DB.

https://code.google.com/p/go/source/browse/src/pkg/database/sql/sql.go#411

conn is wrapped with DB

I see. Could the Exec function be used with special parameters in order to call the backup functions?

Owner

mattn commented Jan 24, 2014

How about driver's option?

See https://github.com/mattn/go-sqlite3/blob/master/_example/mod_regexp/extension.go#L12

For example

        sql.Register("sqlite3_with_extensions",
                &sqlite3.SQLiteDriver{
                        Backup: true,
                })

That sounds interesting! Could we get the SQLiteConn from that hook function? Do you have any sample on how that code would look like?

Owner

mattn commented Jan 24, 2014

Currently, BackupInit/BackupStep/BackupFinsih isn't implemented.

package main

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

func main() {
    var sqlite3conn *sqlite3.SQLiteConn
    sql.Register("sqlite3_with_hook_example",
            &sqlite3.SQLiteDriver{
                    ConnectHook: func(conn *sqlite3.SQLiteConn) error {
                        sqlite3conn = conn
                        return nil
                    },
            })
    os.Remove("./foo.db")

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

    sqlite3conn.BackupInit()

    // Do Something

    for rows.Next() {
        var id int
        var name string
        rows.Scan(&id, &name)
        fmt.Println(id, name)

        sqlite3conn.BackupStep(...)
    }

    sqlite3conn.BackupFinish()
}

That looks great! Can't wait to try it.

Thanks @mattn! You're the best!

mattn closed this in e52d4b0 Jan 30, 2014

Owner

mattn commented Jan 30, 2014

Added backup feature. See _example/hook/hook.go

dinedal commented May 27, 2014

backup.Step(-1) always returns an error, but appears to work?

Owner

mattn commented May 27, 2014

Ah, sorry.
always return error... I'll fix soon.

e52d4b0#diff-2489c938a3abbd8b8ac7d9d34402a8e4R29

dinedal commented May 27, 2014

Also, backup.Finish() always returns "not an error"

dinedal commented May 27, 2014

Thanks @mattn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment