BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
rdbModel::MysqlConnection Class Reference

#include <MysqlConnection.h>

Inheritance diagram for rdbModel::MysqlConnection:

Public Member Functions

 MysqlConnection (std::ostream *out=0, std::ostream *errOut=0)
virtual ~MysqlConnection ()
virtual bool open (const std::string &host, const std::string &userid, const std::string &password, const std::string &dbName)
virtual bool open (const std::string &parms)
virtual bool close ()
virtual bool isConnected ()
 Return true iff open has been done with no matching close.
std::ostream * getOut () const
std::ostream * getErrOut () const
virtual MATCH matchSchema (Rdb *rdb, bool matchDbName=true)
virtual bool insertRow (const std::string &tableName, const StringVector &colNames, const StringVector &values, int *auto_value=0, const StringVector *nullCols=0)
virtual unsigned int update (const std::string &tableName, const StringVector &colNames, const StringVector &values, const Assertion *where=0, const StringVector *nullCols=0)
virtual ResultHandleselect (const std::string &tableName, const StringVector &getCols, const StringVector &orderCols, const Assertion *where=0, int rowLimit=0, int rowOffset=0)
virtual ResultHandledbRequest (const std::string &request)
virtual bool compileAssertion (const Assertion *a, std::string &sqlString) const
virtual void disableModify (bool disable)
virtual Visitor::VisitorState visitRdb (Rdb *)
 This method says if the visitor is recursive or not.
virtual Visitor::VisitorState visitTable (Table *)
virtual Visitor::VisitorState visitColumn (Column *)
virtual Visitor::VisitorState visitIndex (Index *)
virtual Visitor::VisitorState visitAssertion (Assertion *)
virtual VisitorState visitInsertNew (InsertNew *)
virtual VisitorState visitSupersede (Supersede *)
virtual VisitorState visitQuery (Query *)
virtual VisitorState visitSet (Set *)
virtual VisitorState visitInterRow (InterRow *)
 MysqlConnection (std::ostream *out=0, std::ostream *errOut=0)
virtual ~MysqlConnection ()
virtual bool open (const std::string &host, const std::string &userid, const std::string &password, const std::string &dbName)
virtual bool open (const std::string &parms)
virtual bool close ()
virtual bool isConnected ()
 Return true iff open has been done with no matching close.
std::ostream * getOut () const
std::ostream * getErrOut () const
virtual MATCH matchSchema (Rdb *rdb, bool matchDbName=true)
virtual bool insertRow (const std::string &tableName, const StringVector &colNames, const StringVector &values, int *auto_value=0, const StringVector *nullCols=0)
virtual unsigned int update (const std::string &tableName, const StringVector &colNames, const StringVector &values, const Assertion *where=0, const StringVector *nullCols=0)
virtual ResultHandleselect (const std::string &tableName, const StringVector &getCols, const StringVector &orderCols, const Assertion *where=0, int rowLimit=0, int rowOffset=0)
virtual ResultHandledbRequest (const std::string &request)
virtual bool compileAssertion (const Assertion *a, std::string &sqlString) const
virtual void disableModify (bool disable)
virtual Visitor::VisitorState visitRdb (Rdb *)
 This method says if the visitor is recursive or not.
virtual Visitor::VisitorState visitTable (Table *)
virtual Visitor::VisitorState visitColumn (Column *)
virtual Visitor::VisitorState visitIndex (Index *)
virtual Visitor::VisitorState visitAssertion (Assertion *)
virtual VisitorState visitInsertNew (InsertNew *)
virtual VisitorState visitSupersede (Supersede *)
virtual VisitorState visitQuery (Query *)
virtual VisitorState visitSet (Set *)
virtual VisitorState visitInterRow (InterRow *)
 MysqlConnection (std::ostream *out=0, std::ostream *errOut=0)
virtual ~MysqlConnection ()
virtual bool open (const std::string &host, const std::string &userid, const std::string &password, const std::string &dbName)
virtual bool open (const std::string &parms)
virtual bool close ()
virtual bool isConnected ()
 Return true iff open has been done with no matching close.
std::ostream * getOut () const
std::ostream * getErrOut () const
virtual MATCH matchSchema (Rdb *rdb, bool matchDbName=true)
virtual bool insertRow (const std::string &tableName, const StringVector &colNames, const StringVector &values, int *auto_value=0, const StringVector *nullCols=0)
virtual unsigned int update (const std::string &tableName, const StringVector &colNames, const StringVector &values, const Assertion *where=0, const StringVector *nullCols=0)
virtual ResultHandleselect (const std::string &tableName, const StringVector &getCols, const StringVector &orderCols, const Assertion *where=0, int rowLimit=0, int rowOffset=0)
virtual ResultHandledbRequest (const std::string &request)
virtual bool compileAssertion (const Assertion *a, std::string &sqlString) const
virtual void disableModify (bool disable)
virtual Visitor::VisitorState visitRdb (Rdb *)
 This method says if the visitor is recursive or not.
virtual Visitor::VisitorState visitTable (Table *)
virtual Visitor::VisitorState visitColumn (Column *)
virtual Visitor::VisitorState visitIndex (Index *)
virtual Visitor::VisitorState visitAssertion (Assertion *)
virtual VisitorState visitInsertNew (InsertNew *)
virtual VisitorState visitSupersede (Supersede *)
virtual VisitorState visitQuery (Query *)
virtual VisitorState visitSet (Set *)
virtual VisitorState visitInterRow (InterRow *)
Public Member Functions inherited from rdbModel::Connection
 Connection ()
virtual ~Connection ()
 Connection ()
virtual ~Connection ()
 Connection ()
virtual ~Connection ()

Additional Inherited Members

