Blame view

sql/sql.c 7.79 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
Sergey Poznyakoff authored
2
   Copyright (C) 2004-2007, 2009-2012, 2014-2017 Free Software
3
   Foundation, Inc.
4 5 6 7

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
8
   version 3 of the License, or (at your option) any later version.
9 10 11 12 13 14

   This library 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
   Lesser General Public License for more details.

15
   You should have received a copy of the GNU Lesser General
16 17
   Public License along with this library.  If not, see 
   <http://www.gnu.org/licenses/>. */
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

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <mailutils/mailutils.h>
#include <mailutils/sql.h>
#include "modlist.h"

#define NSTATIC_MODS sizeof(static_dispatch_tab)/sizeof(static_dispatch_tab[0])

static mu_sql_dispatch_t **sql_disptab;
size_t sql_disptab_next;
size_t sql_disptab_size;

static int
init_disptab ()
{
  if (!sql_disptab)
    {
      size_t size;

      sql_disptab_size = NSTATIC_MODS;
      size = sql_disptab_size * sizeof sql_disptab[0];
      sql_disptab = malloc (size);
      if (!sql_disptab)
	return ENOMEM;
      memcpy(sql_disptab, static_dispatch_tab, size);
      sql_disptab_next = sql_disptab_size;
    }
  return 0;
}

51 52
/* FIXME: See comment 'For dynamic loading' below */
#if 0
53 54 55 56 57 58
static int
add_disptab (mu_sql_dispatch_t *tab)
{
  if (sql_disptab_next == sql_disptab_size)
    {
      mu_sql_dispatch_t **tmp;
Sergey Poznyakoff authored
59

60 61 62 63 64 65 66 67 68
      tmp = realloc (sql_disptab, sql_disptab_size + 4);
      if (!tmp)
	return ENOMEM;
      sql_disptab = tmp;
      sql_disptab_size += 4;
    }
  sql_disptab[sql_disptab_next] = tab;
  return sql_disptab_next++;
}
69
#endif
70 71

