add enableUpsert=true

and rename config to upsertQuery
This commit is contained in:
LazyDBA247-Anyvision 2021-03-30 00:32:03 +03:00
parent 3cf84b5fae
commit 4c51e6a660
7 changed files with 46 additions and 34 deletions

View File

@ -120,10 +120,9 @@ connection_max_idle = 2
connection_max_open = 100 connection_max_open = 100
connection_max_lifetime_seconds = 0 connection_max_lifetime_seconds = 0
interpolateParams = false interpolateParams = false
# Empty insertQuery will use the default insert query # if insert/upsert failing, you can disable upsert or update query syntax to match your RDBMS syntax:
# example insertQuery can be for UPSERT syntax: upsertQuery = """INSERT INTO ` + "`%s`" + ` (dirhash,name,directory,meta) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE meta = VALUES(meta)"""
# insertQuery = """INSERT INTO ` + "`%s`" + ` (dirhash,name,directory,meta) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE meta = VALUES(meta)""" enableUpsert = true
insertQuery = ""
[mysql2] # or memsql, tidb [mysql2] # or memsql, tidb
enabled = false enabled = false
@ -145,10 +144,9 @@ connection_max_idle = 2
connection_max_open = 100 connection_max_open = 100
connection_max_lifetime_seconds = 0 connection_max_lifetime_seconds = 0
interpolateParams = false interpolateParams = false
# Empty insertQuery will use the default insert query # if insert/upsert failing, you can disable upsert or update query syntax to match your RDBMS syntax:
# example insertQuery can be for UPSERT syntax: upsertQuery = """INSERT INTO ` + "`%s`" + ` (dirhash,name,directory,meta) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE meta = VALUES(meta)"""
# insertQuery = """INSERT INTO ` + "`%s`" + ` (dirhash,name,directory,meta) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE meta = VALUES(meta)""" enableUpsert = true
insertQuery = ""
[postgres] # or cockroachdb, YugabyteDB [postgres] # or cockroachdb, YugabyteDB
# CREATE TABLE IF NOT EXISTS filemeta ( # CREATE TABLE IF NOT EXISTS filemeta (
@ -169,10 +167,9 @@ sslmode = "disable"
connection_max_idle = 100 connection_max_idle = 100
connection_max_open = 100 connection_max_open = 100
connection_max_lifetime_seconds = 0 connection_max_lifetime_seconds = 0
# Empty insertQuery will use the default insert query # if insert/upsert failing, you can disable upsert or update query syntax to match your RDBMS syntax:
# example insertQuery can be for UPSERT syntax: upsertQuery = """INSERT INTO "%[1]s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4) ON CONFLICT ON CONSTRAINT "%[1]s_pkey" DO UPDATE SET meta = EXCLUDED.meta WHERE "%[1]s".meta != EXCLUDED.meta"""
# insertQuery = """INSERT INTO "%[1]s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4) ON CONFLICT ON CONSTRAINT "%[1]s_pkey" DO UPDATE SET meta = EXCLUDED.meta WHERE "%[1]s".meta != EXCLUDED.meta""" enableUpsert = true
insertQuery = ""
[postgres2] [postgres2]
enabled = false enabled = false
@ -195,10 +192,9 @@ sslmode = "disable"
connection_max_idle = 100 connection_max_idle = 100
connection_max_open = 100 connection_max_open = 100
connection_max_lifetime_seconds = 0 connection_max_lifetime_seconds = 0
# Empty insertQuery will use the default insert query # if insert/upsert failing, you can disable upsert or update query syntax to match your RDBMS syntax:
# example insertQuery can be for UPSERT syntax: upsertQuery = """INSERT INTO "%[1]s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4) ON CONFLICT ON CONSTRAINT "%[1]s_pkey" DO UPDATE SET meta = EXCLUDED.meta WHERE "%[1]s".meta != EXCLUDED.meta"""
# insertQuery = """INSERT INTO "%[1]s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4) ON CONFLICT ON CONSTRAINT "%[1]s_pkey" DO UPDATE SET meta = EXCLUDED.meta WHERE "%[1]s".meta != EXCLUDED.meta""" enableUpsert = true
insertQuery = ""
[cassandra] [cassandra]
# CREATE TABLE filemeta ( # CREATE TABLE filemeta (

View File

@ -10,7 +10,7 @@ import (
type SqlGenMysql struct { type SqlGenMysql struct {
CreateTableSqlTemplate string CreateTableSqlTemplate string
DropTableSqlTemplate string DropTableSqlTemplate string
InsertQueryTemplate string UpsertQueryTemplate string
} }
var ( var (
@ -18,8 +18,8 @@ var (
) )
func (gen *SqlGenMysql) GetSqlInsert(tableName string) string { func (gen *SqlGenMysql) GetSqlInsert(tableName string) string {
if gen.InsertQueryTemplate != "" { if gen.UpsertQueryTemplate != "" {
return fmt.Sprintf(gen.InsertQueryTemplate, tableName) return fmt.Sprintf(gen.UpsertQueryTemplate, tableName)
} else { } else {
return fmt.Sprintf("INSERT INTO `%s` (dirhash,name,directory,meta) VALUES(?,?,?,?)", tableName) return fmt.Sprintf("INSERT INTO `%s` (dirhash,name,directory,meta) VALUES(?,?,?,?)", tableName)
} }

View File

@ -30,7 +30,8 @@ func (store *MysqlStore) GetName() string {
func (store *MysqlStore) Initialize(configuration util.Configuration, prefix string) (err error) { func (store *MysqlStore) Initialize(configuration util.Configuration, prefix string) (err error) {
return store.initialize( return store.initialize(
configuration.GetString(prefix+"insertQuery"), configuration.GetString(prefix+"upsertQuery"),
configuration.GetString(prefix+"enableUpsert"),
configuration.GetString(prefix+"username"), configuration.GetString(prefix+"username"),
configuration.GetString(prefix+"password"), configuration.GetString(prefix+"password"),
configuration.GetString(prefix+"hostname"), configuration.GetString(prefix+"hostname"),
@ -43,14 +44,17 @@ func (store *MysqlStore) Initialize(configuration util.Configuration, prefix str
) )
} }
func (store *MysqlStore) initialize(insertQuery, user, password, hostname string, port int, database string, maxIdle, maxOpen, func (store *MysqlStore) initialize(upsertQuery, enableUpsert, user, password, hostname string, port int, database string, maxIdle, maxOpen,
maxLifetimeSeconds int, interpolateParams bool) (err error) { maxLifetimeSeconds int, interpolateParams bool) (err error) {
store.SupportBucketTable = false store.SupportBucketTable = false
if !enableUpsert {
upsertQuery = ""
}
store.SqlGenerator = &SqlGenMysql{ store.SqlGenerator = &SqlGenMysql{
CreateTableSqlTemplate: "", CreateTableSqlTemplate: "",
DropTableSqlTemplate: "drop table `%s`", DropTableSqlTemplate: "drop table `%s`",
InsertQueryTemplate: insertQuery, UpsertQueryTemplate: upsertQuery,
} }
sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, user, password, hostname, port, database) sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, user, password, hostname, port, database)

View File

@ -32,7 +32,8 @@ func (store *MysqlStore2) GetName() string {
func (store *MysqlStore2) Initialize(configuration util.Configuration, prefix string) (err error) { func (store *MysqlStore2) Initialize(configuration util.Configuration, prefix string) (err error) {
return store.initialize( return store.initialize(
configuration.GetString(prefix+"createTable"), configuration.GetString(prefix+"createTable"),
configuration.GetString(prefix+"insertQuery"), configuration.GetString(prefix+"upsertQuery"),
configuration.GetString(prefix+"enableUpsert"),
configuration.GetString(prefix+"username"), configuration.GetString(prefix+"username"),
configuration.GetString(prefix+"password"), configuration.GetString(prefix+"password"),
configuration.GetString(prefix+"hostname"), configuration.GetString(prefix+"hostname"),
@ -45,14 +46,17 @@ func (store *MysqlStore2) Initialize(configuration util.Configuration, prefix st
) )
} }
func (store *MysqlStore2) initialize(createTable, insertQuery, user, password, hostname string, port int, database string, maxIdle, maxOpen, func (store *MysqlStore2) initialize(createTable, upsertQuery, enableUpsert, user, password, hostname string, port int, database string, maxIdle, maxOpen,
maxLifetimeSeconds int, interpolateParams bool) (err error) { maxLifetimeSeconds int, interpolateParams bool) (err error) {
store.SupportBucketTable = true store.SupportBucketTable = true
if !enableUpsert {
upsertQuery = ""
}
store.SqlGenerator = &mysql.SqlGenMysql{ store.SqlGenerator = &mysql.SqlGenMysql{
CreateTableSqlTemplate: createTable, CreateTableSqlTemplate: createTable,
DropTableSqlTemplate: "drop table `%s`", DropTableSqlTemplate: "drop table `%s`",
InsertQueryTemplate: insertQuery, UpsertQueryTemplate: upsertQuery,
} }
sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, user, password, hostname, port, database) sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, user, password, hostname, port, database)

View File

@ -10,7 +10,7 @@ import (
type SqlGenPostgres struct { type SqlGenPostgres struct {
CreateTableSqlTemplate string CreateTableSqlTemplate string
DropTableSqlTemplate string DropTableSqlTemplate string
InsertQueryTemplate string UpsertQueryTemplate string
} }
var ( var (
@ -18,8 +18,8 @@ var (
) )
func (gen *SqlGenPostgres) GetSqlInsert(tableName string) string { func (gen *SqlGenPostgres) GetSqlInsert(tableName string) string {
if gen.InsertQueryTemplate != "" { if gen.UpsertQueryTemplate != "" {
return fmt.Sprintf(gen.InsertQueryTemplate, tableName) return fmt.Sprintf(gen.UpsertQueryTemplate, tableName)
} else { } else {
return fmt.Sprintf(`INSERT INTO "%s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4)`, tableName) return fmt.Sprintf(`INSERT INTO "%s" (dirhash,name,directory,meta) VALUES($1,$2,$3,$4)`, tableName)
} }

View File

@ -29,7 +29,8 @@ func (store *PostgresStore) GetName() string {
func (store *PostgresStore) Initialize(configuration util.Configuration, prefix string) (err error) { func (store *PostgresStore) Initialize(configuration util.Configuration, prefix string) (err error) {
return store.initialize( return store.initialize(
configuration.GetString(prefix+"insertQuery"), configuration.GetString(prefix+"upsertQuery"),
configuration.GetString(prefix+"enableUpsert"),
configuration.GetString(prefix+"username"), configuration.GetString(prefix+"username"),
configuration.GetString(prefix+"password"), configuration.GetString(prefix+"password"),
configuration.GetString(prefix+"hostname"), configuration.GetString(prefix+"hostname"),
@ -43,13 +44,16 @@ func (store *PostgresStore) Initialize(configuration util.Configuration, prefix
) )
} }
func (store *PostgresStore) initialize(insertQuery, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { func (store *PostgresStore) initialize(upsertQuery, enableUpsert, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) {
store.SupportBucketTable = false store.SupportBucketTable = false
if !enableUpsert {
upsertQuery = ""
}
store.SqlGenerator = &SqlGenPostgres{ store.SqlGenerator = &SqlGenPostgres{
CreateTableSqlTemplate: "", CreateTableSqlTemplate: "",
DropTableSqlTemplate: `drop table "%s"`, DropTableSqlTemplate: `drop table "%s"`,
InsertQueryTemplate: insertQuery, UpsertQueryTemplate: upsertQuery,
} }
sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode) sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode)

View File

@ -32,7 +32,8 @@ func (store *PostgresStore2) GetName() string {
func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix string) (err error) { func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix string) (err error) {
return store.initialize( return store.initialize(
configuration.GetString(prefix+"createTable"), configuration.GetString(prefix+"createTable"),
configuration.GetString(prefix+"insertQuery"), configuration.GetString(prefix+"upsertQuery"),
configuration.GetString(prefix+"enableUpsert"),
configuration.GetString(prefix+"username"), configuration.GetString(prefix+"username"),
configuration.GetString(prefix+"password"), configuration.GetString(prefix+"password"),
configuration.GetString(prefix+"hostname"), configuration.GetString(prefix+"hostname"),
@ -46,13 +47,16 @@ func (store *PostgresStore2) Initialize(configuration util.Configuration, prefix
) )
} }
func (store *PostgresStore2) initialize(createTable, insertQuery, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) { func (store *PostgresStore2) initialize(createTable, upsertQuery, enableUpsert, user, password, hostname string, port int, database, schema, sslmode string, maxIdle, maxOpen, maxLifetimeSeconds int) (err error) {
store.SupportBucketTable = true store.SupportBucketTable = true
if !enableUpsert {
upsertQuery = ""
}
store.SqlGenerator = &postgres.SqlGenPostgres{ store.SqlGenerator = &postgres.SqlGenPostgres{
CreateTableSqlTemplate: createTable, CreateTableSqlTemplate: createTable,
DropTableSqlTemplate: `drop table "%s"`, DropTableSqlTemplate: `drop table "%s"`,
InsertQueryTemplate: insertQuery, UpsertQueryTemplate: upsertQuery,
} }
sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode) sqlUrl := fmt.Sprintf(CONNECTION_URL_PATTERN, hostname, port, sslmode)