Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Examples

vos/metaobjects/property/db_property.cc

Go to the documentation of this file.
00001 /* $Id: db_property.cc,v 1.7 2002/08/01 07:16:08 tetron Exp $ */
00002 
00003 /**
00004     @file db_property.cc
00005     
00006     Implements general interface for property with database backend. This class
00007     is intended to be subclassed in order to implement MOS-database
00008     bridges with various SQL-based database systems. Subclasses will
00009     have to implement the actual updateToDB() and updateFromDB(), and
00010     methods, and extend the init() method to connect to the database,
00011     (but call DBProperty::init() to set up query strings)
00012     as well as any dynamic update when the database changes.
00013 
00014     @author Reed Hedges <reed@zerohour.net>
00015 
00016 
00017     This file is part of the Virtual Object System of
00018     the Interreality project (http://interreality.org).
00019 
00020     Copyright (C) 2001, 2002 Reed Hedges
00021 
00022     This library is free software; you can redistribute it and/or
00023     modify it under the terms of the GNU Lesser General Public
00024     License as published by the Free Software Foundation; either
00025     version 2 of the License, or (at your option) any later version.
00026 
00027     This library is distributed in the hope that it will be useful,
00028     but WITHOUT ANY WARRANTY; without even the implied warranty of
00029     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00030     Lesser General Public License for more details.
00031 
00032     You should have received a copy of the GNU Lesser General Public
00033     License along with this library; if not, write to the Free Software
00034     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00035 
00036 */
00037 
00038 #include "db_property.hh"
00039 
00040 
00041 
00042 
00043 
00044 /* Set up query strings, for getting or changing just data, with constant data type.  */
00045 
00046 void DBProperty::init(const string& select_query, const string& update_query, const string& type) {
00047     select_data_query = select_query;
00048     update_data_query = update_query;
00049     force_type = type;
00050 }
00051 
00052 /* Set up query strings, for getting or changing both data value and data type.  */
00053 void DBProperty::init(const string& select_all_q, const string& select_data_q, const string& select_type_q, const string& update_all_q, const string& update_data_q, const string& update_type_q) {
00054     select_all_query = select_all_q;
00055     select_data_query = select_data_q;
00056     select_type_query = select_type_q;
00057     update_all_query = update_all_q;
00058     update_data_query = update_data_q;
00059     update_type_query = update_type_q;  // eventually will be removed.
00060 }
00061 
00062 /* Use given values to construct standard (I think) SQL query strings. 
00063 */
00064 void DBProperty::init_components(const string& table, const string& name_col, const string& val_col, const string& type_col, const string& name_val) {
00065     select_all_query = "SELECT "+val_col+", "+type_col+" FROM "+table+" WHERE "+name_col+" = "+name_val;
00066     select_data_query = "SELECT "+val_col+" FROM "+table+" WHERE "+name_col+" = "+name_val;
00067     select_type_query = "SELECT "+type_col+" FROM "+table+" WHERE "+name_col+" = "+name_val;
00068     update_all_query = "UPDATE "+table+" SET "+val_col+" = \"%d\", "+type_col+" = \"%t\" WHERE "+name_col+" = "+name_val;
00069     update_data_query = "UPDATE "+table+" SET "+val_col+" = \"%d\" WHERE "+name_col+" = "+name_val;
00070 }
00071 
00072 /* fetch stuff from database and change cached variables. */
00073 void DBProperty::updateFromDB() throw(DBAccessError) {
00074     LOG("DBProperty", 9, "updateFromDB(): querying...");
00075     string data, type;
00076     data = queryDB(select_data_query);
00077     if (force_type == "") {
00078         // TODO: use select_all_query, then get each part
00079         type = queryDB(select_type_query);
00080         if( (data != readRaw()) || (type != getDataType()) ) 
00081             LocalProperty::replace(queryDB(select_data_query), queryDB(select_type_query));
00082     } else {
00083         if(data != readRaw())
00084             LocalProperty::replace( queryDB(select_data_query), force_type);
00085     }
00086 }
00087             
00088         
00089 
00090 /* write data and type to DB. */
00091 void DBProperty::updateToDB() throw(DBAccessError) {
00092 
00093     LOG("DBProperty", 9, "updateToDB(): querying...");
00094     if(force_type == "") {
00095         queryDB( sreplace( sreplace(update_all_query, "%d", data), "%t", datatype) );
00096     } else {
00097         queryDB( sreplace( update_data_query, "%d", data) );
00098     }
00099 }
00100 
00101 
00102 
00103 /** replaces substrings. 
00104     @bug does it replace multiple occurances of key?
00105 */
00106 string DBProperty::sreplace( const string& s, const string& key, const string& value) throw (range_error) {
00107     string tmp(s);
00108     unsigned int k;
00109     k = s.find(key);
00110     if(k == s.npos) throw range_error("substring not found.");
00111     return tmp.replace( k, key.length(), value);
00112 }

Generated on Tue Aug 12 03:55:45 2003 for Interreality Project - VOS by doxygen 1.3.2