dblib.pp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. {
  2. This file is part of the Free Component Library (FCL)
  3. Copyright (c) 2010 by the Free Pascal development team
  4. Header files Microsoft DB-Library for C: sqlfront.h, sqldb.h
  5. and FreeTDS: sybdb.h
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. The Original Code was created by (c) 2010 Ladislav Karrach (Windows)
  12. for the Free Pascal project.
  13. **********************************************************************
  14. FreeTDS (http://www.freetds.org/userguide/choosingtdsprotocol.htm):
  15. tds version = 5.0 - Sybase System 10 and above
  16. 7.0 - MS SQL Server 7
  17. 7.1 - MS SQL Server 2000 (*default*)
  18. 7.2 - MS SQL Server 2005
  19. 7.3 - MS SQL Server 2008
  20. tds version can be set using env.var. TDSVER or in freetds.conf or .freetds.conf
  21. }
  22. unit dblib;
  23. {$IFDEF FPC}{$mode objfpc}{$ENDIF}{$H+}
  24. { $DEFINE ntwdblib} //if you are using MS SQL Server Client Library (ntwdblib.dll)
  25. {$IFNDEF ntwdblib}
  26. {$DEFINE freetds} //if you are using db-lib from FreeTDS project (MS SQL Server + Sybase support)
  27. {$ENDIF}
  28. {$DEFINE LOAD_DYNAMICALLY}
  29. interface
  30. const
  31. DBLIBDLL=
  32. {$IFDEF WINDOWS}
  33. {$IFDEF ntwdblib}'ntwdblib.dll'{$ENDIF}
  34. {$IFDEF freetds} 'dblib.dll' {$ENDIF}
  35. {$ELSE}
  36. {$IFDEF DARWIN}
  37. 'libsybdb.dylib'
  38. {$ELSE}
  39. 'libsybdb.so'
  40. {$ENDIF}
  41. {$ENDIF}
  42. ;
  43. //from sybdb.h:
  44. //DBVERSION_xxx are used with dbsetlversion()
  45. DBVERSION_100= 2; // Sybase TDS 5.0
  46. DBVERSION_42 = 3; // This can be used for old Microsoft and Sybase servers
  47. DBVERSION_70 = 4;
  48. DBVERSION_71 = 5;
  49. DBVERSION_72 = 6;
  50. DBVERSION_73 = 7;
  51. //DBTDS_xxx are returned by DBTDS()
  52. DBTDS_UNKNOWN= 0;
  53. DBTDS_42 = 4; // SQL Server 4.2
  54. DBTDS_50 = 7; // Sybase SQL Server 5.0; use this for connecting to Sybase (ASA or ASE)
  55. DBTDS_70 = 8; // Microsoft SQL Server 7.0
  56. DBTDS_71 = 9; // Microsoft SQL Server 2000
  57. DBTDS_72 = 10; // Microsoft SQL Server 2005
  58. DBTDS_73 = 11; // Microsoft SQL Server 2008
  59. //from sqlfront.h , sybdb.h for FreeTDS
  60. DBSETHOST=1;
  61. DBSETUSER=2;
  62. DBSETPWD =3;
  63. DBSETAPP ={$IFDEF freetds}5{$ELSE}4{$ENDIF};
  64. {$IFDEF freetds}
  65. DBSETHID = 4;
  66. DBSETBCP = 6;
  67. DBSETNATLANG= 7;
  68. DBSETNOSHORT= 8;
  69. DBSETHIER = 9;
  70. DBSETCHARSET= 10;
  71. DBSETPACKET = 11;
  72. DBSETENCRYPT= 12;
  73. DBSETLABELED= 13;
  74. DBSETDBNAME = 14;
  75. {$ELSE}
  76. DBSETID = 5;
  77. DBSETLANG = 6;
  78. DBSETSECURE = 7;
  79. DBSET_LOGINTIME=10;
  80. DBSETFALLBACK=12;
  81. {$ENDIF}
  82. //These two are defined by Microsoft for dbsetlversion():
  83. DBVER42={$IFDEF freetds}DBVERSION_42{$ELSE}8{$ENDIF};
  84. DBVER60={$IFDEF freetds}DBVERSION_71{$ELSE}9{$ENDIF};
  85. //dboptions:
  86. DBNOAUTOFREE = {$IFDEF freetds}15{$ELSE}8{$ENDIF};
  87. DBTEXTLIMIT = {$IFDEF freetds}7{$ELSE}4{$ENDIF};
  88. DBTEXTSIZE = {$IFDEF freetds}17{$ELSE}5{$ENDIF};
  89. DBANSItoOEM = 14;
  90. DBOEMtoANSI = 15;
  91. DBQUOTEDIDENT= {$IFDEF freetds}35{$ELSE}18{$ENDIF};
  92. TIMEOUT_IGNORE=-1;
  93. TIMEOUT_INFINITE=0;
  94. SUCCEED=1;
  95. FAIL=0;
  96. NO_MORE_RESULTS=2;
  97. NO_MORE_RPC_RESULTS=3;
  98. MORE_ROWS=-1;
  99. REG_ROW=MORE_ROWS;
  100. NO_MORE_ROWS=-2;
  101. BUF_FULL=-3; //only if buffering is turned on
  102. INT_EXIT=0;
  103. INT_CONTINUE=1;
  104. INT_CANCEL=2;
  105. SQLVOID=$1f;
  106. SQLTEXT=$23;
  107. SQLVARBINARY=$25;
  108. SQLINTN=$26; //all nullable integers
  109. SQLVARCHAR=$27;
  110. SQLBINARY=$2d;
  111. SQLIMAGE=$22;
  112. SQLCHAR=$2f;
  113. SQLINT1=$30;
  114. SQLBIT=$32;
  115. SQLINT2=$34;
  116. SQLINT4=$38;
  117. SQLMONEY=$3c;
  118. SQLDATETIME=$3d;
  119. SQLFLT8=$3e;
  120. SQLFLTN=$6d;
  121. SQLMONEYN=$6e;
  122. SQLDATETIMN=$6f;
  123. SQLFLT4=$3b;
  124. SQLMONEY4=$7a;
  125. SQLDATETIM4=$3a;
  126. SQLDECIMAL=$6a;
  127. SQLNUMERIC=$6c;
  128. //from tds.h:
  129. SYBNTEXT=$63;
  130. SYBINT8=$7F;
  131. SYBUNIQUE=$24;
  132. SYBVARIANT=$62;
  133. //XSYBVARCHAR=$A7;
  134. //XSYBNVARCHAR=$E7;
  135. //XSYBNCHAR = $EF;
  136. //XSYBBINARY= $AD;
  137. MAXTABLENAME ={$IFDEF freetds}512+1{$ELSE}30{$ENDIF};
  138. MAXCOLNAMELEN={$IFDEF freetds}512+1{$ELSE}30{$ENDIF};
  139. MAXNUMERICLEN={$IFDEF freetds}32 {$ELSE}16{$ENDIF};
  140. DBMAXCHAR=256; // Max length of DBVARBINARY and DBVARCHAR, etc.
  141. DEFAULTPRECISION = 18;
  142. DEFAULTSCALE = 0;
  143. // Used by dbcolinfo:
  144. CI_REGULAR=1;
  145. CI_ALTERNATE=2;
  146. CI_CURSOR=3;
  147. DBUNKNOWN = 2; //FALSE = 0, TRUE = 1
  148. // Error codes:
  149. SYBEFCON = 20002; // SQL Server connection failed
  150. type
  151. PLOGINREC=Pointer;
  152. PDBPROCESS=Pointer;
  153. RETCODE=integer;
  154. STATUS=integer;
  155. INT=longint;
  156. SHORT=smallint;
  157. BOOL=longbool;
  158. ULONG=longword;
  159. // DB-Library datatypes
  160. DBBOOL=byte; // unsigned char
  161. DBCHAR=shortint;
  162. DBBIT=byte;
  163. DBTINYINT=byte;
  164. DBSMALLINT=smallint; // 16-bit int (short)
  165. DBUSMALLINT=word; // 16-bit unsigned int (unsigned short)
  166. DBINT=longint; // 32-bit int (int)
  167. DBFLT8=double; // 64-bit real (double)
  168. DBBINARY=byte;
  169. {$PACKRECORDS C}
  170. DBDATETIME=packed record
  171. dtdays: DBINT;
  172. dttime: ULONG;
  173. end;
  174. PDBDATETIME=^DBDATETIME;
  175. // DBDATEREC structure used by dbdatecrack
  176. DBDATEREC=packed record
  177. case boolean of
  178. false:(
  179. oldyear:INT; // 1753 - 9999
  180. oldmonth: INT; // 1 - 12
  181. oldday: INT; // 1 - 31
  182. olddayofyear: INT; // 1 - 366 (in sybdb.h dayofyear and day are changed around!)
  183. oldweekday: INT; // 1 - 7 (Mon - Sun)
  184. oldhour: INT; // 0 - 23
  185. oldminute: INT; // 0 - 59
  186. oldsecond: INT; // 0 - 59
  187. oldmillisecond: INT; // 0 - 999
  188. oldtzone: INT; // 0 - 127 (Sybase only!)
  189. );
  190. true:(
  191. year:INT; // 1753 - 9999
  192. quarter:INT; // 1 - 4
  193. month: INT; // 1 - 12
  194. {$IFDEF freetds}
  195. day: INT; // 1 - 31
  196. dayofyear: INT; // 1 - 366 (in sybdb.h dayofyear and day are changed around!)
  197. {$ELSE}
  198. dayofyear: INT; // 1 - 366 (in sybdb.h dayofyear and day are changed around!)
  199. day: INT; // 1 - 31
  200. {$ENDIF}
  201. week: INT; // 1 - 54 (for leap years)
  202. weekday: INT; // 1 - 7 (Mon - Sun)
  203. hour: INT; // 0 - 23
  204. minute: INT; // 0 - 59
  205. second: INT; // 0 - 59
  206. millisecond: INT; // 0 - 999
  207. tzone: INT; // 0 - 127 (Sybase only!)
  208. );
  209. end;
  210. PDBDATEREC=^DBDATEREC;
  211. DBNUMERIC=packed record
  212. precision: BYTE;
  213. scale: BYTE;
  214. sign: BYTE; // 1 = Positive, 0 = Negative
  215. val: array[0..MAXNUMERICLEN-1] of BYTE;
  216. end;
  217. DBVARYBIN=packed record
  218. len: {$IFDEF freetds}DBINT{$ELSE}DBSMALLINT{$ENDIF};
  219. bytes: array[0..DBMAXCHAR-1] of BYTE;
  220. end;
  221. DBVARYCHAR=packed record
  222. len: {$IFDEF freetds}DBINT{$ELSE}DBSMALLINT{$ENDIF};
  223. str: array[0..DBMAXCHAR-1] of AnsiChar;
  224. end;
  225. DBERRHANDLE_PROC=function(dbproc: PDBPROCESS; severity, dberr, oserr:INT; dberrstr, oserrstr:PChar):INT; cdecl;
  226. DBMSGHANDLE_PROC=function(dbproc: PDBPROCESS; msgno: DBINT; msgstate, severity:INT; msgtext, srvname, procname:PChar; line:DBUSMALLINT):INT; cdecl;
  227. {$IFDEF ntwdblib}
  228. {$PACKRECORDS 2}
  229. {$ENDIF}
  230. DBCOL=record
  231. SizeOfStruct: DBINT;
  232. Name: array[0..MAXCOLNAMELEN] of AnsiChar;
  233. ActualName: array[0..MAXCOLNAMELEN] of AnsiChar;
  234. TableName: array[0..MAXTABLENAME] of AnsiChar;
  235. Typ: SHORT;
  236. UserType: DBINT;
  237. MaxLength: DBINT;
  238. Precision: BYTE;
  239. Scale: BYTE;
  240. VarLength: BOOL; // TRUE, FALSE
  241. Null: BYTE; // TRUE, FALSE or DBUNKNOWN
  242. CaseSensitive: BYTE; // TRUE, FALSE or DBUNKNOWN
  243. Updatable: BYTE; // TRUE, FALSE or DBUNKNOWN
  244. Identity: BOOL; // TRUE, FALSE
  245. end;
  246. PDBCOL=^DBCOL;
  247. {$PACKRECORDS DEFAULT}
  248. var
  249. DBLibInit: boolean=false; //was dbinit() already called ?
  250. {$IFNDEF LOAD_DYNAMICALLY}
  251. function dbinit():{$IFDEF freetds}RETCODE{$ELSE}PChar{$ENDIF}; cdecl; external DBLIBDLL;
  252. function dblogin():PLOGINREC; cdecl; external DBLIBDLL;
  253. function dbsetlname(login:PLOGINREC; value:PChar; which:INT):RETCODE; cdecl; external DBLIBDLL;
  254. function dbsetlogintime(seconds:INT):RETCODE; cdecl; external DBLIBDLL;
  255. function dbsettime(seconds:INT):RETCODE; cdecl; external DBLIBDLL;
  256. function dberrhandle(handler:DBERRHANDLE_PROC):DBERRHANDLE_PROC; cdecl; external DBLIBDLL;
  257. function dbmsghandle(handler:DBMSGHANDLE_PROC):DBMSGHANDLE_PROC; cdecl; external DBLIBDLL;
  258. function dbsetopt(dbproc:PDBPROCESS; option: INT; param:PChar {$IFDEF freetds};int_param:INT{$ENDIF}):RETCODE; cdecl; external DBLIBDLL;
  259. function dbuse(dbproc:PDBPROCESS; dbname:PChar):RETCODE; cdecl; external DBLIBDLL;
  260. function dbcmd(dbproc:PDBPROCESS; cmdstring:PChar):RETCODE; cdecl; external DBLIBDLL;
  261. function dbcmdrow(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  262. function dbsqlexec(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  263. function dbresults(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  264. function dbmorecmds(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  265. function dbnextrow(dbproc:PDBPROCESS):STATUS; cdecl; external DBLIBDLL;
  266. function dbnumcols(dbproc:PDBPROCESS):INT; cdecl; external DBLIBDLL;
  267. function dbcolname(dbproc:PDBPROCESS; column:INT):PChar; cdecl; external DBLIBDLL;
  268. function dbcoltype(dbproc:PDBPROCESS; column:INT):INT; cdecl; external DBLIBDLL;
  269. function dbcollen(dbproc:PDBPROCESS; column:INT):DBINT; cdecl; external DBLIBDLL;
  270. function dbcolinfo(dbproc:PDBPROCESS; typ:INT; column:DBINT; computeid:DBINT; dbcol:PDBCOL):RETCODE; cdecl; external DBLIBDLL;
  271. function dbprtype(token:INT):PChar; cdecl; external DBLIBDLL;
  272. function dbdatlen(dbproc:PDBPROCESS; column:INT):DBINT; cdecl; external DBLIBDLL;
  273. function dbdata(dbproc:PDBPROCESS; column:INT):PByte; cdecl; external DBLIBDLL;
  274. function dbconvert(dbproc:PDBPROCESS; srctype:INT; src:PByte; srclen:DBINT; desttype:INT; dest:PByte; destlen:DBINT):INT; cdecl; external DBLIBDLL;
  275. function dbdatecrack(dbproc:PDBPROCESS; dateinfo:PDBDATEREC; datetime: PDBDATETIME):RETCODE; cdecl; external DBLIBDLL;
  276. function dbcount(dbproc:PDBPROCESS):DBINT; cdecl; external DBLIBDLL;
  277. function dbiscount(dbproc:PDBPROCESS):BOOL; cdecl; external DBLIBDLL;
  278. function dbcancel(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  279. function dbcanquery(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  280. function dbhasretstat(dbproc:PDBPROCESS):DBBOOL; cdecl; external DBLIBDLL;
  281. function dbretstatus(dbproc:PDBPROCESS):DBINT; cdecl; external DBLIBDLL;
  282. procedure dbfreelogin(login:PLOGINREC); cdecl; external DBLIBDLL {$IFDEF freetds}name 'dbloginfree'{$ENDIF};
  283. procedure dbexit(); cdecl; external DBLIBDLL;
  284. {$IFDEF ntwdblib}
  285. function dbopen(login:PLOGINREC; servername:PChar):PDBPROCESS; cdecl; external DBLIBDLL;
  286. function dbclose(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  287. procedure dbwinexit; cdecl; external DBLIBDLL;
  288. {$ENDIF}
  289. {$IFDEF freetds}
  290. function tdsdbopen(login:PLOGINREC; servername:PChar; msdblib:INT):PDBPROCESS; cdecl; external DBLIBDLL;
  291. function dbtablecolinfo(dbproc:PDBPROCESS; column:DBINT; dbcol:PDBCOL):RETCODE; cdecl; external DBLIBDLL;
  292. function dbtds(dbproc:PDBPROCESS):INT; cdecl; external DBLIBDLL;
  293. function dbsetlversion(login:PLOGINREC; version:BYTE):RETCODE; cdecl; external DBLIBDLL;
  294. function dbservcharset(dbproc:PDBPROCESS):PChar; cdecl; external DBLIBDLL;
  295. procedure dbclose(dbproc:PDBPROCESS); cdecl; external DBLIBDLL;
  296. {$ENDIF}
  297. {$ELSE}
  298. var
  299. dbinit: function():{$IFDEF freetds}RETCODE{$ELSE}PChar{$ENDIF}; cdecl;
  300. dblogin: function():PLOGINREC; cdecl;
  301. dbsetlname: function(login:PLOGINREC; value:PChar; which:INT):RETCODE; cdecl;
  302. dbsetlogintime: function(seconds:INT):RETCODE; cdecl;
  303. dbsettime: function(seconds:INT):RETCODE; cdecl;
  304. dberrhandle: function(handler:DBERRHANDLE_PROC):DBERRHANDLE_PROC; cdecl;
  305. dbmsghandle: function(handler:DBMSGHANDLE_PROC):DBMSGHANDLE_PROC; cdecl;
  306. dbsetopt: function(dbproc:PDBPROCESS; option: INT; param:PChar {$IFDEF freetds};int_param:INT{$ENDIF}):RETCODE; cdecl;
  307. dbuse: function(dbproc:PDBPROCESS; dbname:PChar):RETCODE; cdecl;
  308. dbcmd: function(dbproc:PDBPROCESS; cmdstring:PChar):RETCODE; cdecl;
  309. dbcmdrow: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  310. dbsqlexec: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  311. dbresults: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  312. dbmorecmds: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  313. dbnextrow: function(dbproc:PDBPROCESS):STATUS; cdecl;
  314. dbnumcols: function(dbproc:PDBPROCESS):INT; cdecl;
  315. dbcolname: function(dbproc:PDBPROCESS; column:INT):PChar; cdecl;
  316. dbcoltype: function(dbproc:PDBPROCESS; column:INT):INT; cdecl;
  317. dbcollen: function(dbproc:PDBPROCESS; column:INT):DBINT; cdecl;
  318. dbcolinfo: function(dbproc:PDBPROCESS; typ:INT; column:DBINT; computeid:DBINT; dbcol:PDBCOL):RETCODE; cdecl;
  319. dbprtype: function(token:INT):PChar; cdecl;
  320. dbdatlen: function(dbproc:PDBPROCESS; column:INT):DBINT; cdecl;
  321. dbdata: function(dbproc:PDBPROCESS; column:INT):PByte; cdecl;
  322. dbconvert: function(dbproc:PDBPROCESS; srctype:INT; src:PByte; srclen:DBINT; desttype:INT; dest:PByte; destlen:DBINT):INT; cdecl;
  323. dbdatecrack: function(dbproc:PDBPROCESS; dateinfo:PDBDATEREC; datetime: PDBDATETIME):RETCODE; cdecl;
  324. dbcount: function(dbproc:PDBPROCESS):DBINT; cdecl;
  325. dbiscount: function(dbproc:PDBPROCESS):BOOL; cdecl;
  326. dbcancel: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  327. dbcanquery: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  328. dbhasretstat: function(dbproc:PDBPROCESS):DBBOOL; cdecl;
  329. dbretstatus: function(dbproc:PDBPROCESS):DBINT; cdecl;
  330. dbexit: procedure(); cdecl;
  331. dbfreelogin: procedure(login:PLOGINREC); cdecl;
  332. {$IFDEF ntwdblib}
  333. dbopen: function(login:PLOGINREC; servername:PChar):PDBPROCESS; cdecl;
  334. dbclose: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  335. dbwinexit: procedure; cdecl;
  336. {$ENDIF}
  337. {$IFDEF freetds}
  338. tdsdbopen: function(login:PLOGINREC; servername:PChar; msdblib:INT):PDBPROCESS; cdecl;
  339. dbtablecolinfo: function(dbproc:PDBPROCESS; column:DBINT; dbcol:PDBCOL):RETCODE; cdecl;
  340. dbtds: function(dbproc:PDBPROCESS):INT; cdecl;
  341. dbsetlversion: function(login:PLOGINREC; version:BYTE):RETCODE; cdecl;
  342. dbservcharset: function(dbproc:PDBPROCESS):PChar; cdecl;
  343. dbclose: procedure(dbproc:PDBPROCESS); cdecl;
  344. {$ENDIF}
  345. DefaultDBLibLibraryName: string = DBLIBDLL;
  346. DBLibLoadedLibrary: string = '';
  347. {$ENDIF}
  348. {$IFDEF ntwdblib}
  349. function tdsdbopen(login:PLOGINREC; servername:PChar; msdblib:INT):PDBPROCESS;
  350. function dbtablecolinfo(dbproc:PDBPROCESS; column:DBINT; dbcol:PDBCOL):RETCODE;
  351. function dbsetlversion(login:PLOGINREC; version:BYTE):RETCODE;
  352. function dbtds(dbproc:PDBPROCESS):INT;
  353. function dbversion():PChar;
  354. {$ENDIF}
  355. {$IFDEF freetds}
  356. function dbopen(login:PLOGINREC; servername:PChar):PDBPROCESS;
  357. procedure dbwinexit;
  358. {$ENDIF}
  359. function dbsetlcharset(login:PLOGINREC; charset:PChar):RETCODE;
  360. function dbsetlsecure(login:PLOGINREC):RETCODE;
  361. function InitialiseDBLib(const LibraryName : ansistring): integer;
  362. procedure ReleaseDBLib;
  363. implementation
  364. {$IFDEF LOAD_DYNAMICALLY}
  365. uses SysUtils, Dynlibs;
  366. var DBLibLibraryHandle: TLibHandle;
  367. RefCount: integer;
  368. function InitialiseDBLib(const LibraryName : ansistring): integer;
  369. var libname : string;
  370. begin
  371. inc(RefCount);
  372. Result:=RefCount;
  373. if RefCount = 1 then
  374. begin
  375. if LibraryName='' then
  376. libname:=DefaultDBLibLibraryName
  377. else
  378. libname:=LibraryName;
  379. DBLibLibraryHandle := LoadLibrary(libname);
  380. if DBLibLibraryHandle = nilhandle then
  381. begin
  382. RefCount := 0;
  383. raise EInOutError.CreateFmt('Can not load DB-Lib client library "%s". Check your installation.'+LineEnding+'%s',
  384. [libname, SysErrorMessage(GetLastOSError)]);
  385. end;
  386. DBLibLoadedLibrary := libname;
  387. pointer(dbinit) := GetProcedureAddress(DBLibLibraryHandle,'dbinit');
  388. pointer(dblogin) := GetProcedureAddress(DBLibLibraryHandle,'dblogin');
  389. pointer(dbsetlname) := GetProcedureAddress(DBLibLibraryHandle,'dbsetlname');
  390. pointer(dbsetlogintime) := GetProcedureAddress(DBLibLibraryHandle,'dbsetlogintime');
  391. pointer(dbsettime) := GetProcedureAddress(DBLibLibraryHandle,'dbsettime');
  392. pointer(dberrhandle) := GetProcedureAddress(DBLibLibraryHandle,'dberrhandle');
  393. pointer(dbmsghandle) := GetProcedureAddress(DBLibLibraryHandle,'dbmsghandle');
  394. pointer(dbsetopt) := GetProcedureAddress(DBLibLibraryHandle,'dbsetopt');
  395. pointer(dbuse) := GetProcedureAddress(DBLibLibraryHandle,'dbuse');
  396. pointer(dbcmd) := GetProcedureAddress(DBLibLibraryHandle,'dbcmd');
  397. pointer(dbcmdrow) := GetProcedureAddress(DBLibLibraryHandle,'dbcmdrow');
  398. pointer(dbsqlexec) := GetProcedureAddress(DBLibLibraryHandle,'dbsqlexec');
  399. pointer(dbresults) := GetProcedureAddress(DBLibLibraryHandle,'dbresults');
  400. pointer(dbmorecmds) := GetProcedureAddress(DBLibLibraryHandle,'dbmorecmds');
  401. pointer(dbnextrow) := GetProcedureAddress(DBLibLibraryHandle,'dbnextrow');
  402. pointer(dbnumcols) := GetProcedureAddress(DBLibLibraryHandle,'dbnumcols');
  403. pointer(dbcolname) := GetProcedureAddress(DBLibLibraryHandle,'dbcolname');
  404. pointer(dbcoltype) := GetProcedureAddress(DBLibLibraryHandle,'dbcoltype');
  405. pointer(dbcollen) := GetProcedureAddress(DBLibLibraryHandle,'dbcollen');
  406. pointer(dbcolinfo) := GetProcedureAddress(DBLibLibraryHandle,'dbcolinfo');
  407. pointer(dbprtype) := GetProcedureAddress(DBLibLibraryHandle,'dbprtype');
  408. pointer(dbdatlen) := GetProcedureAddress(DBLibLibraryHandle,'dbdatlen');
  409. pointer(dbdata) := GetProcedureAddress(DBLibLibraryHandle,'dbdata');
  410. pointer(dbconvert) := GetProcedureAddress(DBLibLibraryHandle,'dbconvert');
  411. pointer(dbdatecrack) := GetProcedureAddress(DBLibLibraryHandle,'dbdatecrack');
  412. pointer(dbcount) := GetProcedureAddress(DBLibLibraryHandle,'dbcount');
  413. pointer(dbiscount) := GetProcedureAddress(DBLibLibraryHandle,'dbiscount');
  414. pointer(dbcancel) := GetProcedureAddress(DBLibLibraryHandle,'dbcancel');
  415. pointer(dbcanquery) := GetProcedureAddress(DBLibLibraryHandle,'dbcanquery');
  416. pointer(dbhasretstat) := GetProcedureAddress(DBLibLibraryHandle,'dbhasretstat');
  417. pointer(dbretstatus) := GetProcedureAddress(DBLibLibraryHandle,'dbretstatus');
  418. pointer(dbexit) := GetProcedureAddress(DBLibLibraryHandle,'dbexit');
  419. pointer(dbfreelogin) := GetProcedureAddress(DBLibLibraryHandle,{$IFDEF freetds}'dbloginfree'{$ELSE}'dbfreelogin'{$ENDIF});
  420. pointer(dbclose) := GetProcedureAddress(DBLibLibraryHandle,'dbclose');
  421. {$IFDEF ntwdblib}
  422. pointer(dbopen) := GetProcedureAddress(DBLibLibraryHandle,'dbopen');
  423. pointer(dbwinexit) := GetProcedureAddress(DBLibLibraryHandle,'dbwinexit');
  424. {$ENDIF}
  425. {$IFDEF freetds}
  426. pointer(tdsdbopen) := GetProcedureAddress(DBLibLibraryHandle,'tdsdbopen');
  427. pointer(dbtablecolinfo) := GetProcedureAddress(DBLibLibraryHandle,'dbtablecolinfo');
  428. pointer(dbtds) := GetProcedureAddress(DBLibLibraryHandle,'dbtds');
  429. pointer(dbsetlversion) := GetProcedureAddress(DBLibLibraryHandle,'dbsetlversion');
  430. pointer(dbservcharset) := GetProcedureAddress(DBLibLibraryHandle,'dbservcharset');
  431. //if not assigned(dbiscount) then
  432. // raise EInOutError.Create('Minimum supported version of FreeTDS client library is 0.91!');
  433. {$ENDIF}
  434. DBLibInit:=false;
  435. end;
  436. end;
  437. procedure ReleaseDBLib;
  438. begin
  439. if RefCount > 0 then dec(RefCount);
  440. if RefCount = 0 then
  441. begin
  442. dbexit;{$IFDEF WINDOWS}dbwinexit;{$ENDIF}
  443. if UnloadLibrary(DBLibLibraryHandle) then
  444. begin
  445. DBLibLibraryHandle := NilHandle;
  446. DBLibLoadedLibrary := '';
  447. end
  448. else
  449. inc(RefCount);
  450. end;
  451. end;
  452. {$ELSE}
  453. procedure InitialiseDBLib(LibraryName : string);
  454. begin
  455. //no-op for static linked
  456. end;
  457. procedure ReleaseDBLib;
  458. begin
  459. //no-op for static linked
  460. end;
  461. {$ENDIF LOAD_DYNAMICALLY}
  462. //functions, which are not implemented by FreeTDS:
  463. {$IFDEF freetds}
  464. function dbopen(login:PLOGINREC; servername:PChar):PDBPROCESS;
  465. begin
  466. Result:=tdsdbopen(login, servername, 1{1=MSDBLIB or 0=SYBDBLIB});
  467. end;
  468. function dbsetlcharset(login:PLOGINREC; charset:PChar):RETCODE;
  469. begin
  470. Result:=dbsetlname(login, charset, 10);
  471. end;
  472. function dbsetlsecure(login:PLOGINREC):RETCODE;
  473. begin
  474. //not implemented; see http://www.freetds.org/userguide/domains.htm
  475. Result:=SUCCEED;
  476. end;
  477. procedure dbwinexit;
  478. begin
  479. //do nothing
  480. end;
  481. {$ENDIF}
  482. //functions which are not implemented by ntwdblib:
  483. {$IFDEF ntwdblib}
  484. function tdsdbopen(login:PLOGINREC; servername:PChar; msdblib:INT):PDBPROCESS;
  485. begin
  486. Result:=dbopen(login, servername);
  487. end;
  488. function dbtablecolinfo(dbproc:PDBPROCESS; column:DBINT; dbcol:PDBCOL):RETCODE;
  489. begin
  490. Result:=dbcolinfo(dbproc, CI_REGULAR, column, 0, dbcol);
  491. if dbcol^.VarLength {true also when column is nullable} then
  492. case dbcol^.Typ of
  493. SQLCHAR : dbcol^.Typ := SQLVARCHAR;
  494. SQLBINARY: dbcol^.Typ := SQLVARBINARY;
  495. end;
  496. end;
  497. function dbsetlversion(login:PLOGINREC; version:BYTE):RETCODE;
  498. begin
  499. Result:=dbsetlname(login, nil, version);
  500. end;
  501. function dbsetlcharset(login:PLOGINREC; charset:PChar):RETCODE;
  502. begin
  503. Result:=SUCCEED;
  504. end;
  505. function dbsetlsecure(login:PLOGINREC):RETCODE;
  506. begin
  507. Result:=dbsetlname(login, nil, DBSETSECURE);
  508. end;
  509. function dbtds(dbproc:PDBPROCESS):INT;
  510. begin
  511. Result:=0;
  512. end;
  513. function dbversion():PChar;
  514. begin
  515. Result:='DB Library version 8.00';
  516. end;
  517. {$ENDIF}
  518. {
  519. //ntwdblib uses low significant values first
  520. //freetds uses variable length array (based on precision) see numeric.c: tds_numeric_bytes_per_prec
  521. // and starts from high significant values first
  522. function dbnumerictobcd(dbnum: DBNUMERIC): TBCD;
  523. var i: integer;
  524. intval,intbase,intdiv: int64;
  525. bcdval,bcdbase,bcddiv, bcd1: TBCD;
  526. begin
  527. intval:=0;
  528. intbase:=1;
  529. for i:=0 to 6 do
  530. begin
  531. intval := intval + dbnum.val[i] * intbase;
  532. intbase:= intbase*256;
  533. end;
  534. bcdval := IntegerToBCD(intval);
  535. if dbnum.precision > 16 then
  536. begin
  537. bcdbase := IntegerToBCD(intbase);
  538. for i:=7 to length(dbnum.val)-1 do
  539. begin
  540. BCDMultiply(bcdbase, integer(dbnum.val[i]), bcd1);
  541. BCDAdd(bcdval, bcd1, bcdval);
  542. BCDMultiply(bcdbase, 256, bcdbase);
  543. end;
  544. end;
  545. if dbnum.scale > 18 then
  546. begin
  547. bcddiv:=IntegerToBCD(int64(1000000000000000000));
  548. for i:=19 to dbnum.scale do BCDMultiply(bcddiv, 10, bcddiv);
  549. end
  550. else
  551. begin
  552. intdiv:=1;
  553. for i:=1 to dbnum.scale do intdiv:=intdiv*10;
  554. bcddiv:=IntegerToBCD(intdiv);
  555. end;
  556. BCDDivide(bcdval, bcddiv, Result);
  557. if dbnum.sign=0 then BCDNegate(Result);
  558. end;
  559. }
  560. {$IFNDEF LOAD_DYNAMICALLY}
  561. finalization
  562. dbexit; {$IFDEF WINDOWS}dbwinexit;{$ENDIF}
  563. {$ENDIF}
  564. end.