db_class.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 // Author(s): Matei Ciocarlie
36 
37 #ifndef _DB_CLASS_H_
38 #define _DB_CLASS_H_
39 
41 
42 namespace database_interface {
43 
45 
64 class DBClass
65 {
66  private:
68 
72  DBClass& operator = (const DBClass& rhs);
73 
74  protected:
77 
79  std::vector<DBFieldBase*> fields_;
80 
82 
83  std::map<std::string, DBFieldBase*> foreign_keys_;
84 
85  public:
87 
88  size_t getNumFields() const {return fields_.size();}
89 
90  DBFieldBase* getField(size_t i) {return fields_.at(i);}
91  const DBFieldBase* getField(size_t i) const {return fields_.at(i);}
92 
93  DBFieldBase* getField(std::string name)
94  {
95  if (primary_key_field_->getName() == name) return primary_key_field_;
96  for (size_t t=0; t<fields_.size(); t++)
97  {
98  if (fields_[t]->getName() == name) return fields_[t];
99  }
100  return NULL;
101  }
102  const DBFieldBase* getField(std::string name) const {return getField(name);}
103 
106 
108 
111  bool getForeignKey(std::string table, const DBFieldBase* &key) const
112  {
113  std::map<std::string, DBFieldBase*>::const_iterator it;
114  it = foreign_keys_.find(table);
115  if (it==foreign_keys_.end()) return false;
116  key = it->second;
117  return true;
118  }
119 
120  bool getForeignKey(std::string table, DBFieldBase* &key)
121  {
122  return getForeignKey(table, key);
123  }
124 
126  {
128  for (size_t i=0; i<fields_.size(); i++)
129  {
130  fields_[i]->setReadFromDatabase(sync);
131  }
132  }
133 
135  {
137  for (size_t i=0; i<fields_.size(); i++)
138  {
139  fields_[i]->setWriteToDatabase(sync);
140  }
141  }
142 
143 };
144 
145 } //namespace database_interface
146 
147 #endif
void setWriteToDatabase(bool sync)
Definition: db_field.h:139
std::map< std::string, DBFieldBase * > foreign_keys_
List of foreign keys in OTHER tables that reference our PRIMARY KEY.
Definition: db_class.h:83
bool getForeignKey(std::string table, DBFieldBase *&key)
Definition: db_class.h:120
bool getForeignKey(std::string table, const DBFieldBase *&key) const
Returns the name of the foreign key column in a given table that references our primary key...
Definition: db_class.h:111
void setReadFromDatabase(bool sync)
Definition: db_field.h:136
const DBFieldBase * getField(size_t i) const
Definition: db_class.h:91
const DBFieldBase * getPrimaryKeyField() const
Definition: db_class.h:105
The base class for a field of a class stored in the database, corresponding to a column in a table...
Definition: db_field.h:72
The base class for all C++ classes that can be stored in the database.
Definition: db_class.h:64
DBClass & operator=(const DBClass &rhs)
Makes the class non-assignable.
size_t getNumFields() const
Definition: db_class.h:88
std::string getName() const
Definition: db_field.h:149
DBFieldBase * getField(size_t i)
Definition: db_class.h:90
void setAllFieldsReadFromDatabase(bool sync)
Definition: db_class.h:125
void setAllFieldsWriteToDatabase(bool sync)
Definition: db_class.h:134
const DBFieldBase * getField(std::string name) const
Definition: db_class.h:102
std::vector< DBFieldBase * > fields_
The addresses of all the other fields.
Definition: db_class.h:79
DBFieldBase * getField(std::string name)
Definition: db_class.h:93
DBFieldBase * primary_key_field_
The address of the field that acts as a primary key.
Definition: db_class.h:76
DBFieldBase * getPrimaryKeyField()
Definition: db_class.h:104


database_interface
Author(s): Matei Ciocarlie and Lorenz Mosenlechner
autogenerated on Mon Mar 2 2015 01:31:34