-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
198 additions
and
7 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
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
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,67 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
_ "github.com/lib/pq" | ||
"github.com/twiglab/sqlt" | ||
) | ||
|
||
type StaffHandler struct { | ||
Staffs []*Staff | ||
} | ||
|
||
func (sh *StaffHandler) Extract(rs sqlt.Rows) (err error) { | ||
for rs.Next() { | ||
staff := new(Staff) | ||
if err = rs.StructScan(staff); err != nil { | ||
return | ||
} | ||
|
||
sh.Staffs = append(sh.Staffs, staff) | ||
} | ||
|
||
return rs.Err() | ||
} | ||
|
||
func main() { | ||
dbx := sqlt.MustConnect("postgres", "dbname=testdb sslmode=disable") | ||
tpl := sqlt.NewSqlTemplate("tpl/*.tpl") | ||
tpl.SetDebug(true) | ||
|
||
dbop := sqlt.New(dbx, tpl) | ||
|
||
staff1 := new(Staff) | ||
|
||
staff1.CreatedAt = time.Now() | ||
staff1.UpdatedAt = time.Now() | ||
staff1.StaffId = 12345 | ||
staff1.StaffName = "MikeWang" | ||
sqlt.MustExec(dbop, context.Background(), "Staff.insert", staff1) | ||
|
||
staff2 := new(Staff) | ||
staff2.CreatedAt = time.Now() | ||
staff2.UpdatedAt = time.Now() | ||
staff2.StaffId = 67890 | ||
staff2.StaffName = "It512" | ||
sqlt.MustExec(dbop, context.Background(), "Staff.insert", staff2) | ||
|
||
staff := new(Staff) | ||
staff.StaffId = 67890 | ||
h := new(StaffHandler) | ||
sqlt.MustQuery(dbop, context.Background(), "Staff.select", staff, h) | ||
|
||
for _, s := range h.Staffs { | ||
fmt.Println(s) | ||
} | ||
|
||
staff = new(Staff) | ||
staff.StaffId = 12345 | ||
staff.StaffName = "god" | ||
staff.UpdatedAt = time.Now() | ||
sqlt.MustExec(dbop, context.Background(), "Staff.update", staff) | ||
|
||
sqlt.MustExec(dbop, context.Background(), "Staff.delete", nil) | ||
} |
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,5 @@ | ||
{ | ||
"data_base_name" :"postgres", | ||
"data_source_name" :"dbname=testdb sslmode=disable", | ||
"table_name": "staff" | ||
} |
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,64 @@ | ||
const TableName = "Staff" | ||
|
||
const ( | ||
StaffId = "staff_id" | ||
StaffName = "staff_name" | ||
CreatedAt = "created_at" | ||
UpdatedAt = "updated_at" | ||
Age = "age" | ||
) | ||
type Staff struct { | ||
StaffId int `db:"staff_id"` // | ||
StaffName string `db:"staff_name"` // | ||
CreatedAt time.Time `db:"created_at"` // | ||
UpdatedAt time.Time `db:"updated_at"` // | ||
Age int `db:"age"` // | ||
} | ||
{{ define "Staff.insert"}} | ||
insert into Staff ( | ||
{{if .StaffId}} ,staff_id {{end}} | ||
{{if .StaffName}} ,staff_name {{end}} | ||
{{if .CreatedAt}} ,created_at {{end}} | ||
{{if .UpdatedAt}} ,updated_at {{end}} | ||
{{if .Age}} ,age {{end}} | ||
) values ( | ||
{{if .StaffId}} ,:staff_id {{end}} | ||
{{if .StaffName}} ,:staff_name {{end}} | ||
{{if .CreatedAt}} ,:created_at {{end}} | ||
{{if .UpdatedAt}} ,:updated_at {{end}} | ||
{{if .Age}} ,:age {{end}} | ||
) | ||
{{end}} | ||
{{ define "Staff.select"}} | ||
select | ||
staff_id, | ||
staff_name, | ||
created_at, | ||
updated_at, | ||
age, | ||
from | ||
Staff | ||
where | ||
{{if .StaffId}}and staff_id = :staff_id{{end}} | ||
{{if .StaffName}}and staff_name = :staff_name{{end}} | ||
{{if .CreatedAt}}and created_at = :created_at{{end}} | ||
{{if .UpdatedAt}}and updated_at = :updated_at{{end}} | ||
{{if .Age}}and age = :age{{end}} | ||
{{end}} | ||
{{ define "Staff.update"}} | ||
update | ||
Staff | ||
set | ||
{{if .StaffId}} ,staff_id = :staff_id {{end}} | ||
{{if .StaffName}} ,staff_name = :staff_name {{end}} | ||
{{if .CreatedAt}} ,created_at = :created_at {{end}} | ||
{{if .UpdatedAt}} ,updated_at = :updated_at {{end}} | ||
{{if .Age}} ,age = :age {{end}} | ||
|
||
where | ||
{{if .StaffId}} staff_id = :staff_id and {{end}} | ||
{{if .StaffName}} staff_name = :staff_name and {{end}} | ||
{{if .CreatedAt}} created_at = :created_at and {{end}} | ||
{{if .UpdatedAt}} updated_at = :updated_at and {{end}} | ||
{{if .Age}} age = :age and {{end}} | ||
{{end}} |
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,11 @@ | ||
package main | ||
|
||
import "time" | ||
|
||
type Staff struct { | ||
StaffId int `db:"staff_id"` // | ||
StaffName string `db:"staff_name"` // | ||
CreatedAt time.Time `db:"created_at"` // | ||
UpdatedAt time.Time `db:"updated_at"` // | ||
Age int `db:"age"` // | ||
} |
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,8 @@ | ||
create table staff | ||
( | ||
staff_id int, | ||
staff_name varchar(64), | ||
created_at timestamp, | ||
updated_at timestamp, | ||
age int | ||
); |
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,38 @@ | ||
{{ define "Staff.insert"}} | ||
insert into Staff ( | ||
created_at | ||
{{if .StaffId}} ,staff_id {{end}} | ||
{{if .StaffName}} ,staff_name {{end}} | ||
) values ( | ||
:created_at | ||
{{if .StaffId}} ,:staff_id {{end}} | ||
{{if .StaffName}} ,:staff_name {{end}} | ||
) | ||
{{end}} | ||
|
||
{{ define "Staff.select"}} | ||
select | ||
staff_id, | ||
staff_name, | ||
created_at | ||
from | ||
Staff | ||
where | ||
{{if .StaffId}} staff_id = :staff_id {{end}} | ||
{{if .StaffName}}and staff_name = :staff_name {{end}} | ||
{{end}} | ||
|
||
{{ define "Staff.update"}} | ||
update | ||
Staff | ||
set | ||
updated_at = :updated_at | ||
{{if .StaffName}},staff_name = :staff_name {{end}} | ||
|
||
where | ||
staff_id = :staff_id | ||
{{end}} | ||
|
||
{{ define "Staff.delete"}} | ||
delete from staff | ||
{{end}} |