41 # define __attribute__(x)
52 #include <mysql/mysql.h>
54 #include "lsst/pex/exceptions.h"
55 #include "lsst/daf/base/DateTime.h"
59 using lsst::daf::base::DateTime;
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";
150 query +=
" INTO TABLE `" + _tableName;
152 for (std::map<std::string, int>::const_iterator it = _colMap.begin();
153 it != _colMap.end(); ++it) {
154 _rowBuffer[it->second] = it->first;
156 for (std::vector<std::string>::const_iterator it = _rowBuffer.begin();
157 it != _rowBuffer.end(); ++it) {
158 if (it != _rowBuffer.begin()) query +=
" ,";
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) {
235 _tableName = tableName;
236 std::string templ = _tempPath +
"/" + tableName +
".XXXXXX";
237 _fileName =
new char[templ.size() + 1];
238 strncpy(_fileName, templ.c_str(), templ.size());
239 _fileName[templ.size()] =
'\0';
240 int fd = mkstemp(_fileName);
243 _osp.reset(
new std::ofstream(_fileName));
252 int DbTsvStorage::_getColumnIndex(std::string
const& columnName) {
253 std::map<std::string, int>::iterator i = _colMap.find(columnName);
254 if (i == _colMap.end()) {
255 _colMap.insert(std::pair<std::string, int>(columnName,
257 _rowBuffer.push_back(std::string());
258 return _rowBuffer.size() - 1;
269 template <
typename T>
271 int colIndex = _getColumnIndex(columnName);
275 _convertStream.str(std::string());
276 _convertStream << value;
277 _rowBuffer[colIndex] = _convertStream.str();
286 int colIndex = _getColumnIndex(columnName);
287 _convertStream.str(std::string());
288 _convertStream << static_cast<int>(value);
289 _rowBuffer[colIndex] = _convertStream.str();
295 double const& value) {
296 int colIndex = _getColumnIndex(columnName);
297 _convertStream.str(std::string());
298 _convertStream << std::setprecision(17) << value;
299 _rowBuffer[colIndex] = _convertStream.str();
304 float const& value) {
305 int colIndex = _getColumnIndex(columnName);
306 _convertStream.str(std::string());
307 _convertStream << std::setprecision(9) << value;
308 _rowBuffer[colIndex] = _convertStream.str();
314 DateTime
const& value) {
315 int colIndex = _getColumnIndex(columnName);
316 _convertStream.str(std::string());
317 struct tm t = value.gmtime(DateTime::UTC);
319 strftime(buf,
sizeof(buf),
"%F %T", &t);
320 _rowBuffer[colIndex] = std::string(buf);
327 int colIndex = _getColumnIndex(columnName);
328 _rowBuffer[colIndex] =
"\\N";
336 for (std::vector<std::string>::const_iterator i = _rowBuffer.begin();
337 i != _rowBuffer.end(); ++i) {
338 if (i != _rowBuffer.begin()) *_osp <<
'\t';
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);
DbTsvStorage(void)
Constructor.
virtual void startTransaction(void)
Start a transaction.
virtual void setPersistLocation(LogicalLocation const &location)
Set the database location to persist to.
Class for logical location of a persisted Persistable instance.
virtual std::string const & getPort(void) const
Accessor for database port number.
virtual void setPolicy(lsst::pex::policy::Policy::Ptr policy)
Allow a policy to be used to configure the DbTsvStorage.
virtual void setColumnToNull(std::string const &columnName)
Set a given column to NULL.
T const & getColumnByPos(int pos)
Get the value of a column of the query result row by position.
virtual void dropTable(std::string const &tableName)
Drop a table.
Class for database storage.
void outParam(std::string const &columnName, T *location, bool isExpr=false)
Request a column in the query output and bind a destination location.
virtual void startTransaction(void)
Start a transaction.
~DbTsvStorage(void)
Minimal destructor.
Location of a persisted Persistable instance in a database.
virtual void truncateTable(std::string const &tableName)
Truncate a table.
std::string const & locString(void) const
Accessor.
Interface for DbTsvStorage class.
virtual std::string const & getPassword(void) const
Accessor for password.
virtual void setTableForInsert(std::string const &tableName)
Set the table to insert rows into.
virtual void setRetrieveLocation(LogicalLocation const &location)
Set the database location to retrieve from.
virtual void endTransaction(void)
End a transaction.
virtual std::string const & getHostname(void) const
Accessor for database hostname.
virtual void endTransaction(void)
End a transaction.
virtual void query(void)
Execute the query.
virtual void createTableFromTemplate(std::string const &tableName, std::string const &templateName, bool mayAlreadyExist=false)
Create a new table from an existing template table.
virtual void insertRow(void)
Insert the row.
Interface for LogicalLocation class.
virtual void setPersistLocation(LogicalLocation const &location)
Set the database location to persist to.
void setColumn(std::string const &columnName, T const &value)
Set the value to insert in a given column.
virtual void createTableFromTemplate(std::string const &tableName, std::string const &templateName, bool mayAlreadyExist=false)
Create a new table from an existing template table.
virtual std::string const & getDbName(void) const
Accessor for database name.
virtual void setRetrieveLocation(LogicalLocation const &location)
Set the database location to retrieve from.
void condParam(std::string const ¶mName, T const &value)
Bind a value to a WHERE condition parameter.
virtual void dropTable(std::string const &tableName)
Drop a table.
virtual std::string const & getUsername(void) const
Accessor for username.
virtual void truncateTable(std::string const &tableName)
Truncate a table.
Interface for DbStorageLocation class.