Public Types inherited from rdbModel::Visitor
enum  VisitorState {
  VCONTINUE = 0 , VDONE , VBRANCHDONE , VERROR ,
  VERRORABORT , VCONTINUE = 0 , VDONE , VBRANCHDONE ,
  VERROR , VERRORABORT , VCONTINUE = 0 , VDONE ,
  VBRANCHDONE , VERROR , VERRORABORT
}
enum  VisitorState {
  VCONTINUE = 0 , VDONE , VBRANCHDONE , VERROR ,
  VERRORABORT , VCONTINUE = 0 , VDONE , VBRANCHDONE ,
  VERROR , VERRORABORT , VCONTINUE = 0 , VDONE ,
  VBRANCHDONE , VERROR , VERRORABORT
}
enum  VisitorState {
  VCONTINUE = 0 , VDONE , VBRANCHDONE , VERROR ,
  VERRORABORT , VCONTINUE = 0 , VDONE , VBRANCHDONE ,
  VERROR , VERRORABORT , VCONTINUE = 0 , VDONE ,
  VBRANCHDONE , VERROR , VERRORABORT
}

Detailed Description

Class to handle connection to a MySQL database

  • initiate connection
  • make queries, including making returned data from queries available
  • issue SQL statements such as INSERT and UPDATE which have no returned data other than status
  • close down connection. Someday it might also have a method to create a database

Initial design will just use host, password, userid passed in. Will be up to caller to insure that the userid has the right privilages for the operations caller intends to do.

Definition at line 33 of file Calibration/rdbModel/include/rdbModel/Db/MysqlConnection.h.

Constructor & Destructor Documentation

◆ MysqlConnection() [1/3]

rdbModel::MysqlConnection::MysqlConnection ( std::ostream * out = 0,
std::ostream * errOut = 0 )

Open a connection Allowed operations will depend on userid, etc., specified return true if successful

Definition at line 69 of file MysqlConnection.cxx.

70 : m_mysql( 0 )
71 , m_connected( 0 )
72 , m_out( out )
73 , m_err( errOut )
74 , m_visitorType( VISITORundefined )
75 , m_rdb( 0 )
76 , m_tempRes( 0 )
77 , m_writeDisabled( false ) {
78 if ( m_out == 0 ) m_out = &std::cout;
79 if ( m_err == 0 ) m_err = &std::cerr;
80 }
*******DOUBLE PRECISION m_EGridB INTEGER m_out
Definition BStra.h:10

◆ ~MysqlConnection() [1/3]

rdbModel::MysqlConnection::~MysqlConnection ( )
virtual

Definition at line 95 of file MysqlConnection.cxx.

95 {
96 close();
97 delete m_mysql;
98 return;
99 }

◆ MysqlConnection() [2/3]

rdbModel::MysqlConnection::MysqlConnection ( std::ostream * out = 0,
std::ostream * errOut = 0 )

Open a connection Allowed operations will depend on userid, etc., specified return true if successful

◆ ~MysqlConnection() [2/3]

virtual rdbModel::MysqlConnection::~MysqlConnection ( )
virtual

◆ MysqlConnection() [3/3]

rdbModel::MysqlConnection::MysqlConnection ( std::ostream * out = 0,
std::ostream * errOut = 0 )

Open a connection Allowed operations will depend on userid, etc., specified return true if successful

◆ ~MysqlConnection() [3/3]

virtual rdbModel::MysqlConnection::~MysqlConnection ( )
virtual

Member Function Documentation

◆ close() [1/3]

bool rdbModel::MysqlConnection::close ( )
virtual

Close the current open connection , if any. Return true if there was a connection to close and it was closed successfully

Implements rdbModel::Connection.

Definition at line 82 of file MysqlConnection.cxx.

82 {
83 if ( m_tempRes )
84 {
85 mysql_free_result( m_tempRes );
86 m_tempRes = 0;
87 }
88 std::cout << "close connection ================================" << std::endl;
89 mysql_close( m_mysql );
90 m_mysql = 0;
91 m_connected = false;
92 return true;
93 }

Referenced by main(), and ~MysqlConnection().

◆ close() [2/3]

virtual bool rdbModel::MysqlConnection::close ( )
virtual

Close the current open connection , if any. Return true if there was a connection to close and it was closed successfully

Implements rdbModel::Connection.

◆ close() [3/3]

virtual bool rdbModel::MysqlConnection::close ( )
virtual

Close the current open connection , if any. Return true if there was a connection to close and it was closed successfully

Implements rdbModel::Connection.

◆ compileAssertion() [1/3]

bool rdbModel::MysqlConnection::compileAssertion ( const Assertion * a,
std::string & sqlString ) const
virtual

compile method for assertions. Use it internally, but also make it publicly available so assertions belonging to a table can save the compiled version.

Implements rdbModel::Connection.

Definition at line 458 of file MysqlConnection.cxx.

458 {
459 if ( !m_compileInit )
460 {
461 compileInit();
462 m_compileInit = true;
463 }
464 try
465 { return compileOperator( a->getOperator(), sqlString ); } catch ( RdbException ex )
466 {
467 ( *m_out ) << std::endl << ex.getMsg() << std::endl;
468 m_out->flush();
469 return false;
470 }
471 }

Referenced by select(), and update().

◆ compileAssertion() [2/3]

virtual bool rdbModel::MysqlConnection::compileAssertion ( const Assertion * a,
std::string & sqlString ) const
virtual

compile method for assertions. Use it internally, but also make it publicly available so assertions belonging to a table can save the compiled version.

Implements rdbModel::Connection.

◆ compileAssertion() [3/3]

virtual bool rdbModel::MysqlConnection::compileAssertion ( const Assertion * a,
std::string & sqlString ) const
virtual

compile method for assertions. Use it internally, but also make it publicly available so assertions belonging to a table can save the compiled version.

Implements rdbModel::Connection.

◆ dbRequest() [1/3]

ResultHandle * rdbModel::MysqlConnection::dbRequest ( const std::string & request)
virtual

Transmit raw request of any form to our other end. If it is a request that returns results, those results will be stored in a newly-allocated ResultHandle and dbRequest will return a pointer to it. Otherwise dbRequest will return a null pointer. Throw an exception if request fails for any reason.

Implements rdbModel::Connection.

Definition at line 401 of file MysqlConnection.cxx.

