上面的是一部分,我们写数据库,用delphi写了一部分,C++写了大部分。
实现数据库一些功能,但还有SQL语句分析,和网络部分没写,最近很忙,可能空的时候再
完成,花了不少时间,我们取名叫九龙数据库。
做数据库引擎时,通过ODBC32.dll提供的函数实现的。相对比较简单点。
我就公开在这吧,希望对大家都有帮助。
unit UODBC32;
//****************************************************************************
// *
// * ODBC package.
// * www.labking.net
// *
//****************************************************************************
interface
//****************************************************************************
// SQL const definitions:
//****************************************************************************
const SQL_ERROR = -1;
const SQL_INVALID_HANDLE = -2;
const SQL_SUCCESS = 0;
const SQL_SUCCESS_WITH_INFO = 1;
const SQL_STILL_EXECUTING = 2;
const SQL_NEED_DATA = 99;
const SQL_NTS = -3;
//****************************************************************************
// SQL type definitions:
//****************************************************************************
const SQL_CHAR = 1;
const SQL_NUMERIC = 2;
const SQL_DECIMAL = 3;
const SQL_INTEGER = 4;
const SQL_SMALLINT = 5;
const SQL_FLOAT = 6;
const SQL_REAL = 7;
const SQL_DOUBLE = 8;
//****************************************************************************
// SQL function return type definitions:
//****************************************************************************
type SQLRETURN = smallint;
type SQLINTEGER= Integer;
type SQLSMALLINT=smallint;
type SQLCHAR = byte;
type PSQLCHAR = ^SQLCHAR;
function SQLAllocEnv(var hEnv: SQLINTEGER): SQLRETURN; stdcall;
function SQLAllocConnect(hEnv:SQLINTEGER; var hConnection: SQLINTEGER): SQLRETURN; stdcall;
function SQLConnect(
hCon: SQLINTEGER;
strServer: PChar; iServerNameLength: SQLINTEGER;
pcUserName: PChar; iUserNameLength: SQLINTEGER;
pcAuthentication: PChar; iAutherticationLength: SQLINTEGER): SQLRETURN; stdcall;
function SQLAllocStmt(hConnection: SQLINTEGER; var hStatement: SQLINTEGER): SQLRETURN; stdcall;
function SQLExecDirect(hStatement: SQLINTEGER; pcStatement: PChar; iLength: LongInt): SQLRETURN; stdcall;
function SQLExecute(hStatement: SQLINTEGER): SQLRETURN; stdcall;
function SQLGetData(
hStatement:SQLINTEGER;
iColumnNumber:SQLINTEGER; iTargetType: SQLINTEGER;
pTargetValue: Pointer; iBufferLength: SQLINTEGER;
var iStrLen_or_Ind: SQLINTEGER
): SQLRETURN; stdcall;
function SQLFreeConnect(hConnection: SQLINTEGER): SQLRETURN; stdcall;
function SQLDisconnect(hConnection: SQLINTEGER): SQLRETURN; stdcall;
function SQLFreeEnv(hEnvironment: SQLINTEGER): SQLRETURN; stdcall;
function SQLFreeHandle(iHandleType: SQLINTEGER; iHandle: SQLINTEGER): SQLRETURN; stdcall;
function SQLFreeStmt(hStatement: SQLINTEGER; iOption: SQLINTEGER): SQLRETURN; stdcall;
function SQLFetch(hStatement: SQLINTEGER): SQLRETURN; stdcall;
//*************************************************************************
// 动态结果数据集访问
//*************************************************************************
function SQLNumResultCols(hStatement: SQLINTEGER; var iColumnCount :SQLSMALLINT):SQLRETURN; stdcall;
function SQLRowCount(hStatement: SQLINTEGER; var iColumnCount :SQLSMALLINT):SQLRETURN; stdcall;
function SQLError(hEnviroment:SQLINTEGER; hConnection: SQLInteger; hStatement: SQLInteger;
pcState
SQLCHAR; var iNativeError: SQLINTEGER; pcMessageBox: PSQLCHAR;
iBufferLength: SQLSMALLINT; var iTextLength: SQLSMALLINT): SQLRETURN; stdcall;
function SQLGetSystemError(hEnv, hDbc, hStmt: SQLInteger): String;
implementation
uses SysUtils;
const LIB_ODBC32 = 'ODBC32.DLL';
function SQLTables: SQLINTEGER; external LIB_ODBC32 name 'SQLTables';
function SQLAllocEnv; external LIB_ODBC32 name 'SQLAllocEnv';
function SQLAllocConnect; external LIB_ODBC32 name 'SQLAllocConnect';
function SQLConnect; external LIB_ODBC32 name 'SQLConnect';
function SQLDisconnect; external LIB_ODBC32 name 'SQLDisconnect';
function SQLFreeConnect; external LIB_ODBC32 name 'SQLFreeConnect';
function SQLAllocStmt; external LIB_ODBC32 name 'SQLAllocStmt';
function SQLExecDirect; external LIB_ODBC32 name 'SQLExecDirect';
function SQLExecute; external LIB_ODBC32 name 'SQLExecute';
function SQLFetch; external LIB_ODBC32 name 'SQLFetch';
function SQLFreeEnv; external LIB_ODBC32 name 'SQLFreeEnv';
function SQLFreeHandle; external LIB_ODBC32 name 'SQLFreeHandle';
function SQLFreeStmt; external LIB_ODBC32 name 'SQLFreeStmt';
function SQLGetData; external LIB_ODBC32 name 'SQLGetData';
function SQLNumResultCols; external LIB_ODBC32 name 'SQLNumResultCols';
function SQLRowCount; external LIB_ODBC32 name 'SQLRowCount';
function SQLError; external LIB_ODBC32 name 'SQLError';
function SQLGetSystemError(hEnv, hDbc, hStmt: SQLInteger): String;
var
MsgL: SQLSMALLINT;
NativeError: SQLINTEGER;
State: Array [0..255] of char;
Msg: Array [0..255] of char;
begin
SQLError (hEnv, hDbc, hStmt, @State, NativeError,
@Msg, 128, MsgL);
result := 'Error:'+IntToStr(NativeError)+#13+#10+
'State:'+StrPas(@State)+#13+#10+
'Message:'+StrPas(@Msg);
//SystemDebug.printf("***Error: %d: %s: %s/n", NativeError, State, Msg);
end;
end.