~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/drizzle.h

Cleanup of time code (moved into mysys)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 MySQL
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
/*
 
22
  This file defines the client API to DRIZZLE and also the ABI of the
 
23
  dynamically linked libdrizzleclient.
 
24
 
 
25
  In case the file is changed so the ABI is broken, you must also
 
26
  update the SHAREDLIB_MAJOR_VERSION in configure.ac.
 
27
 
 
28
*/
 
29
 
 
30
#ifndef _libdrizzle_drizzle_h
 
31
#define _libdrizzle_drizzle_h
 
32
 
 
33
#ifdef  __cplusplus
 
34
extern "C" {
 
35
#endif
 
36
 
 
37
/** 
 
38
 * @TODO cleanup global.h and include only the necessary stuff here... 
 
39
 * 
 
40
 */
 
41
#ifndef DRIZZLE_SERVER_GLOBAL_H   /* If not standard header */
 
42
#include <sys/types.h>
 
43
#endif /* DRIZZLE_SERVER_GLOBAL_H */
 
44
 
 
45
#include <libdrizzle/drizzle_com.h>
 
46
#include <libdrizzle/drizzle_time.h>
 
47
 
 
48
extern unsigned int drizzle_port;
 
49
extern char *drizzle_unix_port;
 
50
 
 
51
#define CLIENT_NET_READ_TIMEOUT    365*24*3600  /* Timeout on read */
 
52
#define CLIENT_NET_WRITE_TIMEOUT  365*24*3600  /* Timeout on write */
 
53
 
 
54
#define IS_PRI_KEY(n)  ((n) & PRI_KEY_FLAG)
 
55
#define IS_NOT_NULL(n)  ((n) & NOT_NULL_FLAG)
 
56
#define IS_BLOB(n)  ((n) & BLOB_FLAG)
 
57
#define IS_NUM(t)  ((t) <= DRIZZLE_TYPE_LONGLONG || (t) == DRIZZLE_TYPE_NEWDECIMAL)
 
58
#define IS_NUM_FIELD(f)   ((f)->flags & NUM_FLAG)
 
59
#define INTERNAL_NUM_FIELD(f) (((f)->type <= DRIZZLE_TYPE_LONGLONG && ((f)->type != DRIZZLE_TYPE_TIMESTAMP || (f)->length == 14 || (f)->length == 8)))
 
60
#define IS_LONGDATA(t) ((t) == DRIZZLE_TYPE_TINY_BLOB)
 
61
 
 
62
 
 
63
typedef struct st_drizzle_field {
 
64
  char *name;                 /* Name of column */
 
65
  char *org_name;             /* Original column name, if an alias */
 
66
  char *table;                /* Table of column if column was a field */
 
67
  char *org_table;            /* Org table name, if table was an alias */
 
68
  char *db;                   /* Database for table */
 
69
  char *catalog;        /* Catalog for table */
 
70
  char *def;                  /* Default value (set by drizzle_list_fields) */
 
71
  uint32_t length;       /* Width of column (create length) */
 
72
  uint32_t max_length;   /* Max width for selected set */
 
73
  unsigned int name_length;
 
74
  unsigned int org_name_length;
 
75
  unsigned int table_length;
 
76
  unsigned int org_table_length;
 
77
  unsigned int db_length;
 
78
  unsigned int catalog_length;
 
79
  unsigned int def_length;
 
80
  unsigned int flags;         /* Div flags */
 
81
  unsigned int decimals;      /* Number of decimals in field */
 
82
  unsigned int charsetnr;     /* Character set */
 
83
  enum enum_field_types type; /* Type of field. See drizzle_com.h for types */
 
84
  void *extension;
 
85
} DRIZZLE_FIELD;
 
86
 
 
87
typedef char **DRIZZLE_ROW;    /* return data as array of strings */
 
88
typedef unsigned int DRIZZLE_FIELD_OFFSET; /* offset to current field */
 
89
 
 
90
#include <mysys/typelib.h>
 
91
 
 
92
#define DRIZZLE_COUNT_ERROR (~(uint64_t) 0)
 
93
 
 
94
typedef struct st_drizzle_rows {
 
95
  struct st_drizzle_rows *next;    /* list of rows */
 
96
  DRIZZLE_ROW data;
 
97
  unsigned long length;
 
98
} DRIZZLE_ROWS;
 
99
 
 
100
typedef DRIZZLE_ROWS *DRIZZLE_ROW_OFFSET;  /* offset to current row */
 
101
 
 
102
#include <mysys/my_alloc.h>
 
103
 
 
104
typedef struct embedded_query_result EMBEDDED_QUERY_RESULT;
 
105
typedef struct st_drizzle_data {
 
106
  DRIZZLE_ROWS *data;
 
107
  struct embedded_query_result *embedded_info;
 
108
  MEM_ROOT alloc;
 
109
  uint64_t rows;
 
110
  unsigned int fields;
 
111
  /* extra info for embedded library */
 
112
  void *extension;
 
113
} DRIZZLE_DATA;
 
114
 
 
115
enum drizzle_option
 
116
{
 
117
  DRIZZLE_OPT_CONNECT_TIMEOUT, DRIZZLE_OPT_COMPRESS, DRIZZLE_OPT_NAMED_PIPE,
 
118
  DRIZZLE_INIT_COMMAND, DRIZZLE_READ_DEFAULT_FILE, DRIZZLE_READ_DEFAULT_GROUP,
 
119
  DRIZZLE_SET_CHARSET_DIR, DRIZZLE_SET_CHARSET_NAME, DRIZZLE_OPT_LOCAL_INFILE,
 
120
  DRIZZLE_OPT_PROTOCOL, DRIZZLE_SHARED_MEMORY_BASE_NAME, DRIZZLE_OPT_READ_TIMEOUT,
 
121
  DRIZZLE_OPT_WRITE_TIMEOUT, DRIZZLE_OPT_USE_RESULT,
 
122
  DRIZZLE_OPT_USE_REMOTE_CONNECTION,
 
123
  DRIZZLE_OPT_GUESS_CONNECTION, DRIZZLE_SET_CLIENT_IP, DRIZZLE_SECURE_AUTH,
 
124
  DRIZZLE_REPORT_DATA_TRUNCATION, DRIZZLE_OPT_RECONNECT,
 
125
  DRIZZLE_OPT_SSL_VERIFY_SERVER_CERT
 
126
};
 
127
 
 
128
struct st_drizzle_options {
 
129
  unsigned int connect_timeout, read_timeout, write_timeout;
 
130
  unsigned int port, protocol;
 
131
  unsigned long client_flag;
 
132
  char *host,*user,*password,*unix_socket,*db;
 
133
  struct st_dynamic_array *init_commands;
 
134
  char *my_cnf_file,*my_cnf_group, *charset_dir, *charset_name;
 
135
  char *ssl_key;        /* PEM key file */
 
136
  char *ssl_cert;        /* PEM cert file */
 
137
  char *ssl_ca;          /* PEM CA file */
 
138
  char *ssl_capath;        /* PEM directory of CA-s? */
 
139
  char *ssl_cipher;        /* cipher to use */
 
140
  char *shared_memory_base_name;
 
141
  unsigned long max_allowed_packet;
 
142
  bool use_ssl;        /* if to use SSL or not */
 
143
  bool compress,named_pipe;
 
144
  bool unused1;
 
145
  bool unused2;
 
146
  bool unused3;
 
147
  bool unused4;
 
148
  enum drizzle_option methods_to_use;
 
149
  char *client_ip;
 
150
  /* Refuse client connecting to server if it uses old (pre-4.1.1) protocol */
 
151
  bool secure_auth;
 
152
  /* 0 - never report, 1 - always report (default) */
 
153
  bool report_data_truncation;
 
154
 
 
155
  /* function pointers for local infile support */
 
156
  int (*local_infile_init)(void **, const char *, void *);
 
157
  int (*local_infile_read)(void *, char *, unsigned int);
 
158
  void (*local_infile_end)(void *);
 
159
  int (*local_infile_error)(void *, char *, unsigned int);
 
160
  void *local_infile_userdata;
 
161
  void *extension;
 
162
};
 
163
 
 
164
enum drizzle_status
 
165
{
 
166
  DRIZZLE_STATUS_READY,DRIZZLE_STATUS_GET_RESULT,DRIZZLE_STATUS_USE_RESULT
 
167
};
 
168
 
 
169
enum drizzle_protocol_type
 
170
{
 
171
  DRIZZLE_PROTOCOL_TCP
 
172
};
 
173
 
 
174
typedef struct character_set
 
175
{
 
176
  unsigned int      number;     /* character set number              */
 
177
  unsigned int      state;      /* character set state               */
 
178
  const char        *csname;    /* collation name                    */
 
179
  const char        *name;      /* character set name                */
 
180
  const char        *comment;   /* comment                           */
 
181
  const char        *dir;       /* character set directory           */
 
182
  unsigned int      mbminlen;   /* min. length for multibyte strings */
 
183
  unsigned int      mbmaxlen;   /* max. length for multibyte strings */
 
184
} MY_CHARSET_INFO;
 
185
 
 
186
struct st_drizzle_methods;
 
187
struct st_drizzle_stmt;
 
188
 
 
189
typedef struct st_drizzle
 
190
{
 
191
  NET    net;      /* Communication parameters */
 
192
  unsigned char  *connector_fd;    /* ConnectorFd for SSL */
 
193
  char    *host,*user,*passwd,*unix_socket,*server_version,*host_info;
 
194
  char          *info, *db;
 
195
  const struct charset_info_st *charset;
 
196
  DRIZZLE_FIELD  *fields;
 
197
  MEM_ROOT  field_alloc;
 
198
  uint64_t affected_rows;
 
199
  uint64_t insert_id;    /* id if insert on table with NEXTNR */
 
200
  uint64_t extra_info;    /* Not used */
 
201
  uint32_t thread_id;    /* Id for connection in server */
 
202
  uint32_t packet_length;
 
203
  uint32_t  port;
 
204
  uint32_t client_flag,server_capabilities;
 
205
  uint32_t  protocol_version;
 
206
  uint32_t  field_count;
 
207
  uint32_t  server_status;
 
208
  uint32_t  server_language;
 
209
  uint32_t  warning_count;
 
210
  struct st_drizzle_options options;
 
211
  enum drizzle_status status;
 
212
  bool  free_me;    /* If free in drizzle_close */
 
213
  bool  reconnect;    /* set to 1 if automatic reconnect */
 
214
 
 
215
  /* session-wide random string */
 
216
  char          scramble[SCRAMBLE_LENGTH+1];
 
217
  bool unused1;
 
218
  void *unused2, *unused3, *unused4, *unused5;
 
219
 
 
220
  const struct st_drizzle_methods *methods;
 
221
  void *thd;
 
222
  /*
 
223
    Points to boolean flag in DRIZZLE_RES  or DRIZZLE_STMT. We set this flag
 
224
    from drizzle_stmt_close if close had to cancel result set of this object.
 
225
  */
 
226
  bool *unbuffered_fetch_owner;
 
227
  /* needed for embedded server - no net buffer to store the 'info' */
 
228
  char *info_buffer;
 
229
  void *extension;
 
230
} DRIZZLE;
 
231
 
 
232
 
 
233
typedef struct st_drizzle_res {
 
234
  uint64_t  row_count;
 
235
  DRIZZLE_FIELD  *fields;
 
236
  DRIZZLE_DATA  *data;
 
237
  DRIZZLE_ROWS  *data_cursor;
 
238
  uint32_t *lengths;    /* column lengths of current row */
 
239
  DRIZZLE *handle;    /* for unbuffered reads */
 
240
  const struct st_drizzle_methods *methods;
 
241
  DRIZZLE_ROW  row;      /* If unbuffered read */
 
242
  DRIZZLE_ROW  current_row;    /* buffer to current row */
 
243
  MEM_ROOT  field_alloc;
 
244
  uint32_t  field_count, current_field;
 
245
  bool  eof;      /* Used by drizzle_fetch_row */
 
246
  /* drizzle_stmt_close() had to cancel this result */
 
247
  bool       unbuffered_fetch_cancelled; 
 
248
  void *extension;
 
249
} DRIZZLE_RES;
 
250
 
 
251
 
 
252
#if !defined(DRIZZLE_SERVER) && !defined(DRIZZLE_CLIENT)
 
253
#define DRIZZLE_CLIENT
 
254
#endif
 
255
 
 
256
 
 
257
typedef struct st_drizzle_parameters
 
258
{
 
259
  uint32_t *p_max_allowed_packet;
 
260
  uint32_t *p_net_buffer_length;
 
261
  void *extension;
 
262
} DRIZZLE_PARAMETERS;
 
263
 
 
264
#if !defined(DRIZZLE_SERVER)
 
265
#define max_allowed_packet (*drizzle_get_parameters()->p_max_allowed_packet)
 
266
#define net_buffer_length (*drizzle_get_parameters()->p_net_buffer_length)
 
267
#endif
 
268
 
 
269
/*
 
270
  Set up and bring down the server; to ensure that applications will
 
271
  work when linked against either the standard client library or the
 
272
  embedded server library, these functions should be called.
 
273
*/
 
274
void drizzle_server_end(void);
 
275
 
 
276
/*
 
277
  drizzle_server_init/end need to be called when using libdrizzle or
 
278
  libdrizzleclient (exactly, drizzle_server_init() is called by drizzle_init() so
 
279
  you don't need to call it explicitely; but you need to call
 
280
  drizzle_server_end() to free memory). The names are a bit misleading
 
281
  (drizzle_SERVER* to be used when using libdrizzleCLIENT). So we add more general
 
282
  names which suit well whether you're using libdrizzled or libdrizzleclient. We
 
283
  intend to promote these aliases over the drizzle_server* ones.
 
284
*/
 
285
#define drizzle_library_end drizzle_server_end
 
286
 
 
287
const DRIZZLE_PARAMETERS * drizzle_get_parameters(void);
 
288
 
 
289
/*
 
290
  Set up and bring down a thread; these function should be called
 
291
  for each thread in an application which opens at least one MySQL
 
292
  connection.  All uses of the connection(s) should be between these
 
293
  function calls.
 
294
*/
 
295
bool drizzle_thread_init(void);
 
296
void drizzle_thread_end(void);
 
297
 
 
298
/*
 
299
  Functions to get information from the DRIZZLE and DRIZZLE_RES structures
 
300
  Should definitely be used if one uses shared libraries.
 
301
*/
 
302
 
 
303
uint64_t drizzle_num_rows(const DRIZZLE_RES *res);
 
304
unsigned int drizzle_num_fields(const DRIZZLE_RES *res);
 
305
bool drizzle_eof(const DRIZZLE_RES *res);
 
306
const DRIZZLE_FIELD * drizzle_fetch_field_direct(const DRIZZLE_RES *res,
 
307
                unsigned int fieldnr);
 
308
const DRIZZLE_FIELD * drizzle_fetch_fields(const DRIZZLE_RES *res);
 
309
DRIZZLE_ROW_OFFSET drizzle_row_tell(const DRIZZLE_RES *res);
 
310
DRIZZLE_FIELD_OFFSET drizzle_field_tell(const DRIZZLE_RES *res);
 
311
 
 
312
uint32_t drizzle_field_count(const DRIZZLE *drizzle);
 
313
uint64_t drizzle_affected_rows(const DRIZZLE *drizzle);
 
314
uint64_t drizzle_insert_id(const DRIZZLE *drizzle);
 
315
uint32_t drizzle_errno(const DRIZZLE *drizzle);
 
316
const char * drizzle_error(const DRIZZLE *drizzle);
 
317
const char * drizzle_sqlstate(const DRIZZLE *drizzle);
 
318
uint32_t drizzle_warning_count(const DRIZZLE *drizzle);
 
319
const char * drizzle_info(const DRIZZLE *drizzle);
 
320
uint32_t drizzle_thread_id(const DRIZZLE *drizzle);
 
321
const char * drizzle_character_set_name(const DRIZZLE *drizzle);
 
322
int32_t          drizzle_set_character_set(DRIZZLE *drizzle, const char *csname);
 
323
 
 
324
DRIZZLE * drizzle_create(DRIZZLE *drizzle);
 
325
bool   drizzle_change_user(DRIZZLE *drizzle, const char *user,
 
326
            const char *passwd, const char *db);
 
327
DRIZZLE * drizzle_connect(DRIZZLE *drizzle, const char *host,
 
328
             const char *user,
 
329
             const char *passwd,
 
330
             const char *db,
 
331
             uint32_t port,
 
332
             const char *unix_socket,
 
333
             uint32_t clientflag);
 
334
int32_t    drizzle_select_db(DRIZZLE *drizzle, const char *db);
 
335
int32_t    drizzle_query(DRIZZLE *drizzle, const char *q);
 
336
int32_t    drizzle_send_query(DRIZZLE *drizzle, const char *q, uint32_t length);
 
337
int32_t    drizzle_real_query(DRIZZLE *drizzle, const char *q, uint32_t length);
 
338
DRIZZLE_RES * drizzle_store_result(DRIZZLE *drizzle);
 
339
DRIZZLE_RES * drizzle_use_result(DRIZZLE *drizzle);
 
340
 
 
341
void        drizzle_get_character_set_info(const DRIZZLE *drizzle,
 
342
                                                   MY_CHARSET_INFO *charset);
 
343
 
 
344
/* local infile support */
 
345
 
 
346
#define LOCAL_INFILE_ERROR_LEN 512
 
347
 
 
348
void
 
349
drizzle_set_local_infile_handler(DRIZZLE *drizzle,
 
350
        int (*local_infile_init)(void **, const char *, void *),
 
351
        int (*local_infile_read)(void *, char *, unsigned int),
 
352
        void (*local_infile_end)(void *),int (*local_infile_error)
 
353
        (void *, char*, unsigned int), void *);
 
354
 
 
355
void
 
356
drizzle_set_local_infile_default(DRIZZLE *drizzle);
 
357
 
 
358
int32_t    drizzle_shutdown(DRIZZLE *drizzle, enum drizzle_enum_shutdown_level shutdown_level);
 
359
int32_t    drizzle_dump_debug_info(DRIZZLE *drizzle);
 
360
int32_t    drizzle_refresh(DRIZZLE *drizzle, uint32_t refresh_options);
 
361
int32_t    drizzle_kill(DRIZZLE *drizzle, uint32_t pid);
 
362
int32_t    drizzle_set_server_option(DRIZZLE *drizzle, enum enum_drizzle_set_option option);
 
363
int32_t    drizzle_ping(DRIZZLE *drizzle);
 
364
const char *  drizzle_stat(DRIZZLE *drizzle);
 
365
const char *  drizzle_get_server_info(const DRIZZLE *drizzle);
 
366
const char *  drizzle_get_client_info(void);
 
367
uint32_t  drizzle_get_client_version(void);
 
368
const char *  drizzle_get_host_info(const DRIZZLE *drizzle);
 
369
uint32_t  drizzle_get_server_version(const DRIZZLE *drizzle);
 
370
uint32_t  drizzle_get_proto_info(const DRIZZLE *drizzle);
 
371
DRIZZLE_RES *  drizzle_list_tables(DRIZZLE *drizzle,const char *wild);
 
372
DRIZZLE_RES *  drizzle_list_processes(DRIZZLE *drizzle);
 
373
int32_t    drizzle_options(DRIZZLE *drizzle,enum drizzle_option option, const void *arg);
 
374
void    drizzle_free_result(DRIZZLE_RES *result);
 
375
void    drizzle_data_seek(DRIZZLE_RES *result, uint64_t offset);
 
376
DRIZZLE_ROW_OFFSET drizzle_row_seek(DRIZZLE_RES *result, DRIZZLE_ROW_OFFSET offset);
 
377
DRIZZLE_FIELD_OFFSET drizzle_field_seek(DRIZZLE_RES *result, DRIZZLE_FIELD_OFFSET offset);
 
378
DRIZZLE_ROW  drizzle_fetch_row(DRIZZLE_RES *result);
 
379
uint32_t * drizzle_fetch_lengths(DRIZZLE_RES *result);
 
380
DRIZZLE_FIELD *  drizzle_fetch_field(DRIZZLE_RES *result);
 
381
DRIZZLE_RES *     drizzle_list_fields(DRIZZLE *drizzle, const char *table, const char *wild);
 
382
uint32_t  drizzle_escape_string(char *to,const char *from, uint32_t from_length);
 
383
uint32_t  drizzle_hex_string(char *to,const char *from, uint32_t from_length);
 
384
uint32_t        drizzle_real_escape_string(DRIZZLE *drizzle, char *to, const char *from, uint32_t length);
 
385
void    myodbc_remove_escape(const DRIZZLE *drizzle, char *name);
 
386
uint32_t  drizzle_thread_safe(void);
 
387
bool    drizzle_embedded(void);
 
388
bool         drizzle_read_query_result(DRIZZLE *drizzle);
 
389
 
 
390
 
 
391
 
 
392
typedef struct st_drizzle_methods
 
393
{
 
394
  bool (*read_query_result)(DRIZZLE *drizzle);
 
395
  bool (*advanced_command)(DRIZZLE *drizzle,
 
396
                           enum enum_server_command command,
 
397
                           const unsigned char *header,
 
398
                           uint32_t header_length,
 
399
                           const unsigned char *arg,
 
400
                           uint32_t arg_length,
 
401
                           bool skip_check);
 
402
  DRIZZLE_DATA *(*read_rows)(DRIZZLE *drizzle,DRIZZLE_FIELD *drizzle_fields, uint32_t fields);
 
403
  DRIZZLE_RES * (*use_result)(DRIZZLE *drizzle);
 
404
  void (*fetch_lengths)(uint32_t *to, DRIZZLE_ROW column, uint32_t field_count);
 
405
  void (*flush_use_result)(DRIZZLE *drizzle);
 
406
  DRIZZLE_FIELD * (*list_fields)(DRIZZLE *drizzle);
 
407
  int32_t (*unbuffered_fetch)(DRIZZLE *drizzle, char **row);
 
408
  const char *(*read_statistics)(DRIZZLE *drizzle);
 
409
  bool (*next_result)(DRIZZLE *drizzle);
 
410
  int32_t (*read_change_user_result)(DRIZZLE *drizzle);
 
411
} DRIZZLE_METHODS;
 
412
 
 
413
 
 
414
bool drizzle_commit(DRIZZLE *drizzle);
 
415
bool drizzle_rollback(DRIZZLE *drizzle);
 
416
bool drizzle_autocommit(DRIZZLE *drizzle, bool auto_mode);
 
417
bool drizzle_more_results(const DRIZZLE *drizzle);
 
418
int drizzle_next_result(DRIZZLE *drizzle);
 
419
void drizzle_close(DRIZZLE *sock);
 
420
 
 
421
 
 
422
/* status return codes */
 
423
#define DRIZZLE_NO_DATA        100
 
424
#define DRIZZLE_DATA_TRUNCATED 101
 
425
 
 
426
 
 
427
#define drizzle_reload(drizzle) drizzle_refresh((drizzle),REFRESH_GRANT)
 
428
 
 
429
/*
 
430
  The following functions are mainly exported because of binlog;
 
431
  They are not for general usage
 
432
*/
 
433
 
 
434
#define simple_command(drizzle, command, arg, length, skip_check) \
 
435
  (*(drizzle)->methods->advanced_command)(drizzle, command, 0,  \
 
436
                                        0, arg, length, skip_check)
 
437
 
 
438
#ifdef  __cplusplus
 
439
}
 
440
#endif
 
441
 
 
442
#endif /* _libdrizzle_drizzle_h */