Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Commit

Permalink
Change all the sql statements and import sqlx libary (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
aqan213 authored and cdlliuy committed Dec 17, 2019
1 parent 12d173b commit c383315
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 119 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,6 @@
[submodule "src/github.com/cespare/xxhash"]
path = src/github.com/cespare/xxhash
url = https://github.com/cespare/xxhash.git
[submodule "src/github.com/jmoiron/sqlx"]
path = src/github.com/jmoiron/sqlx
url = https://github.com/jmoiron/sqlx
11 changes: 6 additions & 5 deletions src/autoscaler/db/sqldb/appmetric_sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"code.cloudfoundry.org/lager"
. "github.com/lib/pq"
"github.com/jmoiron/sqlx"

"database/sql"
"time"
Expand All @@ -15,13 +16,13 @@ import (
type AppMetricSQLDB struct {
dbConfig db.DatabaseConfig
logger lager.Logger
sqldb *sql.DB
sqldb *sqlx.DB
}

func NewAppMetricSQLDB(dbConfig db.DatabaseConfig, logger lager.Logger) (*AppMetricSQLDB, error) {
var err error

sqldb, err := sql.Open(db.PostgresDriverName, dbConfig.URL)
sqldb, err := sqlx.Open(db.PostgresDriverName, dbConfig.URL)
if err != nil {
logger.Error("open-AppMetric-db", err, lager.Data{"dbConfig": dbConfig})
return nil, err
Expand Down Expand Up @@ -53,7 +54,7 @@ func (adb *AppMetricSQLDB) Close() error {
return nil
}
func (adb *AppMetricSQLDB) SaveAppMetric(appMetric *models.AppMetric) error {
query := "INSERT INTO app_metric(app_id, metric_type, unit, timestamp, value) values($1, $2, $3, $4, $5)"
query := adb.sqldb.Rebind("INSERT INTO app_metric(app_id, metric_type, unit, timestamp, value) values(?, ?, ?, ?, ?)")
_, err := adb.sqldb.Exec(query, appMetric.AppId, appMetric.MetricType, appMetric.Unit, appMetric.Timestamp, appMetric.Value)

if err != nil {
Expand Down Expand Up @@ -118,7 +119,7 @@ func (adb *AppMetricSQLDB) RetrieveAppMetrics(appIdP string, metricTypeP string,
endP = time.Now().UnixNano()
}

query := "SELECT app_id,metric_type,value,unit,timestamp FROM app_metric WHERE app_id=$1 AND metric_type=$2 AND timestamp>=$3 AND timestamp<=$4 ORDER BY timestamp " + orderStr
query := adb.sqldb.Rebind("SELECT app_id,metric_type,value,unit,timestamp FROM app_metric WHERE app_id=? AND metric_type=? AND timestamp>=? AND timestamp<=? ORDER BY timestamp " + orderStr)
appMetricList := []*models.AppMetric{}
rows, err := adb.sqldb.Query(query, appIdP, metricTypeP, startP, endP)
if err != nil {
Expand Down Expand Up @@ -150,7 +151,7 @@ func (adb *AppMetricSQLDB) RetrieveAppMetrics(appIdP string, metricTypeP string,
}

func (adb *AppMetricSQLDB) PruneAppMetrics(before int64) error {
query := "DELETE FROM app_metric WHERE timestamp <= $1"
query := adb.sqldb.Rebind("DELETE FROM app_metric WHERE timestamp <= ?")
_, err := adb.sqldb.Exec(query, before)
if err != nil {
adb.logger.Error("prune-metrics-from-app_metric-table", err, lager.Data{"query": query, "before": before})
Expand Down
31 changes: 16 additions & 15 deletions src/autoscaler/db/sqldb/binding_sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (

"code.cloudfoundry.org/lager"
_ "github.com/lib/pq"
"github.com/jmoiron/sqlx"

"autoscaler/db"
)

type BindingSQLDB struct {
dbConfig db.DatabaseConfig
logger lager.Logger
sqldb *sql.DB
sqldb *sqlx.DB
}

func NewBindingSQLDB(dbConfig db.DatabaseConfig, logger lager.Logger) (*BindingSQLDB, error) {
sqldb, err := sql.Open(db.PostgresDriverName, dbConfig.URL)
sqldb, err := sqlx.Open(db.PostgresDriverName, dbConfig.URL)
if err != nil {
logger.Error("open-binding-db", err, lager.Data{"dbConfig": dbConfig})
return nil, err
Expand Down Expand Up @@ -51,7 +52,7 @@ func (bdb *BindingSQLDB) Close() error {
}

func (bdb *BindingSQLDB) CreateServiceInstance(serviceInstanceId string, orgId string, spaceId string) error {
query := "SELECT org_id, space_id FROM service_instance WHERE service_instance_id = $1"
query := bdb.sqldb.Rebind("SELECT org_id, space_id FROM service_instance WHERE service_instance_id =?")
rows, err := bdb.sqldb.Query(query, serviceInstanceId)
if err != nil {
bdb.logger.Error("create-service-instance", err, lager.Data{"query": query, "serviceinstanceid": serviceInstanceId, "orgid": orgId, "spaceid": spaceId})
Expand All @@ -76,9 +77,9 @@ func (bdb *BindingSQLDB) CreateServiceInstance(serviceInstanceId string, orgId s
}
rows.Close()

query = "INSERT INTO service_instance" +
query = bdb.sqldb.Rebind("INSERT INTO service_instance" +
"(service_instance_id, org_id, space_id) " +
" VALUES($1, $2, $3)"
" VALUES(?, ?, ?)")
_, err = bdb.sqldb.Exec(query, serviceInstanceId, orgId, spaceId)

if err != nil {
Expand All @@ -88,7 +89,7 @@ func (bdb *BindingSQLDB) CreateServiceInstance(serviceInstanceId string, orgId s
}

func (bdb *BindingSQLDB) DeleteServiceInstance(serviceInstanceId string) error {
query := "SELECT FROM service_instance WHERE service_instance_id = $1"
query := bdb.sqldb.Rebind("SELECT FROM service_instance WHERE service_instance_id =?")
rows, err := bdb.sqldb.Query(query, serviceInstanceId)
if err != nil {
bdb.logger.Error("delete-service-instance", err, lager.Data{"query": query, "serviceinstanceid": serviceInstanceId})
Expand All @@ -97,7 +98,7 @@ func (bdb *BindingSQLDB) DeleteServiceInstance(serviceInstanceId string) error {

if rows.Next() {
rows.Close()
query = "DELETE FROM service_instance WHERE service_instance_id = $1"
query = bdb.sqldb.Rebind("DELETE FROM service_instance WHERE service_instance_id =?")
_, err = bdb.sqldb.Exec(query, serviceInstanceId)

if err != nil {
Expand All @@ -110,7 +111,7 @@ func (bdb *BindingSQLDB) DeleteServiceInstance(serviceInstanceId string) error {
}

func (bdb *BindingSQLDB) CreateServiceBinding(bindingId string, serviceInstanceId string, appId string) error {
query := "SELECT FROM binding WHERE app_id = $1"
query := bdb.sqldb.Rebind("SELECT FROM binding WHERE app_id =?")
rows, err := bdb.sqldb.Query(query, appId)
if err != nil {
bdb.logger.Error("create-service-binding", err, lager.Data{"query": query, "appId": appId, "serviceId": serviceInstanceId, "bindingId": bindingId})
Expand All @@ -123,9 +124,9 @@ func (bdb *BindingSQLDB) CreateServiceBinding(bindingId string, serviceInstanceI
}
rows.Close()

query = "INSERT INTO binding" +
query = bdb.sqldb.Rebind("INSERT INTO binding" +
"(binding_id, service_instance_id, app_id, created_at) " +
"VALUES($1, $2, $3, $4)"
"VALUES(?, ?, ?, ?)")
_, err = bdb.sqldb.Exec(query, bindingId, serviceInstanceId, appId, time.Now())

if err != nil {
Expand All @@ -134,7 +135,7 @@ func (bdb *BindingSQLDB) CreateServiceBinding(bindingId string, serviceInstanceI
return err
}
func (bdb *BindingSQLDB) DeleteServiceBinding(bindingId string) error {
query := "SELECT FROM binding WHERE binding_id = $1"
query := bdb.sqldb.Rebind("SELECT FROM binding WHERE binding_id =?")
rows, err := bdb.sqldb.Query(query, bindingId)
if err != nil {
bdb.logger.Error("delete-service-binding", err, lager.Data{"query": query, "bindingId": bindingId})
Expand All @@ -143,7 +144,7 @@ func (bdb *BindingSQLDB) DeleteServiceBinding(bindingId string) error {

if rows.Next() {
rows.Close()
query = "DELETE FROM binding WHERE binding_id = $1"
query = bdb.sqldb.Rebind("DELETE FROM binding WHERE binding_id =?")
_, err = bdb.sqldb.Exec(query, bindingId)

if err != nil {
Expand All @@ -156,7 +157,7 @@ func (bdb *BindingSQLDB) DeleteServiceBinding(bindingId string) error {
return db.ErrDoesNotExist
}
func (bdb *BindingSQLDB) DeleteServiceBindingByAppId(appId string) error {
query := "DELETE FROM binding WHERE app_id = $1"
query := bdb.sqldb.Rebind("DELETE FROM binding WHERE app_id =?")
_, err := bdb.sqldb.Exec(query, appId)

if err != nil {
Expand All @@ -167,7 +168,7 @@ func (bdb *BindingSQLDB) DeleteServiceBindingByAppId(appId string) error {
}
func (bdb *BindingSQLDB) CheckServiceBinding(appId string) bool {
var count int
query := "SELECT COUNT(*) FROM binding WHERE app_id=$1"
query := bdb.sqldb.Rebind("SELECT COUNT(*) FROM binding WHERE app_id=?")
bdb.sqldb.QueryRow(query, appId).Scan(&count)
return count > 0
}
Expand All @@ -176,7 +177,7 @@ func (bdb *BindingSQLDB) GetDBStatus() sql.DBStats {
}
func (bdb *BindingSQLDB) GetAppIdByBindingId(bindingId string) (string, error) {
var appId string
query := "SELECT app_id FROM binding WHERE binding_id=$1"
query := bdb.sqldb.Rebind("SELECT app_id FROM binding WHERE binding_id=?")
err := bdb.sqldb.QueryRow(query, bindingId).Scan(&appId)
if err != nil {
bdb.logger.Error("get-appid-from-binding-table", err, lager.Data{"query": query, "bindingId": bindingId})
Expand Down
37 changes: 19 additions & 18 deletions src/autoscaler/db/sqldb/instancemetrics_sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"code.cloudfoundry.org/lager"
. "github.com/lib/pq"
"github.com/jmoiron/sqlx"

"context"
"database/sql"
Expand All @@ -15,11 +16,11 @@ import (
type InstanceMetricsSQLDB struct {
logger lager.Logger
dbConfig db.DatabaseConfig
sqldb *sql.DB
sqldb *sqlx.DB
}

func NewInstanceMetricsSQLDB(dbConfig db.DatabaseConfig, logger lager.Logger) (*InstanceMetricsSQLDB, error) {
sqldb, err := sql.Open(db.PostgresDriverName, dbConfig.URL)
sqldb, err := sqlx.Open(db.PostgresDriverName, dbConfig.URL)
if err != nil {
logger.Error("failed-open-instancemetrics-db", err, lager.Data{"dbConfig": dbConfig})
return nil, err
Expand Down Expand Up @@ -53,7 +54,7 @@ func (idb *InstanceMetricsSQLDB) Close() error {
}

func (idb *InstanceMetricsSQLDB) SaveMetric(metric *models.AppInstanceMetric) error {
query := "INSERT INTO appinstancemetrics(appid, instanceindex, collectedat, name, unit, value, timestamp) values($1, $2, $3, $4, $5, $6, $7)"
query := idb.sqldb.Rebind("INSERT INTO appinstancemetrics(appid, instanceindex, collectedat, name, unit, value, timestamp) values(?, ?, ?, ?, ?, ?, ?)")
_, err := idb.sqldb.Exec(query, metric.AppId, metric.InstanceIndex, metric.CollectedAt, metric.Name, metric.Unit, metric.Value, metric.Timestamp)

if err != nil {
Expand Down Expand Up @@ -117,20 +118,20 @@ func (idb *InstanceMetricsSQLDB) RetrieveInstanceMetrics(appid string, instanceI
} else {
orderStr = db.DESCSTR
}
query := "SELECT instanceindex, collectedat, unit, value, timestamp FROM appinstancemetrics WHERE " +
" appid = $1 " +
" AND name = $2 " +
" AND timestamp >= $3" +
" AND timestamp <= $4" +
" ORDER BY timestamp " + orderStr + ", instanceindex"

queryByInstanceIndex := "SELECT instanceindex, collectedat, unit, value, timestamp FROM appinstancemetrics WHERE " +
" appid = $1 " +
" AND instanceindex = $2" +
" AND name = $3 " +
" AND timestamp >= $4" +
" AND timestamp <= $5" +
" ORDER BY timestamp " + orderStr
query := idb.sqldb.Rebind("SELECT instanceindex, collectedat, unit, value, timestamp FROM appinstancemetrics WHERE " +
" appid = ? " +
" AND name = ? " +
" AND timestamp >= ?" +
" AND timestamp <= ?" +
" ORDER BY timestamp " + orderStr + ", instanceindex")

queryByInstanceIndex := idb.sqldb.Rebind("SELECT instanceindex, collectedat, unit, value, timestamp FROM appinstancemetrics WHERE " +
" appid = ? " +
" AND instanceindex = ?" +
" AND name = ? " +
" AND timestamp >= ?" +
" AND timestamp <= ?" +
" ORDER BY timestamp " + orderStr)

if end < 0 {
end = time.Now().UnixNano()
Expand Down Expand Up @@ -185,7 +186,7 @@ func (idb *InstanceMetricsSQLDB) RetrieveInstanceMetrics(appid string, instanceI
return mtrcs, nil
}
func (idb *InstanceMetricsSQLDB) PruneInstanceMetrics(before int64) error {
query := "DELETE FROM appinstancemetrics WHERE timestamp <= $1"
query := idb.sqldb.Rebind("DELETE FROM appinstancemetrics WHERE timestamp <= ?")
_, err := idb.sqldb.Exec(query, before)
if err != nil {
idb.logger.Error("failed-prune-instancemetric-from-appinstancemetrics-table", err, lager.Data{"query": query, "before": before})
Expand Down
15 changes: 8 additions & 7 deletions src/autoscaler/db/sqldb/lock_sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"code.cloudfoundry.org/lager"
_ "github.com/lib/pq"
"github.com/jmoiron/sqlx"

"autoscaler/db"
"autoscaler/models"
Expand All @@ -16,11 +17,11 @@ type LockSQLDB struct {
dbConfig db.DatabaseConfig
logger lager.Logger
table string
sqldb *sql.DB
sqldb *sqlx.DB
}

func NewLockSQLDB(dbConfig db.DatabaseConfig, table string, logger lager.Logger) (*LockSQLDB, error) {
sqldb, err := sql.Open(db.PostgresDriverName, dbConfig.URL)
sqldb, err := sqlx.Open(db.PostgresDriverName, dbConfig.URL)
if err != nil {
logger.Error("open-lock-db", err, lager.Data{"dbConfig": dbConfig})
return nil, err
Expand Down Expand Up @@ -84,7 +85,7 @@ func (ldb *LockSQLDB) fetch(tx *sql.Tx) (*models.Lock, error) {

func (ldb *LockSQLDB) remove(owner string, tx *sql.Tx) error {
ldb.logger.Debug("removing-lock", lager.Data{"Owner": owner})
query := "DELETE FROM " + ldb.table + " WHERE owner = $1"
query := ldb.sqldb.Rebind("DELETE FROM " + ldb.table + " WHERE owner = ?")
if _, err := tx.Exec(query, owner); err != nil {
ldb.logger.Error("failed-to-delete-lock-details-during-release-lock", err)
return err
Expand All @@ -99,7 +100,7 @@ func (ldb *LockSQLDB) insert(lockDetails *models.Lock, tx *sql.Tx) error {
ldb.logger.Error("error-getting-timestamp-while-inserting-lock-details", err)
return err
}
query := "INSERT INTO " + ldb.table + " (owner,lock_timestamp,ttl) VALUES ($1,$2,$3)"
query := ldb.sqldb.Rebind("INSERT INTO " + ldb.table + " (owner,lock_timestamp,ttl) VALUES (?,?,?)")
if _, err = tx.Exec(query, lockDetails.Owner, currentTimestamp, int64(lockDetails.Ttl/time.Second)); err != nil {
ldb.logger.Error("failed-to-insert-lock-details", err)
return err
Expand All @@ -114,7 +115,7 @@ func (ldb *LockSQLDB) renew(owner string, tx *sql.Tx) error {
ldb.logger.Error("error-getting-timestamp-while-renewing-lock", err)
return err
}
updatequery := "UPDATE " + ldb.table + " SET lock_timestamp=$1 where owner=$2"
updatequery := ldb.sqldb.Rebind("UPDATE " + ldb.table + " SET lock_timestamp=? where owner=?")
if _, err = tx.Exec(updatequery, currentTimestamp, owner); err != nil {
ldb.logger.Error("failed-to-update-lock-details-during-lock-renewal", err)
return err
Expand All @@ -125,7 +126,7 @@ func (ldb *LockSQLDB) renew(owner string, tx *sql.Tx) error {
func (ldb *LockSQLDB) Release(owner string) error {
ldb.logger.Debug("releasing-lock", lager.Data{"Owner": owner})
err := ldb.transact(ldb.sqldb, func(tx *sql.Tx) error {
query := "DELETE FROM " + ldb.table + " WHERE owner = $1"
query := ldb.sqldb.Rebind("DELETE FROM " + ldb.table + " WHERE owner = ?")
if _, err := tx.Exec(query, owner); err != nil {
ldb.logger.Error("failed-to-delete-lock-details-during-release-lock", err)
return err
Expand Down Expand Up @@ -210,7 +211,7 @@ func (ldb *LockSQLDB) getDatabaseTimestamp(tx *sql.Tx) (time.Time, error) {
return currentTimestamp, nil
}

func (ldb *LockSQLDB) transact(db *sql.DB, f func(tx *sql.Tx) error) error {
func (ldb *LockSQLDB) transact(db *sqlx.DB, f func(tx *sql.Tx) error) error {
var err error
for attempts := 0; attempts < 3; attempts++ {
err = func() error {
Expand Down
Loading

0 comments on commit c383315

Please sign in to comment.