Navigation


Changeset 216

Show
Ignore:
Timestamp:
11/12/08 15:33:14 (8 weeks ago)
Author:
tinova
Message:

Modifying TemplateSQL class to support insert/delete attributes.

Location:
one/branches/DEVELOPMENT/VirtualNetworkManager
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • one/branches/DEVELOPMENT/VirtualNetworkManager/include/Attribute.h

    r110 r216  
    3434public: 
    3535 
    36     Attribute(string& aname):attribute_name(aname) 
     36    Attribute(const string& aname):attribute_name(aname) 
    3737    { 
    3838        transform ( 
     
    105105public: 
    106106 
    107     SingleAttribute(string& name):Attribute(name){}; 
    108  
    109     SingleAttribute(string& name, string& value): 
     107    SingleAttribute(const string& name):Attribute(name){}; 
     108 
     109    SingleAttribute(const string& name, const string& value): 
    110110        Attribute(name),attribute_value(value){}; 
    111111     
    112     SingleAttribute(const char * name, string& value): 
     112    SingleAttribute(const char * name, const string& value): 
    113113        Attribute(name),attribute_value(value){}; 
    114114 
     
    170170public: 
    171171 
    172     VectorAttribute(string& name):Attribute(name){}; 
    173  
    174     VectorAttribute(string& name, map<string,string>& value): 
     172    VectorAttribute(const string& name):Attribute(name){}; 
     173 
     174    VectorAttribute(const string& name,const map<string,string>& value): 
    175175            Attribute(name),attribute_value(value){}; 
    176176 
  • one/branches/DEVELOPMENT/VirtualNetworkManager/include/TemplateSQL.h

    r65 r216  
    9393     *    @param value of the new attribute.  
    9494     */ 
    95     int replace_attribute(SqliteDB * db, string& name, string& value);     
     95    int replace_attribute(SqliteDB * db, const string& name, const string& value);  
     96     
     97    /** 
     98     *  Inserts single attribute  
     99     *    @param db pointer to the database. 
     100     *    @param name of the attribute. 
     101     *    @param value of the attribute. 
     102     */ 
     103    int insert_attribute(SqliteDB * db, const string& name, const string& value);    
     104     
     105    /** 
     106     *  Inserts vector attribute  
     107     *    @param db pointer to the database. 
     108     *    @param name of the attribute. 
     109     *    @param values of the attribute. 
     110     */ 
     111    int insert_attribute(SqliteDB *                 db,  
     112                         const string&              name,  
     113                         const map<string, string>& values); 
     114     
     115    /** 
     116     *  Removes all occurences of a given attribute (SINGLE OR VECTOR) 
     117     *    @param db pointer to the database. 
     118     *    @param name of the attribute. 
     119     */ 
     120    int delete_attribute(SqliteDB * db, const string& name); 
    96121}; 
    97122 
  • one/branches/DEVELOPMENT/VirtualNetworkManager/src/template/TemplateSQL.cc

    r65 r216  
    245245int TemplateSQL::replace_attribute( 
    246246        SqliteDB *                                      db,  
    247         string&                                         name,  
    248         string&                                         value) 
     247        const string&                           name,  
     248        const string&                           value) 
    249249{ 
    250250    ostringstream   oss; 
     
    253253    multimap<string, Attribute *>::const_iterator       i; 
    254254    Attribute *         attribute; 
     255     
     256    string          _name  = name; 
     257    string          _value = value; 
    255258     
    256259    if ( id == -1 || name.empty() || name.empty() ) 
     
    288291    } 
    289292 
    290         attribute = new SingleAttribute(name,value); 
     293        attribute = new SingleAttribute(_name,_value); 
    291294         
    292295        attributes.insert(make_pair(attribute->name(),attribute)); 
     
    303306/* -------------------------------------------------------------------------- */ 
    304307/* -------------------------------------------------------------------------- */ 
     308 
     309int TemplateSQL::insert_attribute(SqliteDB *    db,  
     310                                  const string& name,  
     311                                  const string& value) 
     312{ 
     313    ostringstream   oss; 
     314     
     315    Attribute *         attribute; 
     316     
     317         
     318    attribute = new SingleAttribute(name,value); 
     319         
     320        attributes.insert(make_pair(attribute->name(),attribute)); 
     321         
     322        oss.str(""); 
     323         
     324    oss << "INSERT INTO " << table << " " << db_names 
     325        << " VALUES (" << id << ",'" << name << "'," <<  
     326        Attribute::SIMPLE <<",'" << value << "')"; 
     327 
     328    return db->exec(oss); 
     329} 
     330 
     331/* -------------------------------------------------------------------------- */ 
     332/* -------------------------------------------------------------------------- */ 
     333 
     334int TemplateSQL::insert_attribute(SqliteDB *           db,  
     335                                  const string&        name,  
     336                                  const map<string, string>& values) 
     337{ 
     338    ostringstream   oss; 
     339     
     340    Attribute *     attribute; 
     341    string    *     attr; 
     342              
     343    attribute = new VectorAttribute(name,values); 
     344         
     345        attributes.insert(make_pair(attribute->name(),attribute)); 
     346         
     347        oss.str(""); 
     348         
     349        attr  = attribute->marshall(); 
     350 
     351    if ( attr == 0 ) 
     352    { 
     353        return -1; 
     354    } 
     355 
     356    oss << "INSERT OR REPLACE INTO " << table << " " << db_names 
     357        << " VALUES (" << id << ",'" << name << "',"<< Attribute::VECTOR <<",'" 
     358        << *attr << "')"; 
     359 
     360    return db->exec(oss);     
     361} 
     362 
     363/* -------------------------------------------------------------------------- */ 
     364/* -------------------------------------------------------------------------- */ 
     365 
     366int TemplateSQL::delete_attribute(SqliteDB * db, const string& name) 
     367{ 
     368    ostringstream   oss; 
     369    int                         rc; 
     370             
     371    multimap<string, Attribute *>::const_iterator       i; 
     372     
     373    i = attributes.find(name); 
     374 
     375    if ( i != attributes.end() ) //attribute exists 
     376    { 
     377            oss << "DELETE FROM " << table << " WHERE id=" << id  
     378                << " AND name='" << name << "'"; 
     379 
     380        rc = db->exec(oss); 
     381 
     382        if ( rc != 0 ) 
     383        { 
     384               return rc; 
     385        } 
     386 
     387        attributes.erase(name); 
     388    } 
     389     
     390    return 0; 
     391}