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

Additional Inherited Members

- Public Types inherited from rdbModel::Visitor
enum  VisitorState {
  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
}
 

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 32 of file Calibration/rdbModel/rdbModel-00-01-01/rdbModel/Db/MysqlConnection.h.

Constructor & Destructor Documentation

◆ MysqlConnection() [1/2]

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 70 of file MysqlConnection.cxx.

71 :
72 m_mysql(0), m_connected(0), m_out(out), m_err(errOut),
73 m_visitorType(VISITORundefined), m_rdb(0), m_tempRes(0),
74 m_writeDisabled(false) {
75 if (m_out == 0) m_out = &std::cout;
76 if (m_err == 0) m_err = &std::cerr;
77 }

◆ ~MysqlConnection() [1/2]

rdbModel::MysqlConnection::~MysqlConnection ( )
virtual

Definition at line 91 of file MysqlConnection.cxx.

91 {
92 close();
93 delete m_mysql;
94 return;
95 }

◆ MysqlConnection() [2/2]

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/2]

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

Member Function Documentation

◆ close() [1/2]

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 79 of file MysqlConnection.cxx.

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

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

◆ close() [2/2]

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/2]

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 463 of file MysqlConnection.cxx.

464 {
465 if (!m_compileInit) {
466 compileInit();
467 m_compileInit = true;
468 }
469 try {
470 return compileOperator(a->getOperator(), sqlString);
471 }
472 catch (RdbException ex) {
473 (*m_out) << std::endl << ex.getMsg() << std::endl;
474 m_out->flush();
475 return false;
476 }
477 }

Referenced by select(), and update().

◆ compileAssertion() [2/2]

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/2]

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 409 of file MysqlConnection.cxx.

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

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/2]

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 137 of file Calibration/rdbModel/rdbModel-00-01-01/rdbModel/Db/MysqlConnection.h.

137{m_writeDisabled=disable;}

Referenced by main().

◆ disableModify() [2/2]

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 137 of file InstallArea/include/rdbModel/rdbModel/Db/MysqlConnection.h.

137{m_writeDisabled=disable;}

◆ getErrOut() [1/2]

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

Implements rdbModel::Connection.

Definition at line 57 of file Calibration/rdbModel/rdbModel-00-01-01/rdbModel/Db/MysqlConnection.h.

57{return m_err;}

◆ getErrOut() [2/2]

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

Implements rdbModel::Connection.

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

57{return m_err;}

◆ getOut() [1/2]

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

Implements rdbModel::Connection.

Definition at line 56 of file Calibration/rdbModel/rdbModel-00-01-01/rdbModel/Db/MysqlConnection.h.

56{return m_out;}

◆ getOut() [2/2]

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

Implements rdbModel::Connection.

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

56{return m_out;}

◆ insertRow() [1/2]

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 216 of file MysqlConnection.cxx.

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

◆ insertRow() [2/2]

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/2]

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/rdbModel-00-01-01/rdbModel/Db/MysqlConnection.h.

54{return m_connected;}

◆ isConnected() [2/2]

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/include/rdbModel/rdbModel/Db/MysqlConnection.h.

54{return m_connected;}

◆ matchSchema() [1/2]

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)) {
203 return MATCHfail;
204 }
205 else return m_matchReturn;
206 }

Referenced by main().

◆ matchSchema() [2/2]

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/4]

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 97 of file MysqlConnection.cxx.

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

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

◆ open() [2/4]

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/4]

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 (*m_err) << "parse of connection parameters failed" << std::endl;
170 m_err->flush();
171 return false;
172 }
173 DOMElement* conn = doc->getDocumentElement();
174
175 std::string host = xmlBase::Dom::getAttribute(conn, "host");
176 std::string user = xmlBase::Dom::getAttribute(conn, "user");
177 std::string password = xmlBase::Dom::getAttribute(conn, "password");
178 std::string dbname = xmlBase::Dom::getAttribute(conn, "dbname");
179
180 if (password.size() == 0 ) { // prompt for password?
181 (*m_out) << "interactive login NYI " << std::endl;
182 m_out->flush();
183 return false;
184 }
185
186 return this->open(host, user, password, dbname);
187 }
static std::string getAttribute(const DOMElement *elt, const char *attName)
Definition: Dom.cxx:222
DOMDocument * parse(const char *const filename, const std::string &docType=std::string(""))
Parse an xml file, returning document node if successful.
Definition: XmlParser.cxx:108

