aboutsummaryrefslogtreecommitdiff
path: root/lib/sql
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2021-02-07 20:24:36 +0700
committerShulhan <m.shulhan@gmail.com>2021-02-07 20:24:36 +0700
commit215e4b60542a6cddbcf201b3cd793714d45b65da (patch)
tree6d7d5e38f1c62d9cdbdfd4a2eca0b85002590449 /lib/sql
parent5013fd9b71ce23f9ead93a18c49a5b984d014f39 (diff)
downloadpakakeh.go-215e4b60542a6cddbcf201b3cd793714d45b65da.tar.xz
sql: change the parameter ExtractSQLFields to driver name
Previously, we use the string as parameter to set the returned place holders. This commit changes the parameter to the driver name so if the value is "postgres" the place holders will be returned as counter, for example "$1", "$2", and so on.
Diffstat (limited to 'lib/sql')
-rw-r--r--lib/sql/row.go28
-rw-r--r--lib/sql/row_example_test.go18
2 files changed, 31 insertions, 15 deletions
diff --git a/lib/sql/row.go b/lib/sql/row.go
index 13b43978..2e0eb374 100644
--- a/lib/sql/row.go
+++ b/lib/sql/row.go
@@ -4,7 +4,10 @@
package sql
-import "sort"
+import (
+ "fmt"
+ "sort"
+)
//
// Row represent a column-name and value in a tuple.
@@ -15,18 +18,21 @@ import "sort"
type Row map[string]interface{}
//
-// ExtractSQLFields extract the column's name, column place holder (default is
-// "?"), and column values; as slices.
+// ExtractSQLFields extract the column's name, column place holder, and column
+// values as slices.
+//
+// The driverName define the returned place holders.
+// If the driverName is "postgres" then the list of holders will be returned
+// as counter, for example "$1", "$2" and so on.
+// If the driverName is "mysql" or empty or unknown the the list of holders
+// will be returned as list of "?".
//
// The returned names will be sorted in ascending order.
//
-func (row Row) ExtractSQLFields(placeHolder string) (names, holders []string, values []interface{}) {
+func (row Row) ExtractSQLFields(driverName string) (names, holders []string, values []interface{}) {
if len(row) == 0 {
return nil, nil, nil
}
- if len(placeHolder) == 0 {
- placeHolder = DefaultPlaceHolder
- }
names = make([]string, 0, len(row))
holders = make([]string, 0, len(row))
@@ -37,8 +43,12 @@ func (row Row) ExtractSQLFields(placeHolder string) (names, holders []string, va
}
sort.Strings(names)
- for _, k := range names {
- holders = append(holders, placeHolder)
+ for x, k := range names {
+ if driverName == DriverNamePostgres {
+ holders = append(holders, fmt.Sprintf("$%d", x+1))
+ } else {
+ holders = append(holders, DefaultPlaceHolder)
+ }
values = append(values, row[k])
}
diff --git a/lib/sql/row_example_test.go b/lib/sql/row_example_test.go
index d7cd0918..e22524a4 100644
--- a/lib/sql/row_example_test.go
+++ b/lib/sql/row_example_test.go
@@ -11,17 +11,23 @@ func ExampleRow_ExtractSQLFields() {
"col_2": 1,
"col_1": true,
}
- names, holders, values := row.ExtractSQLFields(DefaultPlaceHolder)
+ names, holders, values := row.ExtractSQLFields("?")
fnames := strings.Join(names, ",")
fholders := strings.Join(holders, ",")
-
q := `INSERT INTO table (` + fnames + `) VALUES (` + fholders + `)`
- fmt.Println(q)
+ fmt.Printf("Query: %s\n", q)
+
+ names, holders, values = row.ExtractSQLFields("postgres")
+ fnames = strings.Join(names, ",")
+ fholders = strings.Join(holders, ",")
+ q = `INSERT INTO table (` + fnames + `) VALUES (` + fholders + `)`
+ fmt.Printf("Query for PostgreSQL: %s\n", q)
// err := db.Exec(q, values...)
fmt.Println(values)
- //Output:
- //INSERT INTO table (col_1,col_2,col_3) VALUES (?,?,?)
- //[true 1 'update']
+ // Output:
+ // Query: INSERT INTO table (col_1,col_2,col_3) VALUES (?,?,?)
+ // Query for PostgreSQL: INSERT INTO table (col_1,col_2,col_3) VALUES ($1,$2,$3)
+ // [true 1 'update']
}