-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Meta contains the DML meta data, including driver name, list of column names, list of column holders, and list of values. The Meta type replace the Row type.
- Loading branch information
Showing
3 changed files
with
195 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2024, Shulhan <[email protected]>. 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" | ||
"strings" | ||
) | ||
|
||
// Meta contains the DML meta data, including driver name, list of column | ||
// names, list of column holders, and list of values. | ||
type Meta struct { | ||
driver string | ||
|
||
// ListName contains list of column name. | ||
ListName []string | ||
|
||
// ListHolder contains list of column holder, as in "?" or "$x", | ||
// depends on driver. | ||
ListHolder []string | ||
|
||
// ListValue contains list of column values, either for insert or | ||
// select. | ||
ListValue []any | ||
|
||
// ListWhereValue contains list of values for where condition. | ||
ListWhereValue []any | ||
} | ||
|
||
// NewMeta create new Meta using specific driver name. | ||
// The driver affect the ListHolder value. | ||
func NewMeta(driverName string) (meta *Meta) { | ||
meta = &Meta{ | ||
driver: driverName, | ||
} | ||
return meta | ||
} | ||
|
||
// Add column name and variable. | ||
func (meta *Meta) Add(colName string, val any) { | ||
meta.ListName = append(meta.ListName, colName) | ||
|
||
if meta.driver == DriverNamePostgres { | ||
meta.ListHolder = append(meta.ListHolder, fmt.Sprintf(`$%d`, len(meta.ListName))) | ||
} else { | ||
meta.ListHolder = append(meta.ListHolder, DefaultPlaceHolder) | ||
} | ||
|
||
meta.ListValue = append(meta.ListValue, val) | ||
} | ||
|
||
// AddWhere add value for where condition. | ||
// It return the length of ListWhereValue in list after addition. | ||
func (meta *Meta) AddWhere(val any) int { | ||
meta.ListWhereValue = append(meta.ListWhereValue, val) | ||
return len(meta.ListWhereValue) | ||
} | ||
|
||
// Names generate string of column names, for example "col1, col2, ...". | ||
func (meta *Meta) Names() string { | ||
return strings.Join(meta.ListName, `,`) | ||
} | ||
|
||
// Holders generate string of holder, for example "$1, $2, ...". | ||
func (meta *Meta) Holders() string { | ||
return strings.Join(meta.ListHolder, `,`) | ||
} | ||
|
||
// UpdateFields generate string of "col1=<holder>, col2=<holder>, ...". | ||
func (meta *Meta) UpdateFields() string { | ||
var ( | ||
sb strings.Builder | ||
x int | ||
) | ||
for ; x < len(meta.ListName); x++ { | ||
if x > 0 { | ||
sb.WriteByte(',') | ||
} | ||
fmt.Fprintf(&sb, `%s=%s`, meta.ListName[x], meta.ListHolder[x]) | ||
} | ||
return sb.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024, Shulhan <[email protected]>. 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_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/shuLhan/share/lib/sql" | ||
) | ||
|
||
func ExampleMeta_Add_mysql() { | ||
type Table struct { | ||
Name string | ||
ID int | ||
} | ||
|
||
var ( | ||
meta = sql.NewMeta(sql.DriverNameMysql) | ||
t = Table{} | ||
) | ||
|
||
meta.Add(`id`, &t.ID) | ||
meta.Add(`name`, &t.Name) | ||
|
||
fmt.Println(meta.Names()) | ||
fmt.Println(meta.Holders()) | ||
fmt.Println(meta.UpdateFields()) | ||
// Output: | ||
// id,name | ||
// ?,? | ||
// id=?,name=? | ||
} | ||
|
||
func ExampleMeta_Add_postgres() { | ||
type Table struct { | ||
Name string | ||
ID int | ||
} | ||
|
||
var ( | ||
meta = sql.NewMeta(sql.DriverNamePostgres) | ||
t = Table{} | ||
) | ||
|
||
meta.Add(`id`, &t.ID) | ||
meta.Add(`name`, &t.Name) | ||
|
||
fmt.Println(meta.Names()) | ||
fmt.Println(meta.Holders()) | ||
fmt.Println(meta.UpdateFields()) | ||
// Output: | ||
// id,name | ||
// $1,$2 | ||
// id=$1,name=$2 | ||
} | ||
|
||
func ExampleMeta_AddWhere() { | ||
var ( | ||
meta = sql.NewMeta(sql.DriverNamePostgres) | ||
vals = []any{ | ||
int(1000), | ||
string(`name`), | ||
} | ||
idx int | ||
) | ||
|
||
idx = meta.AddWhere(vals[0]) | ||
fmt.Println(idx, meta.ListWhereValue) | ||
|
||
idx = meta.AddWhere(vals[1]) | ||
fmt.Println(idx, meta.ListWhereValue) | ||
// Output: | ||
// 1 [1000] | ||
// 2 [1000 name] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters