mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-13 19:56:38 +05:00
start using gorm as ORM
This commit is contained in:
26
engine/models.go
Normal file
26
engine/models.go
Normal file
@@ -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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package engine
|
||||
|
||||
type TpDestination struct {
|
||||
Tbid int64 `gorm:"primary_key:yes"`
|
||||
Tpid string
|
||||
Id string
|
||||
Prefix string
|
||||
}
|
||||
@@ -19,10 +19,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
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
|
||||
}
|
||||
|
||||
@@ -19,9 +19,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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__":
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user