From db62f2cc851a46b3e4a2020a7ff960a80958151d Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 4 Jul 2013 19:20:55 +0200 Subject: [PATCH] Finishing Get and Set for TPDestinations --- apier/tpdestinations.go | 5 +++ .../mysql/create_tariffplan_tables.sql | 3 +- rater/storage_interface.go | 1 + rater/storage_map.go | 4 +++ rater/storage_mongo.go | 4 +++ rater/storage_redis.go | 4 +++ rater/storage_sql.go | 34 ++++++++++++------- utils/consts.go | 10 ++++++ 8 files changed, 51 insertions(+), 14 deletions(-) diff --git a/apier/tpdestinations.go b/apier/tpdestinations.go index 2120dda70..d89ca0749 100644 --- a/apier/tpdestinations.go +++ b/apier/tpdestinations.go @@ -55,6 +55,11 @@ func (self *Apier) SetTPDestination(attrs AttrSetTPDestination, reply *string) e if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationId", "Prefixes"}); len(missing) != 0 { //Params missing return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } + if exists, err := self.StorDb.ExistsTPDestination(attrs.TPid, attrs.DestinationId); err != nil { + return fmt.Errorf("%s:%v", utils.ERR_SERVER_ERROR, err.Error()) + } else if exists { + return errors.New(utils.ERR_DUPLICATE) + } if err := self.StorDb.SetTPDestination(attrs.TPid, &rater.Destination{attrs.DestinationId, attrs.Prefixes}); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } diff --git a/data/storage/mysql/create_tariffplan_tables.sql b/data/storage/mysql/create_tariffplan_tables.sql index f57301477..158f1a610 100644 --- a/data/storage/mysql/create_tariffplan_tables.sql +++ b/data/storage/mysql/create_tariffplan_tables.sql @@ -18,12 +18,13 @@ CREATE TABLE `tp_timings` ( -- Table structure for table `tp_destinations` -- -CREATE TABLE `tp_destinatins` ( +CREATE TABLE `tp_destinations` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tpid` char(40) NOT NULL, `tag` varchar(24) NOT NULL, `prefix` varchar(24) NOT NULL, PRIMARY KEY (`id`), + UNIQUE KEY `tpid_dest_prefix` (`tpid`,`tag`,`prefix`), KEY `tpid` (`tpid`) ); diff --git a/rater/storage_interface.go b/rater/storage_interface.go index b4efccd07..2b743c94d 100644 --- a/rater/storage_interface.go +++ b/rater/storage_interface.go @@ -57,6 +57,7 @@ type DataStorage interface { SetRatingProfile(*RatingProfile) error GetDestination(string) (*Destination, error) SetDestination(*Destination) error + ExistsTPDestination(string, string) (bool, error) GetTPDestination(string, string) (*Destination, error) SetTPDestination(string, *Destination) error GetActions(string) (Actions, error) diff --git a/rater/storage_map.go b/rater/storage_map.go index d6e7934bd..7c1d89390 100644 --- a/rater/storage_map.go +++ b/rater/storage_map.go @@ -73,6 +73,10 @@ func (ms *MapStorage) SetDestination(dest *Destination) (err error) { return } +func (ms *MapStorage) ExistsTPDestination(tpid, destTag string) (bool, error) { + return false, errors.New(utils.ERR_NOT_IMPLEMENTED) +} + // Extracts destinations from StorDB on specific tariffplan id func (ms *MapStorage) GetTPDestination(tpid, destTag string) (*Destination, error) { return nil, nil diff --git a/rater/storage_mongo.go b/rater/storage_mongo.go index c4628eb28..5e1a8ab3d 100644 --- a/rater/storage_mongo.go +++ b/rater/storage_mongo.go @@ -147,6 +147,10 @@ func (ms *MongoStorage) SetDestination(dest *Destination) error { return ms.db.C("destinations").Insert(dest) } +func (ms *MongoStorage) ExistsTPDestination(tpid, destTag string) (bool, error) { + return false, errors.New(utils.ERR_NOT_IMPLEMENTED) +} + // Extracts destinations from StorDB on specific tariffplan id func (ms *MongoStorage) GetTPDestination(tpid, destTag string) (*Destination, error) { return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) diff --git a/rater/storage_redis.go b/rater/storage_redis.go index a459864e1..c69d4576b 100644 --- a/rater/storage_redis.go +++ b/rater/storage_redis.go @@ -102,6 +102,10 @@ func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { return } +func (rs *RedisStorage) ExistsTPDestination(tpid, destTag string) (bool, error) { + return false, errors.New(utils.ERR_NOT_IMPLEMENTED) +} + // Extracts destinations from StorDB on specific tariffplan id func (rs *RedisStorage) GetTPDestination(tpid, destTag string) (*Destination, error) { return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) diff --git a/rater/storage_sql.go b/rater/storage_sql.go index 62a985a14..98152da70 100644 --- a/rater/storage_sql.go +++ b/rater/storage_sql.go @@ -23,7 +23,6 @@ import ( "encoding/json" "fmt" "github.com/cgrates/cgrates/utils" - "errors" ) type SQLStorage struct { @@ -55,9 +54,18 @@ func (sql *SQLStorage) SetDestination(d *Destination) (err error) { return } +func (sql *SQLStorage) ExistsTPDestination(tpid, destTag string) (bool, error) { + var exists bool + err := sql.Db.QueryRow(fmt.Sprintf("SELECT EXISTS (SELECT 1 FROM %s WHERE tpid='%s' AND tag='%s')", utils.TBL_TP_DESTINATIONS, tpid, destTag)).Scan(&exists) + if err != nil { + return false, err + } + return exists, nil +} + // Extracts destinations from StorDB on specific tariffplan id func (sql *SQLStorage) GetTPDestination(tpid, destTag string) (*Destination, error) { - rows, err := sql.Db.Query(fmt.Sprintf("SELECT prefix FROM tp_destinatins WHERE id='%s' AND tag='%s'", tpid, destTag)) + rows, err := sql.Db.Query(fmt.Sprintf("SELECT prefix FROM %s WHERE tpid='%s' AND tag='%s'", utils.TBL_TP_DESTINATIONS, tpid, destTag)) if err != nil { return nil, err } @@ -81,11 +89,11 @@ func (sql *SQLStorage) GetTPDestination(tpid, destTag string) (*Destination, err func (sql *SQLStorage) SetTPDestination(tpid string, dest *Destination) error { for _,prefix := range dest.Prefixes { - if _,err := sql.Db.Exec(fmt.Sprintf("INSERT INTO tp_destinations (tpid, tag, prefix) VALUES( '%s','%s','%s')",tpid, dest.Id, prefix));err!=nil { + if _,err := sql.Db.Exec(fmt.Sprintf("INSERT INTO %s (tpid, tag, prefix) VALUES( '%s','%s','%s')",utils.TBL_TP_DESTINATIONS, tpid, dest.Id, prefix));err!=nil { return err } } - return errors.New(utils.ERR_NOT_IMPLEMENTED) + return nil } func (sql *SQLStorage) GetActions(string) (as Actions, err error) { @@ -207,7 +215,7 @@ func (sql *SQLStorage) GetAllRatedCdr() ([]utils.CDR, error) { func (sql *SQLStorage) GetTpDestinations(tpid, tag string) ([]*Destination, error) { var dests []*Destination - q := fmt.Sprintf("SELECT * FROM tp_destinations WHERE tpid=%s", tpid) + q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_DESTINATIONS, tpid) if tag != "" { q += "AND tag=" + tag } @@ -239,7 +247,7 @@ func (sql *SQLStorage) GetTpDestinations(tpid, tag string) ([]*Destination, erro func (sql *SQLStorage) GetTpRates(tpid, tag string) (map[string][]*Rate, error) { rts := make(map[string][]*Rate) - q := fmt.Sprintf("SELECT * FROM tp_rates WHERE tpid=%s", tpid) + q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_RATES, tpid) if tag != "" { q += "AND tag=" + tag } @@ -270,7 +278,7 @@ func (sql *SQLStorage) GetTpRates(tpid, tag string) (map[string][]*Rate, error) func (sql *SQLStorage) GetTpTimings(tpid, tag string) (map[string]*Timing, error) { tms := make(map[string]*Timing) - q := fmt.Sprintf("SELECT * FROM tp_timings WHERE tpid=%s", tpid) + q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_TIMINGS, tpid) if tag != "" { q += "AND tag=" + tag } @@ -291,7 +299,7 @@ func (sql *SQLStorage) GetTpTimings(tpid, tag string) (map[string]*Timing, error func (sql *SQLStorage) GetTpRateTimings(tpid, tag string) ([]*RateTiming, error) { var rts []*RateTiming - q := fmt.Sprintf("SELECT * FROM tp_rate_timings WHERE tpid=%s", tpid) + q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_RATE_TIMINGS, tpid) if tag != "" { q += "AND tag=" + tag } @@ -319,7 +327,7 @@ func (sql *SQLStorage) GetTpRateTimings(tpid, tag string) ([]*RateTiming, error) func (sql *SQLStorage) GetTpRatingProfiles(tpid, tag string) (map[string]*RatingProfile, error) { rpfs := make(map[string]*RatingProfile) - q := fmt.Sprintf("SELECT * FROM tp_rate_profiles WHERE tpid=%s", tpid) + q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_RATE_PROFILES, tpid) if tag != "" { q += "AND tag=" + tag } @@ -350,7 +358,7 @@ func (sql *SQLStorage) GetTpRatingProfiles(tpid, tag string) (map[string]*Rating } func (sql *SQLStorage) GetTpActions(tpid, tag string) (map[string][]*Action, error) { as := make(map[string][]*Action) - q := fmt.Sprintf("SELECT * FROM tp_actions WHERE tpid=%s", tpid) + q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_ACTIONS, tpid) if tag != "" { q += "AND tag=" + tag } @@ -403,7 +411,7 @@ func (sql *SQLStorage) GetTpActions(tpid, tag string) (map[string][]*Action, err func (sql *SQLStorage) GetTpActionTimings(tpid, tag string) (ats map[string][]*ActionTiming, err error) { ats = make(map[string][]*ActionTiming) - q := fmt.Sprintf("SELECT * FROM tp_action_timings WHERE tpid=%s", tpid) + q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_ACTION_TIMINGS, tpid) if tag != "" { q += "AND tag=" + tag } @@ -432,7 +440,7 @@ func (sql *SQLStorage) GetTpActionTimings(tpid, tag string) (ats map[string][]*A func (sql *SQLStorage) GetTpActionTriggers(tpid, tag string) (map[string][]*ActionTrigger, error) { ats := make(map[string][]*ActionTrigger) - q := fmt.Sprintf("SELECT * FROM tp_action_triggers WHERE tpid=%s", tpid) + q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_ACTION_TRIGGERS, tpid) if tag != "" { q += "AND tag=" + tag } @@ -464,7 +472,7 @@ func (sql *SQLStorage) GetTpActionTriggers(tpid, tag string) (map[string][]*Acti func (sql *SQLStorage) GetTpAccountActions(tpid, tag string) ([]*AccountAction, error) { var acs []*AccountAction - q := fmt.Sprintf("SELECT * FROM tp_account_actions WHERE tpid=%s", tpid) + q := fmt.Sprintf("SELECT * FROM %s WHERE tpid=%s", utils.TBL_TP_ACCOUNT_ACTIONS, tpid) if tag != "" { q += "AND tag=" + tag } diff --git a/utils/consts.go b/utils/consts.go index cf1689f09..fd04a3807 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -13,4 +13,14 @@ const ( ERR_SERVER_ERROR = "SERVER_ERROR" ERR_NOT_FOUND = "NOT_FOUND" ERR_MANDATORY_IE_MISSING = "MANDATORY_IE_MISSING" + ERR_DUPLICATE = "DUPLICATE" + TBL_TP_TIMINGS = "tp_timings" + TBL_TP_DESTINATIONS = "tp_destinations" + TBL_TP_RATES = "tp_rates" + TBL_TP_RATE_TIMINGS = "tp_rate_timings" + TBL_TP_RATE_PROFILES = "tp_rate_profiles" + TBL_TP_ACTIONS = "tp_actions" + TBL_TP_ACTION_TIMINGS = "tp_action_timings" + TBL_TP_ACTION_TRIGGERS = "tp_action_triggers" + TBL_TP_ACCOUNT_ACTIONS = "tp_account_actions" )