LSSTApplications  11.0-24-g0a022a1,14.0+77,15.0,15.0+1
LSSTDataManagementBasePackage
Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | Friends | List of all members
lsst::daf::persistence::DbStorageImpl Class Reference

Class for implementation of database storage. More...

#include <DbStorageImpl.h>

Inheritance diagram for lsst::daf::persistence::DbStorageImpl:
lsst::daf::base::Citizen

Public Types

enum  { magicSentinel = 0xdeadbeef }
 
typedef unsigned long memId
 Type of the block's ID. More...
 
typedef memId(* memNewCallback) (const memId cid)
 A function used to register a callback. More...
 
typedef memId(* memCallback) (const Citizen *ptr)
 

Public Member Functions

virtual ~DbStorageImpl (void)
 Destructor. More...
 
template<>
void setColumn (std::string const &columnName, std::string const &value)
 
template<>
void setColumn (std::string const &columnName, dafBase::DateTime const &value)
 
template<>
void outParam (std::string const &columnName, std::string *location, bool isExpr)
 
template<>
void outParam (std::string const &columnName, dafBase::DateTime *location, bool isExpr)
 
template<>
std::string const & getColumnByPos (int pos)
 
template<>
dafBase::DateTime const & getColumnByPos (int pos)
 
std::string repr () const
 Return a string representation of a Citizen. More...
 
void markPersistent (void)
 Mark a Citizen as persistent and not destroyed until process end. More...
 
memId getId () const
 Return the Citizen's ID. More...
 

Static Public Member Functions

static bool hasBeenCorrupted ()
 Check all allocated blocks for corruption. More...
 
static memId getNextMemId ()
 Return the memId of the next object to be allocated. More...
 
static int init ()
 Called once when the memory system is being initialised. More...
 
Census

Provide a list of current Citizens

static int census (int, memId startingMemId=0)
 How many active Citizens are there? More...
 
static void census (std::ostream &stream, memId startingMemId=0)
 Print a list of all active Citizens to stream, sorted by ID. More...
 
static const std::vector< const Citizen * > * census ()
 Return a (newly allocated) std::vector of active Citizens sorted by ID. More...
 
callbackIDs

Set callback Ids.

The old Id is returned

static memId setNewCallbackId (memId id)
 Call the NewCallback when block is allocated. More...
 
static memId setDeleteCallbackId (memId id)
 Call the current DeleteCallback when block is deleted. More...
 
callbacks

Set the New/Delete callback functions; in each case the previously installed callback is returned.

These callback functions return a value which is Added to the previously registered id.

The default callback functions are called default{New,Delete}Callback; you may want to set a break point in these callbacks from your favourite debugger

static memNewCallback setNewCallback (memNewCallback func)
 Set the NewCallback function. More...
 
static memCallback setDeleteCallback (memCallback func)
 Set the DeleteCallback function. More...
 
static memCallback setCorruptionCallback (memCallback func)
 Set the CorruptionCallback function. More...
 

Private Types

typedef std::unordered_map< std::string, BoundVarBoundVarMap
 

Private Member Functions

 DbStorageImpl (void)
 Default constructor. More...
 
virtual void setPolicy (pexPolicy::Policy::Ptr policy)
 Allow a Policy to be used to configure the DbStorage. More...
 
virtual void setPersistLocation (LogicalLocation const &location)
 Set the database location to persist to. More...
 
virtual void setRetrieveLocation (LogicalLocation const &location)
 Set the database location to retrieve from. More...
 
virtual void startTransaction (void)
 Start a transaction. More...
 
virtual void endTransaction (void)
 End a transaction. More...
 
virtual void startSession (std::string const &location)
 Start a database session. More...
 
virtual void createTableFromTemplate (std::string const &tableName, std::string const &templateName, bool mayAlreadyExist)
 Create a new table from an existing template table. More...
 
virtual void dropTable (std::string const &tableName)
 Drop a table. More...
 
virtual void truncateTable (std::string const &tableName)
 Truncate a table. More...
 
virtual void executeSql (std::string const &sqlStatement)
 Execute an arbitrary SQL statement. More...
 
virtual void setTableForInsert (std::string const &tableName)
 Set the table to insert rows into. More...
 
template<typename T >
void setColumn (std::string const &columnName, T const &value)
 Set the value to insert in a given column. More...
 
virtual void setColumnToNull (std::string const &columnName)
 Set a given column to NULL. More...
 
virtual void insertRow (void)
 Insert the row. More...
 
virtual void setTableForQuery (std::string const &tableName, bool isExpr)
 Set the table to query (single-table queries only). More...
 
virtual void setTableListForQuery (std::vector< std::string > const &tableNameList)
 Set a list of tables to query (multiple-table queries). More...
 
virtual void outColumn (std::string const &columnName, bool isExpr)
 Request a column in the query output. More...
 
template<typename T >
void outParam (std::string const &columnName, T *location, bool isExpr)
 Request a column in the query output and bind a destination location. More...
 
template<typename T >
void condParam (std::string const &paramName, T const &value)
 Bind a value to a WHERE condition parameter. More...
 
virtual void orderBy (std::string const &expression)
 Request that the query output be sorted by an expression. More...
 
virtual void groupBy (std::string const &expression)
 Request that the query output be grouped by an expression. More...
 
virtual void setQueryWhere (std::string const &whereClause)
 Set the condition for the WHERE clause of the query. More...
 
virtual void query (void)
 Execute the query. More...
 
virtual bool next (void)
 Move to the next (first) row of the query result. More...
 
template<typename T >
T const & getColumnByPos (int pos)
 Get the value of a column of the query result row by position. More...
 
bool columnIsNull (int pos)
 Determine if the value of a column is NULL. More...
 
virtual void finishQuery (void)
 Indicate that query processing is finished. More...
 
void executeQuery (std::string const &query)
 Execute a query string. More...
 
std::string quote (std::string const &name)
 Quote a name in ANSI-standard fashion. More...
 
void stError (std::string const &text)
 
void error (std::string const &text, bool mysqlCaused=true)
 
void * allocateMemory (size_t size)
 

Private Attributes

bool _readonly
 Remember if we are supposed to be read-only. More...
 
std::string _location
 Database location string saved for use by raw MySQL interface. More...
 
MYSQL * _db
 MySQL database connection pointer. More...
 
std::string _insertTable
 Name of table into which to insert. More...
 
std::vector< std::string_queryTables
 Names of tables to select from. More...
 
BoundVarMap _inputVars
 Input variable bindings. More...
 
BoundVarMap _outputVars
 Output variable bindings. More...
 
std::vector< boost::shared_array< char > > _bindingMemory
 Memory for bound variables. More...
 
std::vector< std::string_outColumns
 
std::string _whereClause
 
std::string _groupBy
 
std::string _orderBy
 
MYSQL_STMT * _statement
 Prepared query statement. More...
 
MYSQL_FIELD * _resultFields
 Query result field metadata. More...
 
int _numResultFields
 Number of result fields. More...
 
boost::shared_array< unsigned long > _fieldLengths
 Space for lengths of result fields. More...
 
boost::shared_array< my_bool > _fieldNulls
 Space for null flags of result fields. More...
 

Friends

class DbStorage
 

Detailed Description

Class for implementation of database storage.

Use via DbStorage class only.

Definition at line 79 of file DbStorageImpl.h.

Member Typedef Documentation

◆ BoundVarMap

Definition at line 150 of file DbStorageImpl.h.

◆ memCallback

typedef memId(* lsst::daf::base::Citizen::memCallback) (const Citizen *ptr)
inherited

Definition at line 59 of file Citizen.h.

◆ memId

typedef unsigned long lsst::daf::base::Citizen::memId
inherited

Type of the block's ID.

Definition at line 56 of file Citizen.h.

