mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
Merge branch 'filer-postgres-shared-connection' into filer-postgres-flat
This commit is contained in:
commit
9bbc0faee5
3 files changed
+132
-12
No files matched your search
@@ -1,6 +1,8 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -96,3 +98,46 @@ func listQueries(gen *SqlGenMysql) []string {
|
||||
inclusive, _ := gen.GetSqlListInclusive("filemeta", "/dir", "", "%", 10)
|
||||
return []string{exclusive, inclusive}
|
||||
}
|
||||
|
||||
func TestStatementArguments(t *testing.T) {
|
||||
key := abstract_sql.EntryRowKey("/dir", "name")
|
||||
other := abstract_sql.EntryRowKey("/other", "file")
|
||||
meta := []byte("meta")
|
||||
column := map[string]interface{}{"dirhash": key.DirHash, "name": key.Name, "directory": key.Directory, "meta": meta}
|
||||
plain, upsert := &SqlGenMysql{}, &SqlGenMysql{UpsertQueryTemplate: DefaultUpsertQuery}
|
||||
type statement struct {
|
||||
sql string
|
||||
args []interface{}
|
||||
}
|
||||
stmt := func(sql string, args []interface{}) statement { return statement{sql, args} }
|
||||
for name, tc := range map[string]struct {
|
||||
got statement
|
||||
want []interface{}
|
||||
}{
|
||||
"insert": {stmt(plain.GetSqlInsert("t", key, meta)), []interface{}{key.DirHash, "name", "/dir", meta}},
|
||||
"upsert": {stmt(upsert.GetSqlInsert("t", key, meta)), []interface{}{key.DirHash, "name", "/dir", meta}},
|
||||
"update": {stmt(plain.GetSqlUpdate("t", key, meta)), []interface{}{meta, key.DirHash, "name", "/dir"}},
|
||||
"find": {stmt(plain.GetSqlFind("t", key)), []interface{}{key.DirHash, "name", "/dir"}},
|
||||
"find batch": {stmt(plain.GetSqlFindBatch("t", []abstract_sql.RowKey{key, other})), []interface{}{key.DirHash, "name", "/dir", other.DirHash, "file", "/other"}},
|
||||
"delete": {stmt(plain.GetSqlDelete("t", key)), []interface{}{key.DirHash, "name", "/dir"}},
|
||||
"delete children": {stmt(plain.GetSqlDeleteFolderChildren("t", "/dir")), []interface{}{key.DirHash, "/dir"}},
|
||||
"list exclusive": {stmt(plain.GetSqlListExclusive("t", "/dir", "start", "p%", 10)), []interface{}{key.DirHash, "start", "/dir", "p%", int64(10)}},
|
||||
"list inclusive": {stmt(plain.GetSqlListInclusive("t", "/dir", "start", "p%", 10)), []interface{}{key.DirHash, "start", "/dir", "p%", int64(10)}},
|
||||
"scan page": {stmt(plain.GetSqlScanPage("t", key, 10)), []interface{}{key.DirHash, key.DirHash, "name", 10}},
|
||||
} {
|
||||
if !reflect.DeepEqual(tc.got.args, tc.want) {
|
||||
t.Errorf("%s: args %v, want %v", name, tc.got.args, tc.want)
|
||||
}
|
||||
if n := strings.Count(tc.got.sql, "?"); n != len(tc.got.args) {
|
||||
t.Errorf("%s: %d placeholders for %d args in %s", name, n, len(tc.got.args), tc.got.sql)
|
||||
}
|
||||
if name == "find batch" {
|
||||
continue
|
||||
}
|
||||
for i, m := range regexp.MustCompile("(?:`(\\w+)` = )?\\?").FindAllStringSubmatch(tc.got.sql, -1) {
|
||||
if want, ok := column[m[1]]; ok && i < len(tc.got.args) && !reflect.DeepEqual(tc.got.args[i], want) {
|
||||
t.Errorf("%s: `%s` bound to %v, want %v", name, m[1], tc.got.args[i], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,17 +47,17 @@ func ResolveCreateTableQuery(createTable string) string {
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlInsert(tableName string, key abstract_sql.RowKey, meta []byte) (string, []interface{}) {
|
||||
if gen.UpsertQueryTemplate != "" {
|
||||
return fmt.Sprintf(gen.UpsertQueryTemplate, tableName), key.Args(meta)
|
||||
return fmt.Sprintf(gen.UpsertQueryTemplate, tableIdent(tableName)), key.Args(meta)
|
||||
}
|
||||
return fmt.Sprintf(`INSERT INTO "%s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4)`, tableName), key.Args(meta)
|
||||
return fmt.Sprintf(`INSERT INTO "%s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4)`, tableIdent(tableName)), key.Args(meta)
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlUpdate(tableName string, key abstract_sql.RowKey, meta []byte) (string, []interface{}) {
|
||||
return fmt.Sprintf(`UPDATE "%s" SET meta=$4 WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableName), key.Args(meta)
|
||||
return fmt.Sprintf(`UPDATE "%s" SET meta=$4 WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableIdent(tableName)), key.Args(meta)
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlFind(tableName string, key abstract_sql.RowKey) (string, []interface{}) {
|
||||
return fmt.Sprintf(`SELECT meta FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableName), key.Args()
|
||||
return fmt.Sprintf(`SELECT meta FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableIdent(tableName)), key.Args()
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlFindBatch(tableName string, keys []abstract_sql.RowKey) (string, []interface{}) {
|
||||
@@ -67,18 +67,24 @@ func (gen *SqlGenPostgres) GetSqlFindBatch(tableName string, keys []abstract_sql
|
||||
rows[i] = fmt.Sprintf("($%d,$%d,$%d)", 3*i+1, 3*i+2, 3*i+3)
|
||||
args = append(args, key.Args()...)
|
||||
}
|
||||
return fmt.Sprintf(`SELECT name, directory, meta FROM "%s" WHERE (dirhash, name, directory) IN (%s)`, tableName, strings.Join(rows, ",")), args
|
||||
return fmt.Sprintf(`SELECT name, directory, meta FROM "%s" WHERE (dirhash, name, directory) IN (%s)`, tableIdent(tableName), strings.Join(rows, ",")), args
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlDelete(tableName string, key abstract_sql.RowKey) (string, []interface{}) {
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableName), key.Args()
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND name=$2 AND directory=$3`, tableIdent(tableName)), key.Args()
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlDeleteFolderChildren(tableName string, dir string) (string, []interface{}) {
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND directory=$2`, tableName), []interface{}{util.HashStringToLong(dir), dir}
|
||||
return fmt.Sprintf(`DELETE FROM "%s" WHERE dirhash=$1 AND directory=$2`, tableIdent(tableName)), []interface{}{util.HashStringToLong(dir), dir}
|
||||
}
|
||||
|
||||
// nameExpr forces byte ordering on a locale-aware column via COLLATE "C".
|
||||
// tableIdent escapes a table name for the double quotes the statements put
|
||||
// around it.
|
||||
func tableIdent(tableName string) string {
|
||||
return strings.ReplaceAll(tableName, `"`, `""`)
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) nameExpr() string {
|
||||
if gen.ForceBinaryCollation {
|
||||
return `name COLLATE "C"`
|
||||
@@ -90,19 +96,19 @@ func (gen *SqlGenPostgres) nameExpr() string {
|
||||
// PostgreSQL. The name keeps the column's own collation (COLLATE "C" would
|
||||
// sort every page); the scan promises no lexical order.
|
||||
func (gen *SqlGenPostgres) GetSqlScanPage(tableName string, after abstract_sql.RowKey, limit int) (string, []interface{}) {
|
||||
return fmt.Sprintf(`SELECT dirhash, name, directory, meta FROM "%s" WHERE (dirhash, name) > ($1, $2) ORDER BY dirhash ASC, name ASC LIMIT $3`, tableName),
|
||||
return fmt.Sprintf(`SELECT dirhash, name, directory, meta FROM "%s" WHERE (dirhash, name) > ($1, $2) ORDER BY dirhash ASC, name ASC LIMIT $3`, tableIdent(tableName)),
|
||||
[]interface{}{after.DirHash, after.Name, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlListExclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableName, name, name, name),
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableIdent(tableName), name, name, name),
|
||||
[]interface{}{util.HashStringToLong(dir), startFileName, dir, namePattern, limit}
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlListInclusive(tableName string, dir, startFileName, namePattern string, limit int64) (string, []interface{}) {
|
||||
name := gen.nameExpr()
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>=$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableName, name, name, name),
|
||||
return fmt.Sprintf(`SELECT NAME, meta FROM "%s" WHERE dirhash=$1 AND %s>=$2 AND directory=$3 AND %s like $4 ESCAPE '!' ORDER BY %s ASC LIMIT $5`, tableIdent(tableName), name, name, name),
|
||||
[]interface{}{util.HashStringToLong(dir), startFileName, dir, namePattern, limit}
|
||||
}
|
||||
|
||||
@@ -110,9 +116,9 @@ func (gen *SqlGenPostgres) GetSqlCreateTable(tableName string) string {
|
||||
if gen.CreateTableSqlTemplate == "" {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(gen.CreateTableSqlTemplate, tableName)
|
||||
return fmt.Sprintf(gen.CreateTableSqlTemplate, tableIdent(tableName))
|
||||
}
|
||||
|
||||
func (gen *SqlGenPostgres) GetSqlDropTable(tableName string) string {
|
||||
return fmt.Sprintf(gen.DropTableSqlTemplate, tableName)
|
||||
return fmt.Sprintf(gen.DropTableSqlTemplate, tableIdent(tableName))
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -109,3 +112,69 @@ func listQueries(gen *SqlGenPostgres) []string {
|
||||
inclusive, _ := gen.GetSqlListInclusive("filemeta", "/dir", "", "%", 10)
|
||||
return []string{exclusive, inclusive}
|
||||
}
|
||||
|
||||
func TestStatementArguments(t *testing.T) {
|
||||
key := abstract_sql.EntryRowKey("/dir", "name")
|
||||
other := abstract_sql.EntryRowKey("/other", "file")
|
||||
meta := []byte("meta")
|
||||
column := map[string]interface{}{"dirhash": key.DirHash, "name": key.Name, "directory": key.Directory, "meta": meta}
|
||||
plain, upsert := &SqlGenPostgres{}, &SqlGenPostgres{UpsertQueryTemplate: DefaultUpsertQuery}
|
||||
type statement struct {
|
||||
sql string
|
||||
args []interface{}
|
||||
}
|
||||
stmt := func(sql string, args []interface{}) statement { return statement{sql, args} }
|
||||
for name, tc := range map[string]struct {
|
||||
got statement
|
||||
want []interface{}
|
||||
}{
|
||||
"insert": {stmt(plain.GetSqlInsert("t", key, meta)), []interface{}{key.DirHash, "name", "/dir", meta}},
|
||||
"upsert": {stmt(upsert.GetSqlInsert("t", key, meta)), []interface{}{key.DirHash, "name", "/dir", meta}},
|
||||
"update": {stmt(plain.GetSqlUpdate("t", key, meta)), []interface{}{key.DirHash, "name", "/dir", meta}},
|
||||
"find": {stmt(plain.GetSqlFind("t", key)), []interface{}{key.DirHash, "name", "/dir"}},
|
||||
"find batch": {stmt(plain.GetSqlFindBatch("t", []abstract_sql.RowKey{key, other})), []interface{}{key.DirHash, "name", "/dir", other.DirHash, "file", "/other"}},
|
||||
"delete": {stmt(plain.GetSqlDelete("t", key)), []interface{}{key.DirHash, "name", "/dir"}},
|
||||
"delete children": {stmt(plain.GetSqlDeleteFolderChildren("t", "/dir")), []interface{}{key.DirHash, "/dir"}},
|
||||
"list exclusive": {stmt(plain.GetSqlListExclusive("t", "/dir", "start", "p%", 10)), []interface{}{key.DirHash, "start", "/dir", "p%", int64(10)}},
|
||||
"list inclusive": {stmt(plain.GetSqlListInclusive("t", "/dir", "start", "p%", 10)), []interface{}{key.DirHash, "start", "/dir", "p%", int64(10)}},
|
||||
"scan page": {stmt(plain.GetSqlScanPage("t", key, 10)), []interface{}{key.DirHash, "name", 10}},
|
||||
} {
|
||||
if !reflect.DeepEqual(tc.got.args, tc.want) {
|
||||
t.Errorf("%s: args %v, want %v", name, tc.got.args, tc.want)
|
||||
}
|
||||
used := map[int]bool{}
|
||||
for _, m := range regexp.MustCompile(`\$(\d+)`).FindAllStringSubmatch(tc.got.sql, -1) {
|
||||
n, _ := strconv.Atoi(m[1])
|
||||
used[n] = true
|
||||
}
|
||||
for n := 1; n <= len(tc.got.args); n++ {
|
||||
if !used[n] {
|
||||
t.Errorf("%s: $%d unused in %s", name, n, tc.got.sql)
|
||||
}
|
||||
}
|
||||
if len(used) != len(tc.got.args) {
|
||||
t.Errorf("%s: %d placeholders for %d args in %s", name, len(used), len(tc.got.args), tc.got.sql)
|
||||
}
|
||||
if name == "find batch" {
|
||||
continue
|
||||
}
|
||||
for _, m := range regexp.MustCompile(`(\w+)\s*=\s*\$(\d+)`).FindAllStringSubmatch(tc.got.sql, -1) {
|
||||
n, _ := strconv.Atoi(m[2])
|
||||
if want, ok := column[m[1]]; ok && n <= len(tc.got.args) && !reflect.DeepEqual(tc.got.args[n-1], want) {
|
||||
t.Errorf("%s: %s bound to %v, want %v", name, m[1], tc.got.args[n-1], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStatementsEscapeTheTableName(t *testing.T) {
|
||||
gen := &SqlGenPostgres{UpsertQueryTemplate: DefaultUpsertQuery, CreateTableSqlTemplate: DefaultCreateTableQuery}
|
||||
key := abstract_sql.EntryRowKey("/", "x")
|
||||
insert, _ := gen.GetSqlInsert(`a"b`, key, nil)
|
||||
find, _ := gen.GetSqlFind(`a"b`, key)
|
||||
for _, statement := range []string{insert, find, gen.GetSqlCreateTable(`a"b`)} {
|
||||
if !strings.Contains(statement, `"a""b"`) {
|
||||
t.Fatalf("table name not escaped: %s", statement)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user