BOSS 6.6.4.p03
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 *)
 
- 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
 
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
}
 

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 MysqlConnection.h.

Constructor & Destructor Documentation

◆ MysqlConnection()

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 :
71 m_mysql(0), m_connected(0), m_out(out), m_err(errOut),
72 m_visitorType(VISITORundefined), m_rdb(0), m_tempRes(0),
73 m_writeDisabled(false) {
74 if (m_out == 0) m_out = &std::cout;
75 if (m_err == 0) m_err = &std::cerr;
76 }

◆ ~MysqlConnection()

rdbModel::MysqlConnection::~MysqlConnection ( )
virtual

Definition at line 90 of file MysqlConnection.cxx.

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

Member Function Documentation

◆ close()

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

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

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

◆ compileAssertion()

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

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

Referenced by select(), and update().

◆ dbRequest()

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

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

◆ disableModify()

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 MysqlConnection.h.

137{m_writeDisabled=disable;}

Referenced by main().

◆ getErrOut()

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

Implements rdbModel::Connection.

Definition at line 57 of file MysqlConnection.h.

57{return m_err;}

◆ getOut()

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

Implements rdbModel::Connection.

Definition at line 56 of file MysqlConnection.h.

56{return m_out;}

◆ insertRow()

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

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

◆ isConnected()

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 MysqlConnection.h.

54{return m_connected;}

◆ matchSchema()

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

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

Referenced by main().

◆ open() [1/2]

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

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

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

162 {
163 using XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument;
164 using XERCES_CPP_NAMESPACE_QUALIFIER DOMElement;
165 xmlBase::XmlParser parser;
166 DOMDocument* doc = parser.parse(parms.c_str(), "mysqlConnection");
167 if (doc == 0) {
168 (*m_err) << "parse of connection parameters failed" << std::endl;
169 m_err->flush();
170 return false;
171 }
172 DOMElement* conn = doc->getDocumentElement();
173
174 std::string host = xmlBase::Dom::getAttribute(conn, "host");
175 std::string user = xmlBase::Dom::getAttribute(conn, "user");
176 std::string password = xmlBase::Dom::getAttribute(conn, "password");
177 std::string dbname = xmlBase::Dom::getAttribute(conn, "dbname");
178
179 if (password.size() == 0 ) { // prompt for password?
180 (*m_out) << "interactive login NYI " << std::endl;
181 m_out->flush();
182 return false;
183 }
184
185 return this->open(host, user, password, dbname);
186 }
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

◆ select()

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

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

◆ update()

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

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

◆ visitAssertion()

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

Implements rdbModel::Visitor.

Definition at line 840 of file MysqlConnection.cxx.

840 {
841 return Visitor::VCONTINUE;
842 }

◆ visitColumn()

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

Implements rdbModel::Visitor.

Definition at line 684 of file MysqlConnection.cxx.

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

◆ visitIndex()

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

Implements rdbModel::Visitor.

Definition at line 835 of file MysqlConnection.cxx.

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

◆ visitInsertNew()

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

Implements rdbModel::Visitor.

Definition at line 844 of file MysqlConnection.cxx.

844 {
845 return Visitor::VCONTINUE;
846 }

◆ visitInterRow()

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

Implements rdbModel::Visitor.

Definition at line 860 of file MysqlConnection.cxx.

860 {
861 return Visitor::VCONTINUE;
862 }

◆ visitQuery()

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

Implements rdbModel::Visitor.

Definition at line 852 of file MysqlConnection.cxx.

852 {
853 return Visitor::VCONTINUE;
854 }

◆ visitRdb()

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

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

◆ visitSet()

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

Implements rdbModel::Visitor.

Definition at line 856 of file MysqlConnection.cxx.

856 {
857 return Visitor::VCONTINUE;
858 }

◆ visitSupersede()

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

Implements rdbModel::Visitor.

Definition at line 848 of file MysqlConnection.cxx.

848 {
849 return Visitor::VCONTINUE;
850 }

◆ visitTable()

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

Implements rdbModel::Visitor.

Definition at line 641 of file MysqlConnection.cxx.

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

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