int
72
mu_sql_interface_index (char const *name)
73 74 75
{
  int i;
  //mu_sql_dispatch_t *tab;
Sergey Poznyakoff authored
76

77 78 79 80
  init_disptab ();
  for (i = 1; i < sql_disptab_next; i++)
    if (sql_disptab[i] && (!name || strcmp (sql_disptab[i]->name, name) == 0))
      return i;
81 82 83
  /* FIXME: For dynamic loading
      if (name && mu_sql_load_ext (name, "dispatch_tab", &tab))
       return add_disptab (tab);
Sergey Poznyakoff authored
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
  return 0;
}

static mu_sql_dispatch_t *
get_sql_entry (int type)
{
  init_disptab ();
  if (type == 0 && sql_disptab[0] == NULL)
    {
      int i;
      for (i = 1; i < sql_disptab_next; i++)
	if (sql_disptab[i])
	  {
	    sql_disptab[type] = sql_disptab[i];
	    break;
	  }
    }
  if (!sql_disptab[type])
    {
      mu_error (_("SQL dispatcher table empty"));
      abort ();
    }
  return sql_disptab[type];
}

#define SQL_F(c,f) (*get_sql_entry ((c)->interface) -> f)

int
mu_sql_connection_init (mu_sql_connection_t *pconn, int interface,
			char *server, int  port, char *login,
			char *password, char *dbname)
{
  static mu_sql_dispatch_t *tab;
  mu_sql_connection_t conn;
Sergey Poznyakoff authored
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
  tab = get_sql_entry (interface);
  if (!tab)
    return MU_ERR_NO_INTERFACE;

  conn = calloc (1, sizeof (*conn));
  if (!conn)
    return ENOMEM;
  conn->state = mu_sql_not_connected;
  conn->interface = interface;
  conn->server = server;
  conn->port = port;
  conn->login = login;
  conn->password = password;
  conn->dbname = dbname;
  if (tab->init)
    {
      int rc = tab->init (conn);
      if (rc)
	{
	  free (conn);
	  return rc;
	}
    }
  *pconn = conn;
  return 0;
}

int
mu_sql_connection_destroy (mu_sql_connection_t *conn)
{
  if (!conn || !*conn)
    return EINVAL;

  mu_sql_disconnect (*conn);
  SQL_F (*conn, destroy) (*conn);
  free (*conn);
  *conn = NULL;
  return 0;
}

int
mu_sql_connect (mu_sql_connection_t conn)
{
  int rc;
Sergey Poznyakoff authored
164

165 166 167 168 169 170 171 172 173 174 175
  if (!conn)
    return EINVAL;

  switch (conn->state)
    {
    case mu_sql_not_connected:
      break;

    case mu_sql_connected:
    case mu_sql_query_run:
      return MU_ERR_DB_ALREADY_CONNECTED;
Sergey Poznyakoff authored
176

177 178 179 180 181 182 183 184 185 186 187 188 189
    case mu_sql_result_available:
      return MU_ERR_RESULT_NOT_RELEASED;
    }
  rc = SQL_F (conn, connect) (conn);
  if (!rc)
    conn->state = mu_sql_connected;
  return rc;
}

int
mu_sql_disconnect (mu_sql_connection_t conn)
{
  int rc;
Sergey Poznyakoff authored
190

191 192 193 194 195 196 197 198 199 200 201
  if (!conn)
    return EINVAL;

  switch (conn->state)
    {
    case mu_sql_not_connected:
      return 0;

    case mu_sql_connected:
    case mu_sql_query_run:
      break;
Sergey Poznyakoff authored
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
    case mu_sql_result_available:
      return MU_ERR_RESULT_NOT_RELEASED;
    }
  rc = SQL_F (conn, disconnect) (conn);
  if (rc == 0)
    conn->state = mu_sql_not_connected;
  return rc;
}


int
mu_sql_query (mu_sql_connection_t conn, char *query)
{
  int rc;
  if (!conn)
    return EINVAL;

  switch (conn->state)
    {
    case mu_sql_not_connected:
      return MU_ERR_DB_NOT_CONNECTED;

    case mu_sql_connected:
    case mu_sql_query_run:
      break;

    case mu_sql_result_available:
      return MU_ERR_RESULT_NOT_RELEASED;
    }
Sergey Poznyakoff authored
232

233 234 235 236 237 238 239 240 241 242
  rc = SQL_F (conn, query) (conn, query);
  if (rc == 0)
    conn->state = mu_sql_query_run;
  return rc;
}

int
mu_sql_store_result (mu_sql_connection_t conn)
{
  int rc;
Sergey Poznyakoff authored
243

244 245 246 247 248 249 250 251 252 253
  if (!conn)
    return EINVAL;

  switch (conn->state)
    {
    case mu_sql_not_connected:
      return MU_ERR_DB_NOT_CONNECTED;

    case mu_sql_connected:
      return MU_ERR_NO_QUERY;
Sergey Poznyakoff authored
254

255 256 257 258 259 260
    case mu_sql_query_run:
      break;

    case mu_sql_result_available:
      return MU_ERR_RESULT_NOT_RELEASED;
    }
Sergey Poznyakoff authored
261

262 263 264 265 266 267 268 269 270 271
  rc = SQL_F (conn, store_result) (conn);
  if (rc == 0)
    conn->state = mu_sql_result_available;
  return rc;
}

int
mu_sql_release_result (mu_sql_connection_t conn)
{
  int rc;
Sergey Poznyakoff authored
272

273 274 275 276 277 278 279 280 281 282
  if (!conn)
    return EINVAL;

  switch (conn->state)
    {
    case mu_sql_not_connected:
      return MU_ERR_DB_NOT_CONNECTED;

    case mu_sql_connected:
      return MU_ERR_NO_QUERY;
Sergey Poznyakoff authored
283

284 285 286 287 288 289
    case mu_sql_query_run:
      return MU_ERR_NO_RESULT;

    case mu_sql_result_available:
      break;
    }
Sergey Poznyakoff authored
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  rc = SQL_F (conn, release_result) (conn);
  if (rc == 0)
    conn->state = mu_sql_connected;
  return rc;
}

int
mu_sql_num_tuples (mu_sql_connection_t conn, size_t *np)
{
  if (!conn)
    return EINVAL;

  switch (conn->state)
    {
    case mu_sql_not_connected:
      return MU_ERR_DB_NOT_CONNECTED;

    case mu_sql_connected:
      return MU_ERR_NO_QUERY;
Sergey Poznyakoff authored
310

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    case mu_sql_query_run:
      return MU_ERR_NO_RESULT;

    case mu_sql_result_available:
      break;
    }

  return SQL_F (conn, num_tuples) (conn, np);
}

int
mu_sql_num_columns (mu_sql_connection_t conn, size_t *np)
{
  if (!conn)
    return EINVAL;
  switch (conn->state)
    {
    case mu_sql_not_connected:
      return MU_ERR_DB_NOT_CONNECTED;

    case mu_sql_connected:
      return MU_ERR_NO_QUERY;
Sergey Poznyakoff authored
333

334 335 336 337 338 339 340 341
    case mu_sql_query_run:
      return MU_ERR_NO_RESULT;

    case mu_sql_result_available:
      break;
    }
  return SQL_F (conn, num_columns) (conn, np);
}
Sergey Poznyakoff authored
342

343 344 345 346 347 348 349 350 351 352 353 354 355 356

int
mu_sql_get_column (mu_sql_connection_t conn, size_t nrow, size_t ncol,
		   char **pdata)
{
  if (!conn)
    return EINVAL;
  switch (conn->state)
    {
    case mu_sql_not_connected:
      return MU_ERR_DB_NOT_CONNECTED;

    case mu_sql_connected:
      return MU_ERR_NO_QUERY;
Sergey Poznyakoff authored
357

358 359 360 361 362 363 364 365 366
    case mu_sql_query_run:
      return MU_ERR_NO_RESULT;

    case mu_sql_result_available:
      break;
    }
  return SQL_F (conn, get_column) (conn, nrow, ncol, pdata);
}

367
int
Sergey Poznyakoff authored
368
mu_sql_get_field (mu_sql_connection_t conn, size_t nrow, const char *fname,
369 370 371 372
		  char **pdata)
{
  int rc;
  size_t fno;
Sergey Poznyakoff authored
373

374 375 376 377 378 379 380 381 382 383
  if (!conn)
    return EINVAL;

  switch (conn->state)
    {
    case mu_sql_not_connected:
      return MU_ERR_DB_NOT_CONNECTED;

    case mu_sql_connected:
      return MU_ERR_NO_QUERY;
Sergey Poznyakoff authored
384

385 386 387 388 389 390 391 392 393 394 395 396
    case mu_sql_query_run:
      return MU_ERR_NO_RESULT;

    case mu_sql_result_available:
      break;
    }

  rc = SQL_F (conn, get_field_number) (conn, fname, &fno);
  if (rc == 0)
    rc = SQL_F (conn, get_column) (conn, nrow, fno, pdata);
  return rc;
}
397

398
const char *
399 400 401 402 403 404
mu_sql_strerror (mu_sql_connection_t conn)
{
  if (!conn)
    return strerror (EINVAL);
  return SQL_F (conn, errstr) (conn);
}