◆ memNewCallback

typedef memId(* lsst::daf::base::Citizen::memNewCallback) (const memId cid)
inherited

A function used to register a callback.

Definition at line 58 of file Citizen.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
inherited
Enumerator
magicSentinel 

Definition at line 86 of file Citizen.h.

Constructor & Destructor Documentation

◆ ~DbStorageImpl()

lsst::daf::persistence::DbStorageImpl::~DbStorageImpl ( void  )
virtual

Destructor.

End session if present.

Definition at line 175 of file DbStorageImpl.cc.

175  {
176  if (_db) {
177  mysql_close(_db);
178  _db = 0;
179  }
180 }
MYSQL * _db
MySQL database connection pointer.

◆ DbStorageImpl()

lsst::daf::persistence::DbStorageImpl::DbStorageImpl ( void  )
private

Default constructor.

Definition at line 168 of file DbStorageImpl.cc.

168  :
169  lsst::daf::base::Citizen(typeid(*this)), _db(0) {
170 }
MYSQL * _db
MySQL database connection pointer.
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:53

Member Function Documentation

◆ allocateMemory()

void * lsst::daf::persistence::DbStorageImpl::allocateMemory ( size_t  size)
private

Definition at line 289 of file DbStorageImpl.cc.

289  {
290  boost::shared_array<char> mem(new char[size]);
292  return mem.get();
293 }
std::vector< boost::shared_array< char > > _bindingMemory
Memory for bound variables.
T push_back(T... args)

◆ census() [1/3]

int lsst::daf::base::Citizen::census ( int  ,
memId  startingMemId = 0 
)
staticinherited

How many active Citizens are there?

Parameters
startingMemIdDon't print Citizens with lower IDs

Definition at line 267 of file Citizen.cc.

270  {
271  if (startingMemId == 0) { // easy
272  ReadGuard guard(citizenLock);
273  return _activeCitizens.size();
274  }
275 
276  int n = 0;
277  ReadGuard guard(citizenLock);
278  for (table::iterator cur = _activeCitizens.begin();
279  cur != _activeCitizens.end(); cur++) {
280  if (cur->first->_CitizenId >= startingMemId) {
281  n++;
282  }
283  }
284 
285  return n;
286 }
T end(T... args)
static table _activeCitizens
Definition: Citizen.h:102
T size(T... args)
T begin(T... args)

◆ census() [2/3]

void lsst::daf::base::Citizen::census ( std::ostream stream,
memId  startingMemId = 0 
)
staticinherited

Print a list of all active Citizens to stream, sorted by ID.

Parameters
streamstream to print to
startingMemIdDon't print Citizens with lower IDs

Definition at line 290 of file Citizen.cc.

293  {
294  ReadGuard guard(citizenLock);
295 
297 
298  for (std::vector<Citizen const *>::const_iterator citizen = leaks->begin(), end = leaks->end();
299  citizen != end; ++citizen) {
300  if ((*citizen)->getId() >= startingMemId) {
301  stream << (*citizen)->repr() << "\n";
302  }
303  }
304 }
int end
STL class.
STL class.
T begin(T... args)
static const std::vector< const Citizen * > * census()
Return a (newly allocated) std::vector of active Citizens sorted by ID.
Definition: Citizen.cc:322

◆ census() [3/3]

std::vector< dafBase::Citizen const * > const * lsst::daf::base::Citizen::census ( )
staticinherited

Return a (newly allocated) std::vector of active Citizens sorted by ID.

You are responsible for deleting it; or you can say std::unique_ptr<std::vector<Citizen const*> const> leaks(Citizen::census()); and not bother (that becomes std::unique_ptr in C++11)

Definition at line 322 of file Citizen.cc.

322  {
325  ReadGuard guard(citizenLock);
326  vec->reserve(_activeCitizens.size());
327 
328  for (table::iterator cur = _activeCitizens.begin();
329  cur != _activeCitizens.end(); cur++) {
330  vec->push_back(dynamic_cast<Citizen const*>(cur->first));
331  }
332 
333  std::sort(vec->begin(), vec->end(), cmpId);
334 
335  return vec;
336 }
T end(T... args)
static table _activeCitizens
Definition: Citizen.h:102
T push_back(T... args)
T size(T... args)
STL class.
T begin(T... args)
T sort(T... args)
T reserve(T... args)

◆ columnIsNull()

bool lsst::daf::persistence::DbStorageImpl::columnIsNull ( int  pos)
private

Determine if the value of a column is NULL.

Parameters
[in]posPosition of the column (starts at 0)
Returns
true if value is NULL

Definition at line 985 of file DbStorageImpl.cc.

985  {
986  if (pos > _numResultFields) {
988  os << "Nonexistent column: " << pos;
989  error(os.str(), false);
990  }
991  return _fieldNulls[pos];
992 }
int _numResultFields
Number of result fields.
void error(std::string const &text, bool mysqlCaused=true)
T str(T... args)
boost::shared_array< my_bool > _fieldNulls
Space for null flags of result fields.

◆ condParam()

template<typename T >
void lsst::daf::persistence::DbStorageImpl::condParam ( std::string const &  paramName,
T const &  value 
)
private

Bind a value to a WHERE condition parameter.

Parameters
[in]paramNameName of the parameter (prefixed by ":" in the WHERE clause)
[in]valueValue to be bound to the parameter.

Definition at line 633 of file DbStorageImpl.cc.

633  {
634  setColumn<T>(paramName, value);
635 }

◆ createTableFromTemplate()

void lsst::daf::persistence::DbStorageImpl::createTableFromTemplate ( std::string const &  tableName,
std::string const &  templateName,
bool  mayAlreadyExist 
)
privatevirtual

Create a new table from an existing template table.

Parameters
[in]tableNameName of the new table
[in]templateNameName of the existing template table
[in]mayAlreadyExistFalse (default) if the table must not be present

Definition at line 304 of file DbStorageImpl.cc.

306  {
307  std::string query = "CREATE TABLE ";
308  if (mayAlreadyExist) query += "IF NOT EXISTS ";
309  query += quote(tableName) + " LIKE " + quote(templateName);
310  executeQuery(query);
311 }
STL class.
virtual void query(void)
Execute the query.
std::string quote(std::string const &name)
Quote a name in ANSI-standard fashion.
void executeQuery(std::string const &query)
Execute a query string.

◆ dropTable()

void lsst::daf::persistence::DbStorageImpl::dropTable ( std::string const &  tableName)
privatevirtual

Drop a table.

Parameters
[in]tableNameName of the table to drop

Definition at line 316 of file DbStorageImpl.cc.

316  {
317  executeQuery("DROP TABLE " + quote(tableName));
318 }
std::string quote(std::string const &name)
Quote a name in ANSI-standard fashion.
void executeQuery(std::string const &query)
Execute a query string.

◆ endTransaction()

void lsst::daf::persistence::DbStorageImpl::endTransaction ( void  )
privatevirtual

End a transaction.

Definition at line 244 of file DbStorageImpl.cc.

244  {
245  if (_db == 0) error("Database session not initialized "
246  "in DbStorage::endTransaction()", false);
247  if (mysql_commit(_db)) error("Unable to commit transaction");
248  if (mysql_autocommit(_db, true)) error("Unable to turn on autocommit");
249 }
MYSQL * _db
MySQL database connection pointer.
void error(std::string const &text, bool mysqlCaused=true)

◆ error()

void lsst::daf::persistence::DbStorageImpl::error ( std::string const &  text,
bool  mysqlCaused = true 
)
private

Definition at line 280 of file DbStorageImpl.cc.

280  {
281  if (mysqlCause) {
282  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, text + " - * " + mysql_error(_db));
283  }
284  else {
286  }
287 }
MYSQL * _db
MySQL database connection pointer.
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46

◆ executeQuery()

