lsst.daf.persistence  14.0-14-g87d16e8+11
PropertySetFormatter.cc
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 
26 
38 #ifndef __GNUC__
39 # define __attribute__(x) /*NOTHING*/
40 #endif
41 static char const* SVNid __attribute__((unused)) = "$Id$";
42 
44 
45 #include <sstream>
46 #include <stdexcept>
47 #include <string>
48 #include <vector>
49 
50 #include <boost/serialization/nvp.hpp>
51 #include <boost/serialization/shared_ptr.hpp>
52 #include <boost/serialization/vector.hpp>
53 
55 #include "lsst/daf/base/DateTime.h"
61 #include <lsst/pex/exceptions.h>
62 #include <lsst/log/Log.h>
63 #include <lsst/pex/policy/Policy.h>
64 
65 namespace {
66 auto _log = LOG_GET("daf.persistence.PropertySetFormatter");
67 }
68 
69 namespace dafBase = lsst::daf::base;
71 namespace pexPolicy = lsst::pex::policy;
72 
73 using boost::serialization::make_nvp;
74 
79 dafPersist::PropertySetFormatter::registration("PropertySet",
80  typeid(dafBase::PropertySet),
81  createInstance);
82 
86 dafPersist::PropertySetFormatter::PropertySetFormatter(
87  pexPolicy::Policy::Ptr policy) :
88  dafPersist::Formatter(typeid(*this)), _policy(policy) {
89 }
90 
94 }
95 
97  dafBase::Persistable const* persistable,
99  dafBase::PropertySet::Ptr additionalData) {
100  LOGLS_TRACE(_log, "PropertySetFormatter write start");
101  dafBase::PropertySet const* ps =
102  dynamic_cast<dafBase::PropertySet const*>(persistable);
103  if (ps == 0) {
104  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-PropertySet");
105  }
106  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
107  LOGLS_TRACE(_log, "PropertySetFormatter write BoostStorage");
109  dynamic_cast<dafPersist::BoostStorage*>(storage.get());
110  boost->getOArchive() & *ps;
111  LOGLS_TRACE(_log, "PropertySetFormatter write end");
112  return;
113  }
114  else if (typeid(*storage) == typeid(dafPersist::XmlStorage)) {
115  LOGLS_TRACE(_log, "PropertySetFormatter write XmlStorage");
117  dynamic_cast<dafPersist::XmlStorage*>(storage.get());
118  xml->getOArchive() & make_nvp("propertySet", *ps);
119  LOGLS_TRACE(_log, "PropertySetFormatter write end");
120  return;
121  }
122  else if (typeid(*storage) == typeid(dafPersist::DbStorage)) {
123  LOGLS_TRACE(_log, "PropertySetFormatter write DbStorage");
125  dynamic_cast<dafPersist::DbStorage*>(storage.get());
126 
127  std::string itemName = additionalData->getAsString("itemName");
128  std::string tableName = itemName;
129  pexPolicy::Policy::Ptr itemPolicy;
130  if (_policy && _policy->exists(itemName)) {
131  itemPolicy = _policy->getPolicy(itemName);
132  if (itemPolicy->exists("TableName")) {
133  tableName = itemPolicy->getString("TableName");
134  }
135  }
136  db->setTableForInsert(tableName);
137 
139  if (itemPolicy && itemPolicy->exists("KeyList")) {
140  pexPolicy::Policy::StringArray const& array(
141  itemPolicy->getStringArray("KeyList"));
142  for (pexPolicy::Policy::StringArray::const_iterator it =
143  array.begin(); it != array.end(); ++it) {
144  list.push_back(*it);
145  }
146  }
147  else {
148  list = ps->paramNames(false);
149  }
150 
152  it != list.end(); ++it) {
153  std::string::size_type split = it->find('=');
154  std::string colName;
155  std::string key;
156  if (split == std::string::npos) {
157  colName = key = *it;
158  }
159  else {
160  colName = it->substr(0, split);
161  key = it->substr(split + 1);
162  }
163 
164  if (!ps->exists(key)) {
165  db->setColumnToNull(colName);
166  continue;
167  }
168 
169  std::type_info const& type(ps->typeOf(key));
170 
171  if (type == typeid(bool)) {
172  db->setColumn<bool>(colName, ps->get<bool>(key));
173  }
174  else if (type == typeid(char)) {
175  db->setColumn<char>(colName, ps->get<char>(key));
176  }
177  else if (type == typeid(short)) {
178  db->setColumn<short>(colName, ps->get<short>(key));
179  }
180  else if (type == typeid(int)) {
181  db->setColumn<int>(colName, ps->get<int>(key));
182  }
183  else if (type == typeid(long)) {
184  db->setColumn<long>(colName, ps->get<long>(key));
185  }
186  else if (type == typeid(long long)) {
187  db->setColumn<long long>(colName, ps->get<long long>(key));
188  }
189  else if (type == typeid(float)) {
190  db->setColumn<float>(colName, ps->get<float>(key));
191  }
192  else if (type == typeid(double)) {
193  db->setColumn<double>(colName, ps->get<double>(key));
194  }
195  else if (type == typeid(std::string)) {
196  db->setColumn<std::string>(colName, ps->get<std::string>(key));
197  }
198  else if (type == typeid(dafBase::DateTime)) {
200  colName, ps->get<dafBase::DateTime>(key));
201  }
202  else {
204  std::string("Unknown type ") + type.name() +
205  " in PropertySetFormatter write");
206  }
207  }
208  db->insertRow();
209  LOGLS_TRACE(_log, "PropertySetFormatter write end");
210  return;
211  }
212 
213  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for PropertySet");
214 }
215 
218  LOGLS_TRACE(_log, "PropertySetFormatter read start");
220  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
221  LOGLS_TRACE(_log, "PropertySetFormatter read BoostStorage");
223  dynamic_cast<dafPersist::BoostStorage*>(storage.get());
224  boost->getIArchive() & *ps;
225  LOGLS_TRACE(_log, "PropertySetFormatter read end");
226  return ps;
227  }
228  else if (typeid(*storage) == typeid(dafPersist::XmlStorage)) {
229  LOGLS_TRACE(_log, "PropertySetFormatter read XmlStorage");
231  dynamic_cast<dafPersist::XmlStorage*>(storage.get());
232  xml->getIArchive() & make_nvp("propertySet", *ps);
233  LOGLS_TRACE(_log, "PropertySetFormatter read end");
234  return ps;
235  }
236  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for PropertySet");
237 }
238 
241  dafBase::PropertySet::Ptr additionalData) {
242  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for PropertySet");
243 }
244 
249 dafPersist::Formatter::Ptr dafPersist::PropertySetFormatter::createInstance(
250  pexPolicy::Policy::Ptr policy) {
252 }
std::shared_ptr< Policy > Ptr
virtual void update(dafBase::Persistable *persistable, dafPersist::FormatterStorage::Ptr storage, dafBase::PropertySet::Ptr additionalData)
std::shared_ptr< PropertySet > Ptr
Class for XML file storage.
Definition: XmlStorage.h:58
Interface for DbStorage class.
virtual ~PropertySetFormatter(void)
Minimal destructor.
void setColumn(std::string const &columnName, T const &value)
Set the value to insert in a given column.
Definition: DbStorage.cc:152
T get(std::string const &name) const
std::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
std::vector< std::string > paramNames(bool topLevelOnly=true) const
virtual boost::archive::text_iarchive & getIArchive(void)
Get a boost::serialization archive suitable for input.
virtual dafBase::Persistable * read(dafPersist::FormatterStorage::Ptr storage, dafBase::PropertySet::Ptr additionalData)
T end(T... args)
Construct a static instance of this helper class to register a Formatter subclass in the FormatterReg...
Definition: Formatter.h:138
virtual boost::archive::xml_iarchive & getIArchive(void)
Get a boost::serialization XML archive suitable for input.
Definition: XmlStorage.cc:114
Class for database storage.
Definition: DbStorage.h:63
STL class.
Interface for XmlStorage class.
T push_back(T... args)
#define LOGLS_TRACE(logger, message)
virtual void write(dafBase::Persistable const *persistable, dafPersist::FormatterStorage::Ptr storage, dafBase::PropertySet::Ptr additionalData)
std::vector< std::string > StringArray
Interface for PropertySetFormatter class.
bool exists(std::string const &name) const
Abstract base class for all formatters.
Definition: Formatter.h:79
Formatter for persistence of PropertySet instances.
virtual void insertRow(void)
Insert the row.
Definition: DbStorage.cc:166
#define LSST_EXCEPT(type,...)
virtual boost::archive::text_oarchive & getOArchive(void)
Get a boost::serialization archive suitable for output.
Auxiliary global template function for Formatter subclasses.
virtual boost::archive::xml_oarchive & getOArchive(void)
Get a boost::serialization XML archive suitable for output.
Definition: XmlStorage.cc:107
T begin(T... args)
T substr(T... args)
Class for boost::serialization storage.
Definition: BoostStorage.h:59
#define __attribute__(x)
#define LOG_GET(logger)
Interface for LogicalLocation class.
virtual void setTableForInsert(std::string const &tableName)
Set the table to insert rows into.
Definition: DbStorage.cc:143
std::type_info const & typeOf(std::string const &name) const
std::shared_ptr< FormatterStorage > Ptr
virtual void setColumnToNull(std::string const &columnName)
Set a given column to NULL.
Definition: DbStorage.cc:159
Interface for BoostStorage class.