1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/***********************************************************************
Copyright (c) 2009 Innobase Oy. All rights reserved.
Copyright (c) 2009 Oracle. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "ib0config.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "dict0dict.h"
#include "pars0pars.h"
#include "que0que.h"
#include "trx0roll.h"
#include "api0api.h"
UNIV_STATIC int api_sql_enter_func_enabled = 0;
#define UT_DBG_ENTER_FUNC_ENABLED api_sql_enter_func_enabled
/*********************************************************************//**
Function to parse ib_exec_sql() and ib_exec_ddl_sql() args.
@return own: info struct */
UNIV_STATIC
pars_info_t*
ib_exec_vsql(
/*=========*/
const char* sql, /*!< in: sql to execute */
int n_args, /*!< in: no. of args */
va_list ap) /*!< in: arg list */
{
int i;
pars_info_t* info;
UT_DBG_ENTER_FUNC;
info = pars_info_create();
for (i = 0; i < n_args; ++i) {
ib_col_type_t type;
type = va_arg(ap, ib_col_type_t);
switch (type) {
case IB_CHAR:
case IB_VARCHAR: {
const char* n;
const char* v;
char prefix;
n = va_arg(ap, const char *);
v = va_arg(ap, const char *);
prefix = *n;
ut_a(prefix == ':' || prefix == '$');
++n;
if (prefix == '$') {
pars_info_add_id(info, n, v);
} else {
pars_info_add_str_literal(info, n, v);
}
break;
}
case IB_INT: {
byte* p; /* dest buffer */
ulint l; /* length */
ulint s; /* TRUE if signed integer */
const char* n; /* literal name */
ulint prtype;
l = va_arg(ap, ib_ulint_t);
s = va_arg(ap, ib_ulint_t);
n = va_arg(ap, const char *);
prtype = s ? 0 : DATA_UNSIGNED;
p = mem_heap_alloc(info->heap, l);
switch (l) {
case 1: {
byte v;
v = va_arg(ap, int);
mach_write_int_type(p, (byte*)&v, l, s);
break;
}
case 2: {
ib_uint16_t v;
v = va_arg(ap, int);
mach_write_int_type(p, (byte*)&v, l, s);
break;
}
case 4: {
ib_uint32_t v;
v = va_arg(ap, ib_uint32_t);
mach_write_int_type(p, (byte*)&v, l, s);
break;
}
case 8: {
ib_uint64_t v;
v = va_arg(ap, ib_uint64_t);
mach_write_int_type(p, (byte*)&v, l, s);
break;
}
default:
ut_error;
}
pars_info_add_literal(info, n, p, l, DATA_INT, prtype);
break;
}
case IB_SYS: {
const char* n;
pars_user_func_cb_t f;
void* a;
n = va_arg(ap, const char *);
f = va_arg(ap, pars_user_func_cb_t);
a = va_arg(ap, void*);
pars_info_add_function(info, n, f, a);
break;
}
default:
/* FIXME: Do the other types too */
ut_error;
}
}
return(info);
}
/*********************************************************************//**
Execute arbitrary SQL using InnoDB's internal parser. The statement
is executed in a new transaction. Table name parameters must be prefixed
with a '$' symbol and variables with ':'
@return DB_SUCCESS or error code */
ib_err_t
ib_exec_sql(
/*========*/
const char* sql, /*!< in: sql to execute */
ib_ulint_t n_args, /*!< in: no. of args */
...)
{
va_list ap;
trx_t* trx;
ib_err_t err;
pars_info_t* info;
UT_DBG_ENTER_FUNC;
va_start(ap, n_args);
info = ib_exec_vsql(sql, n_args, ap);
va_end(ap);
/* We use the private SQL parser of Innobase to generate
the query graphs needed to execute the SQL statement. */
trx = trx_allocate_for_client(NULL);
err = trx_start(trx, ULINT_UNDEFINED);
ut_a(err == DB_SUCCESS);
trx->op_info = "exec client sql";
dict_mutex_enter();
/* Note that we've already acquired the dictionary mutex. */
err = que_eval_sql(info, sql, FALSE, trx);
ut_a(err == DB_SUCCESS);
dict_mutex_exit();
if (err != DB_SUCCESS) {
trx_rollback(trx, FALSE, NULL);
} else {
trx_commit(trx);
}
trx->op_info = "";
trx_free_for_client(trx);
return(err);
}
/*********************************************************************//**
Execute arbitrary SQL using InnoDB's internal parser. The statement
is executed in a background transaction. It will lock the data
dictionary lock for the duration of the query.
@return DB_SUCCESS or error code */
ib_err_t
ib_exec_ddl_sql(
/*============*/
const char* sql, /*!< in: sql to execute */
ib_ulint_t n_args, /*!< in: no. of args */
...)
{
va_list ap;
trx_t* trx;
ib_err_t err;
pars_info_t* info;
int started;
UT_DBG_ENTER_FUNC;
va_start(ap, n_args);
info = ib_exec_vsql(sql, n_args, ap);
va_end(ap);
/* We use the private SQL parser of Innobase to generate
the query graphs needed to execute the SQL statement. */
trx = trx_allocate_for_background();
started = trx_start(trx, ULINT_UNDEFINED);
ut_a(started);
trx->op_info = "exec client ddl sql";
err = ib_schema_lock_exclusive((ib_trx_t) trx);
ut_a(err == DB_SUCCESS);
/* Note that we've already acquired the dictionary mutex by
setting reserve_dict_mutex to FALSE. */
err = que_eval_sql(info, sql, FALSE, trx);
ut_a(err == DB_SUCCESS);
ib_schema_unlock((ib_trx_t) trx);
if (err != DB_SUCCESS) {
trx_rollback(trx, FALSE, NULL);
} else {
trx_commit(trx);
}
trx->op_info = "";
trx_free_for_background(trx);
return(err);
}
|