Skip to content

Commit 0193694

Browse files
committed
Get ColumnName ([]string) from a statement
This is dangerous to use since it can only be called when the Statement is live, which sounds reasonable, but gets messy around the Row and Rows helper.
1 parent 8c46925 commit 0193694

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (c Conn) Prepare(sql []byte, args ...any) (*Stmt, error) {
185185

186186
func (c Conn) RowB(sql []byte, args ...any) Row {
187187
stmt, err := c.Prepare(sql, args...)
188-
return Row{stmt: stmt, err: err}
188+
return Row{Stmt: stmt, err: err}
189189
}
190190

191191
func (c Conn) Row(sql string, args ...any) Row {
@@ -194,7 +194,7 @@ func (c Conn) Row(sql string, args ...any) Row {
194194

195195
func (c Conn) RowsB(sql []byte, args ...any) Rows {
196196
stmt, err := c.Prepare(sql, args...)
197-
return Rows{stmt: stmt, err: err}
197+
return Rows{Stmt: stmt, err: err}
198198
}
199199

200200
func (c Conn) Rows(sql string, args ...any) Rows {

row.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package sqlite
22

33
type Row struct {
4-
stmt *Stmt
4+
Stmt *Stmt
55
err error
66
}
77

88
func (r Row) Scan(dst ...interface{}) error {
99
if err := r.err; err != nil {
1010
return err
1111
}
12-
stmt := r.stmt
12+
stmt := r.Stmt
1313
defer stmt.Close()
1414

1515
hasRow, err := stmt.Step()

rows.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package sqlite
22

33
type Rows struct {
4-
stmt *Stmt
4+
Stmt *Stmt
55
err error
66
}
77

88
func (r *Rows) Next() bool {
9-
stmt := r.stmt
9+
stmt := r.Stmt
1010
if r.err != nil {
1111
return false
1212
}
@@ -21,7 +21,7 @@ func (r *Rows) Next() bool {
2121
}
2222

2323
func (r *Rows) Scan(dst ...interface{}) error {
24-
stmt := r.stmt
24+
stmt := r.Stmt
2525
for i, v := range dst {
2626
if err := stmt.scan(i, v); err != nil {
2727
r.err = err
@@ -38,7 +38,7 @@ func (r Rows) Error() error {
3838

3939
func (r Rows) Close() {
4040
// will be nil if the query was never valid
41-
if stmt := r.stmt; stmt != nil {
41+
if stmt := r.Stmt; stmt != nil {
4242
stmt.Close()
4343
}
4444
}

sqlite3_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ func Test_Bool_True(t *testing.T) {
9393

9494
var b1, b2 bool
9595
row := db.RowB([]byte("select cint, cintn from test where id = ?"), db.LastInsertRowID())
96+
97+
names := row.Stmt.ColumnNames()
98+
assert.Equal(t, len(names), 2)
99+
assert.Equal(t, names[0], "cint")
100+
assert.Equal(t, names[1], "cintn")
101+
96102
row.Scan(&b1, &b2)
97103
assert.Equal(t, b1, true)
98104
assert.Equal(t, b2, true)

stmt.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ func (s *Stmt) Close() error {
4949
return nil
5050
}
5151

52+
func (s *Stmt) ColumnNames() []string {
53+
stmt := s.stmt
54+
count := s.columnCount
55+
names := make([]string, count)
56+
for i := 0; i < count; i++ {
57+
names[i] = C.GoString(C.sqlite3_column_name(stmt, C.int(i)))
58+
}
59+
return names
60+
}
61+
5262
func (s *Stmt) Exec(args ...interface{}) error {
5363
if err := s.Bind(args...); err != nil {
5464
s.Reset()

0 commit comments

Comments
 (0)