Skip to content

Commit

Permalink
Merge pull request #242 from noborus/cgo-free
Browse files Browse the repository at this point in the history
Enables CGO free build
  • Loading branch information
noborus authored Oct 14, 2023
2 parents 022dcaf + 241e713 commit c7a1504
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 99 deletions.
4 changes: 2 additions & 2 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,8 @@ func Test_quoteOpts(t *testing.T) {
want string
}{
{
name: "testSQLIte3",
driver: "sqlite3",
name: "testSQLite3",
driver: trdsql.DefaultDriver,
want: "\\`",
},
{
Expand Down
47 changes: 0 additions & 47 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ import (
"io"
"log"
"strings"

// MySQL driver.
_ "github.com/go-sql-driver/mysql"

// PostgreSQL driver.
_ "github.com/lib/pq"

// SQLite3 driver.
_ "github.com/mattn/go-sqlite3"
// SQlite3 extension library.
sqlite3_stdlib "github.com/multiprocessio/go-sqlite3-stdlib"
)

var (
Expand Down Expand Up @@ -55,42 +44,6 @@ type DB struct {
importCount int
}

func init() {
// Enable sqlite3 extensions.
// It can be used by setting the driver to "sqlite3_ext".
sqlite3_stdlib.Register("sqlite3_ext")
}

// Connect is connects to the database.
// Currently supported drivers are sqlite3, mysql, postgres.
// Set quote character and maxBulk depending on the driver type.
func Connect(driver, dsn string) (*DB, error) {
sqlDB, err := sql.Open(driver, dsn)
if err != nil {
return nil, err
}

db := &DB{
DB: sqlDB,
driver: driver,
dsn: dsn,
}
debug.Printf("driver: %s, dsn: %s", driver, dsn)

switch driver {
case "sqlite3", "sqlite3_ext":
db.quote = "`"
db.maxBulk = 500
case "mysql":
db.quote = "`"
db.maxBulk = 1000
case "postgres":
db.quote = "\""
}

return db, nil
}

// Disconnect is disconnect the database.
func (db *DB) Disconnect() error {
return db.Close()
Expand Down
51 changes: 51 additions & 0 deletions database_connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//go:build !cgo

package trdsql

import (
"database/sql"

// MySQL driver.

_ "github.com/go-sql-driver/mysql"

// PostgreSQL driver.
_ "github.com/lib/pq"

// SQLite3 driver.
_ "modernc.org/sqlite"
)

var DefaultDriver = "sqlite"

// Connect is connects to the database.
// Currently supported drivers are sqlite3, mysql, postgres.
// Set quote character and maxBulk depending on the driver type.
func Connect(driver, dsn string) (*DB, error) {
sqlDB, err := sql.Open(driver, dsn)
if err != nil {
return nil, err
}

db := &DB{
DB: sqlDB,
driver: driver,
dsn: dsn,
}
debug.Printf("driver: %s, dsn: %s", driver, dsn)

switch driver {
case "sqlite3", "sqlite3_ext", "sqlite":
db.quote = "`"
db.maxBulk = 500
case "mysql":
db.quote = "`"
db.maxBulk = 1000
case "postgres":
db.quote = "\""
default:
db.quote = "\""
}

return db, nil
}
59 changes: 59 additions & 0 deletions database_connect_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//go:build cgo

package trdsql

import (
"database/sql"

// MySQL driver.

_ "github.com/go-sql-driver/mysql"

// PostgreSQL driver.
_ "github.com/lib/pq"

// SQLite3 driver.
_ "github.com/mattn/go-sqlite3"
// SQlite3 extension library.
sqlite3_stdlib "github.com/multiprocessio/go-sqlite3-stdlib"
)

var DefaultDriver = "sqlite3"

func init() {
// Enable sqlite3 extensions.
// It can be used by setting the driver to "sqlite3_ext".
sqlite3_stdlib.Register("sqlite3_ext")
}

// Connect is connects to the database.
// Currently supported drivers are sqlite3, mysql, postgres.
// Set quote character and maxBulk depending on the driver type.
func Connect(driver, dsn string) (*DB, error) {
sqlDB, err := sql.Open(driver, dsn)
if err != nil {
return nil, err
}

db := &DB{
DB: sqlDB,
driver: driver,
dsn: dsn,
}
debug.Printf("driver: %s, dsn: %s", driver, dsn)

switch driver {
case "sqlite3", "sqlite3_ext", "sqlite":
db.quote = "`"
db.maxBulk = 500
case "mysql":
db.quote = "`"
db.maxBulk = 1000
case "postgres":
db.quote = "\""
default:
db.quote = "\""
}

return db, nil
}
41 changes: 15 additions & 26 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
_ "modernc.org/sqlite"
)

func TestConnect(t *testing.T) {
Expand All @@ -20,7 +20,7 @@ func TestConnect(t *testing.T) {
}{
{
name: "testSuccess",
args: args{driver: "sqlite3", dsn: ""},
args: args{driver: "sqlite", dsn: ""},
wantErr: false,
},
{
Expand All @@ -38,11 +38,6 @@ func TestConnect(t *testing.T) {
args: args{driver: "mysql", dsn: myDsn()},
wantErr: false,
},
{
name: "testSqlite3ext",
args: args{driver: "sqlite3_ext", dsn: ""},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -67,7 +62,7 @@ func TestDB_Disconnect(t *testing.T) {
}{
{
name: "testSuccess",
args: args{driver: "sqlite3", dsn: ""},
args: args{driver: "sqlite", dsn: ""},
wantErr: false,
},
}
Expand Down Expand Up @@ -103,7 +98,7 @@ func TestDB_CreateTable(t *testing.T) {
}{
{
name: "testSuccess",
fields: fields{driver: "sqlite3", dsn: ""},
fields: fields{driver: "sqlite", dsn: ""},
args: args{
tableName: "test",
names: []string{"a", "b"},
Expand All @@ -114,7 +109,7 @@ func TestDB_CreateTable(t *testing.T) {
},
{
name: "testSuccess2",
fields: fields{driver: "sqlite3", dsn: ""},
fields: fields{driver: "sqlite", dsn: ""},
args: args{
tableName: "test",
names: []string{"c1"},
Expand All @@ -125,7 +120,7 @@ func TestDB_CreateTable(t *testing.T) {
},
{
name: "testFail",
fields: fields{driver: "sqlite3", dsn: ""},
fields: fields{driver: "sqlite", dsn: ""},
args: args{
tableName: "test",
names: []string{},
Expand All @@ -136,7 +131,7 @@ func TestDB_CreateTable(t *testing.T) {
},
{
name: "testFail2",
fields: fields{driver: "sqlite3", dsn: ""},
fields: fields{driver: "sqlite", dsn: ""},
args: args{
tableName: "test",
names: []string{"c1"},
Expand All @@ -147,7 +142,7 @@ func TestDB_CreateTable(t *testing.T) {
},
{
name: "testFail3",
fields: fields{driver: "sqlite3", dsn: ""},
fields: fields{driver: "sqlite", dsn: ""},
args: args{
tableName: "test",
names: []string{"c1"},
Expand Down Expand Up @@ -196,13 +191,13 @@ func TestDB_Select(t *testing.T) {
}{
{
name: "testErr",
fields: fields{driver: "sqlite3", dsn: ""},
fields: fields{driver: "sqlite", dsn: ""},
args: args{query: ""},
wantErr: true,
},
{
name: "testErr2",
fields: fields{driver: "sqlite3", dsn: ""},
fields: fields{driver: "sqlite", dsn: ""},
args: args{query: "SELEC * FROM test"},
wantErr: true,
},
Expand Down Expand Up @@ -249,23 +244,17 @@ func TestDB_Func(t *testing.T) {
wantErr bool
}{
{
name: "testSqlite3Version",
fields: fields{driver: "sqlite3", dsn: ""},
name: "testsqliteVersion",
fields: fields{driver: "sqlite", dsn: ""},
args: args{query: "SELECT sqlite_version();"},
wantErr: false,
},
{
name: "testSqlite3fail",
fields: fields{driver: "sqlite3", dsn: ""},
name: "testsqlitefail",
fields: fields{driver: "sqlite", dsn: ""},
args: args{query: "SELECT repeat('f', 5);"},
wantErr: true,
},
{
name: "testSqlite3ext",
fields: fields{driver: "sqlite3_ext", dsn: ""},
args: args{query: "SELECT repeat('f', 5);"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -312,7 +301,7 @@ func TestDB_Import(t *testing.T) {
}{
{
name: "testErr",
fields: fields{driver: "sqlite3", dsn: ""},
fields: fields{driver: "sqlite", dsn: ""},
args: args{
tableName: "test",
columnNames: []string{"c1"},
Expand Down
2 changes: 1 addition & 1 deletion exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestWriteFormat_Export(t *testing.T) {
}{
{
name: "testErr",
fields: fields{driver: "sqlite3", dsn: ""},
fields: fields{driver: DefaultDriver, dsn: ""},
args: args{query: "SELECT 1 "},
wantErr: true,
},
Expand Down
24 changes: 20 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,38 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/pierrec/lz4 v2.6.1+incompatible
github.com/ulikunitz/xz v0.5.11
golang.org/x/term v0.12.0
golang.org/x/term v0.13.0
modernc.org/sqlite v1.26.0
)

require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/frankban/quicktest v1.7.2 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/jwalton/go-supportscolor v1.2.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.4 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
gonum.org/v1/gonum v0.14.0 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
modernc.org/cc/v3 v3.41.0 // indirect
modernc.org/ccgo/v3 v3.16.15 // indirect
modernc.org/libc v1.24.1 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)

go 1.19
Loading

0 comments on commit c7a1504

Please sign in to comment.