Преглед изворни кода

Fix dynamic query parameters

Rod Hynes пре 11 година
родитељ
комит
7b57583679
1 измењених фајлова са 6 додато и 10 уклоњено
  1. 6 10
      psiphon/dataStore.go

+ 6 - 10
psiphon/dataStore.go

@@ -234,12 +234,10 @@ func (iterator *ServerEntryIterator) Reset() error {
 		return ContextError(err)
 	}
 	var cursor *sql.Rows
-	query := "select data from serverEntry"
 	whereClause, whereParams := makeServerEntryWhereClause(
 		iterator.region, iterator.protocol, iterator.excludeIds)
-	query += whereClause
-	query += " order by rank desc;"
-	cursor, err = transaction.Query(query, whereParams)
+	query := "select data from serverEntry" + whereClause + " order by rank desc;"
+	cursor, err = transaction.Query(query, whereParams...)
 	if err != nil {
 		transaction.Rollback()
 		return ContextError(err)
@@ -292,10 +290,9 @@ func (iterator *ServerEntryIterator) Next() (serverEntry *ServerEntry, err error
 }
 
 func makeServerEntryWhereClause(
-	region, protocol string, excludeIds []string) (whereClause string, whereParams []string) {
-
+	region, protocol string, excludeIds []string) (whereClause string, whereParams []interface{}) {
 	whereClause = ""
-	whereParams = make([]string, 0)
+	whereParams = make([]interface{}, 0)
 	if region != "" {
 		whereClause += " where region = ?"
 		whereParams = append(whereParams, region)
@@ -335,10 +332,9 @@ func makeServerEntryWhereClause(
 func HasServerEntries(region, protocol string) bool {
 	initDataStore()
 	var count int
-	query := "select count(*) from serverEntry"
 	whereClause, whereParams := makeServerEntryWhereClause(region, protocol, nil)
-	query += whereClause
-	err := singleton.db.QueryRow(query, whereParams).Scan(&count)
+	query := "select count(*) from serverEntry" + whereClause
+	err := singleton.db.QueryRow(query, whereParams...).Scan(&count)
 
 	if region == "" {
 		region = "(any)"