diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-05 15:14:08 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-05 16:21:54 +0700 |
| commit | c6bc6f2f1aa4705333a69880767818e68f01033d (patch) | |
| tree | 47b46ddad2879a57c5fd7823331fdc0136704438 /lib/sql | |
| parent | 7ec26b9f98b5af909e935b69fbaa1db17d89cf6a (diff) | |
| download | pakakeh.go-c6bc6f2f1aa4705333a69880767818e68f01033d.tar.xz | |
lib/sql: remove deprecated Row type
The Row type has been replaced with Meta type with more flexibility
and features for generating type-safe SQL DML.
Diffstat (limited to 'lib/sql')
| -rw-r--r-- | lib/sql/row.go | 68 | ||||
| -rw-r--r-- | lib/sql/row_example_test.go | 37 |
2 files changed, 0 insertions, 105 deletions
diff --git a/lib/sql/row.go b/lib/sql/row.go deleted file mode 100644 index 693ee51e..00000000 --- a/lib/sql/row.go +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2020, Shulhan <ms@kilabit.info>. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package sql - -import ( - "fmt" - "sort" -) - -// Row represent a column-name and value in a tuple. -// The map's key is the column name in database and the map's value is -// the column's value. -// This type can be used to create dynamic insert-update fields. -// -// Deprecated: use [Meta] instead. -type Row map[string]interface{} - -// Meta convert the Row into Meta. -func (row Row) Meta(driverName string) (meta *Meta) { - meta = &Meta{} - - if len(row) == 0 { - return meta - } - - meta.ListName = make([]string, 0, len(row)) - meta.ListHolder = make([]string, 0, len(row)) - meta.ListValue = make([]interface{}, 0, len(row)) - - var colName string - for colName = range row { - meta.ListName = append(meta.ListName, colName) - } - sort.Strings(meta.ListName) - - var x int - for x, colName = range meta.ListName { - if driverName == DriverNamePostgres { - meta.ListHolder = append(meta.ListHolder, fmt.Sprintf(`$%d`, x+1)) - } else { - meta.ListHolder = append(meta.ListHolder, DefaultPlaceHolder) - } - meta.ListValue = append(meta.ListValue, row[colName]) - } - return meta -} - -// 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(driverName string) (names, holders []string, values []interface{}) { - if len(row) == 0 { - return nil, nil, nil - } - - var meta = row.Meta(driverName) - - return meta.ListName, meta.ListHolder, meta.ListValue -} diff --git a/lib/sql/row_example_test.go b/lib/sql/row_example_test.go deleted file mode 100644 index 0bd492cf..00000000 --- a/lib/sql/row_example_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package sql - -import ( - "fmt" - "strings" -) - -func ExampleRow_ExtractSQLFields() { - row := Row{ - "col_3": "'update'", - "col_2": 1, - "col_1": true, - } - names, holders, values := row.ExtractSQLFields("?") - fnames := strings.Join(names, ",") - fholders := strings.Join(holders, ",") - q := `INSERT INTO table (` + fnames + `) VALUES (` + fholders + `)` - fmt.Printf("Query: %s\n", q) - - // err := db.Exec(q, values...) - fmt.Println(values) - - 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: - // Query: INSERT INTO table (col_1,col_2,col_3) VALUES (?,?,?) - // [true 1 'update'] - // Query for PostgreSQL: INSERT INTO table (col_1,col_2,col_3) VALUES ($1,$2,$3) - // [true 1 'update'] -} |
