gda-mysql-recordset.c 34.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Copyright (C) 2001 - 2005 Rodrigo Moya <rodrigo@gnome-db.org>
 * Copyright (C) 2002 Andrew Hill <andru@src.gnome.org>
 * Copyright (C) 2002 - 2003 Gonzalo Paniagua Javier <gonzalo@ximian.com>
 * Copyright (C) 2002 Holger Thon <holger.thon@gnome-db.org>
 * Copyright (C) 2002 Zbigniew Chyla <cyba@gnome.pl>
 * Copyright (C) 2003 Chris Silles <csilles@src.gnome.org>
 * Copyright (C) 2003 Laurent Sansonetti <lrz@gnome.org>
 * Copyright (C) 2003 Paisa Seeluangsawat <paisa@users.sf.net>
10
 * Copyright (C) 2004 - 2016 Vivien Malerba <malerba@gnome-db.org>
11 12 13
 * Copyright (C) 2005 Alan Knowles <alan@akbkhome.com>
 * Copyright (C) 2005 - 2009 Bas Driessen <bas.driessen@xobas.com>
 * Copyright (C) 2005 Mike Fisk <mfisk@woozle.org>
14
 * Copyright (C) 2005 Álvaro Peña <alvaropg@telefonica.net>
15
 * Copyright (C) 2011 Murray Cumming <murrayc@murrayc.com>
Rodrigo Moya's avatar
Rodrigo Moya committed
16
 *
17 18 19 20
 * 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
 * version 2 of the License, or (at your option) any later version.
Rodrigo Moya's avatar
Rodrigo Moya committed
21
 *
22
 * This library is distributed in the hope that it will be useful,
Rodrigo Moya's avatar
Rodrigo Moya committed
23 24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
 * Lesser General Public License for more details.
Rodrigo Moya's avatar
Rodrigo Moya committed
26
 *
27 28 29 30
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
Rodrigo Moya's avatar
Rodrigo Moya committed
31 32
 */

33
#include <stdarg.h>
Holger Thon's avatar
Holger Thon committed
34
#include <string.h>
35 36 37
#include <glib/gi18n-lib.h>
#include <libgda/gda-util.h>
#include <libgda/gda-connection-private.h>
38
#include <libgda/providers-support/gda-data-select-priv.h>
39
#include "gda-mysql.h"
Rodrigo Moya's avatar
Rodrigo Moya committed
40
#include "gda-mysql-recordset.h"
41
#include "gda-mysql-provider.h"
42
#include "gda-mysql-util.h"
43
#include "gda-data-select-extra.h"
44
#include <libgda/libgda-global-variables.h>
45 46 47
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
48

49
#define _GDA_PSTMT(x) ((GdaPStmt*)(x))
50

51 52 53 54 55 56
enum
{
	PROP_0,
	PROP_CHUNK_SIZE,
	PROP_CHUNKS_READ
};
Rodrigo Moya's avatar
Rodrigo Moya committed
57

58
static void
59 60 61 62 63 64
gda_mysql_recordset_class_init (GdaMysqlRecordsetClass  *klass);
static void
gda_mysql_recordset_init (GdaMysqlRecordset       *recset,
			  GdaMysqlRecordsetClass  *klass);
static void
gda_mysql_recordset_dispose (GObject  *object);
65

66 67
/* virtual methods */
static gint
68
gda_mysql_recordset_fetch_nb_rows (GdaDataSelect  *model);
69
static gboolean
70
gda_mysql_recordset_fetch_random (GdaDataSelect  *model,
71 72 73
				  GdaRow        **row,
				  gint            rownum,
				  GError        **error);
74
static gboolean
75
gda_mysql_recordset_fetch_next (GdaDataSelect  *model,
76 77 78
				GdaRow        **row,
				gint            rownum,
				GError        **error);
79
static gboolean
80
gda_mysql_recordset_fetch_prev (GdaDataSelect  *model,
81 82 83
				GdaRow        **row,
				gint            rownum,
				GError        **error);
84
static gboolean
85
gda_mysql_recordset_fetch_at (GdaDataSelect  *model,
86 87 88
			      GdaRow        **row,
			      gint            rownum,
			      GError        **error);
89

90
struct _GdaMysqlRecordsetPrivate {
91
	GdaConnection  *cnc;
92
	
93
	MYSQL_STMT     *mysql_stmt;
94

95 96 97
	gint            chunk_size;    /* Number of rows to fetch at a time when iterating forward/backward. */
	gint            chunks_read;   /* Number of times that we've iterated forward/backward. */
	GdaRow         *tmp_row;       /* Used in cursor mode to store a reference to the latest #GdaRow. */
98 99 100 101

	/* if no prepared statement available */
	gint          ncols;
        GType        *types;
102 103
};
static GObjectClass *parent_class = NULL;
104

105 106 107
/*
 * Object init and finalize
 */
108
static void
109
gda_mysql_recordset_init (GdaMysqlRecordset       *recset,
110
			  G_GNUC_UNUSED GdaMysqlRecordsetClass  *klass)
111 112
{
	g_return_if_fail (GDA_IS_MYSQL_RECORDSET (recset));
113 114
	recset->priv = g_new0 (GdaMysqlRecordsetPrivate, 1);
	recset->priv->cnc = NULL;
115

116
	/* initialize specific information */
117
	recset->priv->chunk_size = 1;
118
	recset->priv->chunks_read = 0;
119 120 121

	recset->priv->ncols = 0;
	recset->priv->types = NULL;
122 123
}

124

125 126 127
gint
gda_mysql_recordset_get_chunk_size (GdaMysqlRecordset  *recset)
{
128
	g_return_val_if_fail (GDA_IS_MYSQL_RECORDSET (recset), -1);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	return recset->priv->chunk_size;
}

void
gda_mysql_recordset_set_chunk_size (GdaMysqlRecordset  *recset,
				    gint                chunk_size)
{
	g_return_if_fail (GDA_IS_MYSQL_RECORDSET (recset));

	if (recset->priv->mysql_stmt == NULL)  // Creation is in progress so it's not set.
		return;

#if MYSQL_VERSION_ID >= 50002
	const unsigned long prefetch_rows = chunk_size;
	if (mysql_stmt_attr_set (recset->priv->mysql_stmt, STMT_ATTR_PREFETCH_ROWS,
				 (void *) &prefetch_rows)) {
145
		g_warning ("%s: %s\n", __func__, mysql_stmt_error (recset->priv->mysql_stmt));
146 147 148 149 150 151 152 153 154 155 156 157 158
		return;
	}
	recset->priv->chunk_size = chunk_size;
	g_object_notify (G_OBJECT(recset), "chunk-size");
#else
	g_warning (_("Could not use CURSOR. Mysql version 5.0 at least is required. "
		     "Chunk size ignored."));
#endif
}

