This is an old revision of the document!


DB Connector

DBConnector.h
/*
 ============================================================================
 Name        : DBConnector.h
 Author      : Stephen Cannon
 Version     : 0.1
 Copyright   : Copyright 2011 Stephen Cannon
 Description :
 ============================================================================
 *
 * This file is part of LikelihoodWeighting.
 *
 * LikelihoodWeighting is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * LikelihoodWeighting is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with LikelihoodWeighting.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
 
#ifndef DBCONNECTOR_H_
#define DBCONNECTOR_H_
 
#include <windows.h>
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>
 
#ifdef __cplusplus
extern "C" {
#endif
 
 
enum { BUFFER_SIZE = 1024, LARGE_BUFFER_SIZE = 10240 };
enum {	DBCONNECTOR_STATE_NOT_CONNECTED = 0,
		DBCONNECTOR_STATE_CONNECTED = 1,
		DBCONNECTOR_STATE_STMT_PREPARED = 2,
        DBCONNECTOR_STATE_STMT_EXECUTED = 3};
enum {	ERR_GENERAL_ERROR = -1,
		ERR_ALREADY_CONNECTED = -9,
		ERR_NOT_CONNECTED = -10,
		ERR_NO_PREPARED_STMT = -11,
		ERR_STMT_NOT_EXECUTED = -12};
 
/**
 * Defines a parameter binding in a prepared statement.  A parameter binding
 * defines information about the parameter being bound to a prepared statement
 * as well as holds the value to be passed into the prepared statement when it
 * is used in a DB query.  There is a parameter binding for each "?" in a
 * prepared statement.
 */
typedef struct _ParamBinding_
{
	SQLSMALLINT		ioType;
	SQLSMALLINT		valueType;
	SQLSMALLINT		paramType;
	SQLULEN			columnSize;
	SQLSMALLINT		decimalDigits;
	SQLPOINTER		paramValuePtr;
	SQLLEN			bufferLength;
	SQLLEN			*indPtr;
} ParamBinding;
 
/**
 * Defines a column binding in a prepared statement.  A column binding defines
 * a column's type and holds the value returned for that column after the DB
 * has been queried.
 */
typedef struct _ColBinding_
{
	SQLSMALLINT		type;
	SQLPOINTER		valuePtr;
	SQLLEN			bufferLength;
	SQLLEN			*indPtr;
} ColBinding;
 
/**
 * Defines a DB connection and allows one to query that DB
 */
typedef struct _DBConnector_
{
	SQLHENV h_env;
	SQLHDBC h_dbc;
	SQLHSTMT h_stmt;
	SQLCHAR *stmt;
	const char *connectionString;
	size_t state;
} DBConnector;
 
char DBConnector__init(DBConnector *self, const char *connectionString);
char DBConnector__connect(DBConnector *self);
char DBConnector__prepareStatement(DBConnector *self, const char *stmt, ParamBinding *paramBinding, size_t numParams, ColBinding *colBinding, size_t numCols);
char DBConnector__executePreparedStatement(DBConnector *self);
char DBConnector__fetchExecutedStatementResult(DBConnector *self, char *error);
char DBConnector__disconnect(DBConnector *self);
char DBConnector__del(DBConnector *self);
void __DBConnector__extractError(const char *fn, SQLHANDLE handle, SQLSMALLINT type);
 
#ifdef __cplusplus
}
#endif
 