401 {
402
403 ( *m_out ) << std::endl << "# About to issue SQL request:" << std::endl;
404 ( *m_out ) << request << std::endl;
405 m_out->flush();
406
407 int i = 0;
408 int mysqlRet = mysql_query( m_mysql, request.c_str() );
409 for ( i = 0; i < 10; i++ )
410 {
411 // int mysqlRet = mysql_query(m_mysql, request.c_str());
412 if ( mysqlRet )
413 {
414 // not connected
415 std::string msg = "rdbModel::MysqlConnection::dbRequest: mysql_query error, code ";
416 std::string codeString;
417 facilities::Util::itoa( mysqlRet, codeString );
418 msg += codeString;
419 ( *m_out ) << std::endl << i << "times not connected++++ " << msg << std::endl;
420 m_out->flush();
421 fprintf( stderr, "mysql_query error %d: %s\n", mysql_errno( m_mysql ),
422 mysql_error( m_mysql ) );
423 if ( i >= 9 )
424 {
425 throw RdbException( msg, mysqlRet );
426 return 0;
427 }
428 mysql_close( m_mysql );
429 m_mysql = 0;
430 sleep( 100 );
431 bool st = open( m_host, m_user, m_password, m_dbName );
432 if ( st == false ) continue;
433 mysqlRet = mysql_query( m_mysql, request.c_str() );
434 }
435 else { break; }
436 }
437
438 MYSQL_RES* myres = mysql_store_result( m_mysql );
439 if ( !myres )
440 {
441 // Was it supposed to return data?
442 if ( mysql_field_count( m_mysql ) == 0 )
443 { // no data expected
444 return 0;
445 }
446 else
447 {
448 std::string msg = "rdbModel::MysqlConnection::dbRequest: expected data; none returned";
449 ( *m_out ) << std::endl << msg << std::endl;
450 m_out->flush();
451 throw RdbException( msg );
452 return 0;
453 }
454 }
455 return new MysqlResults( myres );
456 }
static const char * itoa(int val, std::string &outStr)
virtual bool open(const std::string &host, const std::string &userid, const std::string &password, const std::string &dbName)

Referenced by main().

◆ dbRequest() [2/3]

virtual ResultHandle * rdbModel::MysqlConnection::dbRequest ( const std::string & request)
virtual

Transmit raw request of any form to our other end. If it is a request that returns results, those results will be stored in a newly-allocated ResultHandle and dbRequest will return a pointer to it. Otherwise dbRequest will return a null pointer. Throw an exception if request fails for any reason.

Implements rdbModel::Connection.

◆ dbRequest() [3/3]

virtual ResultHandle * rdbModel::MysqlConnection::dbRequest ( const std::string & request)
virtual

Transmit raw request of any form to our other end. If it is a request that returns results, those results will be stored in a newly-allocated ResultHandle and dbRequest will return a pointer to it. Otherwise dbRequest will return a null pointer. Throw an exception if request fails for any reason.

Implements rdbModel::Connection.

◆ disableModify() [1/3]

virtual void rdbModel::MysqlConnection::disableModify ( bool disable)
inlinevirtual

Turn select and update into no-ops: output SQL string for debugging but don't change db

Implements rdbModel::Connection.

Definition at line 128 of file Calibration/rdbModel/include/rdbModel/Db/MysqlConnection.h.

128{ m_writeDisabled = disable; }

Referenced by main().

◆ disableModify() [2/3]

virtual void rdbModel::MysqlConnection::disableModify ( bool disable)
inlinevirtual

Turn select and update into no-ops: output SQL string for debugging but don't change db

Implements rdbModel::Connection.

Definition at line 128 of file InstallArea/x86_64-el9-gcc13-dbg/include/rdbModel/Db/MysqlConnection.h.

128{ m_writeDisabled = disable; }

◆ disableModify() [3/3]

virtual void rdbModel::MysqlConnection::disableModify ( bool disable)
inlinevirtual

Turn select and update into no-ops: output SQL string for debugging but don't change db

Implements rdbModel::Connection.

Definition at line 128 of file InstallArea/x86_64-el9-gcc13-opt/include/rdbModel/Db/MysqlConnection.h.

128{ m_writeDisabled = disable; }

◆ getErrOut() [1/3]

std::ostream * rdbModel::MysqlConnection::getErrOut ( ) const
inlinevirtual

Implements rdbModel::Connection.

Definition at line 57 of file Calibration/rdbModel/include/rdbModel/Db/MysqlConnection.h.

57{ return m_err; }

◆ getErrOut() [2/3]

std::ostream * rdbModel::MysqlConnection::getErrOut ( ) const
inlinevirtual

Implements rdbModel::Connection.

Definition at line 57 of file InstallArea/x86_64-el9-gcc13-dbg/include/rdbModel/Db/MysqlConnection.h.

57{ return m_err; }

◆ getErrOut() [3/3]

std::ostream * rdbModel::MysqlConnection::getErrOut ( ) const
inlinevirtual

Implements rdbModel::Connection.

Definition at line 57 of file InstallArea/x86_64-el9-gcc13-opt/include/rdbModel/Db/MysqlConnection.h.

57{ return m_err; }

◆ getOut() [1/3]

std::ostream * rdbModel::MysqlConnection::getOut ( ) const
inlinevirtual

Implements rdbModel::Connection.

Definition at line 56 of file Calibration/rdbModel/include/rdbModel/Db/MysqlConnection.h.

56{ return m_out; }

◆ getOut() [2/3]

std::ostream * rdbModel::MysqlConnection::getOut ( ) const
inlinevirtual

Implements rdbModel::Connection.

Definition at line 56 of file InstallArea/x86_64-el9-gcc13-dbg/include/rdbModel/Db/MysqlConnection.h.

56{ return m_out; }

◆ getOut() [3/3]

std::ostream * rdbModel::MysqlConnection::getOut ( ) const
inlinevirtual

Implements rdbModel::Connection.

Definition at line 56 of file InstallArea/x86_64-el9-gcc13-opt/include/rdbModel/Db/MysqlConnection.h.

56{ return m_out; }

◆ insertRow() [1/3]