gint
gda_mysql_recordset_get_chunks_read (GdaMysqlRecordset  *recset)
{
159
	g_return_val_if_fail (GDA_IS_MYSQL_RECORDSET (recset), -1);
160 161 162 163
	return recset->priv->chunks_read;
}


164
static void
165 166 167 168
gda_mysql_recordset_set_property (GObject       *object,
				  guint          param_id,
				  const GValue  *value,
				  GParamSpec    *pspec)
169
{
170
	GdaMysqlRecordset *recordset;
171

172 173 174 175
	g_return_if_fail (object != NULL);
	g_return_if_fail (GDA_IS_MYSQL_RECORDSET(object));
	g_return_if_fail (GDA_MYSQL_RECORDSET(object)->priv != NULL);

176
	recordset = GDA_MYSQL_RECORDSET(object);
177 178 179

	switch (param_id) {
	case PROP_CHUNK_SIZE:
180
		gda_mysql_recordset_set_chunk_size (recordset,
181
						    g_value_get_int (value));
182
		break;
183
	case PROP_CHUNKS_READ:
184
		break;
185 186
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
187 188 189
	}
}

190 191 192 193 194
static void
gda_mysql_recordset_get_property (GObject     *object,
				  guint        param_id,
				  GValue      *value,
				  GParamSpec  *pspec)
Rodrigo Moya's avatar
Rodrigo Moya committed
195
{
196
	GdaMysqlRecordset *recordset;
197

198 199 200
	g_return_if_fail (object != NULL);
	g_return_if_fail (GDA_IS_MYSQL_RECORDSET(object));
	g_return_if_fail (GDA_MYSQL_RECORDSET(object)->priv != NULL);
201

202
	recordset = GDA_MYSQL_RECORDSET(object);
203

204 205
	switch (param_id) {
	case PROP_CHUNK_SIZE:
206
		g_value_set_int (value, recordset->priv->chunk_size);
207 208
		break;
	case PROP_CHUNKS_READ:
209
		g_value_set_int (value, recordset->priv->chunks_read);
210 211 212
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
213
	}
Rodrigo Moya's avatar
Rodrigo Moya committed
214 215
}

216

217 218
static void
gda_mysql_recordset_class_init (GdaMysqlRecordsetClass  *klass)
219
{
220
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
221
	GdaDataSelectClass *pmodel_class = GDA_DATA_SELECT_CLASS (klass);
222

223
	parent_class = g_type_class_peek_parent (klass);
224

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	object_class->dispose = gda_mysql_recordset_dispose;
	pmodel_class->fetch_nb_rows = gda_mysql_recordset_fetch_nb_rows;
	pmodel_class->fetch_random = gda_mysql_recordset_fetch_random;

	pmodel_class->fetch_next = gda_mysql_recordset_fetch_next;
	pmodel_class->fetch_prev = gda_mysql_recordset_fetch_prev;
	pmodel_class->fetch_at = gda_mysql_recordset_fetch_at;
	
	/* Properties. */
	object_class->set_property = gda_mysql_recordset_set_property;
	object_class->get_property = gda_mysql_recordset_get_property;

	g_object_class_install_property
		(object_class,
		 PROP_CHUNK_SIZE,
		 g_param_spec_int ("chunk-size", _("Number of rows fetched at a time"),
				   NULL,
242
				   1, G_MAXINT - 1, 1,
243 244 245 246 247 248 249 250 251 252
				   (G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE)));

	g_object_class_install_property
		(object_class,
		 PROP_CHUNKS_READ,
		 g_param_spec_int ("chunks-read", _("Number of row chunks read since the object creation"),
				   NULL,
				   0, G_MAXINT - 1, 0,
				   (G_PARAM_CONSTRUCT | G_PARAM_WRITABLE | G_PARAM_READABLE)));
	
253 254
}

255 256
static void
gda_mysql_recordset_dispose (GObject  *object)
257
{
258
	GdaMysqlRecordset *recset = (GdaMysqlRecordset *) object;
259

260
	g_return_if_fail (GDA_IS_MYSQL_RECORDSET (recset));
Rodrigo Moya's avatar
Rodrigo Moya committed
261

262
	if (recset->priv) {
263
		GDA_MYSQL_PSTMT (_gda_data_select_get_prep_stmt (GDA_DATA_SELECT (object)))->stmt_used = FALSE;
264

265 266 267 268 269 270 271 272
		if (recset->priv->cnc) {
			g_object_unref (G_OBJECT(recset->priv->cnc));
			recset->priv->cnc = NULL;
		}
		if (recset->priv->tmp_row) {
			g_object_unref (G_OBJECT(recset->priv->tmp_row));
			recset->priv->tmp_row = NULL;
		}
273 274
		if (recset->priv->types)
			g_free (recset->priv->types);
Rodrigo Moya's avatar
Rodrigo Moya committed
275

276 277
		g_free (recset->priv);
		recset->priv = NULL;
Rodrigo Moya's avatar
Rodrigo Moya committed
278 279
	}

280
	parent_class->dispose (object);
281 282
}

283 284 285
/*
 * Public functions
 */
286

287 288 289 290
GType
gda_mysql_recordset_get_type (void)
{
	static GType type = 0;
291

292
	if (G_UNLIKELY (type == 0)) {
293
		static GMutex registering;
294 295 296 297 298 299 300 301 302
		static const GTypeInfo info = {
			sizeof (GdaMysqlRecordsetClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) gda_mysql_recordset_class_init,
			NULL,
			NULL,
			sizeof (GdaMysqlRecordset),
			0,
303 304
			(GInstanceInitFunc) gda_mysql_recordset_init,
			NULL
305
		};
306
		g_mutex_lock (&registering);
307
		if (type == 0)
308
			type = g_type_register_static (GDA_TYPE_DATA_SELECT, "GdaMysqlRecordset", &info, 0);
309
		g_mutex_unlock (&registering);
310 311
	}

312
	return type;
Rodrigo Moya's avatar
Rodrigo Moya committed
313 314
}

315
static GType
316
_gda_mysql_type_to_gda (G_GNUC_UNUSED MysqlConnectionData *cdata,
317
			enum enum_field_types  mysql_type, unsigned int charsetnr)
318
{
319
	GType gtype;
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	switch (mysql_type) {
	case MYSQL_TYPE_TINY:
	case MYSQL_TYPE_SHORT:
	case MYSQL_TYPE_LONG:
	case MYSQL_TYPE_INT24:
	case MYSQL_TYPE_YEAR:
		gtype = G_TYPE_INT;
		break;
	case MYSQL_TYPE_LONGLONG:
		gtype = G_TYPE_LONG;
		break;
	case MYSQL_TYPE_FLOAT:
		gtype = G_TYPE_FLOAT;
		break;
	case MYSQL_TYPE_DECIMAL:
	case MYSQL_TYPE_NEWDECIMAL:
336 337
		gtype = GDA_TYPE_NUMERIC;
		break;
338 339 340 341 342
	case MYSQL_TYPE_DOUBLE:
		gtype = G_TYPE_DOUBLE;
		break;
	case MYSQL_TYPE_TIMESTAMP:
	case MYSQL_TYPE_DATETIME:
343
		gtype = G_TYPE_DATE_TIME;
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
		break;
	case MYSQL_TYPE_DATE:
		gtype = G_TYPE_DATE;
		break;
	case MYSQL_TYPE_TIME:
		gtype = GDA_TYPE_TIME;
		break;
	case MYSQL_TYPE_NULL:
		gtype = GDA_TYPE_NULL;
		break;
	case MYSQL_TYPE_STRING:
	case MYSQL_TYPE_VAR_STRING:
	case MYSQL_TYPE_SET:
	case MYSQL_TYPE_ENUM:
	case MYSQL_TYPE_GEOMETRY:
359 360
	case MYSQL_TYPE_BIT:
	case MYSQL_TYPE_BLOB:
361
	default:
362 363 364 365 366
		if (charsetnr == 63)
			gtype = GDA_TYPE_BLOB;
		else
			gtype = G_TYPE_STRING;
		break;
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	}

	/* g_print ("%s: ", __func__); */
	/* switch (mysql_type) { */
	/* case MYSQL_TYPE_TINY:  g_print ("MYSQL_TYPE_TINY");  break; */
	/* case MYSQL_TYPE_SHORT:  g_print ("MYSQL_TYPE_SHORT");  break; */
	/* case MYSQL_TYPE_LONG:  g_print ("MYSQL_TYPE_LONG");  break; */
	/* case MYSQL_TYPE_INT24:  g_print ("MYSQL_TYPE_INT24");  break; */
	/* case MYSQL_TYPE_YEAR:  g_print ("MYSQL_TYPE_YEAR");  break; */
	/* case MYSQL_TYPE_LONGLONG:  g_print ("MYSQL_TYPE_LONGLONG");  break; */
	/* case MYSQL_TYPE_FLOAT:  g_print ("MYSQL_TYPE_FLOAT");  break; */
	/* case MYSQL_TYPE_DECIMAL:  g_print ("MYSQL_TYPE_DECIMAL");  break; */
	/* case MYSQL_TYPE_NEWDECIMAL:  g_print ("MYSQL_TYPE_NEWDECIMAL");  break; */
	/* case MYSQL_TYPE_DOUBLE:  g_print ("MYSQL_TYPE_DOUBLE");  break; */
	/* case MYSQL_TYPE_BIT:  g_print ("MYSQL_TYPE_BIT");  break; */
	/* case MYSQL_TYPE_BLOB:  g_print ("MYSQL_TYPE_BLOB");  break; */
	/* case MYSQL_TYPE_TIMESTAMP:  g_print ("MYSQL_TYPE_TIMESTAMP");  break; */
	/* case MYSQL_TYPE_DATETIME:  g_print ("MYSQL_TYPE_DATETIME");  break; */
	/* case MYSQL_TYPE_DATE:  g_print ("MYSQL_TYPE_DATE");  break; */
	/* case MYSQL_TYPE_TIME:  g_print ("MYSQL_TYPE_TIME");  break; */
	/* case MYSQL_TYPE_NULL:  g_print ("MYSQL_TYPE_NULL");  break; */
	/* case MYSQL_TYPE_STRING:  g_print ("MYSQL_TYPE_STRING");  break; */
	/* case MYSQL_TYPE_VAR_STRING:  g_print ("MYSQL_TYPE_VAR_STRING");  break; */
	/* case MYSQL_TYPE_SET:  g_print ("MYSQL_TYPE_SET");  break; */
	/* case MYSQL_TYPE_ENUM:  g_print ("MYSQL_TYPE_ENUM");  break; */
	/* case MYSQL_TYPE_GEOMETRY:  g_print ("MYSQL_TYPE_GEOMETRY");  break; */
	/* default:  g_print ("UNKNOWN %d: MYSQL_TYPE_STRING", mysql_type);  break; */
	/* } */
	/* g_print ("\n"); */

	return gtype;
}

400 401 402 403 404 405 406 407 408 409 410 411
GdaDataModel *
gda_mysql_recordset_new_direct (GdaConnection *cnc, GdaDataModelAccessFlags flags, 
				GType *col_types)
{
	GdaMysqlRecordset *model;
        MysqlConnectionData *cdata;
        gint i;
	GdaDataModelAccessFlags rflags;
	GSList *columns = NULL;

        g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL);

412
	cdata = (MysqlConnectionData*) gda_connection_internal_get_provider_data_error (cnc, NULL);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
	if (!cdata)
		return NULL;

	/* determine access mode: RANDOM or CURSOR FORWARD are the only supported */
	if (flags & GDA_DATA_MODEL_ACCESS_RANDOM)
		rflags = GDA_DATA_MODEL_ACCESS_RANDOM;
	else
		rflags = GDA_DATA_MODEL_ACCESS_CURSOR_FORWARD;

	/* create data model */
        model = g_object_new (GDA_TYPE_MYSQL_RECORDSET,
			      "connection", cnc,
			      "model-usage", rflags,
			      NULL);
        model->priv->cnc = cnc;
	g_object_ref (G_OBJECT(cnc));

	/* columns & types */	
	model->priv->ncols = mysql_field_count (cdata->mysql);
	model->priv->types = g_new0 (GType, model->priv->ncols);
	
	/* create columns */
	for (i = 0; i < model->priv->ncols; i++)
		columns = g_slist_prepend (columns, gda_column_new ());
	columns = g_slist_reverse (columns);

	if (col_types) {
		for (i = 0; ; i++) {
			if (col_types [i] > 0) {
				if (col_types [i] == G_TYPE_NONE)
					break;
				if (i >= model->priv->ncols) {
					g_warning (_("Column %d out of range (0-%d), ignoring its specified type"), i,
						   model->priv->ncols - 1);
					break;
				}
				else
					model->priv->types [i] = col_types [i];
			}
		}
	}

	/* fill bind result */
	MYSQL_RES *mysql_res = mysql_store_result (cdata->mysql);
	MYSQL_FIELD *mysql_fields = mysql_fetch_fields (mysql_res);
	GSList *list;

460
	_gda_data_select_set_advertized_nrows ((GdaDataSelect *) model, mysql_affected_rows (cdata->mysql));
461 462 463 464 465 466 467 468 469
	for (i=0, list = columns; 
	     i < model->priv->ncols; 
	     i++, list = list->next) {
		GdaColumn *column = GDA_COLUMN (list->data);
		
		/* use C API to set columns' information using gda_column_set_*() */
		MYSQL_FIELD *field = &mysql_fields[i];
		
		GType gtype = model->priv->types [i];
470
		if (gtype == GDA_TYPE_NULL) {
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
			gtype = _gda_mysql_type_to_gda (cdata, field->type, field->charsetnr);
			model->priv->types [i] = gtype;
		}
		gda_column_set_g_type (column, gtype);
		gda_column_set_name (column, field->name);
		gda_column_set_description (column, field->name);
	}
	gda_data_select_set_columns (GDA_DATA_SELECT (model), columns);

	/* load ALL data */
	MYSQL_ROW mysql_row;
	gint rownum;
	GdaServerProvider *prov;
	prov = gda_connection_get_provider (cnc);
	for (mysql_row = mysql_fetch_row (mysql_res), rownum = 0;
	     mysql_row;
	     mysql_row = mysql_fetch_row (mysql_res), rownum++) {
		GdaRow *row = gda_row_new (model->priv->ncols);
		gint col;
		for (col = 0; col < model->priv->ncols; col++) {
			gint i = col;
		
			GValue *value = gda_row_get_value (row, i);
			GType type = model->priv->types[i];
			char *data = mysql_row[i];

			if (!data || (type == GDA_TYPE_NULL))
				continue;

			gda_value_reset_with_type (value, type);
			if (type == G_TYPE_STRING)
				g_value_set_string (value, data);
			else {
				GdaDataHandler *dh;
				gboolean valueset = FALSE;
				dh = gda_server_provider_get_data_handler_g_type (prov, cnc, type);
				if (dh) {
					GValue *tmpvalue;
					tmpvalue = gda_data_handler_get_value_from_str (dh, data, type);
					if (tmpvalue) {
						*value = *tmpvalue;
						g_free (tmpvalue);
						valueset = TRUE;
					}
				}
				if (!valueset)
					gda_row_invalidate_value (row, value);
			}
		}
		gda_data_select_take_row ((GdaDataSelect*) model, row, rownum);
	}
	mysql_free_result (mysql_res);

        return GDA_DATA_MODEL (model);
}

527

528
/*
Bas Driessen's avatar
Bas Driessen committed
529
 * the @ps struct is modified and transferred to the new data model created in
530
 * this function
531 532 533
 *
 * See MySQL's documentation "C API Prepared Statement Type Codes":
 *     http://docs.oracle.com/cd/E17952_01/refman-5.5-en/c-api-prepared-statement-type-codes.html
534 535 536 537
 */
GdaDataModel *
gda_mysql_recordset_new (GdaConnection            *cnc,
			 GdaMysqlPStmt            *ps,
538
			 GdaSet                   *exec_params,
539 540
			 GdaDataModelAccessFlags   flags,
			 GType                    *col_types)
541
{
542 543 544 545
	GdaMysqlRecordset *model;
        MysqlConnectionData *cdata;
        gint i;
	GdaDataModelAccessFlags rflags;
546

547 548
        g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL);
        g_return_val_if_fail (ps != NULL, NULL);
549

550
	cdata = (MysqlConnectionData*) gda_connection_internal_get_provider_data_error (cnc, NULL);
551 552
	if (!cdata)
		return NULL;
553

554 555
	g_assert (ps->mysql_stmt);

556
	/* make sure @ps reports the correct number of columns using the API*/
557 558
        if (_GDA_PSTMT (ps)->ncols < 0)
		_GDA_PSTMT(ps)->ncols = mysql_stmt_field_count (ps->mysql_stmt);
559 560

        /* completing @ps if not yet done */
561 562
	g_assert (! ps->stmt_used);
        ps->stmt_used = TRUE;
563 564 565 566 567 568 569
        if (!_GDA_PSTMT (ps)->types && (_GDA_PSTMT (ps)->ncols > 0)) {
		/* create prepared statement's columns */
		for (i = 0; i < _GDA_PSTMT (ps)->ncols; i++)
			_GDA_PSTMT (ps)->tmpl_columns = g_slist_prepend (_GDA_PSTMT (ps)->tmpl_columns, 
									 gda_column_new ());
		_GDA_PSTMT (ps)->tmpl_columns = g_slist_reverse (_GDA_PSTMT (ps)->tmpl_columns);

570 571 572 573 574
		/* create prepared statement's types, all types are initialized to GDA_TYPE_NULL */
		_GDA_PSTMT (ps)->types = g_new (GType, _GDA_PSTMT (ps)->ncols);
		for (i = 0; i < _GDA_PSTMT (ps)->ncols; i++)
			_GDA_PSTMT (ps)->types [i] = GDA_TYPE_NULL;

575 576 577 578 579
		if (col_types) {
			for (i = 0; ; i++) {
				if (col_types [i] > 0) {
					if (col_types [i] == G_TYPE_NONE)
						break;
580
					if (i >= _GDA_PSTMT (ps)->ncols) {
581
						g_warning (_("Column %d out of range (0-%d), ignoring its specified type"), i,
582
							   _GDA_PSTMT (ps)->ncols - 1);
583 584
						break;
					}
585 586 587
					else
						_GDA_PSTMT (ps)->types [i] = col_types [i];
				}
588
			}
589
		}
590
	}
591

592 593 594 595 596 597 598 599 600 601 602
	/* get rid of old bound result if any */
	if (ps->mysql_bind_result) {
		gint i;
		for (i = 0; i < ((GdaPStmt *) ps)->ncols; ++i) {
			g_free (ps->mysql_bind_result[i].buffer);
			g_free (ps->mysql_bind_result[i].is_null);
			g_free (ps->mysql_bind_result[i].length);
		}
		g_free (ps->mysql_bind_result);
		ps->mysql_bind_result = NULL;
	}
603

604 605 606 607 608 609
	/* fill bind result */
	MYSQL_RES *mysql_res = mysql_stmt_result_metadata (ps->mysql_stmt);
	MYSQL_FIELD *mysql_fields = mysql_fetch_fields (mysql_res);
	
	MYSQL_BIND *mysql_bind_result = g_new0 (MYSQL_BIND, GDA_PSTMT (ps)->ncols);
	GSList *list;
610

611 612 613 614 615 616 617 618 619
	for (i=0, list = _GDA_PSTMT (ps)->tmpl_columns; 
	     i < GDA_PSTMT (ps)->ncols; 
	     i++, list = list->next) {
		GdaColumn *column = GDA_COLUMN (list->data);
		
		/* use C API to set columns' information using gda_column_set_*() */
		MYSQL_FIELD *field = &mysql_fields[i];
		
		GType gtype = _GDA_PSTMT(ps)->types[i];
620
		if (gtype == GDA_TYPE_NULL) {
621
			gtype = _gda_mysql_type_to_gda (cdata, field->type, field->charsetnr);
622
			_GDA_PSTMT(ps)->types[i] = gtype;
Rodrigo Moya's avatar
Rodrigo Moya committed
623
		}
624 625 626
		gda_column_set_g_type (column, gtype);
		gda_column_set_name (column, field->name);
		gda_column_set_description (column, field->name);
627
		
628 629 630
		/* binding results with types */
		mysql_bind_result[i].buffer_type = field->type;
		mysql_bind_result[i].is_unsigned = field->flags & UNSIGNED_FLAG ? TRUE : FALSE;
631
		mysql_bind_result[i].is_null = g_malloc0 (sizeof (my_bool));
632
		
633 634
		switch (mysql_bind_result[i].buffer_type) {
		case MYSQL_TYPE_TINY:
635 636
			mysql_bind_result[i].buffer = g_malloc0 (sizeof (signed char));
			break;
637
		case MYSQL_TYPE_SHORT:
638 639
			mysql_bind_result[i].buffer = g_malloc0 (sizeof (short int));
			break;
640 641 642
		case MYSQL_TYPE_INT24:
		case MYSQL_TYPE_LONG:
		case MYSQL_TYPE_YEAR:
643
			mysql_bind_result[i].buffer = g_malloc0 (sizeof (int));
644 645
			break;
		case MYSQL_TYPE_LONGLONG:
646
			mysql_bind_result[i].buffer = g_malloc0 (sizeof (long long));
647 648 649 650 651 652 653
			break;
		case MYSQL_TYPE_NULL:
			break;
		case MYSQL_TYPE_TIME:
		case MYSQL_TYPE_DATE:
		case MYSQL_TYPE_DATETIME:
		case MYSQL_TYPE_TIMESTAMP:
654
			mysql_bind_result[i].buffer = g_malloc0 (sizeof (MYSQL_TIME));
655 656 657
			break;
		case MYSQL_TYPE_FLOAT:
		case MYSQL_TYPE_DOUBLE:
658
			mysql_bind_result[i].buffer = g_malloc0 (sizeof (double));
659 660 661 662 663 664 665
			break;
		case MYSQL_TYPE_STRING:
		case MYSQL_TYPE_VAR_STRING:
		case MYSQL_TYPE_BLOB:
		case MYSQL_TYPE_TINY_BLOB:
		case MYSQL_TYPE_MEDIUM_BLOB:
		case MYSQL_TYPE_LONG_BLOB:
666
		case MYSQL_TYPE_DECIMAL:
667 668 669 670
		case MYSQL_TYPE_NEWDECIMAL:
		case MYSQL_TYPE_BIT:
			mysql_bind_result[i].buffer = g_malloc0 (field->max_length + 1);
			mysql_bind_result[i].buffer_length = field->max_length + 1;
671
			mysql_bind_result[i].length = g_malloc0 (sizeof (unsigned long));
672 673 674 675 676
			break;
		default:
			g_warning (_("Invalid column bind data type. %d\n"),
				   mysql_bind_result[i].buffer_type);
		}
677 678 679
		/*g_print ("%s(): NAME=%s, TYPE=%d, GTYPE=%s, unsigned: %d\n",
			 __FUNCTION__, field->name, field->type, g_type_name (gtype),
			 field->flags & UNSIGNED_FLAG);*/
680 681 682 683 684 685 686 687 688
	}
	
	if (mysql_stmt_bind_result (ps->mysql_stmt, mysql_bind_result)) {
		g_warning ("mysql_stmt_bind_result failed: %s\n",
			   mysql_stmt_error (ps->mysql_stmt));
	}
	
	mysql_free_result (mysql_res);
	ps->mysql_bind_result = mysql_bind_result;
689 690 691 692 693 694 695 696 697

	/* determine access mode: RANDOM or CURSOR FORWARD are the only supported */
	if (flags & GDA_DATA_MODEL_ACCESS_RANDOM)
		rflags = GDA_DATA_MODEL_ACCESS_RANDOM;
	else
		rflags = GDA_DATA_MODEL_ACCESS_CURSOR_FORWARD;

	/* create data model */
        model = g_object_new (GDA_TYPE_MYSQL_RECORDSET,
698
			      "connection", cnc,
699 700
			      "prepared-stmt", ps,
			      "model-usage", rflags,
701
			      "exec-params", exec_params, 
702 703 704 705
			      NULL);
        model->priv->cnc = cnc;
	g_object_ref (G_OBJECT(cnc));

706
	model->priv->mysql_stmt = ps->mysql_stmt;
Rodrigo Moya's avatar
Rodrigo Moya committed
707

708
	_gda_data_select_set_advertized_nrows ((GdaDataSelect *) model, mysql_stmt_affected_rows (ps->mysql_stmt));
709

710 711
        return GDA_DATA_MODEL (model);
}
712