#endif /* DBCONNECTOR_H_ */
DBConnector.c
/*
 ============================================================================
 Name        : DBConnectorTest.c
 Author      : Stephen Cannon
 Version     : 0.1
 Copyright   : Copyright 2011 Stephen Cannon
 Description :
 ============================================================================
 *
 * This file is part of LikelihoodWeighting.
 *
 * LikelihoodWeighting is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * LikelihoodWeighting is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with LikelihoodWeighting.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
 
#include <windows.h>
#include <stdio.h>
//#include <tchar.h>
// DEBUG
#include <sql.h>
#include <sqlext.h>
// END DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <string.h>
#include "DBConnector.h"
#include "assert.h"
 
 
void initDestroyTest()
{
	DBConnector dbConnection;
 
	assert(!DBConnector__init(&dbConnection,
		"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test;User=root;Password=francis841;Option=3;"));
	assert(!DBConnector__del(&dbConnection));
}
 
void initConnectDisconnectDestroyTest()
{
	DBConnector dbConnection;
 
	assert(!DBConnector__init(&dbConnection,
		"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test;User=root;Password=francis841;Option=3;"));
	assert(!DBConnector__connect(&dbConnection));
	assert(!DBConnector__disconnect(&dbConnection));
	assert(!DBConnector__del(&dbConnection));
}
 
void queryDBTest()
{
	size_t i = 0;
	char error;
	SQLINTEGER clsInput;
	SQLINTEGER clsInputInd = 0;
	SQLINTEGER s1, r1, s2, r2, s3, r3, s4, r4, s5, r5, cls;
	SQLBIGINT id, freq;
	SQLLEN s1Len, r1Len, s2Len, r2Len, s3Len, r3Len, s4Len, r4Len, s5Len, r5Len, clsLen, idLen, freqLen;
	DBConnector dbConnection;
	ParamBinding pBinding[1] = {{SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0, &clsInput, 0, &clsInputInd}};
	ColBinding cBinding[13] =
	{
		{SQL_C_SLONG, &s1, 0, &s1Len},
		{SQL_C_SLONG, &r1, 0, &r1Len},
		{SQL_C_SLONG, &s2, 0, &s2Len},
		{SQL_C_SLONG, &r2, 0, &r2Len},
		{SQL_C_SLONG, &s3, 0, &s3Len},
		{SQL_C_SLONG, &r3, 0, &r3Len},
		{SQL_C_SLONG, &s4, 0, &s4Len},
		{SQL_C_SLONG, &r4, 0, &r4Len},
		{SQL_C_SLONG, &s5, 0, &s5Len},
		{SQL_C_SLONG, &r5, 0, &r5Len},
		{SQL_C_SLONG, &cls, 0, &clsLen},
		{SQL_C_SBIGINT, &id, 0, &idLen},
		{SQL_C_SBIGINT, &freq, 0, &freqLen}
	};
 
	// Connect
	assert(!DBConnector__init(&dbConnection,
		"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test;User=root;Password=francis841;Option=3;"));
	assert(!DBConnector__connect(&dbConnection));
 
	// Prepare statement
	assert(!DBConnector__prepareStatement(&dbConnection, "SELECT * FROM  `goscratchpad`.`poker_testing` WHERE CLASS = ?;", pBinding, 1, cBinding, 13));
 
	// Make query and verify all hands are flushes
	i = 0;
	clsInput = 5;
	assert(!DBConnector__executePreparedStatement(&dbConnection));
	while(DBConnector__fetchExecutedStatementResult(&dbConnection, &error))
	{
		assert(clsInput == cls);
		assert(s1 == s2);
		assert(s2 == s3);
		assert(s3 == s4);
		assert(s4 == s5);
		assert(!error);
		i++;
	}
	assert(i = 1996);
	i = 0;
	clsInput = 8;
	assert(!DBConnector__executePreparedStatement(&dbConnection));
	while(DBConnector__fetchExecutedStatementResult(&dbConnection, &error))
	{
		assert(clsInput == cls);
		assert(s1 == s2);
		assert(s2 == s3);
		assert(s3 == s4);
		assert(s4 == s5);
		assert(!error);
		i++;
	}
	assert(i = 12);
	i = 0;
	clsInput = 9;
	assert(!DBConnector__executePreparedStatement(&dbConnection));
	while(DBConnector__fetchExecutedStatementResult(&dbConnection, &error))
	{
		assert(clsInput == cls);
		assert(s1 == s2);
		assert(s2 == s3);
		assert(s3 == s4);
		assert(s4 == s5);
		assert(!error);
		i++;
	}
	assert(i = 3);
 
	// Disconnect
	assert(!DBConnector__disconnect(&dbConnection));
	assert(!DBConnector__del(&dbConnection));
}
 
void wrongOrderTestTest()
{
	DBConnector dbConnection;
 
	assert(!DBConnector__init(&dbConnection,
		"Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=test;User=root;Password=francis841;Option=3;"));
	assert(DBConnector__disconnect(&dbConnection) == ERR_NOT_CONNECTED);
	assert(!DBConnector__connect(&dbConnection));
	assert(!DBConnector__disconnect(&dbConnection));
	assert(!DBConnector__del(&dbConnection));
}
 
 
int mainy(int argc, const char* argv[])
{
	_CrtDumpMemoryLeaks();
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
 
	initDestroyTest();
	initConnectDisconnectDestroyTest();
	queryDBTest();
	wrongOrderTestTest();
 
	printf("no errors\npress enter key to continue...\n");
 
	getchar();
 
	return EXIT_SUCCESS;
}

Export page to Open Document format









You could leave a comment if you were logged in.

goplayer/dbconnector.1315486616.txt.gz · Last modified: 2011/09/08 05:56 by aiartificer