void mpl_tab_drv_open(MPL *mpl, int mode) { TABDCA *dca = mpl->dca; xassert(dca->id == 0); xassert(dca->link == NULL); xassert(dca->na >= 1); if (strcmp(dca->arg[1], "CSV") == 0) { dca->id = TAB_CSV; dca->link = csv_open_file(dca, mode); } else if (strcmp(dca->arg[1], "xBASE") == 0) { dca->id = TAB_XBASE; dca->link = dbf_open_file(dca, mode); } else if (strcmp(dca->arg[1], "ODBC") == 0 || strcmp(dca->arg[1], "iODBC") == 0) { dca->id = TAB_ODBC; dca->link = db_iodbc_open(dca, mode); } else if (strcmp(dca->arg[1], "MySQL") == 0) { dca->id = TAB_MYSQL; dca->link = db_mysql_open(dca, mode); } else xprintf("Invalid table driver `%s'\n", dca->arg[1]); if (dca->link == NULL) error(mpl, "error on opening table %s", mpl->stmt->u.tab->name); return; }
/* Check if RDB can be used to load/save data */ int rdb_open() { #ifdef USE_MYSQL return db_mysql_open(); /* db_mysql_open(); */ #endif return 0; }
int db_mysql_init() { SET_SEGV_LOCATION(); /* If the host is not defined, assume we don't want MySQL */ if (!SqlHost) { denora->do_sql = 0; alog(LOG_NORMAL, langstring(ALOG_SQL_DISABLED)); return 0; } else { denora->do_sql = 1; alog(LOG_NORMAL, langstring(ALOG_SQL_ENABLED), "MySQL"); alog(LOG_SQLDEBUG, "MySQL: client version %s.", mysql_get_client_info()); } SET_SEGV_LOCATION(); /* The following configuration options are required. * If missing disable MySQL to avoid any problems. */ if ((denora->do_sql) && (!SqlName || !SqlUser)) { denora->do_sql = 0; alog(LOG_ERROR, langstring(ALOG_SQL_NOTSET)); return 0; } SET_SEGV_LOCATION(); if (!db_mysql_open()) { denora->do_sql = 0; return 0; } else { denora->do_sql = 1; alog(LOG_SQLDEBUG, "MySQL: server version %s.", mysql_get_server_info(mysql)); } SET_SEGV_LOCATION(); return 1; }
int db_mysql_query(char *sql) { int result, lcv; int pingresult; if (!denora->do_sql) { return 0; } alog(LOG_SQLDEBUG, "sql debug: %s", db_mysql_hidepass(sql)); pingresult = mysql_ping(mysql); if (!pingresult) { result = mysql_query(mysql, sql); } else { result = pingresult; } if (result) { switch (mysql_errno(mysql)) { case CR_SERVER_GONE_ERROR: case CR_SERVER_LOST: case CR_CONN_HOST_ERROR: case CR_IPSOCK_ERROR: case CR_CONNECTION_ERROR: case CR_UNKNOWN_ERROR: for (lcv = 0; lcv < SqlRetries; lcv++) { alog(LOG_NORMAL, "%s, trying to reconnect...", mysql_error(mysql)); if (db_mysql_open()) { alog(LOG_NORMAL, "MySQL connection reestablished"); result = mysql_query(mysql, sql); return (result); } sleep(SqlRetryGap); } /* If we get here, we could not connect. */ log_perror("Unable to reconnect to database: %s\n", mysql_error(mysql)); db_mysql_error(SQL_ERROR, "connect"); alog(LOG_ERROR, "Disabling MYSQL due to problem with server"); denora->do_sql = 0; SQLDisableDueServerLost = 1; /* Never reached. */ break; case CR_COMMANDS_OUT_OF_SYNC: db_mysql_error(SQL_ERROR, "Commands out of sync"); break; case ER_PARSE_ERROR: db_mysql_error(SQL_ERROR, "parser error"); break; case ER_WRONG_VALUE_COUNT_ON_ROW: db_mysql_error(SQL_ERROR, "query error"); break; case ER_BAD_FIELD_ERROR: db_mysql_error(SQL_ERROR, "Unknown field"); break; case ER_NO_SUCH_TABLE: db_mysql_error(SQL_ERROR, "Table Doesn't exist"); break; case ER_DUP_FIELDNAME: break; case ER_DUP_ENTRY: alog(LOG_ERROR, "MYSQL said %s", mysql_error(mysql)); alog(LOG_ERROR, "MYSQL query %s", sql); break; case ER_BAD_NULL_ERROR: db_mysql_error(SQL_ERROR, "Column can not be NULL!! - check statement"); break; case ER_USER_LIMIT_REACHED: db_mysql_error(SQL_ERROR, "To Many Connections, closing denora mysql connection"); denora->do_sql = 0; db_mysql_close(); SQLDisableDueServerLost = 1; break; case ER_GET_ERRNO: db_mysql_error(SQL_ERROR, "mysql returned errno code: disabling MYSQL"); denora->do_sql = 0; db_mysql_close(); SQLDisableDueServerLost = 1; break; default: alog(LOG_ERROR, "MYSQL reported Error Code %d", mysql_errno(mysql)); alog(LOG_ERROR, "MYSQL said %s", mysql_error(mysql)); alog(LOG_ERROR, "Report this to Denora Developers"); alog(LOG_ERROR, "Disabling MYSQL due to this error"); denora->do_sql = 0; db_mysql_close(); SQLDisableDueServerLost = 1; return 0; } } return 1; }
void conf_parser_element_start(void *data, XML_Char const *name, XML_Char const **attr) { ConfParser *this = (ConfParser *) data; //<mysql> if (strcmp(name, "mysql") == 0 && this->context == context_none) { this->context = context_mysql; char *host = conf_parser_get_attr(attr, "host"); char *user = conf_parser_get_attr(attr, "user"); char *passwd = conf_parser_get_attr(attr, "passwd"); char *db = conf_parser_get_attr(attr, "db"); if (host && user && passwd && db) { this->connected = db_mysql_open(&this->mysql_handle, host, user, passwd, db); if (!this->connected) log_msg_printf(LOG_ERR, true, "Could not connect to mysql db (host: %s, user: %s, passwd: %s, db: %s)", host, user, passwd, db); } else { log_msg_printf(LOG_ERR, true, "Mysql attributes are mendatory (host: %s, user: %s, passwd: %s, db: %s)", host, user, passwd, db); } } //<sqlite3> else if (strcmp(name, "sqlite3") == 0 && this->context == context_none) { this->context = context_sqlite3; char *path = conf_parser_get_attr(attr, "path"); if (path) { this->connected = db_sqlite3_open(&this->sqlite3_handle, path); if (!this->connected) log_msg_printf(LOG_ERR, true, "Could not connect to sqlite3 db (path: %s)", path); } else { log_msg_printf(LOG_ERR, true, "Sqlite3 attributes \"path\" is mendatory"); } } //<query /> else if (strcmp(name, "query") == 0 && this->context != context_none && this->connected) { char *target = conf_parser_get_attr(attr, "target"); char *str = conf_parser_get_attr(attr, "str"); if (str) { if (this->context == context_mysql) db_mysql_load_query(&this->mysql_handle, target, str); else if (this->context == context_sqlite3) db_sqlite3_load_query(this->sqlite3_handle, target, str); } else { log_msg_printf(LOG_ERR, true, "Query string is mendatory", target, str); } } //<param /> else if (strcmp(name, "param") == 0) { char *name = conf_parser_get_attr(attr, "name"); char *value = conf_parser_get_attr(attr, "value"); if (name && value) { StringListItem *item = stringlistitem_new(name, strdup(value)); stringlist_push_back(this->params, item); } else { log_msg_printf(LOG_ERR, true, "Attributes 'name' and 'value' are mendatory (name: %s, value: %s)", name, value); } } }