LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
DbStorageImpl.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #ifndef LSST_MWI_PERSISTENCE_DBSTORAGEIMPL_H
26 #define LSST_MWI_PERSISTENCE_DBSTORAGEIMPL_H
27 
46 #include <boost/shared_array.hpp>
47 #include <mysql/mysql.h>
48 #include <string>
49 #include <vector>
50 
51 
52 #include "lsst/tr1/unordered_map.h"
53 #include "lsst/daf/base/Citizen.h"
54 #include "lsst/daf/base/DateTime.h"
55 #include "lsst/pex/policy.h"
56 
57 namespace lsst {
58 namespace daf {
59 namespace persistence {
60 
61 namespace dafBase = lsst::daf::base;
62 namespace pexPolicy = lsst::pex::policy;
63 
64 class LogicalLocation;
65 
66 class BoundVar : public dafBase::Citizen {
67 public:
68  BoundVar(void);
69  explicit BoundVar(void* location);
70  BoundVar(BoundVar const& src);
71 
72  enum_field_types _type;
73  bool _isNull;
75  unsigned long _length;
76  void* _data;
77 };
78 
80 public:
81  virtual ~DbStorageImpl(void);
82 
83 
84 private:
85  friend class DbStorage;
86 
87  DbStorageImpl(void);
88 
89  virtual void setPolicy(pexPolicy::Policy::Ptr policy);
90  virtual void setPersistLocation(LogicalLocation const& location);
91  virtual void setRetrieveLocation(LogicalLocation const& location);
92 
93  virtual void startTransaction(void);
94  virtual void endTransaction(void);
95 
96  virtual void startSession(std::string const& location);
97 
98  virtual void createTableFromTemplate(std::string const& tableName,
99  std::string const& templateName,
100  bool mayAlreadyExist);
101  virtual void dropTable(std::string const& tableName);
102  virtual void truncateTable(std::string const& tableName);
103 
104  virtual void executeSql(std::string const& sqlStatement);
105 
106  virtual void setTableForInsert(std::string const& tableName);
107  template <typename T>
108  void setColumn(std::string const& columnName, T const& value);
109  virtual void setColumnToNull(std::string const& columnName);
110  virtual void insertRow(void);
111 
112  virtual void setTableForQuery(std::string const& tableName, bool isExpr);
113  virtual void setTableListForQuery(
114  std::vector<std::string> const& tableNameList);
115  virtual void outColumn(std::string const& columnName, bool isExpr);
116  template <typename T> void outParam(std::string const& columnName,
117  T* location, bool isExpr);
118  template <typename T> void condParam(std::string const& paramName,
119  T const& value);
120  virtual void orderBy(std::string const& expression);
121  virtual void groupBy(std::string const& expression);
122  virtual void setQueryWhere(std::string const& whereClause);
123  virtual void query(void);
124  virtual bool next(void);
125  template <typename T> T const& getColumnByPos(int pos);
126  bool columnIsNull(int pos);
127  virtual void finishQuery(void);
128 
129  // MySQL-specific functions for implementation.
130  void executeQuery(std::string const& query);
131  std::string quote(std::string const& name);
132  void stError(std::string const& text);
133  void error(std::string const& text, bool mysqlCaused = true);
134 
135  void* allocateMemory(size_t size);
136 
137  bool _readonly;
139  std::string _location;
141  MYSQL* _db;
143 
144  std::string _insertTable;
146  std::vector<std::string> _queryTables;
148 
149  typedef std::tr1::unordered_map<std::string, BoundVar> BoundVarMap;
154  std::vector< boost::shared_array<char> > _bindingMemory;
156 
157  // Parts of SQL statement.
158  std::vector<std::string> _outColumns;
159  std::string _whereClause;
160  std::string _groupBy;
161  std::string _orderBy;
162 
163  MYSQL_STMT* _statement;
165  MYSQL_FIELD* _resultFields;
169  boost::shared_array<unsigned long> _fieldLengths;
171  boost::shared_array<my_bool> _fieldNulls;
173 };
174 
175 template <>
176 void DbStorageImpl::setColumn<std::string>(std::string const& columnName,
177  std::string const& value);
178 template <>
179 void DbStorageImpl::setColumn<dafBase::DateTime>(std::string const& columnName,
180  dafBase::DateTime const& value);
181 
182 template <>
183 void DbStorageImpl::outParam<std::string>(std::string const& columnName,
184  std::string* location, bool isExpr);
185 template <>
186 void DbStorageImpl::outParam<dafBase::DateTime>(std::string const& columnName,
187  dafBase::DateTime* location,
188  bool isExpr);
189 
190 template <>
191 std::string const& DbStorageImpl::getColumnByPos<std::string>(int pos);
192 template <>
193 dafBase::DateTime const&
194 DbStorageImpl::getColumnByPos<dafBase::DateTime>(int pos);
195 
196 }}} // namespace lsst::daf::persistence
197 
198 #endif
std::vector< boost::shared_array< char > > _bindingMemory
Memory for bound variables.
virtual void setQueryWhere(std::string const &whereClause)
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:58
table::Key< std::string > name
Definition: ApCorrMap.cc:71
virtual void setTableForInsert(std::string const &tableName)
void outParam(std::string const &columnName, T *location, bool isExpr)
void setColumn(std::string const &columnName, T const &value)
std::vector< std::string > _queryTables
Names of tables to select from.
virtual void executeSql(std::string const &sqlStatement)
Class for logical location of a persisted Persistable instance.
virtual void setPolicy(pexPolicy::Policy::Ptr policy)
boost::shared_array< unsigned long > _fieldLengths
Space for lengths of result fields.
virtual void outColumn(std::string const &columnName, bool isExpr)
boost::shared_ptr< Policy > Ptr
Definition: Policy.h:172
int _numResultFields
Number of result fields.
virtual void groupBy(std::string const &expression)
virtual void startSession(std::string const &location)
Class for database storage.
Definition: DbStorage.h:63
MYSQL * _db
MySQL database connection pointer.
std::tr1::unordered_map< std::string, BoundVar > BoundVarMap
void error(std::string const &text, bool mysqlCaused=true)
virtual void setRetrieveLocation(LogicalLocation const &location)
MYSQL_STMT * _statement
Prepared query statement.
void condParam(std::string const &paramName, T const &value)
boost::shared_array< my_bool > _fieldNulls
Space for null flags of result fields.
Interface for DateTime class.
virtual void setTableListForQuery(std::vector< std::string > const &tableNameList)
MYSQL_FIELD * _resultFields
Query result field metadata.
Class for implementation of database storage.
Definition: DbStorageImpl.h:79
virtual void orderBy(std::string const &expression)
virtual void truncateTable(std::string const &tableName)
std::string _location
Database location string saved for use by raw MySQL interface.
virtual void setColumnToNull(std::string const &columnName)
std::string quote(std::string const &name)
virtual void createTableFromTemplate(std::string const &tableName, std::string const &templateName, bool mayAlreadyExist)
void stError(std::string const &text)
BoundVarMap _inputVars
Input variable bindings.
BoundVarMap _outputVars
Output variable bindings.
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:56
virtual void setPersistLocation(LogicalLocation const &location)
void executeQuery(std::string const &query)
virtual void dropTable(std::string const &tableName)
bool _readonly
Remember if we are supposed to be read-only.
std::vector< std::string > _outColumns
std::string _insertTable
Name of table into which to insert.
virtual void setTableForQuery(std::string const &tableName, bool isExpr)