$search
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2009, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Willow Garage nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 // Author(s): Matei Ciocarlie 00036 00037 #ifndef _DB_CLASS_H_ 00038 #define _DB_CLASS_H_ 00039 00040 #include "database_interface/db_field.h" 00041 00042 namespace database_interface { 00043 00045 00064 class DBClass 00065 { 00066 private: 00068 00072 DBClass& operator = (const DBClass& rhs); 00073 00074 protected: 00076 DBFieldBase* primary_key_field_; 00077 00079 std::vector<DBFieldBase*> fields_; 00080 00082 00083 std::map<std::string, DBFieldBase*> foreign_keys_; 00084 00085 public: 00086 DBClass() : primary_key_field_(NULL) {} 00087 00088 size_t getNumFields() const {return fields_.size();} 00089 00090 DBFieldBase* getField(size_t i) {return fields_.at(i);} 00091 const DBFieldBase* getField(size_t i) const {return fields_.at(i);} 00092 00093 DBFieldBase* getField(std::string name) 00094 { 00095 if (primary_key_field_->getName() == name) return primary_key_field_; 00096 for (size_t t=0; t<fields_.size(); t++) 00097 { 00098 if (fields_[t]->getName() == name) return fields_[t]; 00099 } 00100 return NULL; 00101 } 00102 const DBFieldBase* getField(std::string name) const {return getField(name);} 00103 00104 DBFieldBase* getPrimaryKeyField() {return primary_key_field_;} 00105 const DBFieldBase* getPrimaryKeyField() const {return primary_key_field_;} 00106 00108 00111 bool getForeignKey(std::string table, const DBFieldBase* &key) const 00112 { 00113 std::map<std::string, DBFieldBase*>::const_iterator it; 00114 it = foreign_keys_.find(table); 00115 if (it==foreign_keys_.end()) return false; 00116 key = it->second; 00117 return true; 00118 } 00119 00120 bool getForeignKey(std::string table, DBFieldBase* &key) 00121 { 00122 return getForeignKey(table, key); 00123 } 00124 00125 void setAllFieldsReadFromDatabase(bool sync) 00126 { 00127 if (primary_key_field_) primary_key_field_->setReadFromDatabase(sync); 00128 for (size_t i=0; i<fields_.size(); i++) 00129 { 00130 fields_[i]->setReadFromDatabase(sync); 00131 } 00132 } 00133 00134 void setAllFieldsWriteToDatabase(bool sync) 00135 { 00136 if (primary_key_field_) primary_key_field_->setWriteToDatabase(sync); 00137 for (size_t i=0; i<fields_.size(); i++) 00138 { 00139 fields_[i]->setWriteToDatabase(sync); 00140 } 00141 } 00142 00143 }; 00144 00145 } //namespace database_interface 00146 00147 #endif