Rodrigo Moya's avatar
Rodrigo Moya committed
713

714 715 716 717
/*
 * Get the number of rows in @model, if possible
 */
static gint
718
gda_mysql_recordset_fetch_nb_rows (GdaDataSelect *model)
719 720
{
	GdaMysqlRecordset *imodel;
Rodrigo Moya's avatar
Rodrigo Moya committed
721

722
	imodel = GDA_MYSQL_RECORDSET (model);
723 724
	if (_gda_data_select_get_advertized_nrows (model) >= 0)
		return _gda_data_select_get_advertized_nrows (model);
Rodrigo Moya's avatar
Rodrigo Moya committed
725

726
	_gda_data_select_set_advertized_nrows (model, mysql_stmt_affected_rows (imodel->priv->mysql_stmt));
727
	
728
	return _gda_data_select_get_advertized_nrows (model);
Rodrigo Moya's avatar
Rodrigo Moya committed
729 730
}

731
static GdaRow *
732
new_row_from_mysql_stmt (GdaMysqlRecordset *imodel, G_GNUC_UNUSED gint rownum, GError **error)
733
{
734
	//g_print ("%s(): NCOLS=%d  ROWNUM=%d\n", __func__, ((GdaDataSelect *) imodel)->prep_stmt->ncols, rownum);
735 736
	int res;
	MYSQL_BIND *mysql_bind_result;
737 738
	g_return_val_if_fail (imodel->priv->mysql_stmt != NULL, NULL);

739
	mysql_bind_result = ((GdaMysqlPStmt *) _gda_data_select_get_prep_stmt ((GdaDataSelect *) imodel))->mysql_bind_result;
740 741 742 743 744 745
	g_assert (mysql_bind_result);

	res = mysql_stmt_fetch (imodel->priv->mysql_stmt);
	if (res == MYSQL_NO_DATA) {
		/* should not happen */
		g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
746
			     "%s", "No more data, please report this bug to "
747
			     "http://gitlab.gnome.org/GNOME/libgda/issues");
748 749 750 751 752
	}
	else if (res == MYSQL_DATA_TRUNCATED) {
		GString *string;

		string = g_string_new ("Truncated data, please report this bug to "
753
				       "http://gitlab.gnome.org/GNOME/libgda/issues");
754 755

		gint col;
756
		for (col = 0; col < _gda_data_select_get_prep_stmt ((GdaDataSelect *) imodel)->ncols; ++col) {
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
			my_bool truncated;
			mysql_bind_result[col].error = &truncated;
			mysql_stmt_fetch_column (imodel->priv->mysql_stmt, &(mysql_bind_result[col]),
						 (unsigned int)col, 0);
			if (truncated)
				g_string_append_printf (string, "\n  column %d is truncated\n", col);
			mysql_bind_result[col].error = NULL;
		}
		g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR, "%s", string->str);
		g_string_free (string, TRUE);

		return NULL;
	}
	else if (res) {
		_gda_mysql_make_error (imodel->priv->cnc, NULL, imodel->priv->mysql_stmt, error);
772
		return NULL;
773
	}
774 775 776

	/* g_print ("%s: SQL=%s\n", __func__, ((GdaDataSelect *) imodel)->prep_stmt->sql); */

777
	
778
	GdaRow *row = gda_row_new (_gda_data_select_get_prep_stmt ((GdaDataSelect *) imodel)->ncols);
779
	gint col;
780
	for (col = 0; col < _gda_data_select_get_prep_stmt ((GdaDataSelect *) imodel)->ncols; ++col) {
781 782
		gint i = col;
		
783
		GValue *value = gda_row_get_value (row, i);
784
		GType type = _gda_data_select_get_prep_stmt ((GdaDataSelect *) imodel)->types[i];
785
		
786 787
		/*g_print ("%s: #%d : TYPE=%d, GTYPE=%s\n", __func__, i, mysql_bind_result[i].buffer_type, g_type_name (type));*/

788
		my_bool is_null = FALSE;
789 790
		unsigned long length;
		
791
		memmove (&is_null, mysql_bind_result[i].is_null, sizeof (my_bool));
792 793 794 795 796 797 798
		if (is_null) {
			gda_value_set_null (value);
			continue;
		}
		else
			gda_value_reset_with_type (value, type);

799
		switch (mysql_bind_result[i].buffer_type) {
800 801
		case MYSQL_TYPE_SHORT: {
			short int bvalue = 0;
802
			memmove (&bvalue, mysql_bind_result[i].buffer, sizeof (bvalue));
803 804 805 806 807
			g_value_set_int (value, bvalue);
			break;
		}
		case MYSQL_TYPE_TINY: {
			signed char bvalue = 0;
808
			memmove (&bvalue, mysql_bind_result[i].buffer, sizeof (bvalue));
809 810 811
			g_value_set_int (value, bvalue);
			break;
		}
812 813
		case MYSQL_TYPE_INT24:
		case MYSQL_TYPE_LONG:
814 815
		case MYSQL_TYPE_YEAR: {
			int bvalue = 0;
816
			memmove (&bvalue, mysql_bind_result[i].buffer, sizeof (bvalue));
817 818
			
			if (type == G_TYPE_INT)
819
				g_value_set_int (value, bvalue);
820
			else if (type == G_TYPE_LONG)
821
				g_value_set_long (value, (long) bvalue);
822
			else if (type == G_TYPE_BOOLEAN)
823
				g_value_set_boolean (value, bvalue ? TRUE : FALSE);
824 825 826 827 828
			else {
				gda_row_invalidate_value (row, value);
				g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
					     GDA_SERVER_PROVIDER_DATA_ERROR,
					     _("Type %s not mapped for value %d"),
829
					     g_type_name (type), bvalue);
830
			}
831
			break;
832 833 834
		}
		case MYSQL_TYPE_LONGLONG: {
			long long bvalue = 0;
835
			memmove (&bvalue, mysql_bind_result[i].buffer, sizeof (bvalue));
836

837
			if (type == G_TYPE_BOOLEAN)
838
				g_value_set_boolean (value, bvalue ? TRUE : FALSE);
839
			else if (type == G_TYPE_INT)
840
				g_value_set_int (value, bvalue);
841
			else if (type == G_TYPE_LONG)
842
				g_value_set_long (value, bvalue);
843 844 845 846 847
			else {
				gda_row_invalidate_value (row, value);
				g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
					     GDA_SERVER_PROVIDER_DATA_ERROR,
					     _("Type %s not mapped for value %lld"),
848
					     g_type_name (type), bvalue);
849
			}
850
			break;
851
		}
852
		case MYSQL_TYPE_NULL:
853
			gda_value_set_null (value);
854 855 856 857
			break;
		case MYSQL_TYPE_TIME:
		case MYSQL_TYPE_DATE:
		case MYSQL_TYPE_DATETIME:
858 859
		case MYSQL_TYPE_TIMESTAMP: {
			MYSQL_TIME bvalue = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
860
			memmove (&bvalue, mysql_bind_result[i].buffer, sizeof (bvalue));
861 862

			if (type == GDA_TYPE_TIME) {
863 864 865 866 867 868 869
				GdaTime *time = gda_time_new_from_values (bvalue.hour,
					                                      bvalue.minute,
					                                      bvalue.second,
					                                      bvalue.second_part,
					                                      0); /* GMT */
				gda_value_set_time (value, time);
                gda_time_free (time);
870 871
			}
			else if (type == G_TYPE_DATE) {
872
				GDate *date = g_date_new_dmy
873 874 875
					((bvalue.day != 0) ? bvalue.day : 1,
					 (bvalue.month != 0) ? bvalue.month : 1,
					 (bvalue.year != 0) ? bvalue.year : 1970);
876
				g_value_take_boxed (value, date);
877
			}
878 879 880 881 882 883 884 885 886 887 888
			else if (type == G_TYPE_DATE_TIME) {
				GTimeZone *tz = g_time_zone_new ("Z"); /* UTC */
				GDateTime *timestamp = g_date_time_new (tz,
																								bvalue.year,
																								bvalue.month,
																								bvalue.day,
																								bvalue.hour,
																								bvalue.minute,
																								(gdouble) bvalue.second + bvalue.second_part / 1000000.0);
				g_value_set_boxed (value, timestamp);
				g_date_time_unref (timestamp);
889 890 891 892 893 894
			}
			else {
				gda_row_invalidate_value (row, value);
				g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
					     GDA_SERVER_PROVIDER_DATA_ERROR,
					     _("Type %s not mapped for value %d/%d/%d %d:%d:%d.%lu"),
895 896 897
					     g_type_name (type), bvalue.year, bvalue.month,
					     bvalue.day, bvalue.hour, bvalue.minute,
					     bvalue.second, bvalue.second_part);
898
			}
899
			break;
900
		}
901
		case MYSQL_TYPE_FLOAT: {
902
			float bvalue = 0.;
903
			memmove (&bvalue, mysql_bind_result[i].buffer, sizeof (bvalue));
904 905
			
			if (type == G_TYPE_FLOAT)
906
				g_value_set_float (value, (float) bvalue);
907 908 909 910 911
			else {
				gda_row_invalidate_value (row, value);
				g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
					     GDA_SERVER_PROVIDER_DATA_ERROR,
					     _("Type %s not mapped for value %f"),
912
					     g_type_name (type), bvalue);
913 914 915
			}			
			break;
		}
916
		case MYSQL_TYPE_DOUBLE: {
917
			double bvalue = 0.0;
918
			memmove (&bvalue, mysql_bind_result[i].buffer, sizeof (bvalue));
919
			
920
			if (type == G_TYPE_DOUBLE)
921
				g_value_set_double (value, bvalue);
922
			else {
923 924 925 926
				gda_row_invalidate_value (row, value);
				g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
					     GDA_SERVER_PROVIDER_DATA_ERROR,
					     _("Type %s not mapped for value %f"),
927
					     g_type_name (type), bvalue);
928
			}
929
			break;
930
		}
931
		case MYSQL_TYPE_STRING:
932 933 934 935 936
		case MYSQL_TYPE_VAR_STRING:
		case MYSQL_TYPE_BLOB:
		case MYSQL_TYPE_TINY_BLOB:
		case MYSQL_TYPE_MEDIUM_BLOB:
		case MYSQL_TYPE_LONG_BLOB:
937
		case MYSQL_TYPE_NEWDECIMAL:
938
		case MYSQL_TYPE_DECIMAL:
939
		case MYSQL_TYPE_BIT: {
940
			char *bvalue = NULL;
941
			memmove (&length, mysql_bind_result[i].length, sizeof (unsigned long));
942
			if (length > 0) {
943 944 945
				bvalue = g_malloc (length + 1);
				memcpy (bvalue, mysql_bind_result[i].buffer, length);
				bvalue [length] = 0;
946
			}
947
			
948
			if (type == G_TYPE_STRING) {
949
				g_value_set_string (value, bvalue);
950 951 952 953 954 955
			}
			else if (type == GDA_TYPE_TEXT) {
				GdaText *text = gda_text_new ();
				gda_text_set_string (text, bvalue);
				g_value_take_boxed (value, text);
			}
956
			else if (type == GDA_TYPE_BINARY) {
957 958 959 960
				GdaBinary *bin;
				bin = gda_binary_new ();
				gda_binary_set_data (bin, (guchar*) bvalue, length);
				gda_value_take_binary (value, bin);
961 962
			}
			else if (type == GDA_TYPE_BLOB) {
963
				/* we don't use GdaMysqlBlobOp because it looks like the MySQL
964 965
				 * API does not support BLOBs accessed in a random way,
				 * so we return the whole BLOB at once */
966 967 968 969
				GdaBlob *blob = gda_blob_new ();
				GdaBinary *bin = gda_blob_get_binary (blob);
				gda_binary_set_data (bin, (guchar*) bvalue, length);
				gda_value_take_blob (value, blob);
970
			}
971 972 973 974
			else if (type == GDA_TYPE_NUMERIC) {
				if (length > 0) {
					GdaNumeric *numeric;
					numeric = gda_numeric_new ();
975
					gda_numeric_set_from_string (numeric, bvalue);
976 977 978 979 980 981
					gda_numeric_set_precision (numeric, 6);
					gda_numeric_set_width (numeric, length);
					gda_value_set_numeric (value, numeric);
					gda_numeric_free (numeric);
				}
			}
982
			else if (type == G_TYPE_DOUBLE) {
983 984
				if (length > 0)
					g_value_set_double (value, g_ascii_strtod (bvalue, NULL));
985 986
				else {
					/* error: wrong column type */
987 988 989 990 991 992 993 994 995 996
					gda_row_invalidate_value (row, value);
					g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
						     GDA_SERVER_PROVIDER_DATA_ERROR,
						     _("Invalid column bind data type. %d\n"),
						     mysql_bind_result[i].buffer_type);
					break;
				}
			}
			else if (type == G_TYPE_INT) {
				if (length > 0)
997
					g_value_set_int (value, atoi (bvalue));
998 999
				else {
					/* error: wrong column type */
1000 1001 1002 1003 1004 1005 1006
					gda_row_invalidate_value (row, value);
					g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
						     GDA_SERVER_PROVIDER_DATA_ERROR,
						     _("Invalid column bind data type. %d\n"),
						     mysql_bind_result[i].buffer_type);
					break;
				}
1007 1008
			}
			else if (type == G_TYPE_BOOLEAN) {
1009
				if (length > 0)
1010
					g_value_set_boolean (value, atoi (bvalue));
1011 1012 1013 1014 1015 1016 1017 1018 1019
				else {
					/* error: wrong column type */
					gda_row_invalidate_value (row, value);
					g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
						     GDA_SERVER_PROVIDER_DATA_ERROR,
						     _("Invalid column bind data type. %d\n"),
						     mysql_bind_result[i].buffer_type);
					break;
				}
1020 1021 1022 1023 1024 1025
			}
			else {
				gda_row_invalidate_value (row, value);
				g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
					     GDA_SERVER_PROVIDER_DATA_ERROR,
					     _("Type %s not mapped for value %s"),
1026
					     g_type_name (type), bvalue);
1027
			}
1028
			g_free (bvalue);
1029
			break;
1030
		}
1031
		default:
1032 1033 1034 1035 1036
			gda_row_invalidate_value (row, value);
			g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
				     GDA_SERVER_PROVIDER_DATA_ERROR,
				     _("Invalid column bind data type. %d\n"),
				     mysql_bind_result[i].buffer_type);
1037
		}
1038

1039
	}
1040
	return row;
1041
}
1042 1043


1044
/*
1045
 * Create a new filled #GdaRow object for the row at position @rownum, and put it into *row.
1046
 *
1047
 * Each new GdaRow is given to @model using gda_data_select_take_row().
1048 1049
 */
static gboolean 
1050
gda_mysql_recordset_fetch_random (GdaDataSelect  *model,
1051 1052 1053
				  GdaRow        **row,
				  gint            rownum,
				  GError        **error)
1054 1055
{
	GdaMysqlRecordset *imodel;
1056

1057
	imodel = GDA_MYSQL_RECORDSET (model);
1058

1059 1060
	*row = new_row_from_mysql_stmt (imodel, rownum, error);
	if (!*row)
1061
		return TRUE;
1062

1063
	gda_data_select_take_row (model, *row, rownum);
1064
	
1065
	if (_gda_data_select_get_nb_stored_rows (model) == _gda_data_select_get_advertized_nrows (model)) {
1066 1067 1068 1069
		/* All the row have been converted.  We could free result now */
		/* but it was done before provided no field-based API functions */
		/* that process result set meta data was needed in the middle. */
	}
1070 1071

	return TRUE;
Rodrigo Moya's avatar
Rodrigo Moya committed
1072 1073
}

1074
/*
1075
 * Create a new filled #GdaRow object for the next cursor row, and put it into *row.
1076
 *
1077 1078 1079
 * Each new #GdaRow created is referenced only by imodel->priv->tmp_row (the #GdaDataSelect implementation
 * never keeps a reference to it).
 * Before a new #GdaRow gets created, the previous one, if set, is discarded.
1080 1081
 */
static gboolean 
1082
gda_mysql_recordset_fetch_next (GdaDataSelect  *model,
1083 1084 1085
				GdaRow        **row,
				gint            rownum,
				GError        **error)
1086 1087
{
	GdaMysqlRecordset *imodel = (GdaMysqlRecordset*) model;
1088

1089
	if (imodel->priv->tmp_row)
1090
		g_object_unref (G_OBJECT(imodel->priv->tmp_row));
1091
	*row = new_row_from_mysql_stmt (imodel, rownum, error);
1092 1093
	imodel->priv->tmp_row = *row;

1094
	return TRUE;
1095 1096
}

1097
/*
1098
 * Create a new filled #GdaRow object for the previous cursor row, and put it into *prow.
1099
 *
1100 1101 1102
 * Each new #GdaRow created is referenced only by imodel->priv->tmp_row (the #GdaDataSelect implementation
 * never keeps a reference to it).
 * Before a new #GdaRow gets created, the previous one, if set, is discarded.
1103 1104
 */
static gboolean 
1105 1106 1107 1108
gda_mysql_recordset_fetch_prev (GdaDataSelect  *model,
				GdaRow        **row,
				gint            rownum,
				GError        **error)
1109
{
1110
	GdaMysqlRecordset *imodel = (GdaMysqlRecordset*) model;
1111

1112 1113 1114 1115
	if (imodel->priv->tmp_row)
		g_object_unref (G_OBJECT(imodel->priv->tmp_row));
	*row = new_row_from_mysql_stmt (imodel, rownum, error);
	imodel->priv->tmp_row = *row;
1116

1117
	return TRUE;
1118
}
Rodrigo Moya's avatar
Rodrigo Moya committed
1119

1120
/*
1121
 * Create a new filled #GdaRow object for the cursor row at position @rownum, and put it into *row.
1122
 *
1123 1124 1125
 * Each new #GdaRow created is referenced only by imodel->priv->tmp_row (the #GdaDataSelect implementation
 * never keeps a reference to it).
 * Before a new #GdaRow gets created, the previous one, if set, is discarded.
1126 1127
 */
static gboolean 
1128 1129 1130 1131
gda_mysql_recordset_fetch_at (GdaDataSelect  *model,
			      GdaRow        **row,
			      gint            rownum,
			      GError        **error)
Rodrigo Moya's avatar
Rodrigo Moya committed
1132
{
1133 1134 1135 1136 1137 1138
	GdaMysqlRecordset *imodel = (GdaMysqlRecordset*) model;

	if (imodel->priv->tmp_row)
		g_object_unref (G_OBJECT(imodel->priv->tmp_row));
	*row = new_row_from_mysql_stmt (imodel, rownum, error);
	imodel->priv->tmp_row = *row;
1139

1140
	return TRUE;
Rodrigo Moya's avatar
Rodrigo Moya committed
1141
}
1142