◆ open() [4/4]

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/2]

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 330 of file MysqlConnection.cxx.

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

◆ select() [2/2]

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/2]

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 275 of file MysqlConnection.cxx.

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

◆ update() [2/2]

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/2]

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

◆ visitAssertion() [2/2]

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

Implements rdbModel::Visitor.

◆ visitColumn() [1/2]

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

◆ visitColumn() [2/2]

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

Implements rdbModel::Visitor.

◆ visitIndex() [1/2]

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

Implements rdbModel::Visitor.

Definition at line 836 of file MysqlConnection.cxx.

836 {
837 return Visitor::VCONTINUE;
838 // might put something real here later
839 }

◆ visitIndex() [2/2]

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

Implements rdbModel::Visitor.

◆ visitInsertNew() [1/2]

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

Implements rdbModel::Visitor.

Definition at line 845 of file MysqlConnection.cxx.

845 {
846 return Visitor::VCONTINUE;
847 }

◆ visitInsertNew() [2/2]

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

Implements rdbModel::Visitor.

◆ visitInterRow() [1/2]

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

Implements rdbModel::Visitor.

Definition at line 861 of file MysqlConnection.cxx.

861 {
862 return Visitor::VCONTINUE;
863 }

◆ visitInterRow() [2/2]

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

Implements rdbModel::Visitor.

◆ visitQuery() [1/2]

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

Implements rdbModel::Visitor.

Definition at line 853 of file MysqlConnection.cxx.

853 {
854 return Visitor::VCONTINUE;
855 }

◆ visitQuery() [2/2]

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

Implements rdbModel::Visitor.

◆ visitRdb() [1/2]

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 609 of file MysqlConnection.cxx.

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

◆ visitRdb() [2/2]

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/2]

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

Implements rdbModel::Visitor.

Definition at line 857 of file MysqlConnection.cxx.

857 {
858 return Visitor::VCONTINUE;
859 }

◆ visitSet() [2/2]

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

Implements rdbModel::Visitor.

◆ visitSupersede() [1/2]

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

Implements rdbModel::Visitor.

Definition at line 849 of file MysqlConnection.cxx.

849 {
850 return Visitor::VCONTINUE;
851 }

◆ visitSupersede() [2/2]

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

Implements rdbModel::Visitor.

◆ visitTable() [1/2]

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

Implements rdbModel::Visitor.

Definition at line 642 of file MysqlConnection.cxx.

642 {
643 const std::string& tName = table->getName();
644
645 // Result set will have all fields for the table
646 if (m_tempRes) {
647 mysql_free_result(m_tempRes);
648 m_tempRes = 0;
649 }
650 m_primColName.clear();
651
652 std::string query = "SHOW COLUMNS FROM " + tName;
653
654 (*m_out) << std::endl << "# About to issue SHOW COLUMNS request :"
655 << std::endl;
656 (*m_out) << query << std::endl;
657 m_out->flush();
658
659 int ret = mysql_query(m_mysql, query.c_str());
660 if (ret) {
661 m_matchReturn = MATCHfail;
663 }
664
665 m_tempRes = mysql_store_result(m_mysql);
666 if (!m_tempRes) {
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 MYSQL_ROW colDescrip = mysql_fetch_row(m_tempRes);
677 std::string name = std::string(colDescrip[0]);
678 std::cout<<"name is:"<<name<<std::endl;
679 m_colIx[name] = iRow;
680 }
681 return Visitor::VCONTINUE;
682
683 }

◆ visitTable() [2/2]

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

Implements rdbModel::Visitor.


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