Skip to content

Commit

Permalink
Add condition paraser.
Browse files Browse the repository at this point in the history
  • Loading branch information
liudng committed Aug 11, 2015
1 parent cea1e6d commit e2634d9
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 56 deletions.
28 changes: 28 additions & 0 deletions condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2014 The zhgo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package db

import ()

// Condition struct
type Condition struct {
Eq map[string]string
Ge map[string]string
Gt map[string]string
Le map[string]string
Lt map[string]string
Ne map[string]string
Like map[string]string
In map[string][]string
}

func (c *Condition) Success() {

}

// New condition
func NewCondition() {

}
27 changes: 27 additions & 0 deletions define.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2014 The zhgo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package db

import (
"database/sql"
)

// Alias of map[string]interface{}
type Item map[string]interface{}

// Alias of []map[string]interface{}
type Items []map[string]interface{}

// Alias of map[string]interface{}
type Where map[string]interface{}

// Enviroment: 0, 1, 2, 3
var Env int8 = 0

// Server instance
var dbObjects map[string]*sql.DB = make(map[string]*sql.DB)

// Server list
var Servers = make(map[string]*Server)
40 changes: 5 additions & 35 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

package db

import (
"reflect"
)
import ()

// Model struct
type Model struct {
Expand All @@ -17,24 +15,6 @@ type Model struct {
Table Table
}

// Table struct
type Table struct {
// Table name
Name string

// Table primary
Primary string

// All fields, except primary
Fields []string

// Entity
EntityType reflect.Type
}

// Server list
var Servers = make(map[string]*Server)

// Insert
func (m *Model) Insert() *Query {
q := NewQuery(Servers[m.Module])
Expand All @@ -59,26 +39,16 @@ func (m *Model) Delete() *Query {
// Select
func (m *Model) Select(f ...string) *Query {
if len(f) == 0 {
f = m.Table.Fields
f = m.Table.AllFields
}

q := NewQuery(Servers[m.Module])
q.Select(f...)
q.From(m.Table.Name)
return q
}

// New Model
func NewModel(module string, table Table) *Model {
return &Model{Module: module, Table: table}
}

// 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()}
return t
func NewModel(module string, table Table) Model {
return Model{Module: module, Table: table}
}
51 changes: 48 additions & 3 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,18 @@ func (q *Query) joinFields(fs []string) string {

// Quote fileds
func (q *Query) quoteFields(f []string) []string {
// Slices are passed by reference, copy f to nf.
nf := make([]string, len(f))
for i, v := range f {
f[i] = q.quoteField(v)
nf[i] = q.quoteField(v)
}
return f
return nf
}

// Quote filed
func (q *Query) quoteField(f string) string {
if strings.Trim(f, " \r\n\t") == "*" {
fs := strings.Trim(f, " \r\n\t")
if fs == "*" || fs == "1" {
return f
}
return fmt.Sprintf("\"%s\"", strings.Replace(f, ".", "\".\"", -1))
Expand Down Expand Up @@ -325,6 +328,9 @@ func (q *Query) DeleteFrom(tb string) *Query {
func (q *Query) Select(f ...string) *Query {
q.Type = QuerySelect
if len(f) == 0 {
// Warring!
// If use struct type as return data type, must keep have same sequence
// about struct fileds and table fileds.
q.Sql["Select"] = " SELECT *"
} else {
q.Sql["Select"] = fmt.Sprintf(" SELECT %s ", q.joinFields(f))
Expand Down Expand Up @@ -426,6 +432,45 @@ func (q *Query) Limit(offset, rows int64) *Query {
return q
}

// Parse
func (q *Query) Parse(c Condition) *Query {
conds := []string{q.Eq("1", "1")}

for k, v := range c.Eq {
conds = append(conds, q.AndEq(k, v))
}

for k, v := range c.Ge {
conds = append(conds, q.AndGe(k, v))
}

for k, v := range c.Gt {
conds = append(conds, q.AndGt(k, v))
}

for k, v := range c.Le {
conds = append(conds, q.AndLe(k, v))
}

for k, v := range c.Lt {
conds = append(conds, q.AndLt(k, v))
}

for k, v := range c.Ne {
conds = append(conds, q.AndNe(k, v))
}

for k, v := range c.Like {
conds = append(conds, q.AndLike(k, v))
}

for k, v := range c.In {
conds = append(conds, q.AndIn(k, v))
}

return q.Where(conds...)
}

// Connect all sql part to a corect sql string.
func (q *Query) ToString() string {
str := ""
Expand Down
3 changes: 0 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ type Server struct {
Follow string
}

// Server instance
var dbObjects map[string]*sql.DB = make(map[string]*sql.DB)

// Execute query, only return sql.Result
func (e *Server) Exec(sql string, args ...interface{}) (sql.Result, error) {
sql, args = e.parseSQL(sql, args)
Expand Down
39 changes: 39 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2014 The zhgo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package db

import (
"reflect"
)

// Table struct
type Table struct {
// Table name
Name string

// Table primary
Primary string

// All fields, except primary
Fields []string

// All fields
AllFields []string

// Entity
EntityType reflect.Type
}

// New Table
func NewTable(tableName string, entity interface{}) Table {
p, f, af := tableFields(entity)
t := Table{
Name: tableName,
Primary: p,
Fields: f,
AllFields: af,
EntityType: reflect.ValueOf(entity).Elem().Type()}
return t
}
22 changes: 7 additions & 15 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@ import (
"reflect"
)

// Alias of map[string]interface{}
type Item map[string]interface{}

// Alias of []map[string]interface{}
type Items []map[string]interface{}

// Alias of map[string]interface{}
type Where map[string]interface{}

// Enviroment: 0, 1, 2, 3
var Env int8 = 0

// Get scan variables
func scanVariables(ptr interface{}, columnsLen int, isRows bool) (reflect.Kind, interface{}, []interface{}, error) {
typ := reflect.ValueOf(ptr).Type()
Expand Down Expand Up @@ -108,10 +96,11 @@ func tableAlias(alias []string) string {
}

// Reflect struct, construct Field slice
func tableFields(entity interface{}) (string, []string) {
func tableFields(entity interface{}) (string, []string, []string) {
typ := reflect.Indirect(reflect.ValueOf(entity)).Type()
primary := ""
fields := make([]string, 0)
allFields := make([]string, 0)

for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
Expand All @@ -122,12 +111,15 @@ func tableFields(entity interface{}) (string, []string) {
name = field.Name
}

if field.Tag.Get("pk") == "true" { //!field.Anonymous
//!field.Anonymous
if field.Tag.Get("pk") == "true" {
primary = name
} else {
fields = append(fields, name)
}

allFields = append(allFields, name)
}

return primary, fields
return primary, fields, allFields
}

0 comments on commit e2634d9

Please sign in to comment.