/*
This code was created by Ian Ventura-Whiting (Fizz) - fizz@titania.co.uk

ODBC Data Sources (for QT 4)
Copyright (C) 2006 Ian Ventura-Whiting

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


================================================================

This example program uses a C compiler, unixODBC libraries, a
data source called "web", the database user is called "postgres"
and no password.

compile: gcc -o odbc-example odbc-example.c

*/


#include <sqlext.h>
#include <sqltypes.h>

#define MAX_DATA 300

int main(int argc, char *argv[])
{
	// Variables...
	SQLHENV envHandle;
	SQLHDBC conHandle;
	SQLHSTMT stateHandle;
	long returnCode;
	SQLSMALLINT colNum;
	SQLINTEGER rowNum;
	SQLINTEGER currentCol;
	SQLLEN indicator;
	SQLCHAR sqlValue[MAX_DATA+1];

	// Allocate environment handle...
	returnCode = SQLAllocEnv(&envHandle);
	if ((returnCode == SQL_SUCCESS) || (returnCode == SQL_SUCCESS_WITH_INFO))
	{

		// Allocate connection handle...
		returnCode = SQLAllocConnect(envHandle, &conHandle);
		if ((returnCode == SQL_SUCCESS) || (returnCode == SQL_SUCCESS_WITH_INFO))
		{

			// Connect...
			returnCode = SQLConnect(conHandle, (SQLCHAR*)"web", SQL_NTS, (SQLCHAR*)"postgres", SQL_NTS, (SQLCHAR*)"", SQL_NTS);
			if ((returnCode == SQL_SUCCESS) || (returnCode == SQL_SUCCESS_WITH_INFO))
			{
				printf("Connected to ODBC data source.\n");

				// Allocate statement handle...
				returnCode = SQLAllocStmt(conHandle, &stateHandle);
				if ((returnCode == SQL_SUCCESS) || (returnCode == SQL_SUCCESS_WITH_INFO))
				{

					// Prepare statement...
					returnCode = SQLPrepare(stateHandle, (SQLCHAR*)"select * from user", SQL_NTS);
					if ((returnCode == SQL_SUCCESS) || (returnCode == SQL_SUCCESS_WITH_INFO))
					{

						// Execute statement...
						returnCode = SQLExecute(stateHandle);
						if ((returnCode == SQL_SUCCESS) || (returnCode == SQL_SUCCESS_WITH_INFO))
						{

							// Get number of columns...
							returnCode = SQLNumResultCols(stateHandle, &colNum);
							if ((returnCode == SQL_SUCCESS) || (returnCode == SQL_SUCCESS_WITH_INFO))
							{
								printf("Number of columns returned: %d \n", colNum);
							}

							// Get number of rows...
							returnCode = SQLRowCount(stateHandle, &rowNum);
							if ((returnCode == SQL_SUCCESS) || (returnCode == SQL_SUCCESS_WITH_INFO))
							{
								printf("Number of rows returned: %d \n", rowNum);
							}

							// Get the data...
							while((returnCode = SQLFetch(stateHandle)) == SQL_SUCCESS)
							{
								for(currentCol = 1; currentCol <= colNum; currentCol++)
								{
									returnCode = SQLGetData(stateHandle, currentCol, SQL_C_CHAR, (SQLPOINTER)sqlValue, sizeof(sqlValue), &indicator);
									if ((returnCode == SQL_SUCCESS) && (indicator != SQL_NULL_DATA))
            						{
										printf("'%s' ", sqlValue);
									}
								}
								printf("\n");
							}
						}
					}

					// Free statement handle...
					SQLFreeHandle(SQL_HANDLE_STMT, stateHandle);
				}

				// Disconnect...
				SQLDisconnect(conHandle);
			}

			// Free connection handle...
			SQLFreeHandle(SQL_HANDLE_DBC, conHandle);
		}

	// Free environment handle...
	SQLFreeHandle(SQL_HANDLE_ENV, envHandle);
	}
}
