Skip to content

Commit

Permalink
v0.0.2: update (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysiar authored May 20, 2024
1 parent 8b37d12 commit 5741ff3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ get::


tests::
rm -rf test.db
sqlite3 -batch test.db ""
#go test -v ./...
go test -modfile=go_test.mod ./... -v
rm -rf tests/test.db
go test -count=1 -modfile=go_test.mod ./... -v
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ The main reason that this module has been created was generic querying database
rows, err: = Db.Query("SELECT * FROM album")

defer rows.Close()
var data []RawStringType
var data []go_sql_raw.RawSqlType
for rows.Next() {
rec := Rows2Map(rows)
rec := go_sql_raw.Rows2Map(rows)
data = append(data, rec)
}
```

check also [go_sql_raw_test.go](tests/go_sql_raw_test.go)

## TODO
* type converting from DB to go in function `update`
* update type converting from DB to go in function `convertType`

## Credits
* module inspired by https://gist.github.com/SchumacherFM/69a167bec7dea644a20e
30 changes: 19 additions & 11 deletions go_sql_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"strconv"
"strings"
)

type RawSqlType map[string]interface{}
Expand Down Expand Up @@ -40,17 +41,7 @@ func (s *mapRawSqlScan) update(rows *sql.Rows) error {

for i := 0; i < s.colCount; i++ {
if rb, ok := s.cp[i].(*sql.RawBytes); ok {
val := string(*rb)
var parsed any
switch s.colTypes[i].DatabaseTypeName() {
case "DECIMAL":
parsed, _ = strconv.ParseFloat(val, 64)
case "INT":
parsed, _ = strconv.ParseInt(val, 10, 64)
default:
parsed = val
}
s.row[s.colNames[i]] = parsed
s.row[s.colNames[i]] = convertType(rb, s.colTypes[i].DatabaseTypeName())
*rb = nil
} else {
return fmt.Errorf("Cannot convert index %d column %s to type *sql.RawBytes", i, s.colNames[i])
Expand Down Expand Up @@ -78,3 +69,20 @@ func newMapRawSqlScan(columnNames []string, columnTypes []*sql.ColumnType) *mapR
}
return s
}

func convertType(rb *sql.RawBytes, databaseTypeName string) any {
databaseTypeName = strings.ToUpper(databaseTypeName)

val := string(*rb)
var parsed any

if strings.HasPrefix(databaseTypeName, "INT") {
parsed, _ = strconv.ParseInt(val, 10, 64)
} else if strings.HasPrefix(databaseTypeName, "DECIMAL") {
parsed, _ = strconv.ParseFloat(val, 64)
} else {
parsed = val
}

return parsed
}
34 changes: 18 additions & 16 deletions tests/go_sql_raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,13 @@ import (
_ "github.com/mattn/go-sqlite3"
"github.com/mysiar-org/go-sql-raw"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)

func Test(t *testing.T) {
const file string = "test.db"
db, err := sql.Open("sqlite3", file)
chkError(err)
_, err = db.Exec(dropTable())
chkError(err)
_, err = db.Exec(createTable())
db := setupDb()
rows, err := db.Query("SELECT * FROM album ORDER BY id")
chkError(err)
_, err = db.Exec(insertData())
chkError(err)

var rows *sql.Rows
rows, err = db.Query("SELECT * FROM album ORDER BY id")

var data []go_sql_raw.RawSqlType
for rows.Next() {
Expand All @@ -36,15 +26,27 @@ func Test(t *testing.T) {
var expectedTitle = []string{"Blue Train", "Giant Steps", "Jeru", "Sarah Vaughan", "Upadek"}

for idx, entry := range data {
id, _ := strconv.ParseInt(fmt.Sprintf("%v", entry["id"]), 10, 64)
assert.Equal(t, expectedIds[idx], id)
price, _ := strconv.ParseFloat(fmt.Sprintf("%v", entry["price"]), 64)
assert.Equal(t, expectedPrices[idx], price)
assert.Equal(t, expectedIds[idx], entry["id"])
assert.Equal(t, expectedPrices[idx], entry["price"])
assert.Equal(t, expectedArtists[idx], entry["artist"])
assert.Equal(t, expectedTitle[idx], entry["title"])
}
}

func setupDb() *sql.DB {
const file string = "test.db"
db, err := sql.Open("sqlite3", file)
chkError(err)
_, err = db.Exec(dropTable())
chkError(err)
_, err = db.Exec(createTable())
chkError(err)
_, err = db.Exec(insertData())
chkError(err)

return db
}

func dropTable() string {
return "DROP TABLE IF EXISTS album"
}
Expand Down

0 comments on commit 5741ff3

Please sign in to comment.