-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters