Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use prepared stmt in sql #3100

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions internal/pkg/store/sql/sqlKv.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,15 @@
func (kv *sqlKvStore) Get(key string, value interface{}) (bool, error) {
result := false
err := kv.database.Apply(func(db *sql.DB) error {
query := fmt.Sprintf("SELECT val FROM '%s' WHERE key='%s';", kv.table, key)
row := db.QueryRow(query)
query := fmt.Sprintf("SELECT val FROM '%s' WHERE key=?;", kv.table)
stmt, err := db.Prepare(query)
if err != nil {
result = false
return nil

Check warning on line 96 in internal/pkg/store/sql/sqlKv.go

View check run for this annotation

Codecov / codecov/patch

internal/pkg/store/sql/sqlKv.go#L95-L96

Added lines #L95 - L96 were not covered by tests
}
row := stmt.QueryRow(key)
var tmp []byte
err := row.Scan(&tmp)
err = row.Scan(&tmp)
if err != nil {
result = false
return nil
Expand All @@ -110,9 +115,13 @@
func (kv *sqlKvStore) GetKeyedState(key string) (interface{}, error) {
var value interface{}
err := kv.database.Apply(func(db *sql.DB) error {
query := fmt.Sprintf("SELECT val FROM '%s' WHERE key='%s';", kv.table, key)
row := db.QueryRow(query)
err := row.Scan(&value)
query := fmt.Sprintf("SELECT val FROM '%s' WHERE key=?;", kv.table)
stmt, err := db.Prepare(query)
if err != nil {
return err

Check warning on line 121 in internal/pkg/store/sql/sqlKv.go

View check run for this annotation

Codecov / codecov/patch

internal/pkg/store/sql/sqlKv.go#L121

Added line #L121 was not covered by tests
}
row := stmt.QueryRow(key)
err = row.Scan(&value)
if err != nil {
return err
}
Expand All @@ -137,10 +146,14 @@

func (kv *sqlKvStore) Delete(key string) error {
return kv.database.Apply(func(db *sql.DB) error {
query := fmt.Sprintf("SELECT key FROM '%s' WHERE key='%s';", kv.table, key)
row := db.QueryRow(query)
query := fmt.Sprintf("SELECT key FROM '%s' WHERE key=?;", kv.table)
stmt, err := db.Prepare(query)
if err != nil {
return err

Check warning on line 152 in internal/pkg/store/sql/sqlKv.go

View check run for this annotation

Codecov / codecov/patch

internal/pkg/store/sql/sqlKv.go#L152

Added line #L152 was not covered by tests
}
row := stmt.QueryRow(key)
var tmp []byte
err := row.Scan(&tmp)
err = row.Scan(&tmp)
if nil != err || 0 == len(tmp) {
return errorx.NewWithCode(errorx.NOT_FOUND, fmt.Sprintf("%s is not found", key))
}
Expand Down
24 changes: 18 additions & 6 deletions internal/pkg/store/sql/sqlTs.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@
func (t ts) Get(key int64, value interface{}) (bool, error) {
result := false
err := t.database.Apply(func(db *sql.DB) error {
query := fmt.Sprintf("SELECT val FROM %s WHERE key=%d;", t.table, key)
row := db.QueryRow(query)
query := fmt.Sprintf("SELECT val FROM %s WHERE key=?;", t.table)
stmt, err := db.Prepare(query)
if err != nil {
return err

Check warning on line 83 in internal/pkg/store/sql/sqlTs.go

View check run for this annotation

Codecov / codecov/patch

internal/pkg/store/sql/sqlTs.go#L83

Added line #L83 was not covered by tests
}
row := stmt.QueryRow(key)
var tmp []byte
switch err := row.Scan(&tmp); err {
case sql.ErrNoRows:
Expand Down Expand Up @@ -111,16 +115,24 @@

func (t ts) Delete(key int64) error {
return t.database.Apply(func(db *sql.DB) error {
query := fmt.Sprintf("DELETE FROM %s WHERE key=%d;", t.table, key)
_, err := db.Exec(query)
query := fmt.Sprintf("DELETE FROM %s WHERE key=?;", t.table)
stmt, err := db.Prepare(query)
if err != nil {
return err

Check warning on line 121 in internal/pkg/store/sql/sqlTs.go

View check run for this annotation

Codecov / codecov/patch

internal/pkg/store/sql/sqlTs.go#L121

Added line #L121 was not covered by tests
}
_, err = stmt.Exec(key)
return err
})
}

func (t ts) DeleteBefore(key int64) error {
return t.database.Apply(func(db *sql.DB) error {
query := fmt.Sprintf("DELETE FROM %s WHERE key<%d;", t.table, key)
_, err := db.Exec(query)
query := fmt.Sprintf("DELETE FROM %s WHERE key<?;", t.table)
stmt, err := db.Prepare(query)
if err != nil {
return err

Check warning on line 133 in internal/pkg/store/sql/sqlTs.go

View check run for this annotation

Codecov / codecov/patch

internal/pkg/store/sql/sqlTs.go#L133

Added line #L133 was not covered by tests
}
_, err = stmt.Exec(key)
return err
})
}
Expand Down
Loading