void lsst::daf::persistence::DbStorageImpl::executeQuery ( std::string const &  query)
private

Execute a query string.

Definition at line 257 of file DbStorageImpl.cc.

257  {
258  if (_db == 0) {
259  error("No DB connection for query: " + query, false);
260  }
261  LOGLS_DEBUG(_log, "Query: " << query);
262  if (mysql_query(_db, query.c_str()) != 0) {
263  error("Unable to execute query: " + query);
264  }
265 }
MYSQL * _db
MySQL database connection pointer.
#define LOGLS_DEBUG(logger, message)
Log a debug-level message using an iostream-based interface.
Definition: Log.h:617
void error(std::string const &text, bool mysqlCaused=true)
virtual void query(void)
Execute the query.

◆ executeSql()

void lsst::daf::persistence::DbStorageImpl::executeSql ( std::string const &  sqlStatement)
privatevirtual

Execute an arbitrary SQL statement.

Use primarily to perform server-side computations or complex DDL.

Parameters
[in]sqlStatementSQL statement to be executed. Must not end in ";".

Definition at line 331 of file DbStorageImpl.cc.

331  {
332  executeQuery(sqlStatement);
333 }
void executeQuery(std::string const &query)
Execute a query string.

◆ finishQuery()

void lsst::daf::persistence::DbStorageImpl::finishQuery ( void  )
privatevirtual

Indicate that query processing is finished.

Definition at line 996 of file DbStorageImpl.cc.

996  {
997  mysql_stmt_close(_statement);
998  _statement = 0;
999 }
MYSQL_STMT * _statement
Prepared query statement.

◆ getColumnByPos() [1/3]

template<typename T >
T const & lsst::daf::persistence::DbStorageImpl::getColumnByPos ( int  pos)
private

Get the value of a column of the query result row by position.

Parameters
[in]posPosition of the column (starts at 0)
Returns
Reference to the value of the column

Definition at line 896 of file DbStorageImpl.cc.

896  {
897  if (pos > _numResultFields) {
899  os << "Nonexistent column: " << pos;
900  error(os.str(), false);
901  }
902  MYSQL_BIND bind;
903  memset(&bind, 0, sizeof(MYSQL_BIND));
904  static T t;
905  bind.buffer_type = BoundVarTraits<T>::mysqlType;
906  bind.is_unsigned = BoundVarTraits<T>::isUnsigned;
907  bind.buffer = &t;
908  bind.buffer_length = sizeof(T);
909  bind.length = &(_fieldLengths[pos]);
910  bind.is_null = &(_fieldNulls[pos]);
911  if (mysql_stmt_fetch_column(_statement, &bind, pos, 0)) {
913  os << "Error fetching column: " << pos;
914  error(os.str(), false);
915  }
916  return t;
917 }
boost::shared_array< unsigned long > _fieldLengths
Space for lengths of result fields.
T memset(T... args)
int _numResultFields
Number of result fields.
void error(std::string const &text, bool mysqlCaused=true)
T str(T... args)
MYSQL_STMT * _statement
Prepared query statement.
T bind(T... args)
boost::shared_array< my_bool > _fieldNulls
Space for null flags of result fields.

◆ getColumnByPos() [2/3]

template<>
std::string const& lsst::daf::persistence::DbStorageImpl::getColumnByPos ( int  pos)

Definition at line 920 of file DbStorageImpl.cc.

920  {
921  if (pos > _numResultFields) {
923  os << "Nonexistent column: " << pos;
924  error(os.str(), false);
925  }
926  MYSQL_BIND bind;
927  memset(&bind, 0, sizeof(MYSQL_BIND));
928  if (_resultFields[pos].type == MYSQL_TYPE_BIT) {
929  error("Invalid type for string retrieval", false);
930  }
931  std::unique_ptr<char[]> t(new char[_fieldLengths[pos]]);
932  bind.buffer_type = BoundVarTraits<std::string>::mysqlType;
933  bind.is_unsigned = BoundVarTraits<std::string>::isUnsigned;
934  bind.buffer = t.get();
935  bind.buffer_length = _fieldLengths[pos];
936  bind.length = &(_fieldLengths[pos]);
937  bind.is_null = &(_fieldNulls[pos]);
938  if (mysql_stmt_fetch_column(_statement, &bind, pos, 0)) {
940  os << "Error fetching string column: " << pos;
941  stError(os.str());
942  }
943  static std::string s;
944  s = std::string(t.get(), _fieldLengths[pos]);
945  return s;
946 }
solver_t * s
boost::shared_array< unsigned long > _fieldLengths
Space for lengths of result fields.
T memset(T... args)
int _numResultFields
Number of result fields.
STL class.
void error(std::string const &text, bool mysqlCaused=true)
T str(T... args)
MYSQL_STMT * _statement
Prepared query statement.
T bind(T... args)
boost::shared_array< my_bool > _fieldNulls
Space for null flags of result fields.
MYSQL_FIELD * _resultFields
Query result field metadata.
STL class.
void stError(std::string const &text)

◆ getColumnByPos() [3/3]

template<>
dafBase::DateTime const& lsst::daf::persistence::DbStorageImpl::getColumnByPos ( int  pos)

Definition at line 949 of file DbStorageImpl.cc.

