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);
128 unsigned int port = strtoul(dbLoc.
getPort().
c_str(), 0, 10);
129 if (mysql_real_connect(db,
135 CLIENT_COMPRESS | CLIENT_LOCAL_FILES) == 0) {
137 "Unable to connect to MySQL database: " +
_location);
139 if (mysql_options(db, MYSQL_OPT_LOCAL_INFILE, 0) != 0) {
141 std::string(
"Unable to set LOCAL INFILE option - * ") +
163 if (mysql_query(db, query.
c_str()) != 0) {
166 "Unable to load data into database table: " +
_tableName +
167 "- * " + mysql_error(db));
185 bool mayAlreadyExist) {
240 int fd = mkstemp(_fileName);
269 template <
typename T>
288 _convertStream << static_cast<int>(value);
295 double const& value) {
304 float const& value) {
317 struct tm t = value.
gmtime(DateTime::UTC);
319 strftime(buf,
sizeof(buf),
"%F %T", &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);
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);
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.
Class for handling dates/times, including MJD, UTC, and TAI.
int _getColumnIndex(std::string const &columnName)
Get the index of a given column.
virtual std::string const & getDbName(void) const
Accessor for database name.
virtual void setPersistLocation(LogicalLocation const &location)
Set the database location to persist to.
std::vector< std::string > _rowBuffer
struct tm gmtime(Timescale scale) const
Get date as a tm struct, with truncated fractional seconds.
Class for logical location of a persisted Persistable instance.
virtual void setPolicy(lsst::pex::policy::Policy::Ptr policy)
Allow a policy to be used to configure the DbTsvStorage.
std::string const & locString(void) const
Accessor.
char * _fileName
Full pathname for temporary TSV file.
virtual void setColumnToNull(std::string const &columnName)
Set a given column to NULL.
virtual std::string const & getHostname(void) const
Accessor for database hostname.
std::string _location
Database location URL.
virtual std::string const & getPassword(void) const
Accessor for password.
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.
std::map< std::string, int > _colMap
Map from column names to positions.
Include files required for standard LSST Exception handling.
std::ostringstream _convertStream
Stream to convert to text.
virtual void startTransaction(void)
Start a transaction.
std::string _tempPath
Directory pathname for temporary TSV file.
~DbTsvStorage(void)
Minimal destructor.
Location of a persisted Persistable instance in a database.
virtual void truncateTable(std::string const &tableName)
Truncate a table.
bool _saveTemp
Do not delete temporary TSV file if true.
A base class for image defects.
Interface for DbTsvStorage class.
virtual std::string const & getUsername(void) const
Accessor for username.
virtual std::string const & getPort(void) const
Accessor for database port number.
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.
Interface for DateTime class.
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
virtual void endTransaction(void)
End a transaction.
virtual void query(void)
Execute the query.
T const & getColumnByPos(int pos)
Get the value of a column of the query result row by position.
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.
std::unique_ptr< std::ofstream > _osp
Output TSV stream.
Interface for LogicalLocation class.
T setprecision(T... args)
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 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 void truncateTable(std::string const &tableName)
Truncate a table.
Interface for DbStorageLocation class.