dblib.pp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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 proto.h:
  129. SYBNTEXT=$63;
  130. // MS only types:
  131. SYBINT8 =$7F;
  132. SYBUNIQUE =$24;
  133. SYBVARIANT=$62;
  134. SYBMSUDT =$F0;
  135. SYBMSXML =$F1;
  136. SYBMSDATE =$28;
  137. SYBMSTIME =$29;
  138. SYBMSDATETIME2=$2A;
  139. SYBMSDATETIMEOFFSET=$2B;
  140. MAXTABLENAME ={$IFDEF freetds}512+1{$ELSE}30{$ENDIF};
  141. MAXCOLNAMELEN={$IFDEF freetds}512+1{$ELSE}30{$ENDIF};
  142. MAXNUMERICLEN={$IFDEF freetds}32 {$ELSE}16{$ENDIF};
  143. DBMAXCHAR=256; // Max length of DBVARBINARY and DBVARCHAR, etc.
  144. DEFAULTPRECISION = 18;
  145. DEFAULTSCALE = 0;
  146. // Used by dbcolinfo:
  147. CI_REGULAR=1;
  148. CI_ALTERNATE=2;
  149. CI_CURSOR=3;
  150. DBUNKNOWN = 2; //FALSE = 0, TRUE = 1
  151. // Error codes:
  152. SYBEFCON = 20002; // SQL Server connection failed
  153. SYBESMSG = 20018; // General SQL Server error: Check messages from the SQL Server.
  154. type
  155. PLOGINREC=Pointer;
  156. PDBPROCESS=Pointer;
  157. RETCODE=integer;
  158. STATUS=integer;
  159. INT=longint;
  160. SHORT=smallint;
  161. BOOL=longbool;
  162. ULONG=longword;
  163. // DB-Library datatypes
  164. DBBOOL=byte; // unsigned char
  165. DBCHAR=shortint;
  166. DBBIT=byte;
  167. DBTINYINT=byte;
  168. DBSMALLINT=smallint; // 16-bit int (short)
  169. DBUSMALLINT=word; // 16-bit unsigned int (unsigned short)
  170. DBINT=longint; // 32-bit int (int)
  171. DBFLT8=double; // 64-bit real (double)
  172. DBBINARY=byte;
  173. {$PACKRECORDS C}
  174. DBDATETIME=packed record
  175. dtdays: DBINT;
  176. dttime: ULONG;
  177. end;
  178. PDBDATETIME=^DBDATETIME;
  179. DBDATETIMEALL=record
  180. time: qword; // time, 7 digit precision (64-bit unsigned)
  181. date: longint; // date, 0 = 1900-01-01 (32-bit int)
  182. offset: smallint; // time offset (16-bit int)
  183. info: word; // unsigned short time_prec:3;
  184. // unsigned short _res:10;
  185. // unsigned short has_time:1;
  186. // unsigned short has_date:1;
  187. // unsigned short has_offset:1;
  188. end;
  189. PDBDATETIMEALL=^DBDATETIMEALL;
  190. // DBDATEREC structure used by dbdatecrack
  191. DBDATEREC=packed record
  192. case boolean of
  193. false:(
  194. oldyear:INT; // 1753 - 9999
  195. oldmonth: INT; // 1 - 12
  196. oldday: INT; // 1 - 31
  197. olddayofyear: INT; // 1 - 366 (in sybdb.h dayofyear and day are changed around!)
  198. oldweekday: INT; // 1 - 7 (Mon - Sun)
  199. oldhour: INT; // 0 - 23
  200. oldminute: INT; // 0 - 59
  201. oldsecond: INT; // 0 - 59
  202. oldmillisecond: INT; // 0 - 999
  203. oldtzone: INT; // 0 - 127 (Sybase only!)
  204. );
  205. true:(
  206. year:INT; // 1753 - 9999
  207. quarter:INT; // 1 - 4
  208. month: INT; // 1 - 12
  209. {$IFDEF freetds}
  210. day: INT; // 1 - 31
  211. dayofyear: INT; // 1 - 366 (in sybdb.h dayofyear and day are changed around!)
  212. {$ELSE}
  213. dayofyear: INT; // 1 - 366 (in sybdb.h dayofyear and day are changed around!)
  214. day: INT; // 1 - 31
  215. {$ENDIF}
  216. week: INT; // 1 - 54 (for leap years)
  217. weekday: INT; // 1 - 7 (Mon - Sun)
  218. hour: INT; // 0 - 23
  219. minute: INT; // 0 - 59
  220. second: INT; // 0 - 59
  221. millisecond: INT; // 0 - 999
  222. tzone: INT; // 0 - 127 (Sybase only!)
  223. );
  224. end;
  225. PDBDATEREC=^DBDATEREC;
  226. DBMONEY=record
  227. mnyhigh: DBINT;
  228. mnylow: ULONG;
  229. end;
  230. DBNUMERIC=packed record
  231. precision: BYTE;
  232. scale: BYTE;
  233. sign: BYTE; // 1 = Positive, 0 = Negative
  234. val: array[0..MAXNUMERICLEN-1] of BYTE;
  235. end;
  236. DBVARYBIN=packed record
  237. len: {$IFDEF freetds}DBINT{$ELSE}DBSMALLINT{$ENDIF};
  238. bytes: array[0..DBMAXCHAR-1] of BYTE;
  239. end;
  240. DBVARYCHAR=packed record
  241. len: {$IFDEF freetds}DBINT{$ELSE}DBSMALLINT{$ENDIF};
  242. str: array[0..DBMAXCHAR-1] of AnsiChar;
  243. end;
  244. DBERRHANDLE_PROC=function(dbproc: PDBPROCESS; severity, dberr, oserr:INT; dberrstr, oserrstr:PAnsiChar):INT; cdecl;
  245. DBMSGHANDLE_PROC=function(dbproc: PDBPROCESS; msgno: DBINT; msgstate, severity:INT; msgtext, srvname, procname:PAnsiChar; line:DBUSMALLINT):INT; cdecl;
  246. {$IFDEF ntwdblib}
  247. {$PACKRECORDS 2}
  248. {$ENDIF}
  249. DBCOL=record
  250. SizeOfStruct: DBINT;
  251. Name: array[0..MAXCOLNAMELEN] of AnsiChar;
  252. ActualName: array[0..MAXCOLNAMELEN] of AnsiChar;
  253. TableName: array[0..MAXTABLENAME] of AnsiChar;
  254. Typ: SHORT;
  255. UserType: DBINT;
  256. MaxLength: DBINT;
  257. Precision: BYTE;
  258. Scale: BYTE;
  259. VarLength: BOOL; // TRUE, FALSE
  260. Null: BYTE; // TRUE, FALSE or DBUNKNOWN
  261. CaseSensitive: BYTE; // TRUE, FALSE or DBUNKNOWN
  262. Updatable: BYTE; // TRUE, FALSE or DBUNKNOWN
  263. Identity: BOOL; // TRUE, FALSE
  264. end;
  265. PDBCOL=^DBCOL;
  266. {$PACKRECORDS DEFAULT}
  267. var
  268. DBLibInit: boolean=false; //was dbinit() already called ?
  269. {$IFNDEF LOAD_DYNAMICALLY}
  270. function dbinit():{$IFDEF freetds}RETCODE{$ELSE}PAnsiChar{$ENDIF}; cdecl; external DBLIBDLL;
  271. function dblogin():PLOGINREC; cdecl; external DBLIBDLL;
  272. function dbsetlname(login:PLOGINREC; value:PAnsiChar; which:INT):RETCODE; cdecl; external DBLIBDLL;
  273. function dbsetlogintime(seconds:INT):RETCODE; cdecl; external DBLIBDLL;
  274. function dbsettime(seconds:INT):RETCODE; cdecl; external DBLIBDLL;
  275. function dberrhandle(handler:DBERRHANDLE_PROC):DBERRHANDLE_PROC; cdecl; external DBLIBDLL;
  276. function dbmsghandle(handler:DBMSGHANDLE_PROC):DBMSGHANDLE_PROC; cdecl; external DBLIBDLL;
  277. function dbsetopt(dbproc:PDBPROCESS; option: INT; param:PAnsiChar {$IFDEF freetds};int_param:INT{$ENDIF}):RETCODE; cdecl; external DBLIBDLL;
  278. function dbuse(dbproc:PDBPROCESS; dbname:PAnsiChar):RETCODE; cdecl; external DBLIBDLL;
  279. function dbcmd(dbproc:PDBPROCESS; cmdstring:PAnsiChar):RETCODE; cdecl; external DBLIBDLL;
  280. function dbcmdrow(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  281. function dbsqlexec(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  282. function dbresults(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  283. function dbmorecmds(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  284. function dbnextrow(dbproc:PDBPROCESS):STATUS; cdecl; external DBLIBDLL;
  285. function dbnumcols(dbproc:PDBPROCESS):INT; cdecl; external DBLIBDLL;
  286. function dbcolname(dbproc:PDBPROCESS; column:INT):PAnsiChar; cdecl; external DBLIBDLL;
  287. function dbcoltype(dbproc:PDBPROCESS; column:INT):INT; cdecl; external DBLIBDLL;
  288. function dbcollen(dbproc:PDBPROCESS; column:INT):DBINT; cdecl; external DBLIBDLL;
  289. function dbcolinfo(dbproc:PDBPROCESS; typ:INT; column:DBINT; computeid:DBINT; dbcol:PDBCOL):RETCODE; cdecl; external DBLIBDLL;
  290. function dbprtype(token:INT):PAnsiChar; cdecl; external DBLIBDLL;
  291. function dbdatlen(dbproc:PDBPROCESS; column:INT):DBINT; cdecl; external DBLIBDLL;
  292. function dbdata(dbproc:PDBPROCESS; column:INT):PByte; cdecl; external DBLIBDLL;
  293. function dbwillconvert(srctype, desttype: INT):{$IFDEF freetds}DBBOOL{$ELSE}BOOL{$ENDIF}; cdecl; external DBLIBDLL;
  294. function dbconvert(dbproc:PDBPROCESS; srctype:INT; src:PByte; srclen:DBINT; desttype:INT; dest:PByte; destlen:DBINT):INT; cdecl; external DBLIBDLL;
  295. function dbdatecrack(dbproc:PDBPROCESS; dateinfo:PDBDATEREC; datetime: PDBDATETIME):RETCODE; cdecl; external DBLIBDLL;
  296. function dbcount(dbproc:PDBPROCESS):DBINT; cdecl; external DBLIBDLL;
  297. function dbiscount(dbproc:PDBPROCESS):BOOL; cdecl; external DBLIBDLL;
  298. function dbcancel(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  299. function dbcanquery(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  300. function dbhasretstat(dbproc:PDBPROCESS):DBBOOL; cdecl; external DBLIBDLL;
  301. function dbretstatus(dbproc:PDBPROCESS):DBINT; cdecl; external DBLIBDLL;
  302. procedure dbfreelogin(login:PLOGINREC); cdecl; external DBLIBDLL {$IFDEF freetds}name 'dbloginfree'{$ENDIF};
  303. procedure dbexit(); cdecl; external DBLIBDLL;
  304. {$IFDEF ntwdblib}
  305. function dbopen(login:PLOGINREC; servername:PAnsiChar):PDBPROCESS; cdecl; external DBLIBDLL;
  306. function dbclose(dbproc:PDBPROCESS):RETCODE; cdecl; external DBLIBDLL;
  307. procedure dbwinexit; cdecl; external DBLIBDLL;
  308. {$ENDIF}
  309. {$IFDEF freetds}
  310. function tdsdbopen(login:PLOGINREC; servername:PAnsiChar; msdblib:INT):PDBPROCESS; cdecl; external DBLIBDLL;
  311. function dbtablecolinfo(dbproc:PDBPROCESS; column:DBINT; dbcol:PDBCOL):RETCODE; cdecl; external DBLIBDLL;
  312. function dbtds(dbproc:PDBPROCESS):INT; cdecl; external DBLIBDLL;
  313. function dbsetlversion(login:PLOGINREC; version:BYTE):RETCODE; cdecl; external DBLIBDLL;
  314. function dbservcharset(dbproc:PDBPROCESS):PAnsiChar; cdecl; external DBLIBDLL;
  315. procedure dbclose(dbproc:PDBPROCESS); cdecl; external DBLIBDLL;
  316. {$ENDIF}
  317. {$ELSE}
  318. var
  319. dbinit: function():{$IFDEF freetds}RETCODE{$ELSE}PAnsiChar{$ENDIF}; cdecl;
  320. dblogin: function():PLOGINREC; cdecl;
  321. dbsetlname: function(login:PLOGINREC; value:PAnsiChar; which:INT):RETCODE; cdecl;
  322. dbsetlogintime: function(seconds:INT):RETCODE; cdecl;
  323. dbsettime: function(seconds:INT):RETCODE; cdecl;
  324. dberrhandle: function(handler:DBERRHANDLE_PROC):DBERRHANDLE_PROC; cdecl;
  325. dbmsghandle: function(handler:DBMSGHANDLE_PROC):DBMSGHANDLE_PROC; cdecl;
  326. dbsetopt: function(dbproc:PDBPROCESS; option: INT; param:PAnsiChar {$IFDEF freetds};int_param:INT{$ENDIF}):RETCODE; cdecl;
  327. dbuse: function(dbproc:PDBPROCESS; dbname:PAnsiChar):RETCODE; cdecl;
  328. dbcmd: function(dbproc:PDBPROCESS; cmdstring:PAnsiChar):RETCODE; cdecl;
  329. dbcmdrow: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  330. dbsqlexec: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  331. dbresults: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  332. dbmorecmds: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  333. dbnextrow: function(dbproc:PDBPROCESS):STATUS; cdecl;
  334. dbnumcols: function(dbproc:PDBPROCESS):INT; cdecl;
  335. dbcolname: function(dbproc:PDBPROCESS; column:INT):PAnsiChar; cdecl;
  336. dbcoltype: function(dbproc:PDBPROCESS; column:INT):INT; cdecl;
  337. dbcollen: function(dbproc:PDBPROCESS; column:INT):DBINT; cdecl;
  338. dbcolinfo: function(dbproc:PDBPROCESS; typ:INT; column:DBINT; computeid:DBINT; dbcol:PDBCOL):RETCODE; cdecl;
  339. dbprtype: function(token:INT):PAnsiChar; cdecl;
  340. dbdatlen: function(dbproc:PDBPROCESS; column:INT):DBINT; cdecl;
  341. dbdata: function(dbproc:PDBPROCESS; column:INT):PByte; cdecl;
  342. dbwillconvert: function(srctype, desttype: INT):{$IFDEF freetds}DBBOOL{$ELSE}BOOL{$ENDIF}; cdecl;
  343. dbconvert: function(dbproc:PDBPROCESS; srctype:INT; src:PByte; srclen:DBINT; desttype:INT; dest:PByte; destlen:DBINT):INT; cdecl;
  344. dbdatecrack: function(dbproc:PDBPROCESS; dateinfo:PDBDATEREC; datetime: PDBDATETIME):RETCODE; cdecl;
  345. dbcount: function(dbproc:PDBPROCESS):DBINT; cdecl;
  346. dbiscount: function(dbproc:PDBPROCESS):BOOL; cdecl;
  347. dbcancel: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  348. dbcanquery: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  349. dbhasretstat: function(dbproc:PDBPROCESS):DBBOOL; cdecl;
  350. dbretstatus: function(dbproc:PDBPROCESS):DBINT; cdecl;
  351. dbexit: procedure(); cdecl;
  352. dbfreelogin: procedure(login:PLOGINREC); cdecl;
  353. {$IFDEF ntwdblib}
  354. dbopen: function(login:PLOGINREC; servername:PAnsiChar):PDBPROCESS; cdecl;
  355. dbclose: function(dbproc:PDBPROCESS):RETCODE; cdecl;
  356. dbwinexit: procedure; cdecl;
  357. {$ENDIF}
  358. {$IFDEF freetds}
  359. tdsdbopen: function(login:PLOGINREC; servername:PAnsiChar; msdblib:INT):PDBPROCESS; cdecl;
  360. dbtablecolinfo: function(dbproc:PDBPROCESS; column:DBINT; dbcol:PDBCOL):RETCODE; cdecl;
  361. dbtds: function(dbproc:PDBPROCESS):INT; cdecl;
  362. dbsetlversion: function(login:PLOGINREC; version:BYTE):RETCODE; cdecl;
  363. dbservcharset: function(dbproc:PDBPROCESS):PAnsiChar; cdecl;
  364. dbclose: procedure(dbproc:PDBPROCESS); cdecl;
  365. {$ENDIF}
  366. DefaultDBLibLibraryName: string = DBLIBDLL;
  367. DBLibLoadedLibrary: string = '';
  368. {$ENDIF}
  369. {$IFDEF ntwdblib}
  370. function tdsdbopen(login:PLOGINREC; servername:PAnsiChar; msdblib:INT):PDBPROCESS;
  371. function dbtablecolinfo(dbproc:PDBPROCESS; column:DBINT; dbcol:PDBCOL):RETCODE;
  372. function dbsetlversion(login:PLOGINREC; version:BYTE):RETCODE;
  373. function dbtds(dbproc:PDBPROCESS):INT;
  374. function dbversion():PAnsiChar;
  375. {$ENDIF}
  376. {$IFDEF freetds}
  377. function dbopen(login:PLOGINREC; servername:PAnsiChar):PDBPROCESS;
  378. procedure dbwinexit;
  379. {$ENDIF}
  380. function dbsetlcharset(login:PLOGINREC; charset:PAnsiChar):RETCODE;
  381. function dbsetlsecure(login:PLOGINREC):RETCODE;
  382. function dbdatetimeallcrack(dta: PDBDATETIMEALL): TDateTime;
  383. function dbmoneytocurr(pdbmoney: PQWord): Currency;
  384. function InitialiseDBLib(const LibraryName : ansistring): integer;
  385. procedure ReleaseDBLib;
  386. implementation
  387. {$IFDEF LOAD_DYNAMICALLY}
  388. uses SysUtils, Dynlibs;
  389. var DBLibLibraryHandle: TLibHandle;
  390. RefCount: integer;
  391. function InitialiseDBLib(const LibraryName : ansistring): integer;
  392. var libname : string;
  393. begin
  394. inc(RefCount);
  395. Result:=RefCount;
  396. if RefCount = 1 then
  397. begin
  398. if LibraryName='' then
  399. libname:=DefaultDBLibLibraryName
  400. else
  401. libname:=LibraryName;
  402. DBLibLibraryHandle := LoadLibrary(libname);
  403. if DBLibLibraryHandle = nilhandle then
  404. begin
  405. RefCount := 0;
  406. raise EInOutError.CreateFmt('Can not load DB-Lib client library "%s". Check your installation.'+LineEnding+'%s',
  407. [libname, SysErrorMessage(GetLastOSError)]);
  408. end;
  409. DBLibLoadedLibrary := libname;
  410. pointer(dbinit) := GetProcedureAddress(DBLibLibraryHandle,'dbinit');
  411. pointer(dblogin) := GetProcedureAddress(DBLibLibraryHandle,'dblogin');
  412. pointer(dbsetlname) := GetProcedureAddress(DBLibLibraryHandle,'dbsetlname');
  413. pointer(dbsetlogintime) := GetProcedureAddress(DBLibLibraryHandle,'dbsetlogintime');
  414. pointer(dbsettime) := GetProcedureAddress(DBLibLibraryHandle,'dbsettime');
  415. pointer(dberrhandle) := GetProcedureAddress(DBLibLibraryHandle,'dberrhandle');
  416. pointer(dbmsghandle) := GetProcedureAddress(DBLibLibraryHandle,'dbmsghandle');
  417. pointer(dbsetopt) := GetProcedureAddress(DBLibLibraryHandle,'dbsetopt');
  418. pointer(dbuse) := GetProcedureAddress(DBLibLibraryHandle,'dbuse');
  419. pointer(dbcmd) := GetProcedureAddress(DBLibLibraryHandle,'dbcmd');
  420. pointer(dbcmdrow) := GetProcedureAddress(DBLibLibraryHandle,'dbcmdrow');
  421. pointer(dbsqlexec) := GetProcedureAddress(DBLibLibraryHandle,'dbsqlexec');
  422. pointer(dbresults) := GetProcedureAddress(DBLibLibraryHandle,'dbresults');
  423. pointer(dbmorecmds) := GetProcedureAddress(DBLibLibraryHandle,'dbmorecmds');
  424. pointer(dbnextrow) := GetProcedureAddress(DBLibLibraryHandle,'dbnextrow');
  425. pointer(dbnumcols) := GetProcedureAddress(DBLibLibraryHandle,'dbnumcols');
  426. pointer(dbcolname) := GetProcedureAddress(DBLibLibraryHandle,'dbcolname');
  427. pointer(dbcoltype) := GetProcedureAddress(DBLibLibraryHandle,'dbcoltype');
  428. pointer(dbcollen) := GetProcedureAddress(DBLibLibraryHandle,'dbcollen');
  429. pointer(dbcolinfo) := GetProcedureAddress(DBLibLibraryHandle,'dbcolinfo');
  430. pointer(dbprtype) := GetProcedureAddress(DBLibLibraryHandle,'dbprtype');
  431. pointer(dbdatlen) := GetProcedureAddress(DBLibLibraryHandle,'dbdatlen');
  432. pointer(dbdata) := GetProcedureAddress(DBLibLibraryHandle,'dbdata');
  433. pointer(dbwillconvert) := GetProcedureAddress(DBLibLibraryHandle,'dbwillconvert');
  434. pointer(dbconvert) := GetProcedureAddress(DBLibLibraryHandle,'dbconvert');
  435. pointer(dbdatecrack) := GetProcedureAddress(DBLibLibraryHandle,'dbdatecrack');
  436. pointer(dbcount) := GetProcedureAddress(DBLibLibraryHandle,'dbcount');
  437. pointer(dbiscount) := GetProcedureAddress(DBLibLibraryHandle,'dbiscount');
  438. pointer(dbcancel) := GetProcedureAddress(DBLibLibraryHandle,'dbcancel');
  439. pointer(dbcanquery) := GetProcedureAddress(DBLibLibraryHandle,'dbcanquery');
  440. pointer(dbhasretstat) := GetProcedureAddress(DBLibLibraryHandle,'dbhasretstat');
  441. pointer(dbretstatus) := GetProcedureAddress(DBLibLibraryHandle,'dbretstatus');
  442. pointer(dbexit) := GetProcedureAddress(DBLibLibraryHandle,'dbexit');
  443. pointer(dbfreelogin) := GetProcedureAddress(DBLibLibraryHandle,{$IFDEF freetds}'dbloginfree'{$ELSE}'dbfreelogin'{$ENDIF});
  444. pointer(dbclose) := GetProcedureAddress(DBLibLibraryHandle,'dbclose');
  445. {$IFDEF ntwdblib}
  446. pointer(dbopen) := GetProcedureAddress(DBLibLibraryHandle,'dbopen');
  447. pointer(dbwinexit) := GetProcedureAddress(DBLibLibraryHandle,'dbwinexit');
  448. {$ENDIF}
  449. {$IFDEF freetds}
  450. pointer(tdsdbopen) := GetProcedureAddress(DBLibLibraryHandle,'tdsdbopen');
  451. pointer(dbtablecolinfo) := GetProcedureAddress(DBLibLibraryHandle,'dbtablecolinfo');
  452. pointer(dbtds) := GetProcedureAddress(DBLibLibraryHandle,'dbtds');
  453. pointer(dbsetlversion) := GetProcedureAddress(DBLibLibraryHandle,'dbsetlversion');
  454. pointer(dbservcharset) := GetProcedureAddress(DBLibLibraryHandle,'dbservcharset');
  455. //if not assigned(dbiscount) then
  456. // raise EInOutError.Create('Minimum supported version of FreeTDS client library is 0.91!');
  457. {$ENDIF}
  458. DBLibInit:=false;
  459. end;
  460. end;
  461. procedure ReleaseDBLib;
  462. begin
  463. if RefCount > 0 then dec(RefCount);
  464. if RefCount = 0 then
  465. begin
  466. dbexit;{$IFDEF WINDOWS}dbwinexit;{$ENDIF}
  467. if UnloadLibrary(DBLibLibraryHandle) then
  468. begin
  469. DBLibLibraryHandle := NilHandle;
  470. DBLibLoadedLibrary := '';
  471. end
  472. else
  473. inc(RefCount);
  474. end;
  475. end;
  476. {$ELSE}
  477. procedure InitialiseDBLib(LibraryName : string);
  478. begin
  479. //no-op for static linked
  480. end;
  481. procedure ReleaseDBLib;
  482. begin
  483. //no-op for static linked
  484. end;
  485. {$ENDIF LOAD_DYNAMICALLY}
  486. //functions, which are not implemented by FreeTDS:
  487. {$IFDEF freetds}
  488. function dbopen(login:PLOGINREC; servername:PAnsiChar):PDBPROCESS;
  489. begin
  490. Result:=tdsdbopen(login, servername, 1{1=MSDBLIB or 0=SYBDBLIB});
  491. end;
  492. function dbsetlcharset(login:PLOGINREC; charset:PAnsiChar):RETCODE;
  493. begin
  494. Result:=dbsetlname(login, charset, 10);
  495. end;
  496. function dbsetlsecure(login:PLOGINREC):RETCODE;
  497. begin
  498. //not implemented; see http://www.freetds.org/userguide/domains.htm
  499. Result:=SUCCEED;
  500. end;
  501. procedure dbwinexit;
  502. begin
  503. //do nothing
  504. end;
  505. {$ENDIF}
  506. //functions which are not implemented by ntwdblib:
  507. {$IFDEF ntwdblib}
  508. function tdsdbopen(login:PLOGINREC; servername:PAnsiChar; msdblib:INT):PDBPROCESS;
  509. begin
  510. Result:=dbopen(login, servername);
  511. end;
  512. function dbtablecolinfo(dbproc:PDBPROCESS; column:DBINT; dbcol:PDBCOL):RETCODE;
  513. begin
  514. Result:=dbcolinfo(dbproc, CI_REGULAR, column, 0, dbcol);
  515. if dbcol^.VarLength {true also when column is nullable} then
  516. case dbcol^.Typ of
  517. SQLCHAR : dbcol^.Typ := SQLVARCHAR;
  518. SQLBINARY: dbcol^.Typ := SQLVARBINARY;
  519. end;
  520. end;
  521. function dbsetlversion(login:PLOGINREC; version:BYTE):RETCODE;
  522. begin
  523. Result:=dbsetlname(login, nil, version);
  524. end;
  525. function dbsetlcharset(login:PLOGINREC; charset:PAnsiChar):RETCODE;
  526. begin
  527. Result:=SUCCEED;
  528. end;
  529. function dbsetlsecure(login:PLOGINREC):RETCODE;
  530. begin
  531. Result:=dbsetlname(login, nil, DBSETSECURE);
  532. end;
  533. function dbtds(dbproc:PDBPROCESS):INT;
  534. begin
  535. Result:=0;
  536. end;
  537. function dbversion():PAnsiChar;
  538. begin
  539. Result:='DB Library version 8.00';
  540. end;
  541. {$ENDIF}
  542. function dbdatetimeallcrack(dta: PDBDATETIMEALL): TDateTime;
  543. begin
  544. if dta^.info and $4000 = 0 then
  545. Result := 0
  546. else
  547. Result := dta^.date + 2;
  548. Result := ComposeDateTime(Result, dta^.time/MSecsPerDay/10000 + dta^.offset/MinsPerDay);
  549. end;
  550. function dbmoneytocurr(pdbmoney: PQWord): Currency;
  551. begin
  552. {$IFDEF ENDIAN_LITTLE}
  553. PQWord(@Result)^ := pdbmoney^ shr 32 or pdbmoney^ shl 32;
  554. {$ELSE}
  555. move(pdbmoney^, Result, sizeof(Currency));
  556. {$ENDIF}
  557. end;
  558. {
  559. //ntwdblib uses low significant values first
  560. //freetds uses variable length array (based on precision) see numeric.c: tds_numeric_bytes_per_prec
  561. // and starts from high significant values first
  562. function dbnumerictobcd(dbnum: DBNUMERIC): TBCD;
  563. var i: integer;
  564. intval,intbase,intdiv: int64;
  565. bcdval,bcdbase,bcddiv, bcd1: TBCD;
  566. begin
  567. intval:=0;
  568. intbase:=1;
  569. for i:=0 to 6 do
  570. begin
  571. intval := intval + dbnum.val[i] * intbase;
  572. intbase:= intbase*256;
  573. end;
  574. bcdval := IntegerToBCD(intval);
  575. if dbnum.precision > 16 then
  576. begin
  577. bcdbase := IntegerToBCD(intbase);
  578. for i:=7 to length(dbnum.val)-1 do
  579. begin
  580. BCDMultiply(bcdbase, integer(dbnum.val[i]), bcd1);
  581. BCDAdd(bcdval, bcd1, bcdval);
  582. BCDMultiply(bcdbase, 256, bcdbase);
  583. end;
  584. end;
  585. if dbnum.scale > 18 then
  586. begin
  587. bcddiv:=IntegerToBCD(int64(1000000000000000000));
  588. for i:=19 to dbnum.scale do BCDMultiply(bcddiv, 10, bcddiv);
  589. end
  590. else
  591. begin
  592. intdiv:=1;
  593. for i:=1 to dbnum.scale do intdiv:=intdiv*10;
  594. bcddiv:=IntegerToBCD(intdiv);
  595. end;
  596. BCDDivide(bcdval, bcddiv, Result);
  597. if dbnum.sign=0 then BCDNegate(Result);
  598. end;
  599. }
  600. {$IFNDEF LOAD_DYNAMICALLY}
  601. finalization
  602. dbexit; {$IFDEF WINDOWS}dbwinexit;{$ENDIF}
  603. {$ENDIF}
  604. end.