Skip to content

Commit d2f1629

Browse files
authored
More type safe. (#216)
1 parent e2da469 commit d2f1629

39 files changed

+865
-855
lines changed

backup.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package sqlite3
55
// https://sqlite.org/c3ref/backup.html
66
type Backup struct {
77
c *Conn
8-
handle uint32
9-
otherc uint32
8+
handle ptr_t
9+
otherc ptr_t
1010
}
1111

1212
// Backup backs up srcDB on the src connection to the "main" database in dstURI.
@@ -61,7 +61,7 @@ func (src *Conn) BackupInit(srcDB, dstURI string) (*Backup, error) {
6161
return src.backupInit(dst, "main", src.handle, srcDB)
6262
}
6363

64-
func (c *Conn) backupInit(dst uint32, dstName string, src uint32, srcName string) (*Backup, error) {
64+
func (c *Conn) backupInit(dst ptr_t, dstName string, src ptr_t, srcName string) (*Backup, error) {
6565
defer c.arena.mark()()
6666
dstPtr := c.arena.string(dstName)
6767
srcPtr := c.arena.string(srcName)
@@ -71,19 +71,19 @@ func (c *Conn) backupInit(dst uint32, dstName string, src uint32, srcName string
7171
other = src
7272
}
7373

74-
r := c.call("sqlite3_backup_init",
75-
uint64(dst), uint64(dstPtr),
76-
uint64(src), uint64(srcPtr))
77-
if r == 0 {
74+
ptr := ptr_t(c.call("sqlite3_backup_init",
75+
stk_t(dst), stk_t(dstPtr),
76+
stk_t(src), stk_t(srcPtr)))
77+
if ptr == 0 {
7878
defer c.closeDB(other)
79-
r = c.call("sqlite3_errcode", uint64(dst))
80-
return nil, c.sqlite.error(r, dst)
79+
rc := res_t(c.call("sqlite3_errcode", stk_t(dst)))
80+
return nil, c.sqlite.error(rc, dst)
8181
}
8282

8383
return &Backup{
8484
c: c,
8585
otherc: other,
86-
handle: uint32(r),
86+
handle: ptr,
8787
}, nil
8888
}
8989

@@ -97,38 +97,38 @@ func (b *Backup) Close() error {
9797
return nil
9898
}
9999

100-
r := b.c.call("sqlite3_backup_finish", uint64(b.handle))
100+
rc := res_t(b.c.call("sqlite3_backup_finish", stk_t(b.handle)))
101101
b.c.closeDB(b.otherc)
102102
b.handle = 0
103-
return b.c.error(r)
103+
return b.c.error(rc)
104104
}
105105

106106
// Step copies up to nPage pages between the source and destination databases.
107107
// If nPage is negative, all remaining source pages are copied.
108108
//
109109
// https://sqlite.org/c3ref/backup_finish.html#sqlite3backupstep
110110
func (b *Backup) Step(nPage int) (done bool, err error) {
111-
r := b.c.call("sqlite3_backup_step", uint64(b.handle), uint64(nPage))
112-
if r == _DONE {
111+
rc := res_t(b.c.call("sqlite3_backup_step", stk_t(b.handle), stk_t(nPage)))
112+
if rc == _DONE {
113113
return true, nil
114114
}
115-
return false, b.c.error(r)
115+
return false, b.c.error(rc)
116116
}
117117

118118
// Remaining returns the number of pages still to be backed up
119119
// at the conclusion of the most recent [Backup.Step].
120120
//
121121
// https://sqlite.org/c3ref/backup_finish.html#sqlite3backupremaining
122122
func (b *Backup) Remaining() int {
123-
r := b.c.call("sqlite3_backup_remaining", uint64(b.handle))
124-
return int(int32(r))
123+
n := int32(b.c.call("sqlite3_backup_remaining", stk_t(b.handle)))
124+
return int(n)
125125
}
126126

127127
// PageCount returns the total number of pages in the source database
128128
// at the conclusion of the most recent [Backup.Step].
129129
//
130130
// https://sqlite.org/c3ref/backup_finish.html#sqlite3backuppagecount
131131
func (b *Backup) PageCount() int {
132-
r := b.c.call("sqlite3_backup_pagecount", uint64(b.handle))
133-
return int(int32(r))
132+
n := int32(b.c.call("sqlite3_backup_pagecount", stk_t(b.handle)))
133+
return int(n)
134134
}

blob.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ type Blob struct {
2020
c *Conn
2121
bytes int64
2222
offset int64
23-
handle uint32
24-
bufptr uint32
23+
handle ptr_t
24+
bufptr ptr_t
2525
buflen int64
2626
}
2727

@@ -37,23 +37,23 @@ func (c *Conn) OpenBlob(db, table, column string, row int64, write bool) (*Blob,
3737
tablePtr := c.arena.string(table)
3838
columnPtr := c.arena.string(column)
3939

40-
var flags uint64
40+
var flags int32
4141
if write {
4242
flags = 1
4343
}
4444

4545
c.checkInterrupt(c.handle)
46-
r := c.call("sqlite3_blob_open", uint64(c.handle),
47-
uint64(dbPtr), uint64(tablePtr), uint64(columnPtr),
48-
uint64(row), flags, uint64(blobPtr))
46+
rc := res_t(c.call("sqlite3_blob_open", stk_t(c.handle),
47+
stk_t(dbPtr), stk_t(tablePtr), stk_t(columnPtr),
48+
stk_t(row), stk_t(flags), stk_t(blobPtr)))
4949

50-
if err := c.error(r); err != nil {
50+
if err := c.error(rc); err != nil {
5151
return nil, err
5252
}
5353

5454
blob := Blob{c: c}
55-
blob.handle = util.ReadUint32(c.mod, blobPtr)
56-
blob.bytes = int64(c.call("sqlite3_blob_bytes", uint64(blob.handle)))
55+
blob.handle = util.Read32[ptr_t](c.mod, blobPtr)
56+
blob.bytes = int64(int32(c.call("sqlite3_blob_bytes", stk_t(blob.handle))))
5757
return &blob, nil
5858
}
5959

@@ -67,10 +67,10 @@ func (b *Blob) Close() error {
6767
return nil
6868
}
6969

70-
r := b.c.call("sqlite3_blob_close", uint64(b.handle))
70+
rc := res_t(b.c.call("sqlite3_blob_close", stk_t(b.handle)))
7171
b.c.free(b.bufptr)
7272
b.handle = 0
73-
return b.c.error(r)
73+
return b.c.error(rc)
7474
}
7575

7676
// Size returns the size of the BLOB in bytes.
@@ -94,13 +94,13 @@ func (b *Blob) Read(p []byte) (n int, err error) {
9494
want = avail
9595
}
9696
if want > b.buflen {
97-
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
97+
b.bufptr = b.c.realloc(b.bufptr, want)
9898
b.buflen = want
9999
}
100100

101-
r := b.c.call("sqlite3_blob_read", uint64(b.handle),
102-
uint64(b.bufptr), uint64(want), uint64(b.offset))
103-
err = b.c.error(r)
101+
rc := res_t(b.c.call("sqlite3_blob_read", stk_t(b.handle),
102+
stk_t(b.bufptr), stk_t(want), stk_t(b.offset)))
103+
err = b.c.error(rc)
104104
if err != nil {
105105
return 0, err
106106
}
@@ -109,7 +109,7 @@ func (b *Blob) Read(p []byte) (n int, err error) {
109109
err = io.EOF
110110
}
111111

112-
copy(p, util.View(b.c.mod, b.bufptr, uint64(want)))
112+
copy(p, util.View(b.c.mod, b.bufptr, want))
113113
return int(want), err
114114
}
115115

@@ -127,19 +127,19 @@ func (b *Blob) WriteTo(w io.Writer) (n int64, err error) {
127127
want = avail
128128
}
129129
if want > b.buflen {
130-
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
130+
b.bufptr = b.c.realloc(b.bufptr, want)
131131
b.buflen = want
132132
}
133133

134134
for want > 0 {
135-
r := b.c.call("sqlite3_blob_read", uint64(b.handle),
136-
uint64(b.bufptr), uint64(want), uint64(b.offset))
137-
err = b.c.error(r)
135+
rc := res_t(b.c.call("sqlite3_blob_read", stk_t(b.handle),
136+
stk_t(b.bufptr), stk_t(want), stk_t(b.offset)))
137+
err = b.c.error(rc)
138138
if err != nil {
139139
return n, err
140140
}
141141

142-
mem := util.View(b.c.mod, b.bufptr, uint64(want))
142+
mem := util.View(b.c.mod, b.bufptr, want)
143143
m, err := w.Write(mem[:want])
144144
b.offset += int64(m)
145145
n += int64(m)
@@ -165,14 +165,14 @@ func (b *Blob) WriteTo(w io.Writer) (n int64, err error) {
165165
func (b *Blob) Write(p []byte) (n int, err error) {
166166
want := int64(len(p))
167167
if want > b.buflen {
168-
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
168+
b.bufptr = b.c.realloc(b.bufptr, want)
169169
b.buflen = want
170170
}
171171
util.WriteBytes(b.c.mod, b.bufptr, p)
172172

173-
r := b.c.call("sqlite3_blob_write", uint64(b.handle),
174-
uint64(b.bufptr), uint64(want), uint64(b.offset))
175-
err = b.c.error(r)
173+
rc := res_t(b.c.call("sqlite3_blob_write", stk_t(b.handle),
174+
stk_t(b.bufptr), stk_t(want), stk_t(b.offset)))
175+
err = b.c.error(rc)
176176
if err != nil {
177177
return 0, err
178178
}
@@ -196,17 +196,17 @@ func (b *Blob) ReadFrom(r io.Reader) (n int64, err error) {
196196
want = 1
197197
}
198198
if want > b.buflen {
199-
b.bufptr = b.c.realloc(b.bufptr, uint64(want))
199+
b.bufptr = b.c.realloc(b.bufptr, want)
200200
b.buflen = want
201201
}
202202

203203
for {
204-
mem := util.View(b.c.mod, b.bufptr, uint64(want))
204+
mem := util.View(b.c.mod, b.bufptr, want)
205205
m, err := r.Read(mem[:want])
206206
if m > 0 {
207-
r := b.c.call("sqlite3_blob_write", uint64(b.handle),
208-
uint64(b.bufptr), uint64(m), uint64(b.offset))
209-
err := b.c.error(r)
207+
rc := res_t(b.c.call("sqlite3_blob_write", stk_t(b.handle),
208+
stk_t(b.bufptr), stk_t(m), stk_t(b.offset)))
209+
err := b.c.error(rc)
210210
if err != nil {
211211
return n, err
212212
}
@@ -254,8 +254,8 @@ func (b *Blob) Seek(offset int64, whence int) (int64, error) {
254254
// https://sqlite.org/c3ref/blob_reopen.html
255255
func (b *Blob) Reopen(row int64) error {
256256
b.c.checkInterrupt(b.c.handle)
257-
err := b.c.error(b.c.call("sqlite3_blob_reopen", uint64(b.handle), uint64(row)))
258-
b.bytes = int64(b.c.call("sqlite3_blob_bytes", uint64(b.handle)))
257+
err := b.c.error(res_t(b.c.call("sqlite3_blob_reopen", stk_t(b.handle), stk_t(row))))
258+
b.bytes = int64(int32(b.c.call("sqlite3_blob_bytes", stk_t(b.handle))))
259259
b.offset = 0
260260
return err
261261
}

0 commit comments

Comments
 (0)