Skip to content

Commit

Permalink
lib/pq対応
Browse files Browse the repository at this point in the history
  • Loading branch information
mazrean committed Oct 8, 2023
1 parent ab541bf commit 1ffde02
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 6 deletions.
2 changes: 1 addition & 1 deletion db/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
re: regexp.MustCompile(`(\?\s*,\s*)+`),
to: "..., ",
}, {
re: regexp.MustCompile(`(\(..., \?\)\s*,\s*)+`),
re: regexp.MustCompile(`(\(\.\.\., \?\)\s*,\s*)+`),
to: "..., ",
}}
mysqlNormalizeCacheLocker = &sync.RWMutex{}
Expand Down
88 changes: 88 additions & 0 deletions db/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package isudb

import (
"database/sql"
"regexp"
"strings"
"sync"

"github.com/lib/pq"
)

func init() {
sql.Register("isupostgres", wrapDriver(pq.Driver{}, postgresSegmentBuilder{}))
}

type postgresSegmentBuilder struct{}

func (postgresSegmentBuilder) driver() string {
return "postgres"
}

func (psb postgresSegmentBuilder) parseDSN(dsn string) *measureSegment {
strConfig, err := pq.ParseURL(dsn)
if err != nil {
strConfig = dsn
}

splitedConfigs := strings.Split(strConfig, " ")
host := "localhost"
port := "5432"
for _, config := range splitedConfigs {
switch {
case strings.HasPrefix(config, "host="):
host = strings.TrimPrefix(config, "host=")
case strings.HasPrefix(config, "port="):
port = strings.TrimPrefix(config, "port=")
}
}

return &measureSegment{
driver: psb.driver(),
addr: host + ":" + port,
normalizer: psb.normalizer,
}
}

var (
postgresReList = []struct {
re *regexp.Regexp
to string
}{{
re: regexp.MustCompile(`(\$(\d*)\s*,\s*)+\$(\d*)`),
to: "..., ?",
}, {
re: regexp.MustCompile(`(\(..., \?\)\s*,\s*)+`),
to: "..., ",
}}
postgresNormalizeCacheLocker = &sync.RWMutex{}
postgresNormalizeCache = make(map[string]string, 50)
)

func (postgresSegmentBuilder) normalizer(query string) string {
var (
normalizedQuery string
ok bool
)
func() {
postgresNormalizeCacheLocker.RLock()
defer postgresNormalizeCacheLocker.RUnlock()
normalizedQuery, ok = postgresNormalizeCache[query]
}()
if ok {
return normalizedQuery
}

normalizedQuery = query
for _, re := range postgresReList {
normalizedQuery = re.re.ReplaceAllString(normalizedQuery, re.to)
}

func() {
postgresNormalizeCacheLocker.Lock()
defer postgresNormalizeCacheLocker.Unlock()
postgresNormalizeCache[query] = normalizedQuery
}()

return normalizedQuery
}
42 changes: 42 additions & 0 deletions db/postgres_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package isudb

import (
"fmt"
"strings"
"testing"
)

func BenchmarkPostgresNormalizer(b *testing.B) {
psb := postgresSegmentBuilder{}

queryPart := fmt.Sprintf("(%s$2)", strings.Repeat("$1, ", 5))
query := fmt.Sprintf("INSERT INTO users (name, email, password, salt, created_at, updated_at) VALUES %s", strings.Repeat(queryPart+", ", 999)+queryPart)

for i := 0; i < b.N; i++ {
psb.normalizer(query)
}
}

func TestPostgresNormalizer(t *testing.T) {
psb := postgresSegmentBuilder{}

tests := []string{
"$1",
}

for _, test := range tests {
test := test
t.Run(test, func(t *testing.T) {
t.Parallel()

queryPart := fmt.Sprintf("(%s%s)", strings.Repeat(fmt.Sprintf("%s, ", test), 5), test)
query := fmt.Sprintf("INSERT INTO users (name, email, password, salt, created_at, updated_at) VALUES %s", strings.Repeat(queryPart+", ", 999)+queryPart)

normalizedQuery := psb.normalizer(query)

if normalizedQuery != "INSERT INTO users (name, email, password, salt, created_at, updated_at) VALUES ..., (..., ?)" {
t.Errorf("unexpected query: %s", normalizedQuery)
}
})
}
}
4 changes: 4 additions & 0 deletions db/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func DBMetricsSetup[T interface {
if isutools.Enable {
openDriverName = "isusqlite3"
}
case "postgres":
if isutools.Enable {
openDriverName = "isupostgres"
}
}

CONNECT:
Expand Down
6 changes: 3 additions & 3 deletions db/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ var (
re *regexp.Regexp
to string
}{{
re: regexp.MustCompile(`((?:\?(\d*)|[@:$][0-9A-Fa-f]+)\s*,\s*)+`),
to: "..., ",
re: regexp.MustCompile(`((?:\?(\d*)|[@:$][0-9A-Fa-f]+)\s*,\s*)+(?:\?(\d*)|[@:$][0-9A-Fa-f]+)`),
to: "..., ?",
}, {
re: regexp.MustCompile(`(\(\.\.\., ((\?[0-9]*)|[@:$][0-9A-Fa-f]+)\)\s*,\s*)+`),
re: regexp.MustCompile(`(\(\.\.\., \?\)\s*,\s*)+`),
to: "..., ",
}}
sqlite3NormalizeCacheLocker = &sync.RWMutex{}
Expand Down
4 changes: 2 additions & 2 deletions db/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func BenchmarkSQLite3Normalizer(b *testing.B) {
ssb := sqlite3SegmentBuilder{}

queryPart := fmt.Sprintf("(%s?)", strings.Repeat("?, ", 5))
queryPart := fmt.Sprintf("(%s?2)", strings.Repeat("?1, ", 5))
query := fmt.Sprintf("INSERT INTO users (name, email, password, salt, created_at, updated_at) VALUES %s", strings.Repeat(queryPart+", ", 999)+queryPart)

for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -38,7 +38,7 @@ func TestSQLite3Normalizer(t *testing.T) {

normalizedQuery := ssb.normalizer(query)

if normalizedQuery != fmt.Sprintf("INSERT INTO users (name, email, password, salt, created_at, updated_at) VALUES ..., (..., %s)", test) {
if normalizedQuery != "INSERT INTO users (name, email, password, salt, created_at, updated_at) VALUES ..., (..., ?)" {
t.Errorf("unexpected query: %s", normalizedQuery)
}
})
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ require (
github.com/gofiber/fiber/v2 v2.49.2
github.com/golang/protobuf v1.5.3 // indirect
github.com/labstack/echo/v4 v4.11.1
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.17
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mazrean/iwrapper v0.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
Expand Down

0 comments on commit 1ffde02

Please sign in to comment.