podman
150 строк · 6.0 Кб
1// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
2//
3// Use of this source code is governed by an MIT-style
4// license that can be found in the LICENSE file.
5
6package sqlite3
7
8/*
9#ifndef USE_LIBSQLITE3
10#include "sqlite3-binding.h"
11#else
12#include <sqlite3.h>
13#endif
14*/
15import "C"
16import "syscall"
17
18// ErrNo inherit errno.
19type ErrNo int
20
21// ErrNoMask is mask code.
22const ErrNoMask C.int = 0xff
23
24// ErrNoExtended is extended errno.
25type ErrNoExtended int
26
27// Error implement sqlite error code.
28type Error struct {
29Code ErrNo /* The error code returned by SQLite */
30ExtendedCode ErrNoExtended /* The extended error code returned by SQLite */
31SystemErrno syscall.Errno /* The system errno returned by the OS through SQLite, if applicable */
32err string /* The error string returned by sqlite3_errmsg(),
33this usually contains more specific details. */
34}
35
36// result codes from http://www.sqlite.org/c3ref/c_abort.html
37var (
38ErrError = ErrNo(1) /* SQL error or missing database */
39ErrInternal = ErrNo(2) /* Internal logic error in SQLite */
40ErrPerm = ErrNo(3) /* Access permission denied */
41ErrAbort = ErrNo(4) /* Callback routine requested an abort */
42ErrBusy = ErrNo(5) /* The database file is locked */
43ErrLocked = ErrNo(6) /* A table in the database is locked */
44ErrNomem = ErrNo(7) /* A malloc() failed */
45ErrReadonly = ErrNo(8) /* Attempt to write a readonly database */
46ErrInterrupt = ErrNo(9) /* Operation terminated by sqlite3_interrupt() */
47ErrIoErr = ErrNo(10) /* Some kind of disk I/O error occurred */
48ErrCorrupt = ErrNo(11) /* The database disk image is malformed */
49ErrNotFound = ErrNo(12) /* Unknown opcode in sqlite3_file_control() */
50ErrFull = ErrNo(13) /* Insertion failed because database is full */
51ErrCantOpen = ErrNo(14) /* Unable to open the database file */
52ErrProtocol = ErrNo(15) /* Database lock protocol error */
53ErrEmpty = ErrNo(16) /* Database is empty */
54ErrSchema = ErrNo(17) /* The database schema changed */
55ErrTooBig = ErrNo(18) /* String or BLOB exceeds size limit */
56ErrConstraint = ErrNo(19) /* Abort due to constraint violation */
57ErrMismatch = ErrNo(20) /* Data type mismatch */
58ErrMisuse = ErrNo(21) /* Library used incorrectly */
59ErrNoLFS = ErrNo(22) /* Uses OS features not supported on host */
60ErrAuth = ErrNo(23) /* Authorization denied */
61ErrFormat = ErrNo(24) /* Auxiliary database format error */
62ErrRange = ErrNo(25) /* 2nd parameter to sqlite3_bind out of range */
63ErrNotADB = ErrNo(26) /* File opened that is not a database file */
64ErrNotice = ErrNo(27) /* Notifications from sqlite3_log() */
65ErrWarning = ErrNo(28) /* Warnings from sqlite3_log() */
66)
67
68// Error return error message from errno.
69func (err ErrNo) Error() string {
70return Error{Code: err}.Error()
71}
72
73// Extend return extended errno.
74func (err ErrNo) Extend(by int) ErrNoExtended {
75return ErrNoExtended(int(err) | (by << 8))
76}
77
78// Error return error message that is extended code.
79func (err ErrNoExtended) Error() string {
80return Error{Code: ErrNo(C.int(err) & ErrNoMask), ExtendedCode: err}.Error()
81}
82
83func (err Error) Error() string {
84var str string
85if err.err != "" {
86str = err.err
87} else {
88str = C.GoString(C.sqlite3_errstr(C.int(err.Code)))
89}
90if err.SystemErrno != 0 {
91str += ": " + err.SystemErrno.Error()
92}
93return str
94}
95
96// result codes from http://www.sqlite.org/c3ref/c_abort_rollback.html
97var (
98ErrIoErrRead = ErrIoErr.Extend(1)
99ErrIoErrShortRead = ErrIoErr.Extend(2)
100ErrIoErrWrite = ErrIoErr.Extend(3)
101ErrIoErrFsync = ErrIoErr.Extend(4)
102ErrIoErrDirFsync = ErrIoErr.Extend(5)
103ErrIoErrTruncate = ErrIoErr.Extend(6)
104ErrIoErrFstat = ErrIoErr.Extend(7)
105ErrIoErrUnlock = ErrIoErr.Extend(8)
106ErrIoErrRDlock = ErrIoErr.Extend(9)
107ErrIoErrDelete = ErrIoErr.Extend(10)
108ErrIoErrBlocked = ErrIoErr.Extend(11)
109ErrIoErrNoMem = ErrIoErr.Extend(12)
110ErrIoErrAccess = ErrIoErr.Extend(13)
111ErrIoErrCheckReservedLock = ErrIoErr.Extend(14)
112ErrIoErrLock = ErrIoErr.Extend(15)
113ErrIoErrClose = ErrIoErr.Extend(16)
114ErrIoErrDirClose = ErrIoErr.Extend(17)
115ErrIoErrSHMOpen = ErrIoErr.Extend(18)
116ErrIoErrSHMSize = ErrIoErr.Extend(19)
117ErrIoErrSHMLock = ErrIoErr.Extend(20)
118ErrIoErrSHMMap = ErrIoErr.Extend(21)
119ErrIoErrSeek = ErrIoErr.Extend(22)
120ErrIoErrDeleteNoent = ErrIoErr.Extend(23)
121ErrIoErrMMap = ErrIoErr.Extend(24)
122ErrIoErrGetTempPath = ErrIoErr.Extend(25)
123ErrIoErrConvPath = ErrIoErr.Extend(26)
124ErrLockedSharedCache = ErrLocked.Extend(1)
125ErrBusyRecovery = ErrBusy.Extend(1)
126ErrBusySnapshot = ErrBusy.Extend(2)
127ErrCantOpenNoTempDir = ErrCantOpen.Extend(1)
128ErrCantOpenIsDir = ErrCantOpen.Extend(2)
129ErrCantOpenFullPath = ErrCantOpen.Extend(3)
130ErrCantOpenConvPath = ErrCantOpen.Extend(4)
131ErrCorruptVTab = ErrCorrupt.Extend(1)
132ErrReadonlyRecovery = ErrReadonly.Extend(1)
133ErrReadonlyCantLock = ErrReadonly.Extend(2)
134ErrReadonlyRollback = ErrReadonly.Extend(3)
135ErrReadonlyDbMoved = ErrReadonly.Extend(4)
136ErrAbortRollback = ErrAbort.Extend(2)
137ErrConstraintCheck = ErrConstraint.Extend(1)
138ErrConstraintCommitHook = ErrConstraint.Extend(2)
139ErrConstraintForeignKey = ErrConstraint.Extend(3)
140ErrConstraintFunction = ErrConstraint.Extend(4)
141ErrConstraintNotNull = ErrConstraint.Extend(5)
142ErrConstraintPrimaryKey = ErrConstraint.Extend(6)
143ErrConstraintTrigger = ErrConstraint.Extend(7)
144ErrConstraintUnique = ErrConstraint.Extend(8)
145ErrConstraintVTab = ErrConstraint.Extend(9)
146ErrConstraintRowID = ErrConstraint.Extend(10)
147ErrNoticeRecoverWAL = ErrNotice.Extend(1)
148ErrNoticeRecoverRollback = ErrNotice.Extend(2)
149ErrWarningAutoIndex = ErrWarning.Extend(1)
150)
151