Skip to content

Commit

Permalink
add example and fix pggen bug #1
Browse files Browse the repository at this point in the history
  • Loading branch information
it512 committed Feb 10, 2019
1 parent 272f7e6 commit c5827a6
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 7 deletions.
4 changes: 1 addition & 3 deletions cmd/pggen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (

var db *sqlx.DB

const tableName = "req_order"

type Origin struct {
DataBaseName string `json:"data_base_name"`
DataSourceName string `json:"data_source_name"`
Expand Down Expand Up @@ -51,7 +49,7 @@ func main() {
}

table := new(Table)
table.TableName = tableName
table.TableName = origin.TableName

for r.Next() {
f := new(Field)
Expand Down
8 changes: 4 additions & 4 deletions cmd/pggen/gen_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func genSelect(t *Table) {

fmt.Printf("from\n\t%s\nwhere\n", t.SnakeName())
for _, f := range t.Fields {
fmt.Printf("\t{{if .%s}} %s = :%s and {{end}}\n", f.SnakeName(), f.Name, f.SnakeName())
fmt.Printf("\t{{if .%s}}and %s = :%s{{end}}\n", f.SnakeName(), f.Name, f.Name)
}

fmt.Printf("{{end}}\n")
Expand All @@ -37,11 +37,11 @@ func genUpdate(t *Table) {
fmt.Printf("{{ define \"%s.update\"}}\n", t.SnakeName())
fmt.Printf("update\n\t%s\nset\n", t.SnakeName())
for _, f := range t.Fields {
fmt.Printf("\t{{if .%s}} %s = :%s, {{end}}\n", f.SnakeName(), f.Name, f.SnakeName())
fmt.Printf("\t{{if .%s}} ,%s = :%s {{end}}\n", f.SnakeName(), f.Name, f.Name)
}
fmt.Printf("nwhere\n", t.SnakeName())
fmt.Printf("\nwhere\n")
for _, f := range t.Fields {
fmt.Printf("\t{{if .%s}} %s = :%s and {{end}}\n", f.SnakeName(), f.Name, f.SnakeName())
fmt.Printf("\t{{if .%s}} %s = :%s and {{end}}\n", f.SnakeName(), f.Name, f.Name)
}

fmt.Printf("{{end}}\n")
Expand Down
67 changes: 67 additions & 0 deletions example/main.go
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)
}
5 changes: 5 additions & 0 deletions example/origin.json
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"
}
64 changes: 64 additions & 0 deletions example/staff.gen
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}}
11 changes: 11 additions & 0 deletions example/staff.go
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"` //
}
8 changes: 8 additions & 0 deletions example/staff.sql
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
);
38 changes: 38 additions & 0 deletions example/tpl/staff.tpl
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}}

0 comments on commit c5827a6

Please sign in to comment.