Skip to content

Commit

Permalink
Alias.
Browse files Browse the repository at this point in the history
  • Loading branch information
liudng committed Apr 28, 2015
1 parent 158f73c commit 9e13020
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
6 changes: 5 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func NewModel(module string, table Table) *Model {
// New Table
func NewTable(tableName string, entity interface{}) Table {
p, f := tableFields(entity)
t := Table{Name: tableName, Primary: p, Fields: f, EntityType: reflect.ValueOf(entity).Elem().Type()}
t := Table{
Name: tableName,
Primary: p,
Fields: f,
EntityType: reflect.ValueOf(entity).Elem().Type()}
return t
}
12 changes: 6 additions & 6 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func (q *Query) ToString() string {


// Parse map data to insert SQL
func (q *Query) mapToInsert(d map[string]interface{}) {
func (q *Query) mapToInsert(d Item) {
f := make([]string, 0)
ph := make([]string, 0)
for k, v := range d {
Expand All @@ -467,7 +467,7 @@ func (q *Query) mapToInsert(d map[string]interface{}) {
}

// Parse map data to update SQL
func (q *Query) mapToUpdate(d map[string]interface{}) {
func (q *Query) mapToUpdate(d Item) {
i := 0
for k, v := range d {
if i == 0 {
Expand All @@ -480,7 +480,7 @@ func (q *Query) mapToUpdate(d map[string]interface{}) {
}

// Parse map data to where SQL
func (q *Query) mapToWhere(d map[string]interface{}) {
func (q *Query) mapToWhere(d Where) {
i := 0
for k, v := range d {
if i == 0 {
Expand Down Expand Up @@ -508,7 +508,7 @@ func (q *Query) Exec(d ...map[string]interface{}) (Result, error) {
// https://github.com/lib/pq/issues/24
if q.Server.Type == "postgres" {
q.Sql["Returning"] = fmt.Sprintf("RETURNING %s", q.quoteField(q.Primary))
row := make(map[string]interface{})
row := make(Item)
err := q.Server.Row(&row, q.ToString(), q.Args...)
if err != nil {
return re, err
Expand Down Expand Up @@ -555,7 +555,7 @@ func (q *Query) Exec(d ...map[string]interface{}) (Result, error) {
}

// Row
func (q *Query) Row(ptr interface{}, d ...map[string]interface{}) error {
func (q *Query) Row(ptr interface{}, d ...Where) error {
if q.Server == nil {
return errors.New("DB config not found")
}
Expand All @@ -566,7 +566,7 @@ func (q *Query) Row(ptr interface{}, d ...map[string]interface{}) error {
}

// Rows
func (q *Query) Rows(ptr interface{}, d ...map[string]interface{}) error {
func (q *Query) Rows(ptr interface{}, d ...Where) error {
if q.Server == nil {
return errors.New("DB config not found")
}
Expand Down
2 changes: 1 addition & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (qt *QueryTest) Insert(t *testing.T) {


// Insert confirm
d = make(map[string]interface{})
d = make(Item)
q = NewQuery(qt.Query.Server)
err = q.Select("*").From("passport_user").Where(q.Eq("UserID", 1000001)).Row(&d)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,5 @@ func (e *Server) parseParameters(str string, args []interface{}) (string, []inte

// New Server
func NewServer(name string, typ string, dsn string) *Server {
return &Server{Name: name, Type: typ, DSN: dsn}
return &Server{Name: typ+name, Type: typ, DSN: dsn}
}
8 changes: 4 additions & 4 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (st *ServerTest) Insert(t *testing.T) {
q := `INSERT INTO "passport_user" ("UserID", "CreationTime", "BirthYear", "Gender", "Nickname") VALUES($1, $2, $3, $4, $5)`
if st.Server.Type == "postgres" {
q = `INSERT INTO "passport_user" ("UserID", "CreationTime", "BirthYear", "Gender", "Nickname") VALUES($1, $2, $3, $4, $5) RETURNING *`
row := make(map[string]interface{})
row := make(Item)
err := st.Server.Row(&row, q, vs...)
if err != nil {
t.Fatalf("[%s]: %v\n", st.Server.Type, err)
Expand Down Expand Up @@ -87,7 +87,7 @@ func (st *ServerTest) Insert(t *testing.T) {


// Insert confirm
d := make(map[string]interface{})
d := make(Item)
q = `SELECT * FROM "passport_user" WHERE "UserID" = $1`
err := st.Server.Row(&d, q, 1000000)
if err != nil {
Expand Down Expand Up @@ -116,7 +116,7 @@ func (st *ServerTest) Update(t *testing.T) {


// Update confirm
d := make(map[string]interface{})
d := make(Item)
q = `SELECT * FROM "passport_user" WHERE "UserID" = $1`
err = st.Server.Row(&d, q, 1000000)
if err != nil {
Expand Down Expand Up @@ -145,7 +145,7 @@ func (st *ServerTest) Delete(t *testing.T) {
}

func (st *ServerTest) Rows(t *testing.T) {
d := []map[string]interface{}{}
d := []Item{}
q := `SELECT * FROM "passport_user" WHERE "UserID" > $1`
err := st.Server.Rows(&d, q, 1)
if err != nil {
Expand Down

0 comments on commit 9e13020

Please sign in to comment.