home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 April / PCWorld_2000-04_cd.bin / Komunik / Servery / PinkNet / pnws1076prof.exe / file0138.bin < prev    next >
Encoding:
Text File  |  2000-03-12  |  11.9 KB  |  271 lines

  1. unit _libmysq;
  2.  
  3. {This is a more complete unit to interface with MySQL Server from Delphi
  4. This work was started by Blestan Tabakov. I have included the rest of the
  5. functionality in libmysql.dll.
  6. Delphi 3 Interface to LibMYSQL.dll (Current libmysql.dll version is 3.22)
  7. June 1998
  8. Bob Silva   bsilva@umesd.k12.or.us
  9.  
  10. To find out more on this get the C API docs from www.tcx.se (makers of MySQL)}
  11.  
  12. {TODO: Implement as a VCL I dont think I have the knowledge to make it use Delphis
  13. data controls but Ill look into it, I dont think the component will be too hard to
  14. implement in the near future}
  15.  
  16.  
  17. interface
  18. const
  19. {General Declarations}
  20.  MYSQL_ERRMSG_SIZE=200;
  21.  MYSQL_PORT=3306;
  22.  LOCAL_HOST='localhost';
  23.  MYSQL_UNIX_ADDR='/tmp/mysql.sock';
  24.  NAME_LEN=64;
  25.  PROTOCOL_VERSION=10;
  26.  MYSQL_SERVER_VERSION='3.21.18-beta';
  27.  FRM_VER=6;
  28.  MYSQL_VERSION_ID=32115;
  29.  {Refresh Options}
  30.  REFRESH_GRANT=1;
  31.  REFRESH_LOG=2;
  32.  REFRESH_TABLES=4;
  33.  REFRESH_HOSTS=8;
  34.  REFRESH_FAST=32768;
  35.  {Enum Field Types}
  36.  FIELD_TYPE_DECIMAL=0;
  37.  FIELD_TYPE_TINY=1;
  38.  FIELD_TYPE_SHORT=2;
  39.  FIELD_TYPE_LONG=3;
  40.  FIELD_TYPE_FLOAT=4;
  41.  FIELD_TYPE_DOUBLE=5;
  42.  FIELD_TYPE_NULL=6;
  43.  FIELD_TYPE_TIMESTAMP=7;
  44.  FIELD_TYPE_LONGLONG=8;
  45.  FIELD_TYPE_INT24=9;
  46.  FIELD_TYPE_DATE=10;
  47.  FIELD_TYPE_TIME=11;
  48.  FIELD_TYPE_DATETIME=12;
  49.  FIELD_TYPE_YEAR=13;
  50.  FIELD_TYPE_NEWDATE=14;
  51.  FIELD_TYPE_ENUM=247;
  52.  FIELD_TYPE_SET=248;
  53.  FIELD_TYPE_TINY_BLOB=249;
  54.  FIELD_TYPE_MEDIUM_BLOB=250;
  55.  FIELD_TYPE_LONG_BLOB=251;
  56.  FIELD_TYPE_BLOB=252;
  57.  FIELD_TYPE_VAR_STRING=253;
  58.  FIELD_TYPE_STRING=254;
  59.  {For Compatibility}
  60.  FIELD_TYPE_CHAR=FIELD_TYPE_TINY;
  61.  FIELD_TYPE_INTERVAL=FIELD_TYPE_ENUM;
  62.  
  63.  
  64.  type
  65.  ENUM_FIELD_TYPES= byte;
  66.  MY_BOOL= shortint;
  67.  GPTR= pchar;
  68.  SOCKET= word;
  69.  PCardinal= ^Cardinal; //Delphi didnt like my ^Cardinal as a return value from a function
  70.  {Enumerate return-status codes and option codes}
  71.  mysql_status=(MYSQL_STATUS_READY, MYSQL_STATUS_GET_RESULT, MYSQL_STATUS_USE_RESULT);
  72.  mysql_option=(MYSQL_OPT_CONNECT_TIMEOUT, MYSQL_OPT_COMPRESS);
  73.  
  74.  PUSED_MEM=^USED_MEM;
  75.  USED_MEM=record
  76.             next:pused_mem;
  77.             left,size: Cardinal;
  78.            end;
  79.  
  80.  ERR_PROC=procedure;
  81.  
  82.  PMEM_ROOT=^MEM_ROOT;
  83.  MEM_ROOT=record
  84.            free,used:pused_mem;
  85.            min_malloc,block_size: Cardinal;
  86.            error_handler:err_proc;
  87.           end;
  88.  
  89.  NET=record
  90.        fd: socket;
  91.        fcntl: integer;
  92.        buff,buff_end,write_pos: pchar;
  93.        last_error: array[01..MYSQL_ERRMSG_SIZE] of char;
  94.        last_errno,max_packet,timeout,pkt_nr: Cardinal;
  95.        error,return_errno, compress: MY_BOOL;
  96.       end;
  97.  
  98. PMYSQL_FIELD=^MYSQL_FIELD;
  99. MYSQL_FIELD=record
  100.              name,table,def: pchar;
  101.              _type: ENUM_FIELD_TYPES;
  102.              length,max_length,flags,decimals: Cardinal;
  103.             end;
  104. MYSQL_FIELD_OFFSET= Cardinal;
  105.  
  106. (*************HELP HERE IF YOU CAN*************)
  107. {The C def for this is: typedef byte **MYSQL_ROW;
  108. Blestan originally used the array struct below, but I could not get it to
  109. work like that.
  110. Blestan Code:  mysql_row=array[00..$ffff div sizeof(pchar)] of pchar;
  111. My def of a smaller array seems to solve the problem, but if you were to return
  112. a very large result set, Im sure it will AV. So if you can translate the C Code:
  113. Earlier in code: typedef char       byte;
  114. typedef byte **MYSQL_ROW; into an adequate Delphi type then please do and let
  115. me know how you did it. I could get it to return rows using mysql_row: ^PChar; but
  116. I couldnt figure out how to index into it like that.}
  117.  
  118.  
  119.  
  120.  PMYSQL_ROW=^MYSQL_ROW;
  121.  MYSQL_ROW= array[00..$ff] of pchar;
  122.  //MYSQL_ROW= ^PChar;
  123.  
  124.  
  125. (*********************************************)
  126.  
  127.  
  128.  PMYSQL_ROWS=^MYSQL_ROWS;
  129.  MYSQL_ROWS=record
  130.             next: PMYSQL_ROWS;
  131.             data: PMYSQL_ROW;
  132.            end;
  133.  MYSQL_ROW_OFFSET= PMYSQL_ROWS;
  134.  
  135.  PMYSQL_DATA= ^MYSQL_DATA;
  136.  MYSQL_DATA= record
  137.              rows, fields: Cardinal;
  138.              data: PMYSQL_ROWS;
  139.              alloc: MEM_ROOT;
  140.              end;
  141.  PMYSQL_OPTIONS=^_MYSQL_OPTIONS;
  142.  _MYSQL_OPTIONS= record
  143.                 connect_timeout: Cardinal;
  144.                 compress: MY_BOOL;
  145.                 end;
  146.  
  147.  PMYSQL=^MYSQL;
  148.  MYSQL= record
  149.         _net: NET;
  150.         host,user,passwd,unix_socket,server_version,host_info,info,db: pchar;
  151.         port,client_flag,server_capabilities,protocol_version,field_count: Cardinal;
  152.         thread_id, affected_rows, insert_id, extra_info:longint;
  153.         status: mysql_status;
  154.         fields: PMYSQL_FIELD;
  155.         field_alloc: MEM_ROOT;
  156.         free_me, reconnect: MY_BOOL;
  157.        end;
  158.  
  159.  PMYSQL_RES=^MYSQL_RES;
  160.  MYSQL_RES= record
  161.            row_count: longint;
  162.            field_count, current_field: Cardinal;
  163.            fields: PMYSQL_FIELD;
  164.            data: PMYSQL_DATA;
  165.            data_cursor: PMYSQL_ROWS;
  166.            field_alloc: MEM_ROOT;
  167.            row: PMYSQL_ROW;
  168.            current_row: PMYSQL_ROW;
  169.            lengths: ^Cardinal;
  170.            handle: ^MYSQL;
  171.            eof: MY_BOOL;
  172.            end;
  173.  
  174. const thelib='libmysql.dll';
  175. (*********ALL functions are working unless stated otherwise*************)
  176. {If you can figure why why one of the ones marked bad is not working, please
  177. send me the code to implement it properly}
  178.  
  179. function mysql_init ( _mysql: pmysql):pmysql;stdcall; //Added July 23
  180. function mysql_connect( _mysql: pmysql; const host,user,passwd:pchar):pmysql;stdcall;
  181. {Non-functional: dont know why yet: I think you can do without it for now}
  182. {UPDATE: Folkert Klemme reports that this call has worked in his environment}
  183. function mysql_real_connect(_mysql: pmysql; const host, user, passwd, db:pchar;
  184. port:Cardinal; unix_socket:PChar;clientflag:Cardinal):pmysql;stdcall;
  185. procedure mysql_close( _mysql: pmysql); stdcall;
  186. function mysql_select_db(_mysql:pmysql;const DB: pchar):integer; stdcall;
  187. function mysql_query(_mysql:pmysql;const query: pchar):integer;stdcall;
  188. {Real Query not implemented in current version of libmysql.dll (timestamp:353be33a)}
  189. {UPDATE: Real_Query still not implemented in 3.22 version of libmysql.dll}
  190. // function mysql_real_query(_mysql:pmysql;const query: pchar;length: Cardinal):integer;stdcall;
  191. function mysql_create_db(_mysql:pmysql; const DB:PChar):integer;stdcall;
  192. function mysql_drop_db(_mysql:pmysql; const DB:PChar):integer; stdcall;
  193. function mysql_shutdown(_mysql: pmysql): Integer; stdcall;
  194. {Although undocumented, I believe this is to reload the permissions table;
  195. documentation calls for a reload function which there is none in libmysql.dll,
  196. so I think this may be a replacement, it takes a Cardinal for options but I cant find out
  197. what those options are since its not documented}
  198. {UPDATE: David Blackburn pointed out to me that the 'refresh_options' were defined
  199. in one of the header files that came with the 3.22 WinClients released by TCX, Constants
  200. have been added to use this call properly}
  201. function mysql_refresh(_mysql:pmysql; refresh_options: Cardinal):integer; stdcall;
  202. function mysql_kill(_mysql:pmysql; pid:longint):integer; stdcall;
  203. function mysql_ping(_mysql:pmysql):integer; stdcall; //Added July 23
  204. function mysql_stat(_mysql:pmysql):pchar; stdcall;
  205. function mysql_get_server_info(_mysql:pmysql):PChar; stdcall;
  206. function mysql_get_client_info:PChar; stdcall;
  207. function mysql_get_host_info(_mysql:pmysql):PChar;stdcall;
  208. function mysql_get_proto_info(_mysql:pmysql):Cardinal;stdcall;
  209. function mysql_list_dbs(_mysql:pmysql;wild: pchar):pmysql_res; stdcall;
  210. function mysql_list_tables(_mysql: pmysql; const wild: pchar):pmysql_res; stdcall;
  211. function mysql_list_fields(_mysql: pmysql;const table,wild: pchar):pmysql_res; stdcall;
  212. function mysql_list_processes(_mysql: pmysql):pmysql_res; stdcall;
  213. function mysql_store_result(_mysql:pmysql):pmysql_res; stdcall;
  214. function mysql_use_result(_mysql:pmysql):pmysql_res; stdcall;
  215. function mysql_options(_mysql:pmysql; _option:mysql_option; const arg:PChar):Integer;stdcall;
  216. procedure mysql_free_result(result:pmysql_res); stdcall;
  217. procedure mysql_data_seek(result:pmysql_res; offset:Cardinal); stdcall;
  218. {Row seek is probably functional if I knew how to test it;
  219. I think its a matter of saving a particular row pointer in a mysql_row_offset struct;
  220. then later make a call to row_seek to restore the data_cursor to that row;
  221. then a fetch_row will return the next row after that???? I have no need for it so you tell me}
  222. function mysql_row_seek(result:pmysql_res; row:mysql_row_offset):mysql_row_offset;stdcall;
  223. {Field Seek is not implemented in current version of libmysql.dll(timestamp:353be33a)}
  224. {UPDATE: Field_Seek has been implemented in 3.22 version of libmysql.dll that was released
  225. by TCX in MySQL 3.22.4 Clients for Win32}
  226. function mysql_field_seek(result:pmysql_res;offset:mysql_field_offset):mysql_field_offset;stdcall
  227. function mysql_fetch_row(result:pmysql_res):pmysql_row;stdcall;
  228. {I dont think fetch_length is working properly because of the way that I set up the MYSQL_ROW array}
  229. {UPDATE: The function is supposed to return a POINTER to a Cardinal, I missed that
  230. in my first implementation, sorry, still havent tested it though}
  231. function mysql_fetch_lengths(result:pmysql_res):PCardinal;stdcall;
  232. function mysql_fetch_field(handle: pmysql_res):pmysql_field; stdcall;
  233.  
  234. implementation
  235.  
  236. function mysql_init ( _mysql: pmysql):pmysql;stdcall;external thelib; //Added July 23
  237. function mysql_connect( _mysql: pmysql; const host,user,passwd:pchar):pmysql;stdcall;external thelib;
  238. function mysql_real_connect(_mysql: pmysql; const host, user, passwd, db:pchar;
  239. port:Cardinal; unix_socket:PChar;clientflag:Cardinal):pmysql;stdcall;external thelib;
  240. procedure mysql_close( _mysql: pmysql); stdcall;external thelib;
  241. function mysql_select_db(_mysql:pmysql;const DB: pchar):integer; stdcall;external thelib;
  242. function mysql_query(_mysql:pmysql;const query: pchar):integer;stdcall;external thelib;
  243. // function mysql_real_query(_mysql:pmysql;const query: pchar;length: Cardinal):integer;stdcall;external thelib;
  244. function mysql_create_db(_mysql:pmysql; const DB:PChar):integer;stdcall;external thelib;
  245. function mysql_drop_db(_mysql:pmysql; const DB:PChar):integer;stdcall;external thelib;
  246. function mysql_shutdown(_mysql: pmysql): Integer; stdcall;external thelib;
  247. function mysql_refresh(_mysql:pmysql; refresh_options: Cardinal):integer;stdcall;external thelib;
  248. function mysql_kill(_mysql:pmysql; pid:longint):integer;stdcall;external thelib;
  249. function mysql_ping(_mysql:pmysql):integer; stdcall;external thelib; //Added July 23
  250. function mysql_stat(_mysql:pmysql):pchar; stdcall;external thelib;
  251. function mysql_get_server_info(_mysql:pmysql):PChar;stdcall;external thelib;
  252. function mysql_get_client_info:PChar;stdcall;external thelib;
  253. function mysql_get_host_info(_mysql:pmysql):PChar;stdcall;external thelib;
  254. function mysql_get_proto_info(_mysql:pmysql):Cardinal;stdcall;external thelib;
  255. function mysql_list_dbs(_mysql:pmysql;wild: pchar):pmysql_res; stdcall;external thelib;
  256. function mysql_list_tables(_mysql: pmysql; const wild: pchar):pmysql_res; stdcall;external thelib;
  257. function mysql_list_fields(_mysql: pmysql;const table,wild: pchar):pmysql_res; stdcall;external thelib;
  258. function mysql_list_processes(_mysql: pmysql):pmysql_res; stdcall;external thelib;
  259. function mysql_store_result(_mysql:pmysql):pmysql_res; stdcall;external thelib;
  260. function mysql_use_result(_mysql:pmysql):pmysql_res;stdcall;external thelib;
  261. function mysql_options(_mysql:pmysql; _option:mysql_option; const arg:PChar):Integer;stdcall;external thelib;
  262. procedure mysql_free_result(result:pmysql_res); stdcall;external thelib;
  263. procedure mysql_data_seek(result:pmysql_res; offset:Cardinal); stdcall;external thelib;
  264. function mysql_row_seek(result:pmysql_res; row:mysql_row_offset):mysql_row_offset;stdcall;external thelib;
  265. function mysql_field_seek(result:pmysql_res;offset:mysql_field_offset):mysql_field_offset;stdcall;external thelib;
  266. function mysql_fetch_row(result:pmysql_res):pmysql_row;stdcall;external thelib;
  267. function mysql_fetch_lengths(result:pmysql_res):PCardinal;stdcall;external thelib;
  268. function mysql_fetch_field(handle: pmysql_res):pmysql_field; stdcall;external thelib;
  269.  
  270. end.
  271.