bool rdbModel::MysqlConnection::insertRow ( const std::string & tableName,
const StringVector & colNames,
const StringVector & values,
int * auto_value = 0,
const StringVector * nullCols = 0 )
virtual

Typical derived class will form a syntactically correct INSERT statement from the input arguments and issue it to the dbms. Return true if row was inserted successfully If auto_value is non-zero and the table has an auto-increment column, its value will be returned. If nullCols is non-zero, insertRow will treat each string in the vector as a column name whose value should be set to NULL

Might also want to add a routine for INSERT ... SELECT

Implements rdbModel::Connection.

Definition at line 211 of file MysqlConnection.cxx.

213 {
214 std::string ins;
215 if ( auto_value ) *auto_value = 0;
216
217 // check that sizes of vectors match
218 unsigned nCol = colNames.size();
219 if ( !nCol || ( nCol != values.size() ) )
220 {
221 ( *m_err ) << " MysqlConnection::insertRow: vector lengths incompatible" << std::endl;
222 m_err->flush();
223 return false;
224 }
225
226 // caller should already have checked for validity and should
227 // have supplied all necessary columns
228
229 ins += "insert into " + tableName;
230 ins += " set " + colNames[0] + "='" + values[0] + "' ";
231 for ( unsigned iCol = 1; iCol < nCol; iCol++ )
232 { ins += ", " + colNames[iCol] + "='" + values[iCol] + "' "; }
233 if ( nullCols )
234 {
235 if ( nullCols->size() > 0 )
236 {
237 unsigned nNull = nullCols->size();
238 for ( unsigned iNull = 0; iNull < nNull; iNull++ )
239 { ins += ", " + ( *nullCols )[iNull] + "= NULL "; }
240 }
241 }
242
243 ( *m_out ) << std::endl << "# INSERT string is:" << std::endl;
244 ( *m_out ) << ins << std::endl;
245 m_out->flush();
246
247 if ( m_writeDisabled )
248 {
249 ( *m_out ) << "write to Db previously disabled; INSERT not sent" << std::endl;
250 m_out->flush();
251 return true;
252 }
253
254 int mysqlRet = mysql_query( m_mysql, ins.c_str() );
255
256 if ( mysqlRet )
257 {
258 ( *m_out ) << "MySQL error during INSERT, code " << mysqlRet << std::endl;
259 m_out->flush();
260 return false;
261 }
262 if ( auto_value ) { *auto_value = mysql_insert_id( m_mysql ); }
263 return true;
264 }

◆ insertRow() [2/3]

virtual bool rdbModel::MysqlConnection::insertRow ( const std::string & tableName,
const StringVector & colNames,
const StringVector & values,
int * auto_value = 0,
const StringVector * nullCols = 0 )
virtual

Typical derived class will form a syntactically correct INSERT statement from the input arguments and issue it to the dbms. Return true if row was inserted successfully If auto_value is non-zero and the table has an auto-increment column, its value will be returned. If nullCols is non-zero, insertRow will treat each string in the vector as a column name whose value should be set to NULL

Might also want to add a routine for INSERT ... SELECT

Implements rdbModel::Connection.

◆ insertRow() [3/3]

virtual bool rdbModel::MysqlConnection::insertRow ( const std::string & tableName,
const StringVector & colNames,
const StringVector & values,
int * auto_value = 0,
const StringVector * nullCols = 0 )
virtual

Typical derived class will form a syntactically correct INSERT statement from the input arguments and issue it to the dbms. Return true if row was inserted successfully If auto_value is non-zero and the table has an auto-increment column, its value will be returned. If nullCols is non-zero, insertRow will treat each string in the vector as a column name whose value should be set to NULL

Might also want to add a routine for INSERT ... SELECT

Implements rdbModel::Connection.

◆ isConnected() [1/3]

virtual bool rdbModel::MysqlConnection::isConnected ( )
inlinevirtual

Return true iff open has been done with no matching close.

Implements rdbModel::Connection.

Definition at line 54 of file Calibration/rdbModel/include/rdbModel/Db/MysqlConnection.h.

54{ return m_connected; }

◆ isConnected() [2/3]

virtual bool rdbModel::MysqlConnection::isConnected ( )
inlinevirtual

Return true iff open has been done with no matching close.

Implements rdbModel::Connection.

Definition at line 54 of file InstallArea/x86_64-el9-gcc13-dbg/include/rdbModel/Db/MysqlConnection.h.

54{ return m_connected; }

◆ isConnected() [3/3]

virtual bool rdbModel::MysqlConnection::isConnected ( )
inlinevirtual

Return true iff open has been done with no matching close.

Implements rdbModel::Connection.

Definition at line 54 of file InstallArea/x86_64-el9-gcc13-opt/include/rdbModel/Db/MysqlConnection.h.

54{ return m_connected; }

◆ matchSchema() [1/3]

MATCH rdbModel::MysqlConnection::matchSchema ( Rdb * rdb,
bool matchDbName = true )
virtual

Check to what degree local schema definition is compatible with remote db accessed via this connection. By default check db names match, but this may be disabled.

If match is successful, may also want to cache information about some things; for example, rows for which agent = "service"

Implements rdbModel::Connection.

Definition at line 191 of file MysqlConnection.cxx.

191 {
192 if ( !m_connected ) return MATCHnoConnection;
193
194 m_matchDbName = matchDbName;
195
196 // Check global characteristics;
197 // Could do this via Manager; seems a bit artificial, bypass for now
198 m_visitorType = VISITORmatch;
199 m_matchReturn = MATCHequivalent;
200 unsigned int ret = rdb->accept( this );
201
202 if ( ( ret == Visitor::VERROR ) || ( ret == Visitor::VERRORABORT ) ) { return MATCHfail; }
203 else return m_matchReturn;
204 }

Referenced by main().

◆ matchSchema() [2/3]

virtual MATCH rdbModel::MysqlConnection::matchSchema ( Rdb * rdb,
bool matchDbName = true )
virtual

Check to what degree local schema definition is compatible with remote db accessed via this connection. By default check db names match, but this may be disabled.

If match is successful, may also want to cache information about some things; for example, rows for which agent = "service"

