Skip to content

Commit 06eaf41

Browse files
committed
Fix #151.
1 parent 9638976 commit 06eaf41

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

driver/driver.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ type rows struct {
578578
*stmt
579579
names []string
580580
types []string
581+
nulls []bool
581582
}
582583

583584
func (r *rows) Close() error {
@@ -596,6 +597,22 @@ func (r *rows) Columns() []string {
596597
return r.names
597598
}
598599

600+
func (r *rows) loadTypes() {
601+
if r.nulls == nil {
602+
count := r.Stmt.ColumnCount()
603+
r.nulls = make([]bool, count)
604+
r.types = make([]string, count)
605+
for i := range r.nulls {
606+
if col := r.Stmt.ColumnOriginName(i); col != "" {
607+
r.types[i], _, r.nulls[i], _, _, _ = r.Stmt.Conn().TableColumnMetadata(
608+
r.Stmt.ColumnDatabaseName(i),
609+
r.Stmt.ColumnTableName(i),
610+
col)
611+
}
612+
}
613+
}
614+
}
615+
599616
func (r *rows) declType(index int) string {
600617
if r.types == nil {
601618
count := r.Stmt.ColumnCount()
@@ -608,7 +625,8 @@ func (r *rows) declType(index int) string {
608625
}
609626

610627
func (r *rows) ColumnTypeDatabaseTypeName(index int) string {
611-
decltype := r.declType(index)
628+
r.loadTypes()
629+
decltype := r.types[index]
612630
if len := len(decltype); len > 0 && decltype[len-1] == ')' {
613631
if i := strings.LastIndexByte(decltype, '('); i >= 0 {
614632
decltype = decltype[:i]
@@ -617,6 +635,14 @@ func (r *rows) ColumnTypeDatabaseTypeName(index int) string {
617635
return strings.TrimSpace(decltype)
618636
}
619637

638+
func (r *rows) ColumnTypeNullable(index int) (nullable, ok bool) {
639+
r.loadTypes()
640+
if r.nulls[index] {
641+
return false, true
642+
}
643+
return true, false
644+
}
645+
620646
func (r *rows) Next(dest []driver.Value) error {
621647
old := r.Stmt.Conn().SetInterrupt(r.ctx)
622648
defer r.Stmt.Conn().SetInterrupt(old)

tests/driver_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestDriver(t *testing.T) {
3333
defer conn.Close()
3434

3535
res, err := conn.ExecContext(ctx,
36-
`CREATE TABLE users (id INT, name VARCHAR(10))`)
36+
`CREATE TABLE users (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(10))`)
3737
if err != nil {
3838
t.Fatal(err)
3939
}
@@ -82,11 +82,17 @@ func TestDriver(t *testing.T) {
8282
if err != nil {
8383
t.Fatal(err)
8484
}
85-
if got := typs[0].DatabaseTypeName(); got != "INT" {
86-
t.Errorf("got %s, want INT", got)
85+
if got := typs[0].DatabaseTypeName(); got != "INTEGER" {
86+
t.Errorf("got %s, want INTEGER", got)
8787
}
8888
if got := typs[1].DatabaseTypeName(); got != "VARCHAR" {
89-
t.Errorf("got %s, want INT", got)
89+
t.Errorf("got %s, want VARCHAR", got)
90+
}
91+
if got, ok := typs[0].Nullable(); got || !ok {
92+
t.Errorf("got %v/%v, want false/true", got, ok)
93+
}
94+
if got, ok := typs[1].Nullable(); !got || ok {
95+
t.Errorf("got %v/%v, want true/false", got, ok)
9096
}
9197

9298
row := 0

0 commit comments

Comments
 (0)