949  {
950  if (pos > _numResultFields) {
952  os << "Nonexistent column: " << pos;
953  error(os.str(), false);
954  }
955  MYSQL_BIND bind;
956  memset(&bind, 0, sizeof(MYSQL_BIND));
957  if (_resultFields[pos].type != MYSQL_TYPE_TIME &&
958  _resultFields[pos].type != MYSQL_TYPE_DATE &&
959  _resultFields[pos].type != MYSQL_TYPE_DATETIME &&
960  _resultFields[pos].type != MYSQL_TYPE_TIMESTAMP) {
961  error("Invalid type for DateTime retrieval", false);
962  }
963  static MYSQL_TIME t;
966  bind.buffer = &t;
967  bind.buffer_length = sizeof(MYSQL_TIME);
968  bind.length = &(_fieldLengths[pos]);
969  bind.is_null = &(_fieldNulls[pos]);
970  if (mysql_stmt_fetch_column(_statement, &bind, pos, 0)) {
972  os << "Error fetching DateTime column: " << pos;
973  stError(os.str());
974  }
975  static dafBase::DateTime v;
976  v = dafBase::DateTime(t.year, t.month, t.day, t.hour, t.minute, t.second,
978  return v;
979 }
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:62
boost::shared_array< unsigned long > _fieldLengths
Space for lengths of result fields.
T memset(T... args)
int _numResultFields
Number of result fields.
void error(std::string const &text, bool mysqlCaused=true)
T str(T... args)
MYSQL_STMT * _statement
Prepared query statement.
T bind(T... args)
boost::shared_array< my_bool > _fieldNulls
Space for null flags of result fields.
MYSQL_FIELD * _resultFields
Query result field metadata.
void stError(std::string const &text)

◆ getId()

dafBase::Citizen::memId lsst::daf::base::Citizen::getId ( ) const
inherited

Return the Citizen's ID.

Definition at line 224 of file Citizen.cc.

224  {
225  return _CitizenId;
226 }

◆ getNextMemId()

dafBase::Citizen::memId lsst::daf::base::Citizen::getNextMemId ( )
staticinherited

Return the memId of the next object to be allocated.

Definition at line 229 of file Citizen.cc.

229  {
230  return _nextMemId();
231 }
static memId _nextMemId(void)
Return the memId of the next object to be allocated.
Definition: Citizen.cc:234

◆ groupBy()

void lsst::daf::persistence::DbStorageImpl::groupBy ( std::string const &  expression)
privatevirtual

Request that the query output be grouped by an expression.

Parameters
[in]expressionText of the SQL expression

Definition at line 651 of file DbStorageImpl.cc.

651  {
652  if (!_groupBy.empty()) {
653  _groupBy += ", ";
654  }
655  _groupBy += expression;
656 }
T empty(T... args)

◆ hasBeenCorrupted()

bool lsst::daf::base::Citizen::hasBeenCorrupted ( )
staticinherited

Check all allocated blocks for corruption.

Definition at line 353 of file Citizen.cc.

353  {
354  ReadGuard guard(citizenLock);
355  for (table::iterator cur = _activeCitizens.begin();
356  cur != _activeCitizens.end(); cur++) {
357  if (cur->first->_hasBeenCorrupted()) {
358  return true;
359  }
360  }
361  for (table::iterator cur = _persistentCitizens.begin();
362  cur != _persistentCitizens.end(); cur++) {
363  if (cur->first->_hasBeenCorrupted()) {
364  return true;
365  }
366  }
367 
368  return false;
369 }
T end(T... args)
static table _activeCitizens
Definition: Citizen.h:102
static table _persistentCitizens
Definition: Citizen.h:103
T begin(T... args)

◆ init()

int lsst::daf::base::Citizen::init ( )
staticinherited

Called once when the memory system is being initialised.

Definition at line 214 of file Citizen.cc.

214  {
215  volatile int dummy = 1;
216  return dummy;
217 }

◆ insertRow()

void lsst::daf::persistence::DbStorageImpl::insertRow ( void  )
privatevirtual

Insert the row.

Row values must have been set with setColumn() calls.

Definition at line 441 of file DbStorageImpl.cc.

441  {
442  if (_readonly) {
443  error("Attempt to insert into read-only database", false);
444  }
445  if (_insertTable.empty()) error("Insert table not initialized in DbStorage::insertRow()", false);
446  if (_inputVars.empty()) error("No values to insert", false);
447 
448  std::string query = "INSERT INTO " + quote(_insertTable) + " (";
449 
450  std::unique_ptr<MYSQL_BIND[]> binder(new MYSQL_BIND[_inputVars.size()]);
451  memset(binder.get(), 0, _inputVars.size() * sizeof(MYSQL_BIND));
452 
453  int i = 0;
454  for (BoundVarMap::iterator it = _inputVars.begin();
455  it != _inputVars.end(); ++it) {
456  if (it != _inputVars.begin()) {
457  query += ", ";
458  }
459  query += quote(it->first);
460 
461  // Bind variables
462  MYSQL_BIND& bind(binder[i]);
463  BoundVar& bv(it->second);
464  if (bv._isNull) {
465  bind.buffer_type = MYSQL_TYPE_NULL;
466  }
467  else {
468  bind.buffer_type = bv._type;
469  bind.buffer = bv._data;
470  bind.buffer_length = bv._length;
471  bind.length = &(bv._length);
472  bind.is_null = 0;
473  bind.is_unsigned = bv._isUnsigned;
474  bind.error = 0;
475  }
476  ++i;
477  }
478  query += ") VALUES (";
479  for (size_t i = 0; i < _inputVars.size(); ++i) {
480  if (i != 0) {
481  query += ", ";
482  }
483  query += "?";
484  }
485  query += ")";
486 
487  // Execute statement
488  // Guard statement with mysql_stmt_close()
489  _statement = mysql_stmt_init(_db);
490  if (_statement == 0) {
491  error("Unable to initialize statement: " + query);
492  }
493  if (mysql_stmt_prepare(_statement, query.c_str(), query.length()) != 0) {
494  stError("Unable to prepare statement: " + query);
495  }
496  if (mysql_stmt_bind_param(_statement, binder.get())) {
497  stError("Unable to bind variables in: " + query);
498  }
499  if (mysql_stmt_execute(_statement) != 0) {
500  stError("Unable to execute statement: " + query);
501  }
502  mysql_stmt_close(_statement);
503  _statement = 0;
504 }
T empty(T... args)
T memset(T... args)
T end(T... args)
STL class.
MYSQL * _db
MySQL database connection pointer.
void error(std::string const &text, bool mysqlCaused=true)
MYSQL_STMT * _statement
Prepared query statement.
T bind(T... args)
virtual void query(void)
Execute the query.
T size(T... args)
STL class.
T begin(T... args)
T c_str(T... args)
std::string quote(std::string const &name)
Quote a name in ANSI-standard fashion.
void stError(std::string const &text)
BoundVarMap _inputVars
Input variable bindings.
bool _readonly
Remember if we are supposed to be read-only.
std::string _insertTable
Name of table into which to insert.

◆ markPersistent()

void lsst::daf::base::Citizen::markPersistent ( void  )
inherited

Mark a Citizen as persistent and not destroyed until process end.

Definition at line 254 of file Citizen.cc.

254  {
255  WriteGuard guard(citizenLock);
256  _persistentCitizens[this] = _activeCitizens[this];
257  _activeCitizens.erase(this);
258 }
static table _activeCitizens
Definition: Citizen.h:102
T erase(T... args)
static table _persistentCitizens
Definition: Citizen.h:103

◆ next()

bool lsst::daf::persistence::DbStorageImpl::next ( void  )
privatevirtual

Move to the next (first) row of the query result.

Returns
false if no more rows

Definition at line 851 of file DbStorageImpl.cc.

851  {
852  if (_statement == 0) {
853  error("Statement not initialized in DbStorage::next()", false);
854  }
855  int ret = mysql_stmt_fetch(_statement);
856  if (ret == 0) {
857  // Fix up strings and DateTimes
858  if (!_outputVars.empty()) {
859  for (size_t i = 0; i < _outColumns.size(); ++i) {
860  BoundVarMap::iterator bvit = _outputVars.find(_outColumns[i]);
861  if (bvit == _outputVars.end()) {
862  error("Unbound variable in SELECT clause: " +
863  _outColumns[i], false);
864  }
865  BoundVar& bv = bvit->second;
866  if (bv._type == BoundVarTraits<std::string>::mysqlType) {
867  **reinterpret_cast<std::string**>(bv._data) =
868  std::string(reinterpret_cast<char*>(bv._data) +
869  sizeof(std::string*), _fieldLengths[i]);
870  }
871  else if (bv._type ==
873  char* cp = reinterpret_cast<char*>(bv._data) +
874  sizeof(dafBase::DateTime*);
875  MYSQL_TIME* t = reinterpret_cast<MYSQL_TIME*>(cp);
876  **reinterpret_cast<dafBase::DateTime**>(bv._data) =
877  dafBase::DateTime(t->year, t->month, t->day,
878  t->hour, t->minute, t->second,
880  }
881  }
882  }
883  return true;
884  }
885  if (ret == MYSQL_NO_DATA) return false;
886  if (ret == MYSQL_DATA_TRUNCATED && _outputVars.empty()) return true;
887  stError("Error fetching next row");
888  return false;
889 }
T empty(T... args)
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:62
boost::shared_array< unsigned long > _fieldLengths
Space for lengths of result fields.
T end(T... args)
STL class.
void error(std::string const &text, bool mysqlCaused=true)
MYSQL_STMT * _statement
Prepared query statement.
T find(T... args)
T size(T... args)
void stError(std::string const &text)
BoundVarMap _outputVars
Output variable bindings.
std::vector< std::string > _outColumns

◆ orderBy()

void lsst::daf::persistence::DbStorageImpl::orderBy ( std::string const &  expression)
privatevirtual

Request that the query output be sorted by an expression.

Multiple expressions may be specified, in order.

Parameters
[in]expressionText of the SQL expression

Definition at line 641 of file DbStorageImpl.cc.

641  {
642  if (!_orderBy.empty()) {
643  _orderBy += ", ";
644  }
645  _orderBy += expression;
646 }
T empty(T... args)

◆ outColumn()

void lsst::daf::persistence::DbStorageImpl::outColumn ( std::string const &  columnName,
bool  isExpr 
)
privatevirtual

Request a column in the query output.

Parameters
[in]columnNameName of the column
[in]isExprTrue if the name is actually an expression

The order of outColumn() calls is the order of appearance in the output row. Use either outColumn() or outParam() but not both.

Definition at line 554 of file DbStorageImpl.cc.

555  {
556  std::string col = isExpr ? columnName : quote(columnName);
557  _outColumns.push_back(col);
558 }
STL class.
T push_back(T... args)
std::string quote(std::string const &name)
Quote a name in ANSI-standard fashion.
std::vector< std::string > _outColumns
int col
Definition: CR.cc:156

◆ outParam() [1/3]

template<typename T >
void lsst::daf::persistence::DbStorageImpl::outParam ( std::string const &  columnName,
T *  location,
bool  isExpr 
)
private

Request a column in the query output and bind a destination location.

Parameters
[in]columnNameName of the column
[in]locationPointer to the destination
[in]isExprTrue if the name is actually an expression

The order of outParam() calls is the order of appearance in the output row. Use either outColumn() or outParam() but not both.

Definition at line 569 of file DbStorageImpl.cc.

570  {
571  std::string col = isExpr ? columnName : quote(columnName);
572  _outColumns.push_back(col);
573  size_t size = sizeof(T);
575  BoundVarMap::value_type(col, BoundVar(location)));
576  if (!pair.second) {
577  error("Duplicate column name requested: " + columnName, false);
578  }
579  BoundVar& bv = pair.first->second;
580  bv._type = BoundVarTraits<T>::mysqlType;
581  bv._isNull = false;
582  bv._isUnsigned = BoundVarTraits<T>::isUnsigned;
583  bv._length = size;
584 }
STL class.
T push_back(T... args)
void error(std::string const &text, bool mysqlCaused=true)
T insert(T... args)
std::string quote(std::string const &name)
Quote a name in ANSI-standard fashion.
BoundVarMap _outputVars
Output variable bindings.
std::vector< std::string > _outColumns
int col
Definition: CR.cc:156

◆ outParam() [2/3]

template<>
void lsst::daf::persistence::DbStorageImpl::outParam ( std::string const &  columnName,
std::string location,
bool  isExpr 
)

Definition at line 587 of file DbStorageImpl.cc.

588  {
589  std::string col = isExpr ? columnName : quote(columnName);
590  _outColumns.push_back(col);
591  size_t size = 4096;
593  BoundVarMap::value_type(
594  col, BoundVar(allocateMemory(size + sizeof(std::string*)))));
595  if (!pair.second) {
596  error("Duplicate column name requested: " + columnName, false);
597  }
598  BoundVar& bv = pair.first->second;
599  *reinterpret_cast<std::string**>(bv._data) = location;
601  bv._isNull = false;
603  bv._length = size;
604 }
STL class.
T push_back(T... args)
void error(std::string const &text, bool mysqlCaused=true)
T insert(T... args)
std::string quote(std::string const &name)
Quote a name in ANSI-standard fashion.
BoundVarMap _outputVars
Output variable bindings.
std::vector< std::string > _outColumns
int col
Definition: CR.cc:156

◆ outParam() [3/3]

template<>
void lsst::daf::persistence::DbStorageImpl::outParam ( std::string const &  columnName,
dafBase::DateTime location,
bool  isExpr 
)

Definition at line 607 of file DbStorageImpl.cc.

609  {
610  std::string col = isExpr ? columnName : quote(columnName);
611  _outColumns.push_back(col);
612  size_t size = sizeof(MYSQL_TIME);
614  BoundVarMap::value_type(
615  col, BoundVar(allocateMemory(size + sizeof(dafBase::DateTime*)))));
616  if (!pair.second) {
617  error("Duplicate column name requested: " + columnName, false);
618  }
619  BoundVar& bv = pair.first->second;
620  *reinterpret_cast<dafBase::DateTime**>(bv._data) = location;
622  bv._isNull = false;
624  bv._length = size;
625 }
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:62
STL class.
T push_back(T... args)
void error(std::string const &text, bool mysqlCaused=true)
T insert(T... args)
std::string quote(std::string const &name)
Quote a name in ANSI-standard fashion.
BoundVarMap _outputVars
Output variable bindings.
std::vector< std::string > _outColumns
int col
Definition: CR.cc:156

◆ query()

void lsst::daf::persistence::DbStorageImpl::query ( void  )
privatevirtual

Execute the query.

Definition at line 669 of file DbStorageImpl.cc.

669  {
670  if (_outColumns.empty()) error("No output columns for query", false);
671 
672  // SELECT outVars FROM queryTables WHERE whereClause GROUP BY groupBy
673  // ORDER BY orderBy
674 
675  // SELECT clause
676  std::string query = "SELECT ";
678  it != _outColumns.end(); ++it) {
679  if (it != _outColumns.begin()) {
680  query += ", ";
681  }
682  query += *it;
683  }
684 
685  // FROM clause
686  query += " FROM ";
688  it != _queryTables.end(); ++it) {
689  if (it != _queryTables.begin()) {
690  query += ", ";
691  }
692  query += *it;
693  }
694 
695  // WHERE clause
696  std::vector<std::string> whereBindings;
697  if (!_whereClause.empty()) {
698  boost::regex re(":([A-Za-z_]+)");
699  std::string result;
701  boost::regex_iterator<std::string::iterator> m;
702  for (boost::regex_iterator<std::string::iterator> i(
704  i != boost::regex_iterator<std::string::iterator>(); ++i) {
705  m = i;
706  std::copy(m->prefix().first, m->prefix().second, out);
707  *out++ = '?';
708  assert(m->size() == 2);
709  whereBindings.push_back(m->str(1));
710  }
711  if (m != boost::regex_iterator<std::string::iterator>()) {
712  std::copy(m->suffix().first, m->suffix().second, out);
713  }
714  else {
716  }
717  query += " WHERE " + result;
718  }
719 
720  // GROUP BY clause
721  if (!_groupBy.empty()) query += " GROUP BY " + _groupBy;
722 
723  // ORDER BY clause
724  if (!_orderBy.empty()) query += " ORDER BY " + _orderBy;
725 
726 
727  // Create bindings for input WHERE clause variables, if any
728 
730  new MYSQL_BIND[whereBindings.size()]);
731  memset(inBinder.get(), 0, whereBindings.size() * sizeof(MYSQL_BIND));
732  for (size_t i = 0; i < whereBindings.size(); ++i) {
733  MYSQL_BIND& bind(inBinder[i]);
734  BoundVarMap::iterator it = _inputVars.find(whereBindings[i]);
735  if (it == _inputVars.end()) {
736  error("Unbound variable in WHERE clause: " + whereBindings[i],
737  false);
738  }
739  BoundVar& bv = it->second;
740  bind.buffer_type = bv._type;
741  bind.buffer = bv._data;
742  bind.buffer_length = bv._length;
743  bind.is_null = 0;
744  bind.is_unsigned = bv._isUnsigned;
745  bind.error = 0;
746  }
747 
748 
749  // Initialize and prepare statement
750 
751  _statement = mysql_stmt_init(_db);
752  if (!_statement) {
753  error("Unable to initialize prepared statement");
754  }
755 
756  if (mysql_stmt_prepare(_statement, query.c_str(), query.length()) != 0) {
757  stError("Unable to prepare statement: " + query);
758  }
759 
760 
761  // Check number of input parameters and bind them
762  unsigned int params = mysql_stmt_param_count(_statement);
763  if (_whereClause.empty()) {
764  if (params != 0) {
765  error("Unbound WHERE clause parameters: " + query, false);
766  }
767  }
768  else {
769  if (params != whereBindings.size()) {
770  error("Mismatch in number of WHERE clause parameters: " + query,
771  false);
772  }
773  if (mysql_stmt_bind_param(_statement, inBinder.get())) {
774  stError("Unable to bind WHERE parameters: " + query);
775  }
776  }
777 
778  // Check number of result columns
779  MYSQL_RES* queryMetadata = mysql_stmt_result_metadata(_statement);
780  if (!queryMetadata) {
781  stError("No query metadata: " + query);
782  }
783  _numResultFields = mysql_num_fields(queryMetadata);
784  if (static_cast<unsigned int>(_numResultFields) != _outColumns.size()) {
785  error("Mismatch in number of SELECT items: " + query, false);
786  }
787 
788 
789  // Execute query
790 
791  if (mysql_stmt_execute(_statement) != 0) {
792  stError("MySQL query failed: " + query);
793  }
794 
795 
796  // Create bindings for output variables
797 
798  _resultFields = mysql_fetch_fields(queryMetadata);
799 
800  std::unique_ptr<MYSQL_BIND[]> outBinder(new MYSQL_BIND[_numResultFields]);
801  memset(outBinder.get(), 0, _numResultFields * sizeof(MYSQL_BIND));
802  _fieldLengths.reset(new unsigned long[_numResultFields]);
803  _fieldNulls.reset(new my_bool[_numResultFields]);
804 
805  for (int i = 0; i < _numResultFields; ++i) {
806  MYSQL_BIND& bind(outBinder[i]);
807  if (_outputVars.empty()) {
808  bind.buffer_type = MYSQL_TYPE_STRING;
809  bind.buffer = 0;
810  bind.buffer_length = 0;
811  bind.length = &(_fieldLengths[i]);
812  bind.is_null = &(_fieldNulls[i]);
813  bind.is_unsigned = (_resultFields[i].flags & UNSIGNED_FLAG) != 0;
814  bind.error = 0;
815  }
816  else {
817  BoundVarMap::iterator it = _outputVars.find(_outColumns[i]);
818  if (it == _outputVars.end()) {
819  error("Unbound variable in SELECT clause: " + _outColumns[i],
820  false);
821  }
822  BoundVar& bv = it->second;
823 
824  bind.buffer_type = bv._type;
825  if (bv._type == BoundVarTraits<std::string>::mysqlType) {
826  bind.buffer = reinterpret_cast<char*>(bv._data) +
827  sizeof(std::string*);
828  }
829  else if (bv._type == BoundVarTraits<dafBase::DateTime>::mysqlType) {
830  bind.buffer = reinterpret_cast<char*>(bv._data) +
831  sizeof(std::string*);
832  }
833  else {
834  bind.buffer = bv._data;
835  }
836  bind.buffer_length = bv._length;
837  bind.length = &(_fieldLengths[i]);
838  bind.is_null = &(_fieldNulls[i]);
839  bind.is_unsigned = bv._isUnsigned;
840  bind.error = 0;
841  }
842  }
843  if (mysql_stmt_bind_result(_statement, outBinder.get())) {
844  stError("Unable to bind results: " + query);
845  }
846 }
T empty(T... args)
T copy(T... args)
std::vector< std::string > _queryTables
Names of tables to select from.
boost::shared_array< unsigned long > _fieldLengths
Space for lengths of result fields.
T memset(T... args)
int _numResultFields
Number of result fields.
T end(T... args)
STL class.
T push_back(T... args)
MYSQL * _db
MySQL database connection pointer.
void error(std::string const &text, bool mysqlCaused=true)
MYSQL_STMT * _statement
Prepared query statement.
T bind(T... args)
boost::shared_array< my_bool > _fieldNulls
Space for null flags of result fields.
virtual void query(void)
Execute the query.
MYSQL_FIELD * _resultFields
Query result field metadata.
T find(T... args)
T size(T... args)
STL class.
STL class.
T begin(T... args)
T c_str(T... args)
void stError(std::string const &text)
BoundVarMap _inputVars
Input variable bindings.
BoundVarMap _outputVars
Output variable bindings.
std::vector< std::string > _outColumns

◆ quote()

std::string lsst::daf::persistence::DbStorageImpl::quote ( std::string const &  name)
private

Quote a name in ANSI-standard fashion.

Definition at line 269 of file DbStorageImpl.cc.

269  {
270  std::string::size_type pos = name.find('.');
271  if (pos == std::string::npos) return '`' + name + '`';
272  return '`' + std::string(name, 0, pos) + "`.`" +
273  std::string(name, pos + 1) + '`';
274 }
table::Key< std::string > name
Definition: ApCorrMap.cc:71
STL class.

◆ repr()

std::string lsst::daf::base::Citizen::repr ( ) const
inherited

Return a string representation of a Citizen.

Definition at line 245 of file Citizen.cc.

245  {
246  return boost::str(boost::format("%d: %08x %s")
247  % _CitizenId
248  % this
250  );
251 }
std::string demangleType(std::string const _typeName)
Definition: Demangle.cc:113
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:134
const char * _typeName
Definition: Citizen.h:94

◆ setColumn() [1/3]

template<typename T >
void lsst::daf::persistence::DbStorageImpl::setColumn ( std::string const &  columnName,
T const &  value 
)
private

Set the value to insert in a given column.

Parameters
[in]columnNameName of the column
[in]valueValue to set in the column

Definition at line 355 of file DbStorageImpl.cc.

356  {
357  BoundVarMap::iterator bv = _inputVars.find(columnName);
358  size_t size = sizeof(T);
359  if (bv == _inputVars.end()) {
360  bv = _inputVars.insert(
361  BoundVarMap::value_type(columnName,
362  BoundVar(allocateMemory(size)))).first;
363  }
364  else if (bv->second._length != size) {
365  bv->second._data = allocateMemory(size);
366  }
367  bv->second._type = BoundVarTraits<T>::mysqlType;
368  bv->second._isNull = false;
369  bv->second._isUnsigned = BoundVarTraits<T>::isUnsigned;
370  bv->second._length = size;
371  memcpy(bv->second._data, &value, size);
372 }
T end(T... args)
T memcpy(T... args)
T insert(T... args)
T find(T... args)
BoundVarMap _inputVars
Input variable bindings.

◆ setColumn() [2/3]

template<>
void lsst::daf::persistence::DbStorageImpl::setColumn ( std::string const &  columnName,
std::string const &  value 
)

Definition at line 375 of file DbStorageImpl.cc.

376  {
377  BoundVarMap::iterator bv = _inputVars.find(columnName);
378  size_t size = value.length();
379  if (bv == _inputVars.end()) {
380  bv = _inputVars.insert(
381  BoundVarMap::value_type(columnName,
382  BoundVar(allocateMemory(size)))).first;
383  }
384  else if (bv->second._length != size) {
385  bv->second._data = allocateMemory(size);
386  }
387  bv->second._type = BoundVarTraits<std::string>::mysqlType;
388  bv->second._isNull = false;
389  bv->second._isUnsigned = BoundVarTraits<std::string>::isUnsigned;
390  bv->second._length = size;
391  memcpy(bv->second._data, value.data(), size);
392 }
T end(T... args)
T memcpy(T... args)
T insert(T... args)
T find(T... args)
BoundVarMap _inputVars
Input variable bindings.

◆ setColumn() [3/3]

template<>
void lsst::daf::persistence::DbStorageImpl::setColumn ( std::string const &  columnName,
dafBase::DateTime const &  value 
)

Definition at line 395 of file DbStorageImpl.cc.

396  {
397  BoundVarMap::iterator bv = _inputVars.find(columnName);
398  size_t size = sizeof(MYSQL_TIME);
399  if (bv == _inputVars.end()) {
400  bv = _inputVars.insert(
401  BoundVarMap::value_type(columnName,
402  BoundVar(allocateMemory(size)))).first;
403  }
404  else if (bv->second._length != size) {
405  bv->second._data = allocateMemory(size);
406  }
408  bv->second._isNull = false;
409  bv->second._isUnsigned = BoundVarTraits<dafBase::DateTime>::isUnsigned;
410  bv->second._length = size;
411  struct tm v = value.gmtime(dafBase::DateTime::UTC);
412  MYSQL_TIME* t = reinterpret_cast<MYSQL_TIME*>(bv->second._data);
413  t->year = v.tm_year + 1900;
414  t->month = v.tm_mon + 1;
415  t->day = v.tm_mday;
416  t->hour = v.tm_hour;
417  t->minute = v.tm_min;
418  t->second = v.tm_sec;
419  t->neg = false;
420  t->second_part =
421  static_cast<unsigned long>((value.nsecs() % 1000000000LL) / 1000);
422 }
T end(T... args)
T insert(T... args)
T find(T... args)
BoundVarMap _inputVars
Input variable bindings.

◆ setColumnToNull()

void lsst::daf::persistence::DbStorageImpl::setColumnToNull ( std::string const &  columnName)
privatevirtual

Set a given column to NULL.

Parameters
[in]columnNameName of the column

Definition at line 427 of file DbStorageImpl.cc.

427  {
428  BoundVarMap::iterator bv = _inputVars.find(columnName);
429  if (bv == _inputVars.end()) {
430  bv = _inputVars.insert(
431  BoundVarMap::value_type(columnName,
432  BoundVar(allocateMemory(1)))).first;
433  }
434  bv->second._isNull = true;
435  bv->second._length = 1;
436 }
T end(T... args)
T insert(T... args)
T find(T... args)
BoundVarMap _inputVars
Input variable bindings.

◆ setCorruptionCallback()

dafBase::Citizen::memCallback lsst::daf::base::Citizen::setCorruptionCallback ( Citizen::memCallback  func)
staticinherited

Set the CorruptionCallback function.

Parameters
funcfunction be called when block is found to be corrupted

Definition at line 432 of file Citizen.cc.

434  {
436  _corruptionCallback = func;
437 
438  return old;
439 }
memId(* memCallback)(const Citizen *ptr)
Definition: Citizen.h:59
static memCallback _corruptionCallback
Definition: Citizen.h:113

◆ setDeleteCallback()

dafBase::Citizen::memCallback lsst::daf::base::Citizen::setDeleteCallback ( Citizen::memCallback  func)
staticinherited

Set the DeleteCallback function.

Parameters
funcfunction be called when desired block is deleted

Definition at line 422 of file Citizen.cc.

424  {
426  _deleteCallback = func;
427 
428  return old;
429 }
static memCallback _deleteCallback
Definition: Citizen.h:112
memId(* memCallback)(const Citizen *ptr)
Definition: Citizen.h:59

◆ setDeleteCallbackId()

dafBase::Citizen::memId lsst::daf::base::Citizen::setDeleteCallbackId ( Citizen::memId  id)
staticinherited

Call the current DeleteCallback when block is deleted.

Parameters
idDesired ID

Definition at line 387 of file Citizen.cc.

389  {
390  WriteGuard guard(citizenLock);
391  Citizen::memId oldId = _deleteId;
392  _deleteId = id;
393 
394  return oldId;
395 }
unsigned long memId
Type of the block&#39;s ID.
Definition: Citizen.h:56
int id
Definition: CR.cc:155
static memId _deleteId
Definition: Citizen.h:109

◆ setNewCallback()

dafBase::Citizen::memNewCallback lsst::daf::base::Citizen::setNewCallback ( Citizen::memNewCallback  func)
staticinherited

Set the NewCallback function.

Parameters
funcThe new function to be called when a designated block is allocated

Definition at line 412 of file Citizen.cc.

414  {
416  _newCallback = func;
417 
418  return old;
419 }
memId(* memNewCallback)(const memId cid)
A function used to register a callback.
Definition: Citizen.h:58
static memNewCallback _newCallback
Definition: Citizen.h:111

◆ setNewCallbackId()

dafBase::Citizen::memId lsst::daf::base::Citizen::setNewCallbackId ( Citizen::memId  id)
staticinherited

Call the NewCallback when block is allocated.

Parameters
idDesired ID

Definition at line 376 of file Citizen.cc.

378  {
379  WriteGuard guard(citizenLock);
380  Citizen::memId oldId = _newId;
381  _newId = id;
382 
383  return oldId;
384 }
unsigned long memId
Type of the block&#39;s ID.
Definition: Citizen.h:56
static memId _newId
Definition: Citizen.h:108
int id
Definition: CR.cc:155

◆ setPersistLocation()

void lsst::daf::persistence::DbStorageImpl::setPersistLocation ( LogicalLocation const &  location)
privatevirtual

Set the database location to persist to.

Parameters
[in]locationDatabase connection string to insert into

Definition at line 221 of file DbStorageImpl.cc.

221  {
222  startSession(location.locString());
223  _readonly = false;
224 }
virtual void startSession(std::string const &location)
Start a database session.
bool _readonly
Remember if we are supposed to be read-only.

◆ setPolicy()

void lsst::daf::persistence::DbStorageImpl::setPolicy ( pexPolicy::Policy::Ptr  policy)
privatevirtual

Allow a Policy to be used to configure the DbStorage.

Parameters
[in]policy

Definition at line 185 of file DbStorageImpl.cc.

185  {
186 }

◆ setQueryWhere()

void lsst::daf::persistence::DbStorageImpl::setQueryWhere ( std::string const &  whereClause)
privatevirtual

Set the condition for the WHERE clause of the query.

Parameters
[in]whereClauseSQL text of the WHERE clause

May include join conditions.

Definition at line 663 of file DbStorageImpl.cc.

663  {
664  _whereClause = whereClause;
665 }

◆ setRetrieveLocation()

void lsst::daf::persistence::DbStorageImpl::setRetrieveLocation ( LogicalLocation const &  location)
privatevirtual

Set the database location to retrieve from.

Parameters
[in]locationDatabase connection string to query

Definition at line 229 of file DbStorageImpl.cc.

229  {
230  startSession(location.locString());
231  _readonly = true;
232 }
virtual void startSession(std::string const &location)
Start a database session.
bool _readonly
Remember if we are supposed to be read-only.

◆ setTableForInsert()

void lsst::daf::persistence::DbStorageImpl::setTableForInsert ( std::string const &  tableName)
privatevirtual

Set the table to insert rows into.

Parameters
[in]tableNameName of the table

Definition at line 342 of file DbStorageImpl.cc.

342  {
343  if (_readonly) {
344  error("Attempt to insert into read-only database", false);
345  }
346  _insertTable = tableName;
347  _inputVars.clear();
348 }
void error(std::string const &text, bool mysqlCaused=true)
T clear(T... args)
BoundVarMap _inputVars
Input variable bindings.
bool _readonly
Remember if we are supposed to be read-only.
std::string _insertTable
Name of table into which to insert.

◆ setTableForQuery()

void lsst::daf::persistence::DbStorageImpl::setTableForQuery ( std::string const &  tableName,
bool  isExpr 
)
privatevirtual

Set the table to query (single-table queries only).

Parameters
[in]tableNameName of the table
[in]isExprTrue if the name is actually a table expression

Definition at line 514 of file DbStorageImpl.cc.

515  {
516  if (_db == 0) error("Database session not initialized in DbStorage::setTableForQuery()", false);
518  _queryTables.push_back(isExpr ? tableName : quote(tableName));
519  _inputVars.clear();
520  _outputVars.clear();
521  _outColumns.clear();
523  _groupBy.clear();
524  _orderBy.clear();
525  _statement = 0;
526 }
std::vector< std::string > _queryTables
Names of tables to select from.
T push_back(T... args)
MYSQL * _db
MySQL database connection pointer.
void error(std::string const &text, bool mysqlCaused=true)
MYSQL_STMT * _statement
Prepared query statement.
T clear(T... args)
std::string quote(std::string const &name)
Quote a name in ANSI-standard fashion.
BoundVarMap _inputVars
Input variable bindings.
BoundVarMap _outputVars
Output variable bindings.
std::vector< std::string > _outColumns

◆ setTableListForQuery()

void lsst::daf::persistence::DbStorageImpl::setTableListForQuery ( std::vector< std::string > const &  tableNameList)
privatevirtual

Set a list of tables to query (multiple-table queries).

Parameters
[in]tableNameListVector of names of tables

Definition at line 531 of file DbStorageImpl.cc.

532  {
533  if (_db == 0) error("Database session not initialized in DbStorage::setTableListForQuery()", false);
534  for (std::vector<std::string>::const_iterator it = tableNameList.begin();
535  it != tableNameList.end(); ++it) {
537  }
538  _inputVars.clear();
539  _outputVars.clear();
540  _outColumns.clear();
542  _groupBy.clear();
543  _orderBy.clear();
544  _statement = 0;
545 }
std::vector< std::string > _queryTables
Names of tables to select from.
T end(T... args)
T push_back(T... args)
MYSQL * _db
MySQL database connection pointer.
void error(std::string const &text, bool mysqlCaused=true)
MYSQL_STMT * _statement
Prepared query statement.
T clear(T... args)
STL class.
T begin(T... args)
std::string quote(std::string const &name)
Quote a name in ANSI-standard fashion.
BoundVarMap _inputVars
Input variable bindings.
BoundVarMap _outputVars
Output variable bindings.
std::vector< std::string > _outColumns

◆ startSession()

void lsst::daf::persistence::DbStorageImpl::startSession ( std::string const &  location)
privatevirtual

Start a database session.

Parameters
[in]locationPhysical database location
[in]amAccess mode for the database (ReadOnly or Update)

Definition at line 196 of file DbStorageImpl.cc.

196  {
197  // Set the timezone for any DATE/TIME/TIMESTAMP fields.
198  setenv("TZ", "UTC", 1);
199 
200  DbStorageLocation dbloc(location);
201 
202  if (_db) {
203  mysql_close(_db);
204  }
205  _db = mysql_init(0);
206 
207  unsigned int port = strtoul(dbloc.getPort().c_str(), 0, 10);
208  if (mysql_real_connect(_db,
209  dbloc.getHostname().c_str(),
210  dbloc.getUsername().c_str(),
211  dbloc.getPassword().c_str(),
212  dbloc.getDbName().c_str(),
213  port, 0, 0) == 0) {
214  error("Unable to connect to MySQL database: " + _location);
215  }
216 }
MYSQL * _db
MySQL database connection pointer.
void error(std::string const &text, bool mysqlCaused=true)
std::string _location
Database location string saved for use by raw MySQL interface.
T strtoul(T... args)

◆ startTransaction()

void lsst::daf::persistence::DbStorageImpl::startTransaction ( void  )
privatevirtual

Start a transaction.

Definition at line 236 of file DbStorageImpl.cc.

236  {
237  if (_db == 0) error("Database session not initialized "
238  "in DbStorage::startTransaction()", false);
239  if (mysql_autocommit(_db, false)) error("Unable to turn off autocommit");
240 }
MYSQL * _db
MySQL database connection pointer.
void error(std::string const &text, bool mysqlCaused=true)

◆ stError()

void lsst::daf::persistence::DbStorageImpl::stError ( std::string const &  text)
private

Definition at line 276 of file DbStorageImpl.cc.

276  {
277  error(text + " - * " + mysql_stmt_error(_statement), false);
278 }
void error(std::string const &text, bool mysqlCaused=true)
MYSQL_STMT * _statement
Prepared query statement.

◆ truncateTable()

void lsst::daf::persistence::DbStorageImpl::truncateTable ( std::string const &  tableName)
privatevirtual

Truncate a table.

Parameters
[in]tableNameName of the table to truncate

Definition at line 323 of file DbStorageImpl.cc.

323  {
324  executeQuery("TRUNCATE TABLE " + quote(tableName));
325 }
std::string quote(std::string const &name)
Quote a name in ANSI-standard fashion.
void executeQuery(std::string const &query)
Execute a query string.

Friends And Related Function Documentation

◆ DbStorage

friend class DbStorage
friend

Definition at line 85 of file DbStorageImpl.h.

Member Data Documentation

◆ _bindingMemory

std::vector< boost::shared_array<char> > lsst::daf::persistence::DbStorageImpl::_bindingMemory
private

Memory for bound variables.

Definition at line 155 of file DbStorageImpl.h.

◆ _db

MYSQL* lsst::daf::persistence::DbStorageImpl::_db
private

MySQL database connection pointer.

Definition at line 141 of file DbStorageImpl.h.

◆ _fieldLengths

boost::shared_array<unsigned long> lsst::daf::persistence::DbStorageImpl::_fieldLengths
private

Space for lengths of result fields.

Definition at line 170 of file DbStorageImpl.h.

◆ _fieldNulls

boost::shared_array<my_bool> lsst::daf::persistence::DbStorageImpl::_fieldNulls
private

Space for null flags of result fields.

Definition at line 172 of file DbStorageImpl.h.

◆ _groupBy

std::string lsst::daf::persistence::DbStorageImpl::_groupBy
private

Definition at line 161 of file DbStorageImpl.h.

◆ _inputVars

BoundVarMap lsst::daf::persistence::DbStorageImpl::_inputVars
private

Input variable bindings.

Definition at line 151 of file DbStorageImpl.h.

◆ _insertTable

std::string lsst::daf::persistence::DbStorageImpl::_insertTable
private

Name of table into which to insert.

Definition at line 144 of file DbStorageImpl.h.

◆ _location

std::string lsst::daf::persistence::DbStorageImpl::_location
private

Database location string saved for use by raw MySQL interface.

Definition at line 139 of file DbStorageImpl.h.

◆ _numResultFields

int lsst::daf::persistence::DbStorageImpl::_numResultFields
private

Number of result fields.

Definition at line 168 of file DbStorageImpl.h.

◆ _orderBy

std::string lsst::daf::persistence::DbStorageImpl::_orderBy
private

Definition at line 162 of file DbStorageImpl.h.

◆ _outColumns

std::vector<std::string> lsst::daf::persistence::DbStorageImpl::_outColumns
private

Definition at line 159 of file DbStorageImpl.h.

◆ _outputVars

BoundVarMap lsst::daf::persistence::DbStorageImpl::_outputVars
private

Output variable bindings.

Definition at line 153 of file DbStorageImpl.h.

◆ _queryTables

std::vector<std::string> lsst::daf::persistence::DbStorageImpl::_queryTables
private

Names of tables to select from.

Definition at line 146 of file DbStorageImpl.h.

◆ _readonly

bool lsst::daf::persistence::DbStorageImpl::_readonly
private

Remember if we are supposed to be read-only.

Definition at line 137 of file DbStorageImpl.h.

◆ _resultFields

MYSQL_FIELD* lsst::daf::persistence::DbStorageImpl::_resultFields
private

Query result field metadata.

Definition at line 166 of file DbStorageImpl.h.

◆ _statement

MYSQL_STMT* lsst::daf::persistence::DbStorageImpl::_statement
private

Prepared query statement.

Definition at line 164 of file DbStorageImpl.h.

◆ _whereClause

std::string lsst::daf::persistence::DbStorageImpl::_whereClause
private

Definition at line 160 of file DbStorageImpl.h.


The documentation for this class was generated from the following files: