39 # define __attribute__(x)
50 #include <boost/serialization/nvp.hpp>
51 #include <boost/serialization/shared_ptr.hpp>
52 #include <boost/serialization/vector.hpp>
54 #include <lsst/daf/base/PropertySet.h>
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>
66 auto _log = LOG_GET(
"daf.persistence.PropertySetFormatter");
69 namespace dafBase = lsst::daf::base;
70 namespace dafPersist = lsst::daf::persistence;
71 namespace pexPolicy = lsst::pex::policy;
73 using boost::serialization::make_nvp;
79 dafPersist::PropertySetFormatter::registration(
"PropertySet",
80 typeid(dafBase::PropertySet),
86 dafPersist::PropertySetFormatter::PropertySetFormatter(
87 pexPolicy::Policy::Ptr policy) :
88 dafPersist::Formatter(typeid(*this)), _policy(policy) {
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);
104 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Persisting non-PropertySet");
107 LOGLS_TRACE(_log,
"PropertySetFormatter write BoostStorage");
111 LOGLS_TRACE(_log,
"PropertySetFormatter write end");
115 LOGLS_TRACE(_log,
"PropertySetFormatter write XmlStorage");
119 LOGLS_TRACE(_log,
"PropertySetFormatter write end");
123 LOGLS_TRACE(_log,
"PropertySetFormatter write DbStorage");
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");
138 std::vector<std::string> list;
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) {
148 list = ps->paramNames(
false);
151 for (std::vector<std::string>::const_iterator it = list.begin();
152 it != list.end(); ++it) {
153 std::string::size_type split = it->find(
'=');
156 if (split == std::string::npos) {
160 colName = it->substr(0, split);
161 key = it->substr(split + 1);
164 if (!ps->exists(key)) {
169 std::type_info
const& type(ps->typeOf(key));
171 if (type ==
typeid(
bool)) {
172 db->
setColumn<
bool>(colName, ps->get<
bool>(key));
174 else if (type ==
typeid(
char)) {
175 db->
setColumn<
char>(colName, ps->get<
char>(key));
177 else if (type ==
typeid(
short)) {
178 db->
setColumn<
short>(colName, ps->get<
short>(key));
180 else if (type ==
typeid(
int)) {
181 db->
setColumn<
int>(colName, ps->get<
int>(key));
183 else if (type ==
typeid(
long)) {
184 db->
setColumn<
long>(colName, ps->get<
long>(key));
186 else if (type ==
typeid(
long long)) {
187 db->
setColumn<
long long>(colName, ps->get<
long long>(key));
189 else if (type ==
typeid(
float)) {
190 db->
setColumn<
float>(colName, ps->get<
float>(key));
192 else if (type ==
typeid(
double)) {
193 db->
setColumn<
double>(colName, ps->get<
double>(key));
195 else if (type ==
typeid(std::string)) {
196 db->
setColumn<std::string>(colName, ps->get<std::string>(key));
198 else if (type ==
typeid(dafBase::DateTime)) {
200 colName, ps->get<dafBase::DateTime>(key));
203 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
204 std::string(
"Unknown type ") + type.name() +
205 " in PropertySetFormatter write");
209 LOGLS_TRACE(_log,
"PropertySetFormatter write end");
213 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for PropertySet");
218 LOGLS_TRACE(_log,
"PropertySetFormatter read start");
219 dafBase::PropertySet* ps =
new dafBase::PropertySet;
221 LOGLS_TRACE(_log,
"PropertySetFormatter read BoostStorage");
225 LOGLS_TRACE(_log,
"PropertySetFormatter read end");
229 LOGLS_TRACE(_log,
"PropertySetFormatter read XmlStorage");
233 LOGLS_TRACE(_log,
"PropertySetFormatter read end");
236 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for PropertySet");
241 dafBase::PropertySet::Ptr additionalData) {
242 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unexpected call to update for PropertySet");
250 pexPolicy::Policy::Ptr policy) {
Class for XML file storage.
Interface for DbStorage class.
void setColumn(std::string const &columnName, T const &value)
Set the value to insert in a given column.
virtual boost::archive::text_iarchive & getIArchive(void)
Get a boost::serialization archive suitable for input.
virtual boost::archive::xml_iarchive & getIArchive(void)
Get a boost::serialization XML archive suitable for input.
Class for database storage.
Interface for XmlStorage class.
virtual void insertRow(void)
Insert the row.
virtual boost::archive::text_oarchive & getOArchive(void)
Get a boost::serialization archive suitable for output.
virtual boost::archive::xml_oarchive & getOArchive(void)
Get a boost::serialization XML archive suitable for output.
Class for boost::serialization storage.
Interface for LogicalLocation class.
virtual void setTableForInsert(std::string const &tableName)
Set the table to insert rows into.
virtual void setColumnToNull(std::string const &columnName)
Set a given column to NULL.
Interface for BoostStorage class.