Skip to content

Commit

Permalink
Minor changes related to error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
feyeleanor committed Feb 18, 2012
1 parent d92bebf commit 2d7f557
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 30 deletions.
32 changes: 11 additions & 21 deletions backup.go
Expand Up @@ -20,27 +20,23 @@ type Backup struct {

// NewBackup initializes and returns the handle to a backup.
func NewBackup(d *Database, ddb string, s *Database, sdb string) (b *Backup, e error) {
dcs := C.CString(ddb)
defer C.free(unsafe.Pointer(dcs))
scs := C.CString(sdb)
defer C.free(unsafe.Pointer(scs))
if cptr := C.sqlite3_backup_init(d.handle, dcs, s.handle, scs); cptr != nil {
dname := C.CString(ddb)
defer C.free(unsafe.Pointer(dname))
sname := C.CString(sdb)
defer C.free(unsafe.Pointer(sname))

if cptr := C.sqlite3_backup_init(d.handle, dname, s.handle, sname); cptr != nil {
b = &Backup{cptr: cptr, db: d}
} else {
if e = d.Error(); e == OK {
e = nil
}
e = d.Error()
}
return
}

// Step will copy up to `pages` between the source and destination database.
// If `pages` is negative, all remaining source pages are copied.
func (b *Backup) Step(pages int) error {
if e := Errno(C.sqlite3_backup_step(b.cptr, C.int(pages))); e != OK {
return e
}
return nil
return SQLiteError(C.sqlite3_backup_step(b.cptr, C.int(pages)))
}

// Remaining returns the number of pages still to be backed up.
Expand All @@ -56,18 +52,12 @@ func (b *Backup) PageCount() int {
// Finish should be called when the backup is done, an error occured or when
// the application wants to abandon the backup operation.
func (b *Backup) Finish() error {
if e := Errno(C.sqlite3_backup_finish(b.cptr)); e != OK {
return e
}
return nil
return SQLiteError(C.sqlite3_backup_finish(b.cptr))
}

// Full creates a full backup of the database.
func (b *Backup) Full() error {
b.Step(-1)
b.Finish()
if e := b.db.Error(); e != OK {
return e
}
return nil
}
return b.db.Error()
}
7 changes: 1 addition & 6 deletions database.go
Expand Up @@ -141,11 +141,6 @@ func (db *Database) Open(flags ...int) (e error) {
defer C.free(unsafe.Pointer(cs))
e = SQLiteError(C.sqlite3_open_v2(cs, &db.handle, db.Flags, nil))

// if err :; err != OK {
// e = err
// } else if db.handle == nil {
// e = CANTOPEN
// }
if e == nil && db.handle == nil {
e = CANTOPEN
}
Expand Down Expand Up @@ -201,7 +196,7 @@ func (db *Database) TotalChanges() int {
// Error returns the numeric result code for the most recently failed database
// call.
func (db *Database) Error() error {
return Errno(C.sqlite3_errcode(db.handle))
return SQLiteError(C.sqlite3_errcode(db.handle))
}

// Prepare compiles the SQL query into a byte-code program and binds the
Expand Down
6 changes: 3 additions & 3 deletions database_test.go
Expand Up @@ -62,9 +62,9 @@ func TestTransfers(t *testing.T) {
func (r *Reporter) finished(t *testing.T) bool {
report, ok := <- (*r)
if report != nil {
switch e := report.Error.(type) {
case Errno: if e != DONE { t.Fatalf("Backup error %v", e) }
// case nil: t.Logf("Backup still has %v pages of %v to copy to %v", report.Remaining, report.PageCount, report.Target)
switch e := report.Error; {
case e == nil: t.Logf("Backup still has %v pages of %v to copy to %v", report.Remaining, report.Total, report.Target)
case e != DONE: t.Fatalf("Backup error %v", e)
}
}
return !ok
Expand Down

0 comments on commit 2d7f557

Please sign in to comment.