~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/drizzle.c

  • Committer: Monty Taylor
  • Date: 2008-09-16 01:37:05 UTC
  • mto: This revision was merged to the branch mainline in revision 391.
  • Revision ID: monty@inaugust.com-20080916013705-772d1t7rh9ah9j1x
Moved more functions into drizzle.c as part of the split of code.
Added accessor function for drizzle_port.

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 Sun Microsystems, Inc.
 
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; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#include <libdrizzle/libdrizzle.h>
 
23
#include <libdrizzle/errmsg.h>
 
24
#include <libdrizzle/drizzle.h>
 
25
#include <libdrizzle/gettext.h>
 
26
#include "libdrizzle_priv.h"
 
27
 
 
28
#include <vio/violite.h>
 
29
 
 
30
#include <drizzled/version.h>
 
31
 
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <stdarg.h>
 
35
#include <string.h>
 
36
#include <netdb.h>
 
37
#include <assert.h>
 
38
 
 
39
#define CONNECT_TIMEOUT 0
 
40
 
 
41
const char  *unknown_sqlstate= "HY000";
 
42
const char  *not_error_sqlstate= "00000";
 
43
const char  *cant_connect_sqlstate= "08001";
 
44
 
 
45
static bool drizzle_client_init= false;
 
46
unsigned int drizzle_server_last_errno;
 
47
 
 
48
/* Server error code and message */
 
49
char drizzle_server_last_error[DRIZZLE_ERRMSG_SIZE];
 
50
 
 
51
/****************************************************************************
 
52
  Init DRIZZLE structure or allocate one
 
53
****************************************************************************/
 
54
 
 
55
DRIZZLE *
 
56
drizzle_create(DRIZZLE *ptr)
 
57
{
 
58
 
 
59
  if (!drizzle_client_init)
 
60
  {
 
61
    drizzle_client_init=true;
 
62
 
 
63
    if (!drizzle_get_default_port())
 
64
    {
 
65
      drizzle_set_default_port(DRIZZLE_PORT);
 
66
      {
 
67
        struct servent *serv_ptr;
 
68
        char *env;
 
69
 
 
70
        /*
 
71
          if builder specifically requested a default port, use that
 
72
          (even if it coincides with our factory default).
 
73
          only if they didn't do we check /etc/services (and, failing
 
74
          on that, fall back to the factory default of 4427).
 
75
          either default can be overridden by the environment variable
 
76
          DRIZZLE_TCP_PORT, which in turn can be overridden with command
 
77
          line options.
 
78
        */
 
79
 
 
80
#if DRIZZLE_PORT_DEFAULT == 0
 
81
        if ((serv_ptr = getservbyname("drizzle", "tcp")))
 
82
          drizzle_set_default_port((uint) ntohs((ushort) serv_ptr->s_port));
 
83
#endif
 
84
        if ((env = getenv("DRIZZLE_TCP_PORT")))
 
85
          drizzle_set_default_port((uint) atoi(env));
 
86
      }
 
87
    }
 
88
#if defined(SIGPIPE)
 
89
    (void) signal(SIGPIPE, SIG_IGN);
 
90
#endif
 
91
  }
 
92
 
 
93
  if (ptr == NULL)
 
94
  {
 
95
    ptr= (DRIZZLE *) malloc(sizeof(DRIZZLE));
 
96
 
 
97
    if (ptr == NULL)
 
98
    {
 
99
      drizzle_set_error(NULL, CR_OUT_OF_MEMORY, unknown_sqlstate);
 
100
      return 0;
 
101
    }
 
102
    memset(ptr, 0, sizeof(DRIZZLE));
 
103
    ptr->free_me=1;
 
104
  }
 
105
  else
 
106
  {
 
107
    memset(ptr, 0, sizeof(DRIZZLE));
 
108
  }
 
109
 
 
110
  ptr->options.connect_timeout= CONNECT_TIMEOUT;
 
111
  strcpy(ptr->net.sqlstate, not_error_sqlstate);
 
112
 
 
113
  /*
 
114
    Only enable LOAD DATA INFILE by default if configured with
 
115
    --enable-local-infile
 
116
  */
 
117
 
 
118
#if defined(ENABLED_LOCAL_INFILE)
 
119
  ptr->options.client_flag|= CLIENT_LOCAL_FILES;
 
120
#endif
 
121
 
 
122
  ptr->options.methods_to_use= DRIZZLE_OPT_GUESS_CONNECTION;
 
123
  ptr->options.report_data_truncation= true;  /* default */
 
124
 
 
125
  /*
 
126
    By default we don't reconnect because it could silently corrupt data (after
 
127
    reconnection you potentially lose table locks, user variables, session
 
128
    variables (transactions but they are specifically dealt with in
 
129
    drizzle_reconnect()).
 
130
    This is a change: < 5.0.3 drizzle->reconnect was set to 1 by default.
 
131
    How this change impacts existing apps:
 
132
    - existing apps which relyed on the default will see a behaviour change;
 
133
    they will have to set reconnect=1 after drizzle_connect().
 
134
    - existing apps which explicitely asked for reconnection (the only way they
 
135
    could do it was by setting drizzle.reconnect to 1 after drizzle_connect())
 
136
    will not see a behaviour change.
 
137
    - existing apps which explicitely asked for no reconnection
 
138
    (drizzle.reconnect=0) will not see a behaviour change.
 
139
  */
 
140
  ptr->reconnect= 0;
 
141
 
 
142
  return ptr;
 
143
}
 
144
/**************************************************************************
 
145
  Shut down connection
 
146
**************************************************************************/
 
147
 
 
148
void drizzle_disconnect(DRIZZLE *drizzle)
 
149
{
 
150
  int save_errno= errno;
 
151
  if (drizzle->net.vio != 0)
 
152
  {
 
153
    vio_delete(drizzle->net.vio);
 
154
    drizzle->net.vio= 0;          /* Marker */
 
155
  }
 
156
  net_end(&drizzle->net);
 
157
  free_old_query(drizzle);
 
158
  errno= save_errno;
 
159
}
 
160
 
 
161
 
 
162
/**
 
163
   Set the internal error message to DRIZZLE handler
 
164
 
 
165
   @param drizzle connection handle (client side)
 
166
   @param errcode  CR_ error code, passed to ER macro to get
 
167
   error text
 
168
   @parma sqlstate SQL standard sqlstate
 
169
*/
 
170
 
 
171
void drizzle_set_error(DRIZZLE *drizzle, int errcode, const char *sqlstate)
 
172
{
 
173
  NET *net;
 
174
  assert(drizzle != 0);
 
175
 
 
176
  if (drizzle)
 
177
  {
 
178
    net= &drizzle->net;
 
179
    net->last_errno= errcode;
 
180
    strcpy(net->last_error, ER(errcode));
 
181
    strcpy(net->sqlstate, sqlstate);
 
182
  }
 
183
  else
 
184
  {
 
185
    drizzle_server_last_errno= errcode;
 
186
    strcpy(drizzle_server_last_error, ER(errcode));
 
187
  }
 
188
  return;
 
189
}
 
190
 
 
191
 
 
192
unsigned int drizzle_errno(const DRIZZLE *drizzle)
 
193
{
 
194
  return drizzle ? drizzle->net.last_errno : drizzle_server_last_errno;
 
195
}
 
196
 
 
197
 
 
198
const char * drizzle_error(const DRIZZLE *drizzle)
 
199
{
 
200
  return drizzle ? _(drizzle->net.last_error) : _(drizzle_server_last_error);
 
201
}
 
202
 
 
203
/**
 
204
   Set an error message on the client.
 
205
 
 
206
   @param drizzle connection handle
 
207
   @param errcode   CR_* errcode, for client errors
 
208
   @param sqlstate  SQL standard sql state, unknown_sqlstate for the
 
209
   majority of client errors.
 
210
   @param format    error message template, in sprintf format
 
211
   @param ...       variable number of arguments
 
212
*/
 
213
 
 
214
void drizzle_set_extended_error(DRIZZLE *drizzle, int errcode,
 
215
                                const char *sqlstate,
 
216
                                const char *format, ...)
 
217
{
 
218
  NET *net;
 
219
  va_list args;
 
220
  assert(drizzle != 0);
 
221
 
 
222
  net= &drizzle->net;
 
223
  net->last_errno= errcode;
 
224
  va_start(args, format);
 
225
  vsnprintf(net->last_error, sizeof(net->last_error)-1,
 
226
            format, args);
 
227
  va_end(args);
 
228
  strcpy(net->sqlstate, sqlstate);
 
229
 
 
230
  return;
 
231
}