Skip to content

Commit

Permalink
Merge pull request #234 from felixgunawan/b_fix_null_error_sql
Browse files Browse the repository at this point in the history
bugfix: sql null column error
  • Loading branch information
tmc authored Aug 11, 2023
2 parents 6f9f536 + df2bf2c commit 4d6b7ab
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
6 changes: 5 additions & 1 deletion tools/sqldatabase/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ func (m MySQL) Query(ctx context.Context, query string, args ...any) ([]string,
results := make([][]string, 0)
for rows.Next() {
row := make([]string, len(cols))
rowNullable := make([]sql.NullString, len(cols))
rowPtrs := make([]interface{}, len(cols))
for i := range row {
rowPtrs[i] = &row[i]
rowPtrs[i] = &rowNullable[i]
}
err = rows.Scan(rowPtrs...)
if err != nil {
return nil, nil, err
}
for i := range rowNullable {
row[i] = rowNullable[i].String
}
results = append(results, row)
}
return cols, results, nil
Expand Down
13 changes: 13 additions & 0 deletions tools/sqldatabase/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package mysql_test

import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"testing"

Expand All @@ -28,4 +31,14 @@ func Test(t *testing.T) {
require.NoError(t, err)

t.Log(desc)

for _, tableName := range tbs {
_, err = db.Query(context.Background(), fmt.Sprintf("SELECT * from %s LIMIT 1", tableName))
/* exclude no row error,
since we only need to check if db.Query function can perform query correctly*/
if errors.Is(err, sql.ErrNoRows) {
continue
}
require.NoError(t, err)
}
}
6 changes: 5 additions & 1 deletion tools/sqldatabase/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ func (p PostgreSQL) Query(ctx context.Context, query string, args ...any) ([]str
results := make([][]string, 0)
for rows.Next() {
row := make([]string, len(cols))
rowNullable := make([]sql.NullString, len(cols))
rowPtrs := make([]interface{}, len(cols))
for i := range row {
rowPtrs[i] = &row[i]
rowPtrs[i] = &rowNullable[i]
}
err = rows.Scan(rowPtrs...)
if err != nil {
return nil, nil, err
}
for i := range rowNullable {
row[i] = rowNullable[i].String
}
results = append(results, row)
}
return cols, results, nil
Expand Down
13 changes: 13 additions & 0 deletions tools/sqldatabase/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package postgresql_test

import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"testing"

Expand All @@ -27,4 +30,14 @@ func Test(t *testing.T) {
require.NoError(t, err)

t.Log(desc)

for _, tableName := range tbs {
_, err = db.Query(context.Background(), fmt.Sprintf("SELECT * from %s LIMIT 1", tableName))
/* exclude no row error,
since we only need to check if db.Query function can perform query correctly*/
if errors.Is(err, sql.ErrNoRows) {
continue
}
require.NoError(t, err)
}
}
6 changes: 5 additions & 1 deletion tools/sqldatabase/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ func (m SQLite3) Query(ctx context.Context, query string, args ...any) ([]string
results := make([][]string, 0)
for rows.Next() {
row := make([]string, len(cols))
rowNullable := make([]sql.NullString, len(cols))
rowPtrs := make([]interface{}, len(cols))
for i := range row {
rowPtrs[i] = &row[i]
rowPtrs[i] = &rowNullable[i]
}
err = rows.Scan(rowPtrs...)
if err != nil {
return nil, nil, err
}
for i := range rowNullable {
row[i] = rowNullable[i].String
}
results = append(results, row)
}
return cols, results, nil
Expand Down
12 changes: 12 additions & 0 deletions tools/sqldatabase/sqlite3/sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package sqlite3_test
import (
"context"
"database/sql"
"errors"
"fmt"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -46,4 +48,14 @@ func Test(t *testing.T) {
require.True(t, strings.Contains(desc, "Activity")) //nolint:stylecheck
require.True(t, strings.Contains(desc, "Activity1")) //nolint:stylecheck
require.True(t, strings.Contains(desc, "Activity2")) //nolint:stylecheck

for _, tableName := range tbs {
_, err = db.Query(context.Background(), fmt.Sprintf("SELECT * from %s LIMIT 1", tableName))
/* exclude no row error,
since we only need to check if db.Query function can perform query correctly*/
if errors.Is(err, sql.ErrNoRows) {
continue
}
require.NoError(t, err)
}
}

0 comments on commit 4d6b7ab

Please sign in to comment.