Implements rdbModel::Connection.

◆ matchSchema() [3/3]

virtual MATCH rdbModel::MysqlConnection::matchSchema ( Rdb * rdb,
bool matchDbName = true )
virtual

Check to what degree local schema definition is compatible with remote db accessed via this connection. By default check db names match, but this may be disabled.

If match is successful, may also want to cache information about some things; for example, rows for which agent = "service"

Implements rdbModel::Connection.

◆ open() [1/6]

bool rdbModel::MysqlConnection::open ( const std::string & host,
const std::string & userid,
const std::string & password,
const std::string & dbName )
virtual

Implements rdbModel::Connection.

Definition at line 101 of file MysqlConnection.cxx.

102 {
103 // , unsigned int port) {
104 if ( dbName.size() == 0 )
105 {
106 ( *m_err ) << "rdbModel::MysqlConnection::open : null db name not allowed!" << std::endl;
107 m_err->flush();
108 return false;
109 }
110 // huangb add
111 m_host = host;
112 m_user = user;
113 m_password = password;
114
115 m_mysql = new MYSQL;
116 mysql_init( m_mysql );
117
118 // 'host' argument is of the form hostname[:port]
119 // That is, port section is optional. If not supplied, use
120 // default port.
121 std::string hostOnly;
122 int port = 0;
123 std::string::size_type colonLoc = host.find( ":" );
124 if ( colonLoc == std::string::npos ) { hostOnly = host; }
125 else
126 {
127 hostOnly = host.substr( 0, colonLoc );
128 std::string portString = host.substr( colonLoc + 1 );
129 try
130 {
131 port = facilities::Util::stringToInt( portString );
132 } catch ( facilities::WrongType ex )
133 {
134 ( *m_err ) << "From MysqlConnection::connect. Bad port: " << ex.getMsg() << std::endl;
135 m_err->flush();
136 return false;
137 }
138 }
139 // mysql_init(m_mysql);
140 std::cout << "host is:" << hostOnly.c_str() << "::use is:: " << user.c_str()
141 << ":pass is::" << password.c_str() << ":db is::" << dbName.c_str() << std::endl;
142 MYSQL* connected = mysql_real_connect( m_mysql, hostOnly.c_str(), user.c_str(),
143 password.c_str(), dbName.c_str(), 3306, NULL, 0 );
144
145 if ( connected != 0 )
146 { // Everything is fine. Put out an info message
147 ( *m_out ) << "Successfully connected to MySQL host " << host << ", database " << dbName
148 << std::endl;
149 m_out->flush();
150 m_connected = true;
151 m_dbName = dbName;
152 }
153 else
154 {
155 ( *m_err ) << "Failed to connect to MySQL host " << host
156 << "with error:::: " << mysql_error( m_mysql ) << std::endl;
157 m_err->flush();
158 m_connected = false;
159 }
160 return m_connected;
161 }
static int stringToInt(const std::string &InStr)

Referenced by dbRequest(), main(), and open().

◆ open() [2/6]

virtual bool rdbModel::MysqlConnection::open ( const std::string & host,
const std::string & userid,
const std::string & password,
const std::string & dbName )
virtual

Implements rdbModel::Connection.

◆ open() [3/6]

virtual bool rdbModel::MysqlConnection::open ( const std::string & host,
const std::string & userid,
const std::string & password,
const std::string & dbName )
virtual

Implements rdbModel::Connection.

◆ open() [4/6]

bool rdbModel::MysqlConnection::open ( const std::string & parms)
virtual

Parameter is normally path for an xml file descrbing the connection parameters

Implements rdbModel::Connection.

Definition at line 163 of file MysqlConnection.cxx.

163 {
164 using XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument;
165 using XERCES_CPP_NAMESPACE_QUALIFIER DOMElement;
166 xmlBase::XmlParser parser;
167 DOMDocument* doc = parser.parse( parms.c_str(), "mysqlConnection" );
168 if ( doc == 0 )
169 {
170 ( *m_err ) << "parse of connection parameters failed" << std::endl;
171 m_err->flush();
172 return false;
173 }
174 DOMElement* conn = doc->getDocumentElement();
175
176 std::string host = xmlBase::Dom::getAttribute( conn, "host" );
177 std::string user = xmlBase::Dom::getAttribute( conn, "user" );
178 std::string password = xmlBase::Dom::getAttribute( conn, "password" );
179 std::string dbname = xmlBase::Dom::getAttribute( conn, "dbname" );
180
181 if ( password.size() == 0 )
182 { // prompt for password?
183 ( *m_out ) << "interactive login NYI " << std::endl;
184 m_out->flush();
185 return false;
186 }
187
188 return this->open( host, user, password, dbname );
189 }
static std::string getAttribute(const DOMElement *elt, const char *attName)
Definition Dom.cxx:199
DOMDocument * parse(const char *const filename, const std::string &docType=std::string(""))
Parse an xml file, returning document node if successful.

◆ open() [5/6]

virtual bool rdbModel::MysqlConnection::open ( const std::string & parms)
virtual

Parameter is normally path for an xml file descrbing the connection parameters

Implements rdbModel::Connection.

◆ open() [6/6]

virtual bool rdbModel::MysqlConnection::open ( const std::string & parms)
virtual

Parameter is normally path for an xml file descrbing the connection parameters

Implements rdbModel::Connection.

◆ select() [1/3]

ResultHandle * rdbModel::MysqlConnection::select ( const std::string & tableName,
const StringVector & getCols,
const StringVector & orderCols,
const Assertion * where = 0,
int rowLimit = 0,
int rowOffset = 0 )
virtual

Support only for relatively simple SELECT, involving just one table.

Parameters
tableName
getColsvector of columns to be retrieved
whereptr to an Assertion object
rowLimitmax number of rows to return
rowOffsetoffset for first row returned among those satisfying conditions; ignored if 0.
Returns
If the SELECT succeeds, a pointer to an object which manages the returned data; else 0. Caller is responsible for deleting the ResultHandle object.

Implements rdbModel::Connection.

