Skip to content

Commit 9a6786b

Browse files
committed
Refactor
1 parent 8177f83 commit 9a6786b

File tree

1 file changed

+30
-56
lines changed

1 file changed

+30
-56
lines changed

sqlite3_session.go

Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -133,83 +133,57 @@ func (ci *ChangesetIterator) Op() (tblName string, numCol int, oper int, indirec
133133

134134
// Old retrieves the old value for the specified column in the change payload.
135135
func (ci *ChangesetIterator) Old(dest []any) error {
136-
for i := 0; i < len(dest); i++ {
137-
var val *C.sqlite3_value
138-
var src any
139-
140-
rc := C.sqlite3changeset_old(ci.iter, C.int(i), &val)
141-
if rc != C.SQLITE_OK {
142-
return fmt.Errorf("sqlite3changeset_old: %s", C.GoString(C.sqlite3_errstr(rc)))
143-
}
136+
return ci.row(dest, true)
137+
}
144138

145-
switch C.sqlite3_value_type(val) {
146-
case C.SQLITE_INTEGER:
147-
src = int64(C.sqlite3_value_int64(val))
148-
case C.SQLITE_FLOAT:
149-
src = float64(C.sqlite3_value_double(val))
150-
case C.SQLITE_BLOB:
151-
len := C.sqlite3_value_bytes(val)
152-
blobptr := C.sqlite3_value_blob(val)
153-
src = C.GoBytes(blobptr, len)
154-
case C.SQLITE_TEXT:
155-
len := C.sqlite3_value_bytes(val)
156-
cstrptr := unsafe.Pointer(C.sqlite3_value_text(val))
157-
src = C.GoBytes(cstrptr, len)
158-
case C.SQLITE_NULL:
159-
src = nil
160-
}
139+
// New retrieves the new value for the specified column in the change payload.
140+
func (ci *ChangesetIterator) New(dest []any) error {
141+
return ci.row(dest, false)
142+
}
161143

162-
err := convertAssign(&dest[i], src)
163-
if err != nil {
164-
return err
144+
// Finalize deletes a changeset iterator.
145+
func (ci *ChangesetIterator) Finalize() error {
146+
if ci.iter != nil {
147+
rc := C.sqlite3changeset_finalize(ci.iter)
148+
ci.iter = nil
149+
if rc != C.SQLITE_OK {
150+
return fmt.Errorf("sqlite3changeset_finalize: %s", C.GoString(C.sqlite3_errstr(rc)))
165151
}
166152
}
167153
return nil
168154
}
169155

170156
// New retrieves the new value for the specified column in the change payload.
171-
func (ci *ChangesetIterator) New(dest []any) error {
157+
func (ci *ChangesetIterator) row(dest []any, old bool) error {
158+
var val *C.sqlite3_value
159+
var rc C.int
172160
for i := 0; i < len(dest); i++ {
173-
var val *C.sqlite3_value
174-
var src any
175-
176-
rc := C.sqlite3changeset_new(ci.iter, C.int(i), &val)
161+
fn := ""
162+
if old {
163+
fn = "old"
164+
rc = C.sqlite3changeset_old(ci.iter, C.int(i), &val)
165+
} else {
166+
fn = "new"
167+
rc = C.sqlite3changeset_new(ci.iter, C.int(i), &val)
168+
}
177169
if rc != C.SQLITE_OK {
178-
return fmt.Errorf("sqlite3changeset_new: %s", C.GoString(C.sqlite3_errstr(rc)))
170+
return fmt.Errorf("sqlite3changeset_%s: %s", fn, C.GoString(C.sqlite3_errstr(rc)))
179171
}
180172

181173
switch C.sqlite3_value_type(val) {
182174
case C.SQLITE_INTEGER:
183-
src = int64(C.sqlite3_value_int64(val))
175+
dest[i] = int64(C.sqlite3_value_int64(val))
184176
case C.SQLITE_FLOAT:
185-
src = float64(C.sqlite3_value_double(val))
177+
dest[i] = float64(C.sqlite3_value_double(val))
186178
case C.SQLITE_BLOB:
187179
len := C.sqlite3_value_bytes(val)
188180
blobptr := C.sqlite3_value_blob(val)
189-
src = C.GoBytes(blobptr, len)
181+
dest[i] = C.GoBytes(blobptr, len)
190182
case C.SQLITE_TEXT:
191-
len := C.sqlite3_value_bytes(val)
192183
cstrptr := unsafe.Pointer(C.sqlite3_value_text(val))
193-
src = C.GoBytes(cstrptr, len)
184+
dest[i] = C.GoString((*C.char)(cstrptr))
194185
case C.SQLITE_NULL:
195-
src = nil
196-
}
197-
198-
err := convertAssign(&dest[i], src)
199-
if err != nil {
200-
return err
201-
}
202-
}
203-
return nil
204-
}
205-
206-
// Finalize deletes a changeset iterator.
207-
func (ci *ChangesetIterator) Finalize() error {
208-
if ci.iter != nil {
209-
rc := C.sqlite3changeset_finalize(ci.iter)
210-
ci.iter = nil
211-
if rc != C.SQLITE_OK {
212-
return fmt.Errorf("sqlite3changeset_finalize: %s", C.GoString(C.sqlite3_errstr(rc)))
186+
dest[i] = nil
213187
}
214188
}
215189
return nil

0 commit comments

Comments
 (0)