23 #include <NdbAutoPtr.hpp>
37 const char* env= getenv(
"MYSQL_HOME");
38 if (env && strlen(env))
40 m_default_file.
assfmt(
"%s/my.cnf", env);
44 m_default_group.
assfmt(
"client%s", _suffix);
47 m_default_group.
assign(
"client.1.master");
50 ndbout <<
"default_file: " << m_default_file.
c_str() << endl;
51 ndbout <<
"default_group: " << m_default_group.
c_str() << endl;
65 DbUtil::isConnected(){
66 if (m_connected ==
true)
76 DbUtil::waitConnected(
int timeout) {
78 while(!isConnected()){
81 NdbSleep_MilliSleep(100);
108 DbUtil::databaseLogin(
const char* system,
const char* usr,
109 const char* password,
unsigned int portIn,
110 const char* sockIn,
bool transactional)
112 if (!(m_mysql = mysql_init(NULL)))
114 myerror(
"DB Login-> mysql_init() failed");
119 setPassword(password);
124 if (!(mysql_real_connect(m_mysql,
130 m_socket.
c_str(), 0)))
132 myerror(
"connection failed");
137 m_mysql->reconnect = TRUE;
141 mysql_autocommit(m_mysql, TRUE);
143 mysql_autocommit(m_mysql, FALSE);
146 printf(
"\n\tConnected to MySQL server version: %s (%lu)\n\n",
147 mysql_get_server_info(m_mysql),
148 (
unsigned long) mysql_get_server_version(m_mysql));
160 if (!(m_mysql = mysql_init(NULL)))
162 myerror(
"DB connect-> mysql_init() failed");
167 if (mysql_options(m_mysql, MYSQL_READ_DEFAULT_FILE, m_default_file.
c_str()) ||
168 mysql_options(m_mysql, MYSQL_READ_DEFAULT_GROUP, m_default_group.
c_str()))
170 myerror(
"DB Connect -> mysql_options failed");
179 if (mysql_real_connect(m_mysql, NULL,
185 myerror(
"connection failed");
199 DbUtil::databaseLogout()
203 printf(
"\n\tClosing the MySQL database connection ...\n\n");
205 mysql_close(m_mysql);
212 DbUtil::mysqlSimplePrepare(
const char *
query)
215 printf(
"Inside DbUtil::mysqlSimplePrepare\n");
217 MYSQL_STMT *my_stmt= mysql_stmt_init(this->getMysql());
218 if (my_stmt && mysql_stmt_prepare(my_stmt, query, strlen(query))){
219 this->printStError(my_stmt,
"Prepare Statement Failed");
220 mysql_stmt_close(my_stmt);
229 DbUtil::mysqlCloseStmHandle(
MYSQL_STMT *my_stmt)
231 mysql_stmt_close(my_stmt);
237 DbUtil::printError(
const char *
msg)
239 if (m_mysql && mysql_errno(m_mysql))
241 if (m_mysql->server_version)
242 printf(
"\n [MySQL-%s]", m_mysql->server_version);
244 printf(
"\n [MySQL]");
245 printf(
"[%d] %s\n", getErrorNumber(), getError());
248 printf(
" [MySQL] %s\n", msg);
252 DbUtil::printStError(
MYSQL_STMT *stmt,
const char *msg)
254 if (stmt && mysql_stmt_errno(stmt))
256 if (m_mysql && m_mysql->server_version)
257 printf(
"\n [MySQL-%s]", m_mysql->server_version);
259 printf(
"\n [MySQL]");
261 printf(
"[%d] %s\n", mysql_stmt_errno(stmt),
262 mysql_stmt_error(stmt));
265 printf(
"[MySQL] %s\n", msg);
273 if ((getDbName()) != NULL)
275 if(mysql_select_db(m_mysql, this->getDbName()))
282 printError(
"getDbName() == NULL");
287 DbUtil::selectDb(
const char * m_db)
290 if(mysql_select_db(m_mysql, m_db))
292 printError(
"mysql_select_db failed");
303 setDbName(m_db.
c_str());
309 if(!doQuery(m_db.
c_str()))
313 if(!doQuery(m_db.
c_str()))
323 DbUtil::selectCountTable(
const char *
table)
328 query.
assfmt(
"select count(*) as count from %s", table);
329 if (!doQuery(query, result)) {
330 printError(
"select count(*) failed");
333 return result.columnAsLong(
"count");
341 DbUtil::runQuery(
const char* sql,
351 g_debug <<
"runQuery: " << endl
352 <<
" sql: '" << sql <<
"'" << endl;
356 if (mysql_stmt_prepare(stmt, sql, strlen(sql)))
358 report_error(
"Failed to prepare: ", m_mysql);
362 uint params= mysql_stmt_param_count(stmt);
366 memset(bind_param, 0, params *
sizeof(
MYSQL_BIND));
368 for(uint
i= 0;
i < mysql_stmt_param_count(stmt);
i++)
375 g_err <<
"param " <<
i <<
" missing" << endl;
381 args.getTypeOf(name.
c_str(), &t);
383 case PropertiesType_Uint32:
384 args.get(name.
c_str(), &val_i);
385 bind_param[
i].buffer_type= MYSQL_TYPE_LONG;
386 bind_param[
i].buffer= (
char*)&val_i;
387 g_debug <<
" param" << name.
c_str() <<
": " << val_i << endl;
389 case PropertiesType_char:
390 args.get(name.
c_str(), &val_s);
391 bind_param[
i].buffer_type= MYSQL_TYPE_STRING;
392 bind_param[
i].buffer= (
char*)val_s;
393 bind_param[
i].buffer_length= strlen(val_s);
394 g_debug <<
" param" << name.
c_str() <<
": " << val_s << endl;
401 if (mysql_stmt_bind_param(stmt, bind_param))
403 report_error(
"Failed to bind param: ", m_mysql);
404 mysql_stmt_close(stmt);
408 if (mysql_stmt_execute(stmt))
410 report_error(
"Failed to execute: ", m_mysql);
411 mysql_stmt_close(stmt);
420 mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (
void*) &one);
422 if (mysql_stmt_store_result(stmt))
424 report_error(
"Failed to store result: ", m_mysql);
425 mysql_stmt_close(stmt);
430 MYSQL_RES* res= mysql_stmt_result_metadata(stmt);
434 uint num_fields= mysql_num_fields(res);
437 memset(bind_result, 0, num_fields *
sizeof(
MYSQL_BIND));
439 for (uint
i= 0;
i < num_fields;
i++)
441 unsigned long buf_len=
sizeof(int);
443 switch(fields[
i].
type){
444 case MYSQL_TYPE_STRING:
445 buf_len = fields[
i].length + 1;
447 case MYSQL_TYPE_VARCHAR:
448 case MYSQL_TYPE_VAR_STRING:
449 buf_len= fields[
i].max_length + 1;
451 case MYSQL_TYPE_LONGLONG:
452 buf_len=
sizeof(
long long);
454 case MYSQL_TYPE_LONG:
455 buf_len =
sizeof(long);
461 bind_result[
i].buffer_type= fields[
i].type;
462 bind_result[
i].buffer= malloc(buf_len);
463 bind_result[
i].buffer_length= buf_len;
464 bind_result[
i].is_null = (my_bool*)malloc(
sizeof(my_bool));
465 * bind_result[
i].is_null = 0;
468 if (mysql_stmt_bind_result(stmt, bind_result)){
469 report_error(
"Failed to bind result: ", m_mysql);
470 mysql_stmt_close(stmt);
474 while (mysql_stmt_fetch(stmt) != MYSQL_NO_DATA)
477 for (uint
i= 0;
i < num_fields;
i++){
478 if (* bind_result[
i].is_null)
480 switch(fields[
i].type){
481 case MYSQL_TYPE_STRING:
482 ((
char*)bind_result[
i].buffer)[fields[
i].max_length] = 0;
483 case MYSQL_TYPE_VARCHAR:
484 case MYSQL_TYPE_VAR_STRING:
485 curr.put(fields[
i].name, (
char*)bind_result[
i].buffer);
488 case MYSQL_TYPE_LONGLONG:
489 curr.put64(fields[
i].name,
490 *(
unsigned long long*)bind_result[
i].buffer);
494 curr.put(fields[
i].name, *(
int*)bind_result[
i].buffer);
498 rows.
put(
"row", row++, &curr);
501 mysql_free_result(res);
503 for (uint
i= 0;
i < num_fields;
i++)
505 free(bind_result[
i].buffer);
506 free(bind_result[
i].is_null);
511 rows.
put(
"rows", row);
512 rows.put64(
"affected_rows", mysql_affected_rows(m_mysql));
513 rows.
put(
"mysql_errno", mysql_errno(m_mysql));
514 rows.
put(
"mysql_error", mysql_error(m_mysql));
515 rows.
put(
"mysql_sqlstate", mysql_sqlstate(m_mysql));
516 rows.put64(
"insert_id", mysql_insert_id(m_mysql));
518 mysql_stmt_close(stmt);
524 DbUtil::doQuery(
const char* query){
527 return doQuery(query, args, result);
532 DbUtil::doQuery(
const char* query,
SqlResultSet& result){
534 return doQuery(query, args, result);
539 DbUtil::doQuery(
const char* query,
const Properties& args,
541 if (!runQuery(query, args, result))
548 DbUtil::doQuery(
const char* query,
const Properties& args){
550 return doQuery(query, args, result);
556 return doQuery(str.
c_str());
562 return doQuery(str.
c_str(), result);
569 return doQuery(str.
c_str(), args, result);
575 return doQuery(str.
c_str(), args);
584 return mysql_error(this->getMysql());
590 DbUtil::getErrorNumber()
592 return mysql_errno(this->getMysql());
598 m_last_errno= mysql_errno(mysql);
599 m_last_error.
assfmt(
"%d: %s", m_last_errno, mysql_error(mysql));
602 g_err << message << m_last_error << endl;
609 DbUtil::die(
const char *
file,
int line,
const char *expr)
611 printf(
"%s:%d: check failed: '%s'\n", file, line, expr);
619 SqlResultSet::get_row(
int row_num){
620 if(!
get(
"row", row_num, &m_curr_row)){
628 SqlResultSet::next(
void){
629 return get_row(++m_curr_row_num);
634 void SqlResultSet::reset(
void){
641 void SqlResultSet::remove(){
643 row_name.
assfmt(
"row_%d", m_curr_row_num);
644 Properties::remove(row_name.
c_str());
649 void SqlResultSet::clear(){
655 SqlResultSet::SqlResultSet(): m_curr_row(0), m_curr_row_num(-1){
659 SqlResultSet::~SqlResultSet(){
663 const char* SqlResultSet::column(
const char* col_name){
666 g_err <<
"ERROR: SqlResultSet::column("<< col_name <<
")" << endl
667 <<
"There is no row loaded, call next() before "
668 <<
"acessing the column values" << endl;
671 if (!m_curr_row->get(col_name, &value))
677 uint SqlResultSet::columnAsInt(
const char* col_name){
680 g_err <<
"ERROR: SqlResultSet::columnAsInt("<< col_name <<
")" << endl
681 <<
"There is no row loaded, call next() before "
682 <<
"acessing the column values" << endl;
685 if (!m_curr_row->get(col_name, &value))
690 unsigned long long SqlResultSet::columnAsLong(
const char* col_name){
691 unsigned long long value;
693 g_err <<
"ERROR: SqlResultSet::columnAsLong("<< col_name <<
")" << endl
694 <<
"There is no row loaded, call next() before "
695 <<
"acessing the column values" << endl;
698 if (!m_curr_row->get(col_name, &value))
704 unsigned long long SqlResultSet::insertId(){
705 return get_long(
"insert_id");
709 unsigned long long SqlResultSet::affectedRows(){
710 return get_long(
"affected_rows");
713 uint SqlResultSet::numRows(
void){
714 return get_int(
"rows");
718 uint SqlResultSet::mysqlErrno(
void){
719 return get_int(
"mysql_errno");
723 const char* SqlResultSet::mysqlError(
void){
724 return get_string(
"mysql_error");
728 const char* SqlResultSet::mysqlSqlstate(
void){
729 return get_string(
"mysql_sqlstate");
733 uint SqlResultSet::get_int(
const char* name){
739 unsigned long long SqlResultSet::get_long(
const char* name){
740 unsigned long long value;
746 const char* SqlResultSet::get_string(
const char* name){