Skip to content

Commit

Permalink
feat(query) : Add mysql sqlite psql mariadb QueryInserter
Browse files Browse the repository at this point in the history
  • Loading branch information
seipan committed Oct 21, 2023
1 parent f2dd216 commit f1bbddf
Show file tree
Hide file tree
Showing 11 changed files with 436 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ module github.com/seipan/csql
go 1.20

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
32 changes: 32 additions & 0 deletions lib/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// MIT License

// Copyright (c) 2023 Yamasaki Shotaro

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package lib

type KeyValue struct {
Key string
Value string
}

type Inserter interface {
Query() string
}
30 changes: 30 additions & 0 deletions mariadb/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,33 @@
// SOFTWARE.

package mariadb

import (
"fmt"
"strings"

"github.com/seipan/csql/lib"
)

type MariaDBSQLInserter struct {
keys []lib.KeyValue
tableName string
}

func (i *MariaDBSQLInserter) Query() string {
placeholders := make([]string, 0, len(i.keys))
keys := make([]string, 0, len(i.keys))

for _, kv := range i.keys {
keys = append(keys, kv.Key)
placeholders = append(placeholders, "?")
}

query := fmt.Sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
i.tableName,
strings.Join(keys, ", "),
strings.Join(placeholders, ", "),
)
return query
}
69 changes: 69 additions & 0 deletions mariadb/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// MIT License

// Copyright (c) 2023 Yamasaki Shotaro

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package mariadb

import (
"testing"

"github.com/seipan/csql/lib"
"github.com/stretchr/testify/assert"
)

func TestMariaDBSQLInserter_Query(t *testing.T) {
tests := []struct {
name string
keys []lib.KeyValue
tableName string
expected string
}{
{
name: "single key-value pair",
keys: []lib.KeyValue{
{Key: "name", Value: "John"},
},
tableName: "users",
expected: "INSERT INTO users (name) VALUES (?)",
},
{
name: "multiple key-value pairs",
keys: []lib.KeyValue{
{Key: "name", Value: "John"},
{Key: "age", Value: "30"},
},
tableName: "users",
expected: "INSERT INTO users (name, age) VALUES (?, ?)",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inserter := &MariaDBSQLInserter{
keys: tt.keys,
tableName: tt.tableName,
}

result := inserter.Query()
assert.Equal(t, tt.expected, result)
})
}
}
30 changes: 30 additions & 0 deletions mysql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,33 @@
// SOFTWARE.

package mysql

import (
"fmt"
"strings"

"github.com/seipan/csql/lib"
)

type MySQLInserter struct {
keys []lib.KeyValue
tableName string
}

func (i *MySQLInserter) Query() string {
placeholders := make([]string, 0, len(i.keys))
keys := make([]string, 0, len(i.keys))

for _, kv := range i.keys {
keys = append(keys, kv.Key)
placeholders = append(placeholders, "?")
}

query := fmt.Sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
i.tableName,
strings.Join(keys, ", "),
strings.Join(placeholders, ", "),
)
return query
}
67 changes: 67 additions & 0 deletions mysql/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// MIT License

// Copyright (c) 2023 Yamasaki Shotaro

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package mysql

import (
"testing"

"github.com/seipan/csql/lib"
"github.com/stretchr/testify/assert"
)

func TestMtSQLInserter_Query(t *testing.T) {
tests := []struct {
name string
keys []lib.KeyValue
tableName string
expected string
}{
{
name: "single key-value pair",
keys: []lib.KeyValue{
{Key: "name", Value: "John"},
},
tableName: "users",
expected: "INSERT INTO users (name) VALUES (?)",
},
{
name: "multiple key-value pairs",
keys: []lib.KeyValue{
{Key: "name", Value: "John"},
{Key: "age", Value: "30"},
},
tableName: "users",
expected: "INSERT INTO users (name, age) VALUES (?, ?)",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inserter := &MySQLInserter{
keys: tt.keys,
tableName: tt.tableName,
}
assert.Equal(t, tt.expected, inserter.Query())
})
}
}
30 changes: 30 additions & 0 deletions postgresql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,33 @@
// SOFTWARE.

package postgresql

import (
"fmt"
"strings"

"github.com/seipan/csql/lib"
)

type PostgresSQLInserter struct {
keys []lib.KeyValue
tableName string
}

func (i *PostgresSQLInserter) Query() string {
placeholders := make([]string, 0, len(i.keys))
keys := make([]string, 0, len(i.keys))

for idx, kv := range i.keys {
keys = append(keys, kv.Key)
placeholders = append(placeholders, fmt.Sprintf("$%d", idx+1))
}

query := fmt.Sprintf(
"INSERT INTO %s (%s) VALUES (%s)",
i.tableName,
strings.Join(keys, ", "),
strings.Join(placeholders, ", "),
)
return query
}
68 changes: 68 additions & 0 deletions postgresql/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// MIT License

// Copyright (c) 2023 Yamasaki Shotaro

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package postgresql

import (
"testing"

"github.com/seipan/csql/lib"
"github.com/stretchr/testify/assert"
)

func TestPostgresSQLInserter_Query(t *testing.T) {
tests := []struct {
name string
keys []lib.KeyValue
tableName string
expected string
}{
{
name: "single key-value pair",
keys: []lib.KeyValue{
{Key: "name", Value: "John"},
},
tableName: "users",
expected: "INSERT INTO users (name) VALUES ($1)",
},
{
name: "multiple key-value pairs",
keys: []lib.KeyValue{
{Key: "name", Value: "John"},
{Key: "age", Value: "30"},
},
tableName: "users",
expected: "INSERT INTO users (name, age) VALUES ($1, $2)",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inserter := &PostgresSQLInserter{
keys: tt.keys,
tableName: tt.tableName,
}

assert.Equal(t, tt.expected, inserter.Query())
})
}
}
Loading

0 comments on commit f1bbddf

Please sign in to comment.