Skip to content

Commit 23d646f

Browse files
charlesvdvAdamSLevy
authored andcommitted
implement GetType(string) ColumnType
Almost every `Column*` has its equivalent in `Get*` except for a few methods. Since the type information are heavily used to check for a `NULL` value. `GetType` provides a convenient method to access it without knowing the column index.
1 parent 5d42d10 commit 23d646f

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

sqlite.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,15 @@ func (stmt *Stmt) ColumnIndex(colName string) int {
10541054
return col
10551055
}
10561056

1057+
// GetType returns a query result type for colName
1058+
func (stmt *Stmt) GetType(colName string) ColumnType {
1059+
col, found := stmt.colNames[colName]
1060+
if !found {
1061+
return SQLITE_NULL
1062+
}
1063+
return stmt.ColumnType(col)
1064+
}
1065+
10571066
// GetInt64 returns a query result value for colName as an int64.
10581067
func (stmt *Stmt) GetInt64(colName string) int64 {
10591068
col, found := stmt.colNames[colName]

sqlite_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,16 @@ func TestConn(t *testing.T) {
103103
if typ != sqlite.SQLITE_TEXT {
104104
t.Errorf(`ColumnType(0)=%q, want %q`, typ, sqlite.SQLITE_TEXT)
105105
}
106+
if getType := stmt.GetType("foo1"); getType != typ {
107+
t.Errorf(`GetType("foo1")=%q, want %q`, getType, typ)
108+
}
106109
intTyp := stmt.ColumnType(1)
107110
if intTyp != sqlite.SQLITE_INTEGER {
108111
t.Errorf(`ColumnType(1)=%q, want %q`, intTyp, sqlite.SQLITE_INTEGER)
109112
}
113+
if getIntType := stmt.GetType("foo2"); getIntType != intTyp {
114+
t.Errorf(`GetType("foo2")=%q, want %q`, getIntType, intTyp)
115+
}
110116
gotVals = append(gotVals, val)
111117
gotInts = append(gotInts, intVal)
112118
}

0 commit comments

Comments
 (0)