Definition at line 320 of file MysqlConnection.cxx.

323 {
324 std::string sqlString = "SELECT ";
325 unsigned nGet = getCols.size();
326 unsigned nOrder = orderCols.size();
327
328 std::cout << "tableName is:" << tableName << std::endl;
329
330 // tableName="metadata_v2r1";
331
332 sqlString += getCols[0];
333 for ( unsigned iGet = 1; iGet < nGet; iGet++ )
334 {
335 sqlString += ",";
336 sqlString += getCols[iGet];
337 }
338 sqlString += " FROM " + tableName + " ";
339 std::cout << "sqlString is11:" << sqlString << std::endl;
340 if ( where != 0 )
341 {
342 sqlString += " WHERE ";
343 bool ret = compileAssertion( where, sqlString );
344 if ( !ret ) return 0;
345 }
346
347 std::cout << "sqlString is22:" << sqlString << std::endl;
348
349 /*maqm if (nOrder > 0 ) {
350 sqlString += " ORDER BY " + orderCols[0];
351 for (unsigned iOrder = 1; iOrder < nOrder; iOrder++) {
352 sqlString += ",";
353 sqlString += orderCols[iOrder];
354 }
355 }
356 */
357 // sqlString ="SELECT ser_no FROM metadata_v2r1 WHERE ((completion='OK') AND
358 // (instrument='LAT') AND (calib_type='MDC_t0') AND (flavor='vanilla'))";
359 if ( rowLimit > 0 )
360 {
361 // SQL format is LIMIT offset,limit or
362 // LIMIT limit or
363 // LIMIT limit OFFSET offset [don't use this one]
364 sqlString += " LIMIT ";
365 std::string limitStr;
366 if ( rowOffset > 0 )
367 {
368 facilities::Util::itoa( rowOffset, limitStr );
369 sqlString += limitStr + ",";
370 }
371 limitStr.clear();
372 facilities::Util::itoa( rowLimit, limitStr );
373 std::cout << "limitStr is:" << limitStr << std::endl;
374 sqlString += limitStr;
375 }
376
377 ( *m_out ) << std::endl << " # About to issue SELECT:" << std::endl;
378 ( *m_out ) << sqlString << std::endl;
379 m_out->flush();
380 // maqm add
381 // sqlString="SELECT ser_no FROM metadata_v2r1";
382
383 int mysqlRet = mysql_query( m_mysql, sqlString.c_str() );
384 if ( mysqlRet )
385 {
386 std::string msg = "rdbModel::MysqlConnection::select: mysql_query error, code ";
387 std::string codeString;
388 facilities::Util::itoa( mysqlRet, codeString );
389 msg += codeString;
390 ( *m_out ) << std::endl << msg << std::endl;
391 m_out->flush();
392 throw RdbException( msg, mysqlRet );
393 return 0;
394 }
395
396 MYSQL_RES* myres = mysql_store_result( m_mysql );
397 MysqlResults* results = new MysqlResults( myres );
398 return results;
399 }
virtual bool compileAssertion(const Assertion *a, std::string &sqlString) const

◆ select() [2/3]

virtual ResultHandle * rdbModel::MysqlConnection::select ( const std::string & tableName,
const StringVector & getCols,
const StringVector & orderCols,
const Assertion * where = 0,
int rowLimit = 0,
int rowOffset = 0 )
virtual

Support only for relatively simple SELECT, involving just one table.

Parameters
tableName
getColsvector of columns to be retrieved
whereptr to an Assertion object
rowLimitmax number of rows to return
rowOffsetoffset for first row returned among those satisfying conditions; ignored if 0.
Returns
If the SELECT succeeds, a pointer to an object which manages the returned data; else 0. Caller is responsible for deleting the ResultHandle object.

Implements rdbModel::Connection.

◆ select() [3/3]

virtual ResultHandle * rdbModel::MysqlConnection::select ( const std::string & tableName,
const StringVector & getCols,
const StringVector & orderCols,
const Assertion * where = 0,
int rowLimit = 0,
int rowOffset = 0 )
virtual

Support only for relatively simple SELECT, involving just one table.

Parameters
tableName
getColsvector of columns to be retrieved
whereptr to an Assertion object
rowLimitmax number of rows to return
rowOffsetoffset for first row returned among those satisfying conditions; ignored if 0.
Returns
If the SELECT succeeds, a pointer to an object which manages the returned data; else 0. Caller is responsible for deleting the ResultHandle object.

Implements rdbModel::Connection.

◆ update() [1/3]

unsigned int rdbModel::MysqlConnection::update ( const std::string & tableName,
const StringVector & colNames,
const StringVector & values,
const Assertion * where = 0,
const StringVector * nullCols = 0 )
virtual

Generic UPDATE. Return value is number of rows changed.

Implements rdbModel::Connection.

Definition at line 266 of file MysqlConnection.cxx.

269 {
270
271 unsigned int nCol = colNames.size();
272 if ( nCol != values.size() )
273 {
274 ( *m_err ) << "rdbModel::mysqlConnection::update: ";
275 ( *m_err ) << "Incompatible vector arguments " << std::endl;
276 m_err->flush();
277 return 0;
278 }
279 std::string sqlString = "UPDATE " + tableName + " SET ";
280 sqlString += colNames[0] + " = '" + values[0] + "'";
281 for ( unsigned int iCol = 1; iCol < nCol; iCol++ )
282 { sqlString += "," + colNames[iCol] + " = '" + values[iCol] + "'"; }
283 if ( nullCols )
284 {
285 unsigned nNull = nullCols->size();
286 for ( unsigned iNull = 0; iNull < nNull; iNull++ )
287 { sqlString += ", " + ( *nullCols )[iNull] + "= NULL "; }
288 }
289
290 if ( where )
291 {
292 sqlString += " WHERE ";
293 bool ret = compileAssertion( where, sqlString );
294 if ( !ret ) return 0;
295 }
296 ( *m_out ) << std::endl << "# UPDATE to be issued:" << std::endl;
297 ( *m_out ) << sqlString << std::endl;
298 m_out->flush();
299 if ( m_writeDisabled )
300 {
301 ( *m_out ) << "write to Db previously disabled; UPDATE not sent" << std::endl;
302 m_out->flush();
303 return 0;
304 }
305 int mysqlRet = mysql_query( m_mysql, sqlString.c_str() );
306
307 if ( mysqlRet )
308 {
309 ( *m_out ) << "rdbModel::MysqlConnection::update: ";
310 ( *m_out ) << "MySQL error during UPDATE, code " << mysqlRet << std::endl;
311 m_out->flush();
312 return 0;
313 }
314 my_ulonglong nModLong = mysql_affected_rows( m_mysql );
315 // Not much chance that we'll change more rows than will fit in just long
316 unsigned nMod = nModLong;
317 return nMod;
318 }

