1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
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.
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.
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
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"
28
#include <vio/violite.h>
30
#include <drizzled/version.h>
39
#define CONNECT_TIMEOUT 0
41
const char *unknown_sqlstate= "HY000";
42
const char *not_error_sqlstate= "00000";
43
const char *cant_connect_sqlstate= "08001";
45
static bool drizzle_client_init= false;
46
unsigned int drizzle_server_last_errno;
48
/* Server error code and message */
49
char drizzle_server_last_error[DRIZZLE_ERRMSG_SIZE];
51
/****************************************************************************
52
Init DRIZZLE structure or allocate one
53
****************************************************************************/
56
drizzle_create(DRIZZLE *ptr)
59
if (!drizzle_client_init)
61
drizzle_client_init=true;
63
if (!drizzle_get_default_port())
65
drizzle_set_default_port(DRIZZLE_PORT);
67
struct servent *serv_ptr;
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
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));
84
if ((env = getenv("DRIZZLE_TCP_PORT")))
85
drizzle_set_default_port((uint) atoi(env));
89
(void) signal(SIGPIPE, SIG_IGN);
95
ptr= (DRIZZLE *) malloc(sizeof(DRIZZLE));
99
drizzle_set_error(NULL, CR_OUT_OF_MEMORY, unknown_sqlstate);
102
memset(ptr, 0, sizeof(DRIZZLE));
107
memset(ptr, 0, sizeof(DRIZZLE));
110
ptr->options.connect_timeout= CONNECT_TIMEOUT;
111
strcpy(ptr->net.sqlstate, not_error_sqlstate);
114
Only enable LOAD DATA INFILE by default if configured with
115
--enable-local-infile
118
#if defined(ENABLED_LOCAL_INFILE)
119
ptr->options.client_flag|= CLIENT_LOCAL_FILES;
122
ptr->options.methods_to_use= DRIZZLE_OPT_GUESS_CONNECTION;
123
ptr->options.report_data_truncation= true; /* default */
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.
144
/**************************************************************************
146
**************************************************************************/
148
void drizzle_disconnect(DRIZZLE *drizzle)
150
int save_errno= errno;
151
if (drizzle->net.vio != 0)
153
vio_delete(drizzle->net.vio);
154
drizzle->net.vio= 0; /* Marker */
156
net_end(&drizzle->net);
157
free_old_query(drizzle);
163
Set the internal error message to DRIZZLE handler
165
@param drizzle connection handle (client side)
166
@param errcode CR_ error code, passed to ER macro to get
168
@parma sqlstate SQL standard sqlstate
171
void drizzle_set_error(DRIZZLE *drizzle, int errcode, const char *sqlstate)
174
assert(drizzle != 0);
179
net->last_errno= errcode;
180
strcpy(net->last_error, ER(errcode));
181
strcpy(net->sqlstate, sqlstate);
185
drizzle_server_last_errno= errcode;
186
strcpy(drizzle_server_last_error, ER(errcode));
192
unsigned int drizzle_errno(const DRIZZLE *drizzle)
194
return drizzle ? drizzle->net.last_errno : drizzle_server_last_errno;
198
const char * drizzle_error(const DRIZZLE *drizzle)
200
return drizzle ? _(drizzle->net.last_error) : _(drizzle_server_last_error);
204
Set an error message on the client.
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
214
void drizzle_set_extended_error(DRIZZLE *drizzle, int errcode,
215
const char *sqlstate,
216
const char *format, ...)
220
assert(drizzle != 0);
223
net->last_errno= errcode;
224
va_start(args, format);
225
vsnprintf(net->last_error, sizeof(net->last_error)-1,
228
strcpy(net->sqlstate, sqlstate);