ODBC - Library functions

From ISXKB

Jump to: navigation, search

File odbclib.iss

[Code]


const
	SQL_SUCCESS 	=	0;
	SQL_SUCCESS_WITH_INFO	=	1;
	SQL_NO_DATA	= 100;

	SQL_HANDLE_ENV	=	1;
	SQL_HANDLE_DBC	=	2;
	SQL_HANDLE_STMT	=	3;
	SQL_HANDLE_DESC	=	4;

	SQL_NULL_HENV		=	0;
	SQL_NULL_HDBC		=	0;
	SQL_NULL_HSTMT	=	0;
	SQL_NULL_HDESC	=	0;
	SQL_NULL_HANDLE	=	0;

const SQL_DRIVER_NOPROMPT = 0;
const SQL_DRIVER_COMPLETE = 1;
const SQL_DRIVER_PROMPT = 2;
const SQL_DRIVER_COMPLETE_REQUIRED = 3;


const SQL_FETCH_NEXT = 1;
const SQL_FETCH_FIRST_USER = 31;
const SQL_FETCH_FIRST_SYSTEM = 32;



const
	SQL_ATTR_ODBC_VERSION = 200;
	SQL_OV_ODBC3 = 3;
	SQL_NTS = ( - 3);

type SQLSMALLINT = Smallint;
type SQLRETURN = SQLSMALLINT;
type SQLHANDLE = LongInt;
type SQLHENV = SQLHANDLE;
type SQLHDBC = SQLHANDLE;
type SQLINTEGER = LongInt;
type SQLPOINTER = SQLHANDLE;  // Pointer
type PSQLCHAR = PChar;
type SQLUSMALLINT = Word;


const BUFSIZE = 500;

// ------
// SQLDriverConnect
// ------
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odch21dpr_3.asp
// ------
{
SQLRETURN SQLDriverConnect(
     SQLHDBC     ConnectionHandle,
     SQLHWND     WindowHandle,
     SQLCHAR *     InConnectionString,
     SQLSMALLINT     StringLength1,
     SQLCHAR *     OutConnectionString,
     SQLSMALLINT     BufferLength,
     SQLSMALLINT *     StringLength2Ptr,
     SQLUSMALLINT     DriverCompletion);
}

function SQLDriverConnect(
  hdbc: SQLHDBC;
  hwnd: Integer;
  szCsin: PChar;
  szCLen: SQLSMALLINT;
  szCsout: PChar;
  cbCSMax: SQLSMALLINT;
  var cbCsOut: SQLSMALLINT;
  f: Integer
):SQLRETURN;
external 'SQLDriverConnect@ODBC32.DLL stdcall';



// ------
// SQLAllocHandle
// ------
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odch21bpr_2.asp
// ------
{
SQLRETURN SQLAllocHandle(
     SQLSMALLINT     HandleType,
     SQLHANDLE     InputHandle,
     SQLHANDLE *     OutputHandlePtr);
}

function SQLAllocHandle(
	HandleType: SQLSMALLINT;
	InputHandle: SQLHANDLE;
	var OutputHandlePtr: SQLHANDLE
	): SQLRETURN;
external 'SQLAllocHandle@ODBC32.DLL stdcall';



// ------
// SQLFreeHandle
// ------
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odch21fpr_3.asp
// ------
{
SQLRETURN SQLFreeHandle(
     SQLSMALLINT     HandleType,
     SQLHANDLE     Handle);
}

function SQLFreeHandle(
	HandleType: SQLSMALLINT;
	Handle: SQLHANDLE
	): SQLRETURN;
external 'SQLFreeHandle@ODBC32.DLL stdcall';


// ------
// SQLSetEnvAttr
// ------
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odch21jpr_8.asp
// ------
{
SQLRETURN SQLSetEnvAttr(
     SQLHENV     EnvironmentHandle,
     SQLINTEGER     Attribute,
     SQLPOINTER     ValuePtr,
     SQLINTEGER     StringLength);
}

