Permalink
Browse files

Model part is now structured in 3 types: connection, model and instance

  • Loading branch information...
nono committed Jan 12, 2010
1 parent d9e0022 commit c0a801db1a3f5b75a3d076048f6bfad70da0319e
Showing with 105 additions and 23 deletions.
  1. +1 −1 README
  2. +104 −22 model.go
View
2 README
@@ -23,7 +23,7 @@ Links
* http://golang.org/ The Go Programming Language
* http://www.getwebgo.com/ Web.Go framework
* http://github.com/hoisie/gostache Gostache
-* http://github.com/hoisie/Go-Redis Go-Redis
+* http://github.com/alphazero/Go-Redis Go-Redis
* http://github.com/antirez/redis Redis
View
126 model.go
@@ -5,49 +5,131 @@ import (
"redis"
"strconv"
"strings"
+ "os"
)
+type Connection struct {
+ app string
+ db int
+ client redis.Client
+}
+
type Model struct {
- db int
- name string
- client redis.Client
+ conn *Connection
+ name string
+}
+
+type Instance struct {
+ model *Model
+ id int64
+ param string
}
const keySeparator = ":"
+const modelNamespace = "_models"
+const keyNextId = "_id"
+
-func NewModel(name string) *Model {
- fullname := GetConfig("appname") + keySeparator + name
+/**************/
+/* Connection */
+/**************/
+
+func Connect() *Connection {
+ fullname := GetConfig("appname")
database := GetConfig("database")
db, err := strconv.Atoi(database)
if err != nil {
- log.Exitf("Can't read the database", err)
+ log.Stderrf("Can't read the database", err)
+ return nil
+ }
+ spec := redis.DefaultSpec().Db(db)
+ cli, err := redis.NewSynchClientWithSpec(spec)
+ if err != nil {
+ log.Stderrf("Can't connect to the database", err)
+ return nil
}
- spec := redis.DefaultSpec().Db(db)
- m := new(Model)
- m.db = db
- m.name = fullname
- m.client, err = redis.NewSynchClientWithSpec(spec)
+ err = cli.Ping()
if err != nil {
- log.Exitf("Can't create the client", err)
+ log.Stderrf("Can't ping the database", err)
+ return nil
}
- return m
+ return &Connection{fullname, db, cli}
}
+func (this *Connection) NewModel(name string) *Model {
+ return &Model{this, name}
+}
+
+
+/*********/
+/* Model */
+/*********/
+
func (this *Model) FullKey(key string) string {
- return this.name + keySeparator + key
+ return this.conn.app + keySeparator + this.name + keySeparator + key
+}
+
+func (this *Model) NextId() (id int64, err os.Error) {
+ key := this.conn.app + keySeparator + modelNamespace + keySeparator + this.name
+ return this.conn.client.Incr(key)
+}
+
+func (this *Model) Get(key string) (string, os.Error) {
+ value, err := this.conn.client.Get(this.FullKey(key))
+ return string(value), err
+}
+
+func (this *Model) Set(key string, value string) os.Error {
+ return this.conn.client.Set(this.FullKey(key), strings.Bytes(value))
+}
+
+func (this *Model) Create(param string) *Instance {
+ id, err := this.NextId()
+ if err != nil {
+ log.Stderrf("Impossible to create an instance of %s", this.name, err)
+ return nil
+ }
+ err = this.Set(param, string(id))
+ if err != nil {
+ log.Stderrf("Impossible to create an instance of %s", this.name, err)
+ return nil
+ }
+ return &Instance{this, id, param}
+}
+
+func (this *Model) Find(param string) *Instance {
+ value, err := this.Get(param)
+ if err != nil {
+ log.Stderrf("Impossible to find %s (%s)", param, this.name, err)
+ return nil
+ }
+ id, err := strconv.Atoi64(value)
+ if err != nil {
+ log.Stderrf("Impossible to convert %s to int", value, err)
+ return nil
+ }
+ return &Instance{this, id, param};
+}
+
+
+/************/
+/* Instance */
+/************/
+
+func (this *Instance) FieldKey(field string) string {
+ return this.param + keySeparator + field
}
-func (this *Model) Get(key string) string {
- value, err := this.client.Get(this.FullKey(key))
+func (this *Instance) Get(field string) string {
+ value, err := this.model.Get(this.FieldKey(field))
if err != nil {
- log.Stderr("Error on Get", err)
- // TODO return something
+ log.Stderrf("Impossible to get %s", field, err)
+ return ""
}
- return string(value);
+ return value
}
-func (this *Model) Set(key string, value string) {
- this.client.Set(this.FullKey(key), strings.Bytes(value))
- // TODO error handling
+func (this *Instance) Set(field string, value string) os.Error {
+ return this.model.Set(this.FieldKey(field), value)
}

0 comments on commit c0a801d

Please sign in to comment.