◆ update() [2/3]

virtual unsigned int rdbModel::MysqlConnection::update ( const std::string & tableName,
const StringVector & colNames,
const StringVector & values,
const Assertion * where = 0,
const StringVector * nullCols = 0 )
virtual

Generic UPDATE. Return value is number of rows changed.

Implements rdbModel::Connection.

◆ update() [3/3]

virtual unsigned int rdbModel::MysqlConnection::update ( const std::string & tableName,
const StringVector & colNames,
const StringVector & values,
const Assertion * where = 0,
const StringVector * nullCols = 0 )
virtual

Generic UPDATE. Return value is number of rows changed.

Implements rdbModel::Connection.

◆ visitAssertion() [1/3]

Visitor::VisitorState rdbModel::MysqlConnection::visitAssertion ( Assertion * )
virtual

◆ visitAssertion() [2/3]

virtual Visitor::VisitorState rdbModel::MysqlConnection::visitAssertion ( Assertion * )
virtual

Implements rdbModel::Visitor.

◆ visitAssertion() [3/3]

virtual Visitor::VisitorState rdbModel::MysqlConnection::visitAssertion ( Assertion * )
virtual

Implements rdbModel::Visitor.

◆ visitColumn() [1/3]

Visitor::VisitorState rdbModel::MysqlConnection::visitColumn ( Column * col)
virtual

Implements rdbModel::Visitor.

Definition at line 685 of file MysqlConnection.cxx.

685 {
686 std::string myName = col->getName();
687 if ( m_colIx.find( myName ) == m_colIx.end() )
688 {
689 m_matchReturn = MATCHfail;
691 }
692 unsigned int ix = m_colIx[myName];
693 mysql_data_seek( m_tempRes, ix );
694 MYSQL_ROW colDescrip = mysql_fetch_row( m_tempRes );
695
696 // Type
697 std::string sqlDtype = std::string( colDescrip[1] );
698 Datatype* dtype = col->getDatatype();
699 if ( !checkDType( dtype, sqlDtype ) )
700 {
701 m_matchReturn = MATCHfail;
702 ( *m_out ) << "Failed dtype match of col " << myName << std::endl;
704 }
705
706 // Null
707 bool nullable = ( std::string( colDescrip[2] ) == std::string( "YES" ) );
708 if ( nullable != col->nullAllowed() )
709 {
710 m_matchReturn = MATCHfail;
711 ( *m_out ) << "Failed null/not null match of col " << myName << std::endl;
713 }
714 // Key (PRI for primary, MUL if first in a multiple-field key
715 // Save primary key info, if any
716 if ( std::string( colDescrip[3] ) == std::string( "PRI" ) ) { m_primColName = myName; }
717
718 // Field 4 is default
719 // Extra (may say auto_increment)
720 bool autoInc = ( std::string( colDescrip[5] ) == std::string( "auto_increment" ) );
721 if ( autoInc != col->isAutoIncrement() )
722 {
723 m_matchReturn = MATCHfail;
724 ( *m_out ) << "Failed isAutoIncrement match of col " << myName << std::endl;
726 }
727 return Visitor::VCONTINUE;
728 }
#define ix(i)

◆ visitColumn() [2/3]

virtual Visitor::VisitorState rdbModel::MysqlConnection::visitColumn ( Column * )
virtual

Implements rdbModel::Visitor.

◆ visitColumn() [3/3]

virtual Visitor::VisitorState rdbModel::MysqlConnection::visitColumn ( Column * )
virtual

Implements rdbModel::Visitor.

◆ visitIndex() [1/3]

Visitor::VisitorState rdbModel::MysqlConnection::visitIndex ( Index * )
virtual

Implements rdbModel::Visitor.

Definition at line 833 of file MysqlConnection.cxx.

833 {
834 return Visitor::VCONTINUE;
835 // might put something real here later
836 }

◆ visitIndex() [2/3]

virtual Visitor::VisitorState rdbModel::MysqlConnection::visitIndex ( Index * )
virtual

Implements rdbModel::Visitor.

◆ visitIndex() [3/3]

virtual Visitor::VisitorState rdbModel::MysqlConnection::visitIndex ( Index * )
virtual

Implements rdbModel::Visitor.

◆ visitInsertNew() [1/3]

Visitor::VisitorState rdbModel::MysqlConnection::visitInsertNew ( InsertNew * )
virtual

Implements rdbModel::Visitor.

Definition at line 842 of file MysqlConnection.cxx.

842 {
843 return Visitor::VCONTINUE;
844 }

◆ visitInsertNew() [2/3]

virtual VisitorState rdbModel::MysqlConnection::visitInsertNew ( InsertNew * )
virtual

Implements rdbModel::Visitor.

◆ visitInsertNew() [3/3]

virtual VisitorState rdbModel::MysqlConnection::visitInsertNew ( InsertNew * )
virtual

Implements rdbModel::Visitor.

◆ visitInterRow() [1/3]

Visitor::VisitorState rdbModel::MysqlConnection::visitInterRow ( InterRow * )
virtual

Implements rdbModel::Visitor.

Definition at line 854 of file MysqlConnection.cxx.

854 {
855 return Visitor::VCONTINUE;
856 }

◆ visitInterRow() [2/3]

virtual VisitorState rdbModel::MysqlConnection::visitInterRow ( InterRow * )
virtual