function SQLSetEnvAttr(
  EnvironmentHandle: SQLHENV;
  Attribute: SQLINTEGER;
  Value: SQLPOINTER;
  StringLength: SQLINTEGER
):SQLRETURN;
external 'SQLSetEnvAttr@ODBC32.DLL stdcall';



// ------
// SQLDisconnect
// ------
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odch21dpr_2.asp
// ------
{
SQLRETURN SQLDisconnect(
     SQLHDBC     ConnectionHandle);

}

function SQLDisconnect(
  ConnectionHandle: SQLHDBC
):SQLRETURN;
external 'SQLDisconnect@ODBC32.DLL stdcall';



// ------
// SQLGetDiagRec
// ------
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odch21gpr_4.asp
// ------
{
SQLRETURN SQLGetDiagRec(
     SQLSMALLINT     HandleType,
     SQLHANDLE     Handle,
     SQLSMALLINT     RecNumber,
     SQLCHAR *     Sqlstate,
     SQLINTEGER *     NativeErrorPtr,
     SQLCHAR *     MessageText,
     SQLSMALLINT     BufferLength,
     SQLSMALLINT *     TextLengthPtr);
}


function SQLGetDiagRec(
  HandleType: SQLSMALLINT;
  Handle: SQLHANDLE;
  RecNumber: SQLSMALLINT;
  Sqlstate: PSQLCHAR;
  var NativeError: SQLINTEGER;
  MessageText: PSQLCHAR;
  BufferLength: SQLSMALLINT;
  var TextLength: SQLSMALLINT
):SQLRETURN;
external 'SQLGetDiagRec@ODBC32.DLL stdcall';




// ------
// SQLDataSources
// ------
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odch21cpr_4.asp
// ------
{
SQLRETURN SQLDataSources(
     SQLHENV     EnvironmentHandle,
     SQLUSMALLINT     Direction,
     SQLCHAR *     ServerName,
     SQLSMALLINT     BufferLength1,
     SQLSMALLINT *     NameLength1Ptr,
     SQLCHAR *     Description,
     SQLSMALLINT     BufferLength2,
     SQLSMALLINT *     NameLength2Ptr);
}


function SQLDataSources(
  EnvironmentHandle: SQLHENV;
  Direction: SQLUSMALLINT;
  ServerName: PSQLCHAR;
  BufferLength1: SQLSMALLINT;
  // NameLength1: PSQLSMALLINT;
  var NameLength1: SQLSMALLINT;
  Description: PSQLCHAR;
  BufferLength2: SQLSMALLINT;
  // NameLength2: PSQLSMALLINT
  var NameLength2: SQLSMALLINT
):SQLRETURN;
external 'SQLDataSources@ODBC32.DLL stdcall';





function IsSQLRetOk( sqlret: SQLRETURN ) : Boolean;
begin
	Result := ( sqlret = SQL_SUCCESS ) or ( sqlret = SQL_SUCCESS_WITH_INFO );
end;


function GetODBCMsg( HandleType : SQLSMALLINT; Handle : SQLHANDLE ): String;
var
	sqlret : SQLRETURN;
	recnum : SQLSMALLINT;
	sqlstate: String;
	errmsg: String;
	nativeerr: SQLINTEGER;
	textlen: SQLSMALLINT;

begin

	recnum := 1;

	sqlstate := StringOfChar( ' ', 6 );
	errmsg := StringOfChar( ' ', BUFSIZE );


	sqlret := SQLGetDiagRec( HandleType,
							Handle,
							recnum,
							sqlstate,
							nativeerr,
							errmsg,
							BUFSIZE - 1,
							textlen );


    sqlstate := CastIntegerToString( CastStringToInteger(sqlstate) );
    errmsg := CastIntegerToString( CastStringToInteger(errmsg) );

	if IsSQLRetOk( sqlret ) then
		Result := sqlstate + ' - ' + errmsg
	else
		Result := 'Error retriving error message!';

end;
Personal tools
Ads: