/*Method to execute sql statements like SELECT and return Cursor
Inputs:
query- string containing sql query
qlength - length of query (for binary data). if 0 then assume null terminated.
Output: NULL cursor on error
*/
DBCursor *DBConnection_ORACLE::sqlQuery(char *p_query, DBString *p_arguments, int p_argument_count, int p_rows)
{
	if (!isConnected)
		return NULL;

	Cda_Def *t_cursor;
	t_cursor = ExecuteQuery(p_query, p_arguments, p_argument_count);
	if (t_cursor == NULL)
		return NULL;

	DBCursor_ORACLE *t_rev_cursor;
	t_rev_cursor = new DBCursor_ORACLE();
	if (!t_rev_cursor -> open((DBConnection *)this, t_cursor, p_rows))
	{
		// OK-2007-09-10 : Bug 5360
		delete t_rev_cursor;
		t_rev_cursor = NULL;
		char t_error_message[512];
		oerhms((cda_def *)getLDA(), lda.rc, (text *)t_error_message, (sword) sizeof(t_error_message));
		errorMessageSet(t_error_message);
	}
	else
	{
		// OK-2007-09-10 : Bug 5360
		errorMessageSet(NULL);
		addCursor(t_rev_cursor);
	}

	lda . rc = t_cursor -> rc;

	return (DBCursor *)t_rev_cursor;
}
/*Method to execute sql statements like SELECT and return Cursor
Inputs:
query- string containing sql query
qlength - length of query (for binary data). if 0 then assume null terminated.
Output: NULL cursor on error
*/
DBCursor *DBConnection_MYSQL::sqlQuery(char *p_query, DBString *p_arguments, int p_argument_count, int p_rows)
{
	DBCursor_MYSQL *t_cursor;
	t_cursor = NULL;

	if (ExecuteQuery(p_query, p_arguments, p_argument_count))
	{
		t_cursor = new DBCursor_MYSQL();
		if (!t_cursor -> open((DBConnection *)this))
		{
			delete t_cursor;
			t_cursor = NULL;
		}
		else
			addCursor(t_cursor); 
	}

	// OK-2007-09-10 : Bug 5360
	if (t_cursor != NULL)
		errorMessageSet(NULL);
	else
		errorMessageSet(mysql_error(getMySQL()));

	return (DBCursor *)t_cursor;
}
/*Method to execute quick and fast sql statements like UPDATE and INSERT
Inputs:
query- string containing sql query
qlength - length of query (for binary data). if 0 then assume null terminated.
affectedrows - will recieve number of rows updated or inserted
Output: False on error
*/
Bool DBConnection_MYSQL::sqlExecute(char *p_query, DBString *p_arguments, int p_argument_count, unsigned int &p_affected_rows)
{
	int t_affected_rows;
	t_affected_rows = 0;

	Bool t_result;
	t_result = False;

	if (!ExecuteQuery(p_query, p_arguments, p_argument_count) || mysql_errno(getMySQL()))
	{
		// OK-2007-09-10 : Bug 5360
		errorMessageSet(mysql_error(getMySQL()));
		return t_result;
	}

	// First we need to establish if the query returned a result set. This will be the case if it was 
	// a select query. If there is a result set, we need to store it temporarily and free it to avoid
	// errors ocurring in subsequent queries.
	unsigned int t_field_count;
	t_field_count = mysql_field_count(getMySQL());

	MYSQL_RES *t_result_set;
	t_result_set = NULL;
	if (t_field_count != 0)
	{
		t_result_set = mysql_store_result(getMySQL());
		if (t_result_set == NULL)
			return False;

		t_affected_rows = 0;
	}
	else
		t_affected_rows = (int)mysql_affected_rows(&mysql);

	if (t_affected_rows != -1)
		t_result = True;
	
	if (t_result_set != NULL)
	{
		mysql_free_result(t_result_set);
	}

	p_affected_rows = t_affected_rows;

	// OK-2007-09-10 : Bug 5360
	if (t_result)
		errorMessageSet(NULL);
	else
		errorMessageSet(mysql_error(getMySQL()));

	return t_result;
}
/*Method to disconnect from MySQL server*/
void DBConnection_POSTGRESQL::disconnect()
{
    if (!isConnected)
        return;
    closeCursors();
    PQfinish(dbconn);
    isConnected = False;
    errorMessageSet(NULL);
}
/*Method to execute quick and fast sql statements like UPDATE and INSERT
Inputs:
query- string containing sql query
qlength - length of query (for binary data). if 0 then assume null terminated.
affectedrows - will recieve number of rows updated or inserted
Output: False on error
*/
Bool DBConnection_POSTGRESQL::sqlExecute(char *p_query, DBString *p_arguments, int p_argument_count, unsigned int &r_affected_rows)
{
    PGresult *t_postgres_result;
    t_postgres_result = ExecuteQuery(p_query, p_arguments, p_argument_count);
    if (t_postgres_result == NULL)
        return False;

    ExecStatusType t_status;
    t_status = PQresultStatus(t_postgres_result);

    Bool t_result;
    t_result = False;

    if (t_status == PGRES_COMMAND_OK)
    {
        t_result = True;
        char *t_affected_rows;
        t_affected_rows = PQcmdTuples(t_postgres_result);

        if (strcmp(t_affected_rows, "") == 0)
            r_affected_rows = 0;
        else
            r_affected_rows = atol(t_affected_rows);
    }

    if (t_status == PGRES_TUPLES_OK)
    {
        // This means the query was a select query. No rows would have been "affected"
        // so the result should be zero.
        t_result = True;
        r_affected_rows = 0;
    }

    // OK-2007-09-10 : Bug 5360, if the execution succeeded, we clear the error message, otherwise we set it.
    if (t_result)
        errorMessageSet(NULL);
    else
        errorMessageSet(PQerrorMessage(dbconn));

    PQclear(t_postgres_result);
    return t_result;
}
/*Method to execute sql statements like SELECT and return Cursor
Inputs:
query- string containing sql query
qlength - length of query (for binary data). if 0 then assume null terminated.
Output: NULL cursor on error
*/
DBCursor *DBConnection_POSTGRESQL::sqlQuery(char *p_query, DBString *p_arguments, int p_argument_count, int p_rows)
{
    PGresult *t_postgres_result;
    t_postgres_result = ExecuteQuery(p_query, p_arguments, p_argument_count);

    ExecStatusType t_status;
    t_status = PQresultStatus(t_postgres_result);

    DBCursor_POSTGRESQL *t_cursor;
    t_cursor = NULL;

    if (t_status == PGRES_TUPLES_OK)
    {
        int t_column_count;
        t_column_count = PQnfields(t_postgres_result);
        if (t_column_count != 0)
        {
            t_cursor = new DBCursor_POSTGRESQL();
            if (!t_cursor -> open((DBConnection *)this, t_postgres_result))
            {
                delete t_cursor;
                t_cursor = NULL;
            }
            else
                addCursor(t_cursor);
        }
        t_postgres_result = NULL;
    }

    // OK-2007-09-10 : Bug 5360, if the query succeeded, we clear the error message, otherwise we set it.
    if (t_cursor != NULL)
        errorMessageSet(NULL);
    else
        errorMessageSet(PQerrorMessage(dbconn));

    return (DBCursor *)t_cursor;
}
/*Method to execute quick and fast sql statements like UPDATE and INSERT
Inputs:
query- string containing sql query
qlength - length of query (for binary data). if 0 then assume null terminated.
affectedrows - will recieve number of rows updated or inserted
Output: False on error
*/
Bool DBConnection_ORACLE::sqlExecute(char *p_query, DBString *p_arguments, int p_argument_count, unsigned int &p_affected_rows)
{
	if (!isConnected)
		return False;

	Cda_Def *t_cursor;
	t_cursor = ExecuteQuery(p_query, p_arguments, p_argument_count);
	if (t_cursor == NULL)
	{
		// OK-2007-09-10 : Bug 5360
		char t_error_message[512];
		oerhms((cda_def *)getLDA(), lda.rc, (text *)t_error_message, (sword) sizeof(t_error_message));
		errorMessageSet(t_error_message);
		return False;
	}

	int t_affected_rows;
	t_affected_rows = (int)t_cursor -> rpc;
	p_affected_rows = t_affected_rows;

	oclose(t_cursor);
	errorMessageSet(NULL);
	return True;
}
Bool DBConnection_MYSQL::connect(char **args, int numargs)
{
	if (isConnected)
		return True;

	if (numargs < 4)
    	return False;

	char *t_dbname;
	t_dbname = args[1];

	char *t_user;
    t_user = args[2];

	char *t_password;
    t_password = args[3];

	char *t_use_ssl_string;
	t_use_ssl_string = NULL;

	if (numargs >= 5)
		t_use_ssl_string = args[4];

	// Parse args[0] to obtain the host and port.
	char *t_delimiter;
	t_delimiter = strchr(args[0], ':');

	char *t_host;
	t_host = args[0];

	char *t_port_string;
	t_port_string = NULL;

	if (t_delimiter != NULL)
	{
		t_port_string = (t_delimiter + (1 * sizeof(char)));
		*t_delimiter = NULL;
	}

	int t_port;
	t_port = 0;

	if (t_port_string != NULL)
		t_port = atoi(t_port_string);

	int t_use_ssl;
	t_use_ssl = 0;

	if (t_use_ssl_string != NULL && (strlen(t_use_ssl_string) != 0))
		if (strcasecmp(t_use_ssl_string, "true") == 0)
			t_use_ssl = 1;
		else
			t_use_ssl = atoi(t_use_ssl_string);
	else
		t_use_ssl = 0;
		
#ifndef _MOBILE
	if (t_use_ssl && !load_ssl_library())
	{
		errorMessageSet("Unable to load SSL library");
		return false;
	}
#endif
	
	//initialize mysql data structure for connection
	if (!mysql_init(getMySQL()))
		return False;

	// MM-2011-09-09: [[ BZ 9712]] Allow the user to specify the socket or named pipe to connect with
	char *t_socket;
	if (numargs >= 6 && strlen(args[5]) != 0)
		t_socket = args[5];
	else
		t_socket = NULL;	
	
	// Set timeout value for TCP/IP connections to 20 seconds.
	// According to API docs, total effective timeout is 3 times this due to retries,
	// but testing on windows shows connections timing out after 20 seconds
	// MM-2011-09-09: Updated to allow the user to specify the read/write timeout
	int t_read_write_timeout;
	if(numargs < 7 || args[6] == NULL || strlen(args[6]) == 0 || sscanf(args[6], "%d", &t_read_write_timeout) == 0 || t_read_write_timeout < 0)
		t_read_write_timeout = 20;
	if (mysql_options(getMySQL(), MYSQL_OPT_READ_TIMEOUT, &t_read_write_timeout) ||
		mysql_options(getMySQL(), MYSQL_OPT_WRITE_TIMEOUT, &t_read_write_timeout))
		return false;
	
	// MM-2011-09-09: Allow the user to specify the auto-reconnect option.  If connection to the server has been lost,
	//	the driver will automatically try to reconnect.  To prevent the reconnect hanging, set the reconnect timeout also.
	bool t_auto_reconnect;
	t_auto_reconnect = (numargs >= 8 && args[7] != NULL && strlen(args[7]) != 0 && strcasecmp(args[7], "true") == 0);
	if (t_auto_reconnect)
		if (mysql_options(getMySQL(), MYSQL_OPT_RECONNECT, &t_auto_reconnect) ||
			mysql_options(getMySQL(), MYSQL_OPT_CONNECT_TIMEOUT, &t_read_write_timeout))
			return false;	
	
	//connect to mysql database.
	if (!mysql_real_connect(getMySQL(), t_host, t_user, t_password, t_dbname, t_port, t_socket, t_use_ssl > 0 ? CLIENT_SSL : 0))
		return False;

	connectionType = CT_MYSQL,
	isConnected = True;
	
	return True;
}
Bool DBConnection_POSTGRESQL::connect(char **args, int numargs)
{
    if (isConnected)
        return True;

    char *t_database;
    t_database = args[1];

    char *t_user;
    t_user = args[2];

    char *t_password;
    t_password = args[3];

    // OK-2007-06-25: Fix for bug 3232. Parse the host string to obtain the host and port parts
    char *t_delimiter;
    t_delimiter = strchr(args[0], ':');

    char *t_host;
    t_host = args[0];

    char *t_port;
    t_port = NULL;

    if (t_delimiter != NULL)
    {
        t_port = (t_delimiter + (1 * sizeof(char)));
        *t_delimiter = NULL;
    }

    dbconn = NULL;
    dbconn = PQsetdbLogin(t_host, t_port, NULL, NULL, t_database, t_user, t_password);

    // OK-2008-05-16 : Bug where failed connections to postgres databases would
    // not return any error information. According to the postgres docs, dbconn
    // will only be null if there was not enough memory to allocate it.
    if (dbconn == NULL)
    {
        char *t_error_message;
        t_error_message = strdup("revdb,insufficient memory to connect to database");
        errorMessageSet(t_error_message);
        return false;
    }

    // If the connection failed, return the standard postgres error message.
    if (PQstatus(dbconn) == CONNECTION_BAD)
    {
        errorMessageSet(PQerrorMessage(dbconn));
        return false;
    }


    if (PQstatus(dbconn) == CONNECTION_BAD || !dbconn)
        return False;

    isConnected = true;
    connectionType = CT_POSTGRESQL,
    isConnected = True;
    // OK-2007-09-10 : Bug 5360
    errorMessageSet(NULL);

    return True;
}
Exemple #10
0
//
// Error messages
//
void State::setErrorMessage(const QString &errorMesg)
{
    _lastError = errorMesg;
    emit errorMessageSet(_lastError);
}