Skip to content

Commit f65efee

Browse files
committed
Add and implement QueryBuilder.SelectStatement()
1 parent 647d090 commit f65efee

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

database/query_builder.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
type QueryBuilder interface {
1212
InsertStatement(stmt InsertStatement) string
1313

14+
SelectStatement(stmt SelectStatement) string
15+
1416
BuildColumns(entity Entity, columns []string, excludedColumns []string) []string
1517
}
1618

@@ -41,6 +43,25 @@ func (qb *queryBuilder) InsertStatement(stmt InsertStatement) string {
4143
)
4244
}
4345

46+
func (qb *queryBuilder) SelectStatement(stmt SelectStatement) string {
47+
columns := qb.BuildColumns(stmt.Entity(), stmt.Columns(), stmt.ExcludeColumns())
48+
from := stmt.Table()
49+
if from == "" {
50+
from = TableName(stmt.Entity())
51+
}
52+
where := stmt.Where()
53+
if where != "" {
54+
where = fmt.Sprintf("WHERE %s", where)
55+
}
56+
57+
return fmt.Sprintf(
58+
`SELECT "%s" FROM "%s" %s`,
59+
strings.Join(columns, `", "`),
60+
from,
61+
where,
62+
)
63+
}
64+
4465
func (qb *queryBuilder) BuildColumns(entity Entity, columns []string, excludedColumns []string) []string {
4566
var deltaColumns []string
4667
if len(columns) > 0 {

0 commit comments

Comments
 (0)