From 6a1928458980467f2dd807c5ff1bb417af33ddec Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 21 Aug 2014 17:25:22 +0300 Subject: [PATCH] start using gorm as ORM --- engine/models.go | 26 +++++++++++++++++++ engine/storage_mysql.go | 15 ++++++++--- engine/storage_postgres.go | 16 +++++++++--- engine/storage_sql.go | 49 +++++++++++++++++------------------- hard_update_external_libs.py | 1 + update_external_libs.sh | 1 + 6 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 engine/models.go diff --git a/engine/models.go b/engine/models.go new file mode 100644 index 000000000..73582bd93 --- /dev/null +++ b/engine/models.go @@ -0,0 +1,26 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2013 ITsysCOM + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package engine + +type TpDestination struct { + Tbid int64 `gorm:"primary_key:yes"` + Tpid string + Id string + Prefix string +} diff --git a/engine/storage_mysql.go b/engine/storage_mysql.go index fa3dd040c..3d3d29a3d 100644 --- a/engine/storage_mysql.go +++ b/engine/storage_mysql.go @@ -19,10 +19,10 @@ along with this program. If not, see package engine import ( - "database/sql" "fmt" _ "github.com/go-sql-driver/mysql" + "github.com/jinzhu/gorm" ) type MySQLStorage struct { @@ -30,9 +30,18 @@ type MySQLStorage struct { } func NewMySQLStorage(host, port, name, user, password string) (Storage, error) { - db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&loc=Local&parseTime=true", user, password, host, port, name)) + connectString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&loc=Local&parseTime=true", user, password, host, port, name) + db, err := gorm.Open("mysql", connectString) if err != nil { return nil, err } - return &MySQLStorage{&SQLStorage{db}}, nil + err = db.DB().Ping() + if err != nil { + return nil, err + } + db.DB().SetMaxIdleConns(10) + db.DB().SetMaxOpenConns(100) + db.LogMode(true) + + return &MySQLStorage{&SQLStorage{Db: db.DB(), db: db}}, nil } diff --git a/engine/storage_postgres.go b/engine/storage_postgres.go index 420ee39f8..431c2bc01 100644 --- a/engine/storage_postgres.go +++ b/engine/storage_postgres.go @@ -19,9 +19,10 @@ along with this program. If not, see package engine import ( - "database/sql" "fmt" + _ "github.com/bmizerany/pq" + "github.com/jinzhu/gorm" ) type PostgresStorage struct { @@ -29,9 +30,18 @@ type PostgresStorage struct { } func NewPostgresStorage(host, port, name, user, password string) (Storage, error) { - db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=disable", host, port, name, user, password)) + connectString := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=disable", host, port, name, user, password) + db, err := gorm.Open("postgres", connectString) if err != nil { return nil, err } - return &PostgresStorage{&SQLStorage{db}}, nil + err = db.DB().Ping() + if err != nil { + return nil, err + } + db.DB().SetMaxIdleConns(10) + db.DB().SetMaxOpenConns(100) + //db.LogMode(true) + + return &PostgresStorage{&SQLStorage{Db: db.DB(), db: db}}, nil } diff --git a/engine/storage_sql.go b/engine/storage_sql.go index a1cbe994c..07e132bda 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -24,23 +24,26 @@ import ( "encoding/json" "fmt" "io/ioutil" + "log" "path" "strconv" "strings" "time" - "github.com/go-sql-driver/mysql" - "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" + "github.com/go-sql-driver/mysql" + "github.com/jinzhu/gorm" ) type SQLStorage struct { Db *sql.DB + db gorm.DB } func (self *SQLStorage) Close() { self.Db.Close() + self.db.Close() } func (self *SQLStorage) Flush() (err error) { @@ -184,19 +187,16 @@ func (self *SQLStorage) SetTPDestination(tpid string, dest *Destination) error { if len(dest.Prefixes) == 0 { return nil } - var buffer bytes.Buffer // Use bytes buffer istead of string concatenation since that becomes quite heavy on large prefixes - buffer.WriteString(fmt.Sprintf("INSERT INTO %s (tpid, id, prefix) VALUES ", utils.TBL_TP_DESTINATIONS)) - for idx, prefix := range dest.Prefixes { - if idx != 0 { - buffer.WriteRune(',') - } - buffer.WriteString(fmt.Sprintf("('%s','%s','%s')", tpid, dest.Id, prefix)) - idx++ - } - buffer.WriteString(" ON DUPLICATE KEY UPDATE prefix=values(prefix)") - if _, err := self.Db.Exec(buffer.String()); err != nil { - return err + tx := self.db.Begin() + tx.Where("tpid = ?", tpid).Where("id = ?", dest.Id).Delete(TpDestination{}) + for _, prefix := range dest.Prefixes { + tx.Save(TpDestination{ + Tpid: tpid, + Id: dest.Id, + Prefix: prefix, + }) } + tx.Commit() return nil } @@ -1007,28 +1007,25 @@ func (self *SQLStorage) RemStoredCdrs(cgrIds []string) error { func (self *SQLStorage) GetTpDestinations(tpid, tag string) (map[string]*Destination, error) { dests := make(map[string]*Destination) - q := fmt.Sprintf("SELECT * FROM %s WHERE tpid='%s'", utils.TBL_TP_DESTINATIONS, tpid) + var tpDests []TpDestination + q := self.db.Where("tpid = ?", tpid) if len(tag) != 0 { - q += fmt.Sprintf(" AND id='%s'", tag) + q = q.Where("id = ?", tag) } - rows, err := self.Db.Query(q) - if err != nil { + log.Printf("%+v", q) + if err := q.Find(&tpDests).Error; err != nil { return nil, err } - defer rows.Close() - for rows.Next() { - var id int - var tpid, tag, prefix string - if err := rows.Scan(&id, &tpid, &tag, &prefix); err != nil { - return nil, err - } + + for _, tpDest := range tpDests { + log.Print(tpDest) var dest *Destination var found bool if dest, found = dests[tag]; !found { dest = &Destination{Id: tag} dests[tag] = dest } - dest.AddPrefix(prefix) + dest.AddPrefix(tpDest.Prefix) } return dests, nil } diff --git a/hard_update_external_libs.py b/hard_update_external_libs.py index 7488f3bdc..bb59c3f72 100755 --- a/hard_update_external_libs.py +++ b/hard_update_external_libs.py @@ -13,6 +13,7 @@ libs = ('code.google.com/p/goconf/conf', 'github.com/go-sql-driver/mysql', 'github.com/hoisie/redis' 'github.com/howeyc/fsnotify', + 'github.com/jinzhu/gorm', ) if __name__ == "__main__": diff --git a/update_external_libs.sh b/update_external_libs.sh index 605b8194c..95453330c 100755 --- a/update_external_libs.sh +++ b/update_external_libs.sh @@ -10,3 +10,4 @@ go get -u -v github.com/hoisie/redis go get -u -v github.com/howeyc/fsnotify go get -u -v github.com/cgrates/liner go get -u -v github.com/cgrates/rpcclient +go get -u -v github.com/jinzhu/gorm