Implements rdbModel::Visitor.

◆ visitInterRow() [3/3]

virtual VisitorState rdbModel::MysqlConnection::visitInterRow ( InterRow * )
virtual

Implements rdbModel::Visitor.

◆ visitQuery() [1/3]

Visitor::VisitorState rdbModel::MysqlConnection::visitQuery ( Query * )
virtual

Implements rdbModel::Visitor.

Definition at line 850 of file MysqlConnection.cxx.

850{ return Visitor::VCONTINUE; }

◆ visitQuery() [2/3]

virtual VisitorState rdbModel::MysqlConnection::visitQuery ( Query * )
virtual

Implements rdbModel::Visitor.

◆ visitQuery() [3/3]

virtual VisitorState rdbModel::MysqlConnection::visitQuery ( Query * )
virtual

Implements rdbModel::Visitor.

◆ visitRdb() [1/3]

Visitor::VisitorState rdbModel::MysqlConnection::visitRdb ( Rdb * )
virtual

This method says if the visitor is recursive or not.

This method sets if the visitor is recursive or not

Implements rdbModel::Visitor.

Definition at line 603 of file MysqlConnection.cxx.

603 {
604
605 if ( m_matchDbName )
606 {
607 if ( m_dbName != rdb->getDbName() )
608 {
609 m_matchReturn = MATCHfail;
610 return Visitor::VDONE;
611 }
612 }
613
614 unsigned int nLocal = rdb->getNTable();
615
616 // Null pointer for 2nd argument means "list all tables"
617
618 MYSQL_RES* res = mysql_list_tables( m_mysql, 0 );
619 if ( !res )
620 {
621 m_matchReturn = MATCHfail;
623 }
624 unsigned int nRemote = mysql_num_rows( res );
625 mysql_free_result( res );
626
627 if ( nRemote < nLocal )
628 {
629 m_matchReturn = MATCHfail;
630 return Visitor::VDONE;
631 }
632 else if ( nRemote > nLocal ) m_matchReturn = MATCHcompatible;
633
634 // Tell Rdb about this
635 rdb->setConnection( this );
636
637 return Visitor::VCONTINUE;
638 }

◆ visitRdb() [2/3]

virtual Visitor::VisitorState rdbModel::MysqlConnection::visitRdb ( Rdb * )
virtual

This method says if the visitor is recursive or not.

This method sets if the visitor is recursive or not

Implements rdbModel::Visitor.

◆ visitRdb() [3/3]

virtual Visitor::VisitorState rdbModel::MysqlConnection::visitRdb ( Rdb * )
virtual

This method says if the visitor is recursive or not.

This method sets if the visitor is recursive or not

Implements rdbModel::Visitor.

◆ visitSet() [1/3]

Visitor::VisitorState rdbModel::MysqlConnection::visitSet ( Set * )
virtual

Implements rdbModel::Visitor.

Definition at line 852 of file MysqlConnection.cxx.

852{ return Visitor::VCONTINUE; }

◆ visitSet() [2/3]

virtual VisitorState rdbModel::MysqlConnection::visitSet ( Set * )
virtual

Implements rdbModel::Visitor.

◆ visitSet() [3/3]

virtual VisitorState rdbModel::MysqlConnection::visitSet ( Set * )
virtual

Implements rdbModel::Visitor.

◆ visitSupersede() [1/3]

Visitor::VisitorState rdbModel::MysqlConnection::visitSupersede ( Supersede * )
virtual

Implements rdbModel::Visitor.

Definition at line 846 of file MysqlConnection.cxx.

846 {
847 return Visitor::VCONTINUE;
848 }

◆ visitSupersede() [2/3]

virtual VisitorState rdbModel::MysqlConnection::visitSupersede ( Supersede * )
virtual

Implements rdbModel::Visitor.

◆ visitSupersede() [3/3]

virtual VisitorState rdbModel::MysqlConnection::visitSupersede ( Supersede * )
virtual

Implements rdbModel::Visitor.

◆ visitTable() [1/3]

Visitor::VisitorState rdbModel::MysqlConnection::visitTable ( Table * table)
virtual

Implements rdbModel::Visitor.

Definition at line 640 of file MysqlConnection.cxx.

640 {
641 const std::string& tName = table->getName();
642
643 // Result set will have all fields for the table
644 if ( m_tempRes )
645 {
646 mysql_free_result( m_tempRes );
647 m_tempRes = 0;
648 }
649 m_primColName.clear();
650
651 std::string query = "SHOW COLUMNS FROM " + tName;
652
653 ( *m_out ) << std::endl << "# About to issue SHOW COLUMNS request :" << std::endl;
654 ( *m_out ) << query << std::endl;
655 m_out->flush();
656
657 int ret = mysql_query( m_mysql, query.c_str() );
658 if ( ret )
659 {
660 m_matchReturn = MATCHfail;
662 }
663
664 m_tempRes = mysql_store_result( m_mysql );
665 if ( !m_tempRes )
666 {
667 m_matchReturn = MATCHfail;
669 }
670 // Result set is a table with fields "Field"(the name) "Type" "Null"(yes
671 // or no) "Key" "Default", "Extra"
672 // Make it easier for accept(Column* ) to find relevant information
673 unsigned int nRow = mysql_num_rows( m_tempRes );
674 m_colIx.clear();
675 for ( unsigned iRow = 0; iRow < nRow; iRow++ )
676 {
677 MYSQL_ROW colDescrip = mysql_fetch_row( m_tempRes );
678 std::string name = std::string( colDescrip[0] );
679 std::cout << "name is:" << name << std::endl;
680 m_colIx[name] = iRow;
681 }
682 return Visitor::VCONTINUE;
683 }

◆ visitTable() [2/3]

virtual Visitor::VisitorState rdbModel::MysqlConnection::visitTable ( Table * )
virtual

Implements rdbModel::Visitor.

◆ visitTable() [3/3]

virtual Visitor::VisitorState rdbModel::MysqlConnection::visitTable ( Table * )
virtual

Implements rdbModel::Visitor.


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