Last active
December 8, 2015 12:18
-
-
Save gebv/719e6ff769ab8480788b to your computer and use it in GitHub Desktop.
helpful models
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
package internal | |
import ( | |
"fmt" | |
"regexp" | |
"strconv" | |
"strings" | |
) | |
type SqlTypeUpdate string | |
var SQL_TYPE_INSERT SqlTypeUpdate = "TYPE_INSERT" | |
var SQL_TYPE_UPDATE SqlTypeUpdate = "TYPE_UPDATE" | |
var SQL_TYPE_SELECT SqlTypeUpdate = "TYPE_SELECT" | |
func SqlSelect(tableName string, fields []string) string { | |
return formatSql(SQL_TYPE_SELECT, tableName, fields) | |
} | |
func SqlInsert(tableName string, fields []string) string { | |
return formatSql(SQL_TYPE_INSERT, tableName, fields) | |
} | |
func SqlUpdate(tableName string, fields []string) string { | |
return formatSql(SQL_TYPE_UPDATE, tableName, fields) | |
} | |
func FormateToPQuery(query string) string { | |
var qrx = regexp.MustCompile(`\?`) | |
var pref = "$" | |
// Postgres = $# | |
// Oracle = : | |
// Default = ? | |
n := 0 | |
return qrx.ReplaceAllStringFunc(query, func(string) string { | |
n++ | |
return pref + strconv.Itoa(n) | |
}) | |
} | |
func FormateToPG(query string, fields []string) string { | |
var qrx = regexp.MustCompile(`\?`) | |
var pref = "?" | |
// Postgres = $# | |
// Oracle = : | |
// Default = ? | |
n := 0 | |
return qrx.ReplaceAllStringFunc(query, func(string) string { | |
res := pref + fields[n] | |
n++ | |
return res | |
}) | |
} | |
func formatSql(t SqlTypeUpdate, tableName string, arraysField []string) string { | |
switch t { | |
case SQL_TYPE_SELECT: | |
return fmt.Sprintf("SELECT %s FROM %s", strings.Join(arraysField, ", "), tableName) | |
case SQL_TYPE_INSERT: | |
return fmt.Sprintf("INSERT INTO %s(%s) VALUES(%s)", tableName, strings.Join(arraysField, ", "), strings.Repeat("?,", len(arraysField))[:len(arraysField)*2-1]) | |
case SQL_TYPE_UPDATE: | |
setFields := []string{} | |
for _, fieldName := range arraysField { | |
setFields = append(setFields, fmt.Sprintf("%s = ?", fieldName)) | |
} | |
return fmt.Sprintf("UPDATE %s SET %s", tableName, strings.Join(setFields, ", ")) | |
default: | |
// Пустое сообщение будет говорить о том что SQL собрался не корректно | |
return "" | |
} | |
} | |
func FiedlsWithout(m map[string]interface{}, without []string) (keys []string, fields []interface{}) { | |
_without := make(map[string]bool) | |
for _, v := range without { | |
_without[v] = true | |
} | |
for fieldName, field := range m { | |
if _without[fieldName] { | |
continue | |
} | |
keys = append(keys, fieldName) | |
fields = append(fields, field) | |
} | |
return | |
} |
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
package core | |
import ( | |
"github.com/lib/pq" | |
"time" | |
) | |
type ModelAbstract struct { | |
Code string `json:"-" sql:"type:text;default:null"` | |
IsRemoved bool `json:"-" sql:"type:boolean;default:false"` | |
IsEnabled bool `json:"-" sql:"type:boolean;default:false"` | |
AtCreated time.Time `json:"-" sql:"type:timestamp;default:null"` | |
AtUpdated time.Time `json:"-" sql:"type:timestamp;default:null"` | |
AtRemoved pq.NullTime `json:"-" sql:"type:timestamp;default:null"` | |
} | |
func (c *ModelAbstract) BeforeCreate() { | |
c.AtCreated = time.Now() | |
} | |
func (c *ModelAbstract) BeforeUpdate() { | |
c.AtUpdated = time.Now() | |
} | |
func (c *ModelAbstract) BeforeSave() { | |
c.AtUpdated = time.Now() | |
} | |
func (c *ModelAbstract) BeforeDelete() { | |
c.AtRemoved = pq.NullTime{ | |
time.Now(), | |
true, | |
} | |
} | |
func (c *ModelAbstract) Maps() map[string]interface{} { | |
return map[string]interface{}{ | |
"code": &c.Code, | |
"is_removed": &c.IsRemoved, | |
"is_enabled": &c.IsEnabled, | |
"at_created": &c.AtCreated, | |
"at_updated": &c.AtUpdated, | |
"at_removed": &c.AtRemoved, | |
} | |
} |
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
package internal | |
import ( | |
"database/sql/driver" | |
"encoding/json" | |
) | |
type JsonMap map[string]interface{} | |
func (m *JsonMap) Set(data map[string]interface{}) { | |
*m = JsonMap(data) | |
} | |
func (m *JsonMap) Scan(value interface{}) error { | |
return json.Unmarshal(value.([]byte), m) | |
} | |
func (m JsonMap) Value() (driver.Value, error) { | |
b, err := json.Marshal(m) | |
if err != nil { | |
return nil, err | |
} | |
return string(b), 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
package internal | |
import ( | |
"bytes" | |
"strings" | |
) | |
type Int64Slice []int64 | |
func (c *Int64Slice) IsExist(v int64) bool { | |
for _, value := range *c { | |
if v == value { | |
return true | |
} | |
} | |
return false | |
} | |
func (c *Int64Slice) Add(v int64) { | |
if !c.IsExist(v) { | |
*c = append(*c, v) | |
} | |
} | |
func (c *Int64Slice) Del(v int64) { | |
if !c.IsExist(v) { | |
return | |
} | |
for index, value := range *c { | |
if value == v { | |
(*c)[index] = (*c)[len((*c))-1] | |
(*c) = (*c)[:len((*c))-1] | |
return | |
} | |
} | |
} | |
type StringSlice []string | |
func (c *StringSlice) Del(str string) { | |
str = strings.TrimSpace(str) | |
if !c.IsExist(str) { | |
return | |
} | |
for index, value := range *c { | |
if bytes.Equal([]byte(str), []byte(value)) { | |
(*c)[index] = (*c)[len((*c))-1] | |
(*c) = (*c)[:len((*c))-1] | |
return | |
} | |
} | |
} | |
func (c *StringSlice) IsExist(str string) bool { | |
str = strings.TrimSpace(str) | |
for _, value := range *c { | |
if bytes.Equal([]byte(str), []byte(value)) { | |
return true | |
} | |
} | |
return false | |
} | |
func (c *StringSlice) Add(str string) { | |
str = strings.TrimSpace(str) | |
if len(str) == 0 { | |
return | |
} | |
if !c.IsExist(str) { | |
*c = append(*c, str) | |
} | |
} |
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
package core | |
type Users struct { | |
UserId int64 | |
Username string | |
Emails []string | |
} | |
func (c *Users) TableName() string { | |
return "users" | |
} | |
func (c *Users) Maps() map[string]interface{} { | |
maps := c.ModelAbstract.Maps() | |
for key, field := range map[string]interface{}{ | |
"user_id": &c.UserId, | |
"username": &c.Username, | |
"emails": &c.Emails, | |
} { | |
if _, exist := maps[key]; exist { | |
glog.Warningf("FieldName existing '%s'", key) | |
} | |
maps[key] = field | |
} | |
return maps | |
} | |
func (c *Users) FiedlsWithout(without ...string) (keys []string, fields []interface{}) { | |
return FiedlsWithout(c.Maps(), without) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment