home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 July & August / PCWorld_2003-07-08_cd.bin / Software / Vyzkuste / mysql / data1.cab / Development / include / mysql.h < prev    next >
C/C++ Source or Header  |  2003-05-17  |  16KB  |  436 lines

  1. /* Copyright (C) 2000-2003 MySQL AB
  2.  
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.  
  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.  See the
  11.    GNU General Public License for more details.
  12.  
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16.  
  17. #ifndef _mysql_h
  18. #define _mysql_h
  19.  
  20. #ifdef __CYGWIN__     /* CYGWIN implements a UNIX API */
  21. #undef WIN
  22. #undef _WIN
  23. #undef _WIN32
  24. #undef _WIN64
  25. #undef __WIN__
  26. #endif
  27.  
  28. #ifdef    __cplusplus
  29. extern "C" {
  30. #endif
  31.  
  32. #ifndef _global_h                /* If not standard header */
  33. #include <sys/types.h>
  34. #ifdef __LCC__
  35. #include <winsock.h>                /* For windows */
  36. #endif
  37. typedef char my_bool;
  38. #if (defined(_WIN32) || defined(_WIN64)) && !defined(__WIN__)
  39. #define __WIN__
  40. #endif
  41. #if !defined(__WIN__)
  42. #define STDCALL
  43. #else
  44. #define STDCALL __stdcall
  45. #endif
  46. typedef char * gptr;
  47.  
  48. #ifndef my_socket_defined
  49. #ifdef __WIN__
  50. #define my_socket SOCKET
  51. #else
  52. typedef int my_socket;
  53. #endif /* __WIN__ */
  54. #endif /* my_socket_defined */
  55. #endif /* _global_h */
  56.  
  57. #include "mysql_com.h"
  58. #include "mysql_version.h"
  59.  
  60. extern unsigned int mysql_port;
  61. extern char *mysql_unix_port;
  62.  
  63. #ifdef __NETWARE__
  64. #pragma pack(push, 8)        /* 8 byte alignment */
  65. #endif
  66.  
  67. #define IS_PRI_KEY(n)    ((n) & PRI_KEY_FLAG)
  68. #define IS_NOT_NULL(n)    ((n) & NOT_NULL_FLAG)
  69. #define IS_BLOB(n)    ((n) & BLOB_FLAG)
  70. #define IS_NUM(t)    ((t) <= FIELD_TYPE_INT24 || (t) == FIELD_TYPE_YEAR)
  71. #define IS_NUM_FIELD(f)     ((f)->flags & NUM_FLAG)
  72. #define INTERNAL_NUM_FIELD(f) (((f)->type <= FIELD_TYPE_INT24 && ((f)->type != FIELD_TYPE_TIMESTAMP || (f)->length == 14 || (f)->length == 8)) || (f)->type == FIELD_TYPE_YEAR)
  73.  
  74. typedef struct st_mysql_field {
  75.   char *name;            /* Name of column */
  76.   char *table;            /* Table of column if column was a field */
  77.   char *org_table;        /* Org table name if table was an alias */
  78.   char *db;            /* Database for table */
  79.   char *def;            /* Default value (set by mysql_list_fields) */
  80.   unsigned long length;        /* Width of column */
  81.   unsigned long max_length;    /* Max width of selected set */
  82.   unsigned int flags;        /* Div flags */
  83.   unsigned int decimals;    /* Number of decimals in field */
  84.   enum enum_field_types type;    /* Type of field. Se mysql_com.h for types */
  85. } MYSQL_FIELD;
  86.  
  87. typedef char **MYSQL_ROW;        /* return data as array of strings */
  88. typedef unsigned int MYSQL_FIELD_OFFSET; /* offset to current field */
  89.  
  90. #if defined(NO_CLIENT_LONG_LONG)
  91. typedef unsigned long my_ulonglong;
  92. #elif defined (__WIN__)
  93. typedef unsigned __int64 my_ulonglong;
  94. #else
  95. typedef unsigned long long my_ulonglong;
  96. #endif
  97.  
  98. #define MYSQL_COUNT_ERROR (~(my_ulonglong) 0)
  99.  
  100. typedef struct st_mysql_rows {
  101.   struct st_mysql_rows *next;        /* list of rows */
  102.   MYSQL_ROW data;
  103. } MYSQL_ROWS;
  104.  
  105. typedef MYSQL_ROWS *MYSQL_ROW_OFFSET;    /* offset to current row */
  106.  
  107. #include "my_alloc.h"
  108.  
  109. typedef struct st_mysql_data {
  110.   my_ulonglong rows;
  111.   unsigned int fields;
  112.   MYSQL_ROWS *data;
  113.   MEM_ROOT alloc;
  114. } MYSQL_DATA;
  115.  
  116. struct st_mysql_options {
  117.   unsigned int connect_timeout,client_flag;
  118.   unsigned int port;
  119.   char *host,*init_command,*user,*password,*unix_socket,*db;
  120.   char *my_cnf_file,*my_cnf_group, *charset_dir, *charset_name;
  121.   char *ssl_key;                /* PEM key file */
  122.   char *ssl_cert;                /* PEM cert file */
  123.   char *ssl_ca;                    /* PEM CA file */
  124.   char *ssl_capath;                /* PEM directory of CA-s? */
  125.   char *ssl_cipher;                /* cipher to use */
  126.   unsigned long max_allowed_packet;
  127.   my_bool use_ssl;                /* if to use SSL or not */
  128.   my_bool compress,named_pipe;
  129.  /*
  130.    On connect, find out the replication role of the server, and
  131.    establish connections to all the peers
  132.  */
  133.   my_bool rpl_probe;
  134.  /*
  135.    Each call to mysql_real_query() will parse it to tell if it is a read
  136.    or a write, and direct it to the slave or the master
  137.  */
  138.   my_bool rpl_parse;
  139.  /*
  140.    If set, never read from a master,only from slave, when doing
  141.    a read that is replication-aware
  142.  */
  143.   my_bool no_master_reads;
  144. };
  145.  
  146. enum mysql_option { MYSQL_OPT_CONNECT_TIMEOUT, MYSQL_OPT_COMPRESS,
  147.             MYSQL_OPT_NAMED_PIPE, MYSQL_INIT_COMMAND,
  148.             MYSQL_READ_DEFAULT_FILE, MYSQL_READ_DEFAULT_GROUP,
  149.             MYSQL_SET_CHARSET_DIR, MYSQL_SET_CHARSET_NAME,
  150.             MYSQL_OPT_LOCAL_INFILE};
  151.  
  152. enum mysql_status { MYSQL_STATUS_READY,MYSQL_STATUS_GET_RESULT,
  153.             MYSQL_STATUS_USE_RESULT};
  154.  
  155. /*
  156.   There are three types of queries - the ones that have to go to
  157.   the master, the ones that go to a slave, and the adminstrative
  158.   type which must happen on the pivot connectioin
  159. */
  160. enum mysql_rpl_type { MYSQL_RPL_MASTER, MYSQL_RPL_SLAVE,
  161.               MYSQL_RPL_ADMIN };
  162.  
  163.  
  164. typedef struct st_mysql {
  165.   NET        net;            /* Communication parameters */
  166.   gptr        connector_fd;        /* ConnectorFd for SSL */
  167.   char        *host,*user,*passwd,*unix_socket,*server_version,*host_info,
  168.         *info,*db;
  169.   struct charset_info_st *charset;
  170.   MYSQL_FIELD    *fields;
  171.   MEM_ROOT    field_alloc;
  172.   my_ulonglong affected_rows;
  173.   my_ulonglong insert_id;        /* id if insert on table with NEXTNR */
  174.   my_ulonglong extra_info;        /* Used by mysqlshow */
  175.   unsigned long thread_id;        /* Id for connection in server */
  176.   unsigned long packet_length;
  177.   unsigned int    port,client_flag,server_capabilities;
  178.   unsigned int    protocol_version;
  179.   unsigned int    field_count;
  180.   unsigned int     server_status;
  181.   unsigned int  server_language;
  182.   struct st_mysql_options options;
  183.   enum mysql_status status;
  184.   my_bool    free_me;        /* If free in mysql_close */
  185.   my_bool    reconnect;        /* set to 1 if automatic reconnect */
  186.   char            scramble_buff[9];
  187.  
  188.  /*
  189.    Set if this is the original connection, not a master or a slave we have
  190.    added though mysql_rpl_probe() or mysql_set_master()/ mysql_add_slave()
  191.  */
  192.   my_bool rpl_pivot;
  193.   /*
  194.     Pointers to the master, and the next slave connections, points to
  195.     itself if lone connection.
  196.   */
  197.   struct st_mysql* master, *next_slave;
  198.  
  199.   struct st_mysql* last_used_slave; /* needed for round-robin slave pick */
  200.  /* needed for send/read/store/use result to work correctly with replication */
  201.   struct st_mysql* last_used_con;
  202. } MYSQL;
  203.  
  204.  
  205. typedef struct st_mysql_res {
  206.   my_ulonglong row_count;
  207.   MYSQL_FIELD    *fields;
  208.   MYSQL_DATA    *data;
  209.   MYSQL_ROWS    *data_cursor;
  210.   unsigned long *lengths;        /* column lengths of current row */
  211.   MYSQL        *handle;        /* for unbuffered reads */
  212.   MEM_ROOT    field_alloc;
  213.   unsigned int    field_count, current_field;
  214.   MYSQL_ROW    row;            /* If unbuffered read */
  215.   MYSQL_ROW    current_row;        /* buffer to current row */
  216.   my_bool    eof;            /* Used by mysql_fetch_row */
  217. } MYSQL_RES;
  218.  
  219. #define MAX_MYSQL_MANAGER_ERR 256  
  220. #define MAX_MYSQL_MANAGER_MSG 256
  221.  
  222. #define MANAGER_OK           200
  223. #define MANAGER_INFO         250
  224. #define MANAGER_ACCESS       401
  225. #define MANAGER_CLIENT_ERR   450
  226. #define MANAGER_INTERNAL_ERR 500
  227.  
  228.  
  229.  
  230. typedef struct st_mysql_manager
  231. {
  232.   NET net;
  233.   char *host,*user,*passwd;
  234.   unsigned int port;
  235.   my_bool free_me;
  236.   my_bool eof;
  237.   int cmd_status;
  238.   int last_errno;
  239.   char* net_buf,*net_buf_pos,*net_data_end;
  240.   int net_buf_size;
  241.   char last_error[MAX_MYSQL_MANAGER_ERR];
  242. } MYSQL_MANAGER;
  243.  
  244. /*
  245.   Set up and bring down the server; to ensure that applications will
  246.   work when linked against either the standard client library or the
  247.   embedded server library, these functions should be called.
  248. */
  249. int STDCALL mysql_server_init(int argc, char **argv, char **groups);
  250. void STDCALL mysql_server_end(void);
  251.  
  252. /*
  253.   Set up and bring down a thread; these function should be called
  254.   for each thread in an application which opens at least one MySQL
  255.   connection.  All uses of the connection(s) should be between these
  256.   function calls.
  257. */
  258. my_bool STDCALL mysql_thread_init(void);
  259. void STDCALL mysql_thread_end(void);
  260.  
  261. /*
  262.   Functions to get information from the MYSQL and MYSQL_RES structures
  263.   Should definitely be used if one uses shared libraries.
  264. */
  265.  
  266. my_ulonglong STDCALL mysql_num_rows(MYSQL_RES *res);
  267. unsigned int STDCALL mysql_num_fields(MYSQL_RES *res);
  268. my_bool STDCALL mysql_eof(MYSQL_RES *res);
  269. MYSQL_FIELD *STDCALL mysql_fetch_field_direct(MYSQL_RES *res,
  270.                           unsigned int fieldnr);
  271. MYSQL_FIELD * STDCALL mysql_fetch_fields(MYSQL_RES *res);
  272. MYSQL_ROW_OFFSET STDCALL mysql_row_tell(MYSQL_RES *res);
  273. MYSQL_FIELD_OFFSET STDCALL mysql_field_tell(MYSQL_RES *res);
  274.  
  275. unsigned int STDCALL mysql_field_count(MYSQL *mysql);
  276. my_ulonglong STDCALL mysql_affected_rows(MYSQL *mysql);
  277. my_ulonglong STDCALL mysql_insert_id(MYSQL *mysql);
  278. unsigned int STDCALL mysql_errno(MYSQL *mysql);
  279. const char * STDCALL mysql_error(MYSQL *mysql);
  280. const char * STDCALL mysql_info(MYSQL *mysql);
  281. unsigned long STDCALL mysql_thread_id(MYSQL *mysql);
  282. const char * STDCALL mysql_character_set_name(MYSQL *mysql);
  283.  
  284. MYSQL *        STDCALL mysql_init(MYSQL *mysql);
  285. int        STDCALL mysql_ssl_set(MYSQL *mysql, const char *key,
  286.                       const char *cert, const char *ca,
  287.                       const char *capath, const char *cipher);
  288. my_bool        STDCALL mysql_change_user(MYSQL *mysql, const char *user, 
  289.                       const char *passwd, const char *db);
  290. MYSQL *        STDCALL mysql_real_connect(MYSQL *mysql, const char *host,
  291.                        const char *user,
  292.                        const char *passwd,
  293.                        const char *db,
  294.                        unsigned int port,
  295.                        const char *unix_socket,
  296.                        unsigned int clientflag);
  297. void        STDCALL mysql_close(MYSQL *sock);
  298. int        STDCALL mysql_select_db(MYSQL *mysql, const char *db);
  299. int        STDCALL mysql_query(MYSQL *mysql, const char *q);
  300. int        STDCALL mysql_send_query(MYSQL *mysql, const char *q,
  301.                      unsigned long length);
  302. int        STDCALL mysql_read_query_result(MYSQL *mysql);
  303. int        STDCALL mysql_real_query(MYSQL *mysql, const char *q,
  304.                     unsigned long length);
  305. /* perform query on master */
  306. int        STDCALL mysql_master_query(MYSQL *mysql, const char *q,
  307.                     unsigned long length);
  308. int        STDCALL mysql_master_send_query(MYSQL *mysql, const char *q,
  309.                     unsigned long length);
  310. /* perform query on slave */  
  311. int        STDCALL mysql_slave_query(MYSQL *mysql, const char *q,
  312.                     unsigned long length);
  313. int        STDCALL mysql_slave_send_query(MYSQL *mysql, const char *q,
  314.                     unsigned long length);
  315.  
  316. /*
  317.   enable/disable parsing of all queries to decide if they go on master or
  318.   slave
  319. */
  320. void            STDCALL mysql_enable_rpl_parse(MYSQL* mysql);
  321. void            STDCALL mysql_disable_rpl_parse(MYSQL* mysql);
  322. /* get the value of the parse flag */  
  323. int             STDCALL mysql_rpl_parse_enabled(MYSQL* mysql);
  324.  
  325. /*  enable/disable reads from master */
  326. void            STDCALL mysql_enable_reads_from_master(MYSQL* mysql);
  327. void            STDCALL mysql_disable_reads_from_master(MYSQL* mysql);
  328. /* get the value of the master read flag */  
  329. int             STDCALL mysql_reads_from_master_enabled(MYSQL* mysql);
  330.  
  331. enum mysql_rpl_type     STDCALL mysql_rpl_query_type(const char* q, int len);  
  332.  
  333. /* discover the master and its slaves */  
  334. int             STDCALL mysql_rpl_probe(MYSQL* mysql);
  335.  
  336. /* set the master, close/free the old one, if it is not a pivot */
  337. int             STDCALL mysql_set_master(MYSQL* mysql, const char* host,
  338.                      unsigned int port,
  339.                      const char* user,
  340.                      const char* passwd);
  341. int             STDCALL mysql_add_slave(MYSQL* mysql, const char* host,
  342.                     unsigned int port,
  343.                     const char* user,
  344.                     const char* passwd);
  345.  
  346. int        STDCALL mysql_shutdown(MYSQL *mysql);
  347. int        STDCALL mysql_dump_debug_info(MYSQL *mysql);
  348. int        STDCALL mysql_refresh(MYSQL *mysql,
  349.                      unsigned int refresh_options);
  350. int        STDCALL mysql_kill(MYSQL *mysql,unsigned long pid);
  351. int        STDCALL mysql_ping(MYSQL *mysql);
  352. const char *        STDCALL mysql_stat(MYSQL *mysql);
  353. const char *        STDCALL mysql_get_server_info(MYSQL *mysql);
  354. const char *        STDCALL mysql_get_client_info(void);
  355. const char *        STDCALL mysql_get_host_info(MYSQL *mysql);
  356. unsigned int    STDCALL mysql_get_proto_info(MYSQL *mysql);
  357. MYSQL_RES *    STDCALL mysql_list_dbs(MYSQL *mysql,const char *wild);
  358. MYSQL_RES *    STDCALL mysql_list_tables(MYSQL *mysql,const char *wild);
  359. MYSQL_RES *    STDCALL mysql_list_fields(MYSQL *mysql, const char *table,
  360.                      const char *wild);
  361. MYSQL_RES *    STDCALL mysql_list_processes(MYSQL *mysql);
  362. MYSQL_RES *    STDCALL mysql_store_result(MYSQL *mysql);
  363. MYSQL_RES *    STDCALL mysql_use_result(MYSQL *mysql);
  364. int        STDCALL mysql_options(MYSQL *mysql,enum mysql_option option,
  365.                       const char *arg);
  366. void        STDCALL mysql_free_result(MYSQL_RES *result);
  367. void        STDCALL mysql_data_seek(MYSQL_RES *result,
  368.                     my_ulonglong offset);
  369. MYSQL_ROW_OFFSET STDCALL mysql_row_seek(MYSQL_RES *result,
  370.                         MYSQL_ROW_OFFSET offset);
  371. MYSQL_FIELD_OFFSET STDCALL mysql_field_seek(MYSQL_RES *result,
  372.                        MYSQL_FIELD_OFFSET offset);
  373. MYSQL_ROW    STDCALL mysql_fetch_row(MYSQL_RES *result);
  374. unsigned long * STDCALL mysql_fetch_lengths(MYSQL_RES *result);
  375. MYSQL_FIELD *    STDCALL mysql_fetch_field(MYSQL_RES *result);
  376. unsigned long    STDCALL mysql_escape_string(char *to,const char *from,
  377.                         unsigned long from_length);
  378. unsigned long STDCALL mysql_real_escape_string(MYSQL *mysql,
  379.                            char *to,const char *from,
  380.                            unsigned long length);
  381. void        STDCALL mysql_debug(const char *debug);
  382. char *        STDCALL mysql_odbc_escape_string(MYSQL *mysql,
  383.                          char *to,
  384.                          unsigned long to_length,
  385.                          const char *from,
  386.                          unsigned long from_length,
  387.                          void *param,
  388.                          char *
  389.                          (*extend_buffer)
  390.                          (void *, char *to,
  391.                           unsigned long *length));
  392. void         STDCALL myodbc_remove_escape(MYSQL *mysql,char *name);
  393. unsigned int    STDCALL mysql_thread_safe(void);
  394. MYSQL_MANAGER*  STDCALL mysql_manager_init(MYSQL_MANAGER* con);  
  395. MYSQL_MANAGER*  STDCALL mysql_manager_connect(MYSQL_MANAGER* con,
  396.                           const char* host,
  397.                           const char* user,
  398.                           const char* passwd,
  399.                           unsigned int port);
  400. void            STDCALL mysql_manager_close(MYSQL_MANAGER* con);
  401. int             STDCALL mysql_manager_command(MYSQL_MANAGER* con,
  402.                         const char* cmd, int cmd_len);
  403. int             STDCALL mysql_manager_fetch_line(MYSQL_MANAGER* con,
  404.                           char* res_buf,
  405.                          int res_buf_size);
  406. #define mysql_reload(mysql) mysql_refresh((mysql),REFRESH_GRANT)
  407.  
  408. #ifdef USE_OLD_FUNCTIONS
  409. MYSQL *        STDCALL mysql_connect(MYSQL *mysql, const char *host,
  410.                       const char *user, const char *passwd);
  411. int        STDCALL mysql_create_db(MYSQL *mysql, const char *DB);
  412. int        STDCALL mysql_drop_db(MYSQL *mysql, const char *DB);
  413. #define     mysql_reload(mysql) mysql_refresh((mysql),REFRESH_GRANT)
  414. #endif
  415. #define HAVE_MYSQL_REAL_CONNECT
  416.  
  417. /*
  418.   The following functions are mainly exported because of mysqlbinlog;
  419.   They are not for general usage
  420. */
  421.  
  422. int simple_command(MYSQL *mysql,enum enum_server_command command,
  423.            const char *arg, unsigned long length, my_bool skipp_check);
  424. unsigned long net_safe_read(MYSQL* mysql);
  425. void mysql_once_init(void);
  426.  
  427. #ifdef __NETWARE__
  428. #pragma pack(pop)        /* restore alignment */
  429. #endif
  430.  
  431. #ifdef    __cplusplus
  432. }
  433. #endif
  434.  
  435. #endif /* _mysql_h */
  436.