41 # define __attribute__(x)
52 #include <mysql/mysql.h>
63 namespace persistence {
80 if (policy && policy->exists(
"TempPath")) {
81 _tempPath = policy->getString(
"TempPath");
83 if (policy && policy->exists(
"SaveTemp") && policy->getBool(
"SaveTemp")) {
95 setenv(
"TZ",
"UTC", 1);
123 MYSQL* db = mysql_init(0);
125 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unable to allocate MySQL connection");
128 unsigned int port = strtoul(dbLoc.
getPort().c_str(), 0, 10);
129 if (mysql_real_connect(db,
135 CLIENT_COMPRESS | CLIENT_LOCAL_FILES) == 0) {
136 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
137 "Unable to connect to MySQL database: " +
_location);
139 if (mysql_options(db, MYSQL_OPT_LOCAL_INFILE, 0) != 0) {
140 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
141 std::string(
"Unable to set LOCAL INFILE option - * ") +
145 std::string
query =
"LOAD DATA LOCAL INFILE";
152 for (std::map<std::string, int>::const_iterator it =
_colMap.begin();
156 for (std::vector<std::string>::const_iterator it =
_rowBuffer.begin();
163 if (mysql_query(db, query.c_str()) != 0) {
165 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
166 "Unable to load data into database table: " +
_tableName +
167 "- * " + mysql_error(db));
184 std::string
const& templateName,
185 bool mayAlreadyExist) {
236 std::string templ =
_tempPath +
"/" + tableName +
".XXXXXX";
238 strncpy(
_fileName, templ.c_str(), templ.size());
240 int fd = mkstemp(_fileName);
243 _osp.reset(
new std::ofstream(_fileName));
253 std::map<std::string, int>::iterator i =
_colMap.find(columnName);
255 _colMap.insert(std::pair<std::string, int>(columnName,
269 template <
typename T>
288 _convertStream << static_cast<int>(value);
295 double const& value) {
304 float const& value) {
317 struct tm t = value.
gmtime();
319 strftime(buf,
sizeof(buf),
"%F %T", &t);
336 for (std::vector<std::string>::const_iterator i =
_rowBuffer.begin();
356 template <
typename T>
359 DbStorage::outParam<T>(columnName, location, isExpr);
367 template <
typename T>
369 DbStorage::condParam<T>(paramName, value);
375 template <
typename T>
377 return DbStorage::getColumnByPos<T>(pos);
384 template void DbTsvStorage::setColumn<>(std::string
const& columnName,
char const& value);
385 template void DbTsvStorage::setColumn<>(std::string
const& columnName,
short const& value);
386 template void DbTsvStorage::setColumn<>(std::string
const& columnName,
int const& value);
387 template void DbTsvStorage::setColumn<>(std::string
const& columnName,
long const& value);
388 template void DbTsvStorage::setColumn<>(std::string
const& columnName,
long long const& value);
389 template void DbTsvStorage::setColumn<>(std::string
const& columnName,
float const& value);
390 template void DbTsvStorage::setColumn<>(std::string
const& columnName,
double const& value);
391 template void DbTsvStorage::setColumn<>(std::string
const& columnName, std::string
const& value);
392 template void DbTsvStorage::setColumn<>(std::string
const& columnName,
bool const& value);
393 template void DbTsvStorage::setColumn<>(std::string
const& columnName,
DateTime const& value);
395 template void DbTsvStorage::outParam<>(std::string
const& columnName,
char* location,
bool isExpr);
396 template void DbTsvStorage::outParam<>(std::string
const& columnName,
short* location,
bool isExpr);
397 template void DbTsvStorage::outParam<>(std::string
const& columnName,
int* location,
bool isExpr);
398 template void DbTsvStorage::outParam<>(std::string
const& columnName,
long* location,
bool isExpr);
399 template void DbTsvStorage::outParam<>(std::string
const& columnName,
long long* location,
bool isExpr);
400 template void DbTsvStorage::outParam<>(std::string
const& columnName,
float* location,
bool isExpr);
401 template void DbTsvStorage::outParam<>(std::string
const& columnName,
double* location,
bool isExpr);
402 template void DbTsvStorage::outParam<>(std::string
const& columnName, std::string* location,
bool isExpr);
403 template void DbTsvStorage::outParam<>(std::string
const& columnName,
bool* location,
bool isExpr);
404 template void DbTsvStorage::outParam<>(std::string
const& columnName,
DateTime* location,
bool isExpr);
406 template void DbTsvStorage::condParam<>(std::string
const& paramName,
char const& value);
407 template void DbTsvStorage::condParam<>(std::string
const& paramName,
short const& value);
408 template void DbTsvStorage::condParam<>(std::string
const& paramName,
int const& value);
409 template void DbTsvStorage::condParam<>(std::string
const& paramName,
long const& value);
410 template void DbTsvStorage::condParam<>(std::string
const& paramName,
long long const& value);
411 template void DbTsvStorage::condParam<>(std::string
const& paramName,
float const& value);
412 template void DbTsvStorage::condParam<>(std::string
const& paramName,
double const& value);
413 template void DbTsvStorage::condParam<>(std::string
const& paramName, std::string
const& value);
414 template void DbTsvStorage::condParam<>(std::string
const& paramName,
bool const& value);
415 template void DbTsvStorage::condParam<>(std::string
const& paramName,
DateTime const& value);
417 template char const& DbTsvStorage::getColumnByPos<>(
int pos);
418 template short const& DbTsvStorage::getColumnByPos<>(
int pos);
419 template int const& DbTsvStorage::getColumnByPos<>(
int pos);
420 template long const& DbTsvStorage::getColumnByPos<>(
int pos);
421 template long long const& DbTsvStorage::getColumnByPos<>(
int pos);
422 template float const& DbTsvStorage::getColumnByPos<>(
int pos);
423 template double const& DbTsvStorage::getColumnByPos<>(
int pos);
424 template std::string
const& DbTsvStorage::getColumnByPos<>(
int pos);
425 template bool const& DbTsvStorage::getColumnByPos<>(
int pos);
426 template DateTime const& DbTsvStorage::getColumnByPos<>(
int pos);
virtual void startTransaction(void)
Class for handling dates/times, including MJD, UTC, and TAI.
int _getColumnIndex(std::string const &columnName)
virtual void setPersistLocation(LogicalLocation const &location)
std::vector< std::string > _rowBuffer
Class for logical location of a persisted Persistable instance.
virtual std::string const & getPort(void) const
virtual void setPolicy(lsst::pex::policy::Policy::Ptr policy)
char * _fileName
Full pathname for temporary TSV file.
virtual void setColumnToNull(std::string const &columnName)
boost::shared_ptr< Policy > Ptr
std::string _location
Database location URL.
T const & getColumnByPos(int pos)
virtual void dropTable(std::string const &tableName)
Class for database storage.
void outParam(std::string const &columnName, T *location, bool isExpr=false)
std::map< std::string, int > _colMap
Map from column names to positions.
std::ostringstream _convertStream
Stream to convert to text.
virtual void startTransaction(void)
std::string _tempPath
Directory pathname for temporary TSV file.
Location of a persisted Persistable instance in a database.
virtual void truncateTable(std::string const &tableName)
bool _saveTemp
Do not delete temporary TSV file if true.
boost::scoped_ptr< std::ofstream > _osp
Output TSV stream.
std::string const & locString(void) const
Interface for DbTsvStorage class.
virtual std::string const & getPassword(void) const
virtual void setTableForInsert(std::string const &tableName)
virtual void setRetrieveLocation(LogicalLocation const &location)
virtual void endTransaction(void)
Interface for DateTime class.
virtual std::string const & getHostname(void) const
#define LSST_EXCEPT(type,...)
virtual void endTransaction(void)
struct tm gmtime(void) const
virtual void createTableFromTemplate(std::string const &tableName, std::string const &templateName, bool mayAlreadyExist=false)
virtual void insertRow(void)
Interface for LogicalLocation class.
virtual void setPersistLocation(LogicalLocation const &location)
void setColumn(std::string const &columnName, T const &value)
virtual void createTableFromTemplate(std::string const &tableName, std::string const &templateName, bool mayAlreadyExist=false)
virtual std::string const & getDbName(void) const
virtual void setRetrieveLocation(LogicalLocation const &location)
void condParam(std::string const ¶mName, T const &value)
virtual void dropTable(std::string const &tableName)
virtual std::string const & getUsername(void) const
Include files required for standard LSST Exception handling.
virtual void truncateTable(std::string const &tableName)
Interface for DbStorageLocation class.