diff options
author | Jochen Voss <voss@seehuhn.de> | 2013-06-20 21:52:38 +0100 |
---|---|---|
committer | mattn <mattn.jp@gmail.com> | 2013-08-13 21:45:05 +0900 |
commit | ff8e6729ce0628c3da97bd8e85c636f3645c2516 (patch) | |
tree | 33f84d46d24644dc2db3289b35ede1d154c7592d /sqlite3.go | |
parent | Upgrade amalgramation code (diff) | |
download | golite-ff8e6729ce0628c3da97bd8e85c636f3645c2516.tar.gz golite-ff8e6729ce0628c3da97bd8e85c636f3645c2516.tar.xz |
Start work on introducing machine-readable error codes.
This commit introduces a new type 'ErrNo', implementing the error
interface. Constants for all sqlite3 error codes are provided
in the new source file "error.go".
Diffstat (limited to 'sqlite3.go')
-rw-r--r-- | sqlite3.go | 26 |
1 files changed, 15 insertions, 11 deletions
@@ -132,7 +132,7 @@ func (c *SQLiteConn) exec(cmd string) error { defer C.free(unsafe.Pointer(pcmd)) rv := C.sqlite3_exec(c.db, pcmd, nil, nil, nil) if rv != C.SQLITE_OK { - return errors.New(C.GoString(C.sqlite3_errmsg(c.db))) + return ErrNo(rv) } return nil } @@ -145,6 +145,10 @@ func (c *SQLiteConn) Begin() (driver.Tx, error) { return &SQLiteTx{c}, nil } +func errorString(err ErrNo) string { + return C.GoString(C.sqlite3_errstr(C.int(err))) +} + // Open database and return a new connection. // You can specify DSN string with URI filename. // test.db @@ -165,7 +169,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { C.SQLITE_OPEN_CREATE, nil) if rv != 0 { - return nil, errors.New(C.GoString(C.sqlite3_errmsg(db))) + return nil, ErrNo(rv) } if db == nil { return nil, errors.New("sqlite succeeded without returning a database") @@ -173,7 +177,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { rv = C.sqlite3_busy_timeout(db, 5000) if rv != C.SQLITE_OK { - return nil, errors.New(C.GoString(C.sqlite3_errmsg(db))) + return nil, ErrNo(rv) } return &SQLiteConn{db}, nil @@ -188,7 +192,7 @@ func (c *SQLiteConn) Close() error { } rv := C.sqlite3_close(c.db) if rv != C.SQLITE_OK { - return errors.New("error while closing sqlite database connection") + return ErrNo(rv) } c.db = nil return nil @@ -202,7 +206,7 @@ func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) { var perror *C.char rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &perror) if rv != C.SQLITE_OK { - return nil, errors.New(C.GoString(C.sqlite3_errmsg(c.db))) + return nil, ErrNo(rv) } var t string if perror != nil && C.strlen(perror) > 0 { @@ -222,7 +226,7 @@ func (s *SQLiteStmt) Close() error { } rv := C.sqlite3_finalize(s.s) if rv != C.SQLITE_OK { - return errors.New(C.GoString(C.sqlite3_errmsg(s.c.db))) + return ErrNo(rv) } return nil } @@ -235,7 +239,7 @@ func (s *SQLiteStmt) NumInput() int { func (s *SQLiteStmt) bind(args []driver.Value) error { rv := C.sqlite3_reset(s.s) if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE { - return errors.New(C.GoString(C.sqlite3_errmsg(s.c.db))) + return ErrNo(rv) } for i, v := range args { @@ -280,7 +284,7 @@ func (s *SQLiteStmt) bind(args []driver.Value) error { rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b))) } if rv != C.SQLITE_OK { - return errors.New(C.GoString(C.sqlite3_errmsg(s.c.db))) + return ErrNo(rv) } } return nil @@ -311,7 +315,7 @@ func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) { } rv := C.sqlite3_step(s.s) if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE { - return nil, errors.New(C.GoString(C.sqlite3_errmsg(s.c.db))) + return nil, ErrNo(rv) } res := &SQLiteResult{ @@ -325,7 +329,7 @@ func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) { func (rc *SQLiteRows) Close() error { rv := C.sqlite3_reset(rc.s.s) if rv != C.SQLITE_OK { - return errors.New(C.GoString(C.sqlite3_errmsg(rc.s.c.db))) + return ErrNo(rv) } return nil } @@ -348,7 +352,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error { return io.EOF } if rv != C.SQLITE_ROW { - return errors.New(C.GoString(C.sqlite3_errmsg(rc.s.c.db))) + return ErrNo(rv) } if rc.decltype == nil { |