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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
/*
* Copyright (C) 2007 Armin Burgmeier <armin@openismus.com>
* Copyright (C) 2007 - 2008 Vivien Malerba <malerba@gnome-db.org>
* Copyright (C) 2009 Bas Driessen <bas.driessen@xobas.com>
* Copyright (C) 2010 David King <davidk@openismus.com>
*
* 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.
*
* 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.
*
* 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.
*/
#include <glib/gi18n-lib.h>
#include <string.h>
#include "gda-postgres.h"
#include "gda-postgres-blob-op.h"
#include "gda-postgres-util.h"
struct _GdaPostgresBlobOpPrivate {
GdaConnection *cnc;
Oid blobid; /* SQL ID in database */
gint fd; /* to use with lo_read, lo_write, lo_lseek, lo_tell, and lo_close */
};
static void gda_postgres_blob_op_class_init (GdaPostgresBlobOpClass *klass);
static void gda_postgres_blob_op_init (GdaPostgresBlobOp *blob,
GdaPostgresBlobOpClass *klass);
static void gda_postgres_blob_op_finalize (GObject *object);
static glong gda_postgres_blob_op_get_length (GdaBlobOp *op);
static glong gda_postgres_blob_op_read (GdaBlobOp *op, GdaBlob *blob, glong offset, glong size);
static glong gda_postgres_blob_op_write (GdaBlobOp *op, GdaBlob *blob, glong offset);
static GObjectClass *parent_class = NULL;
/*
* Object init and finalize
*/
GType
gda_postgres_blob_op_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
static GStaticMutex registering = G_STATIC_MUTEX_INIT;
static const GTypeInfo info = {
sizeof (GdaPostgresBlobOpClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gda_postgres_blob_op_class_init,
NULL,
NULL,
sizeof (GdaPostgresBlobOp),
0,
(GInstanceInitFunc) gda_postgres_blob_op_init,
0
};
g_static_mutex_lock (®istering);
if (type == 0)
type = g_type_register_static (GDA_TYPE_BLOB_OP, "GdaPostgresBlobOp", &info, 0);
g_static_mutex_unlock (®istering);
}
return type;
}
static void
gda_postgres_blob_op_init (GdaPostgresBlobOp *op,
G_GNUC_UNUSED GdaPostgresBlobOpClass *klass)
{
g_return_if_fail (GDA_IS_POSTGRES_BLOB_OP (op));
op->priv = g_new0 (GdaPostgresBlobOpPrivate, 1);
op->priv->blobid = InvalidOid;
op->priv->fd = -1;
}
static void
gda_postgres_blob_op_class_init (GdaPostgresBlobOpClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GdaBlobOpClass *blob_class = GDA_BLOB_OP_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = gda_postgres_blob_op_finalize;
blob_class->get_length = gda_postgres_blob_op_get_length;
blob_class->read = gda_postgres_blob_op_read;
blob_class->write = gda_postgres_blob_op_write;
}
static PGconn *
get_pconn (GdaConnection *cnc)
{
PostgresConnectionData *cdata;
cdata = (PostgresConnectionData*) gda_connection_internal_get_provider_data (cnc);
if (!cdata)
return NULL;
return cdata->pconn;
}
static gboolean
blob_op_open (GdaPostgresBlobOp *pgop)
{
gboolean use_svp = FALSE;
if (pgop->priv->blobid == InvalidOid)
return FALSE;
if (pgop->priv->fd >= 0)
return TRUE;
if (gda_connection_get_transaction_status (pgop->priv->cnc))
/* add a savepoint to prevent a blob open failure from rendering the transaction unusable */
use_svp = TRUE;
if (use_svp)
use_svp = gda_connection_add_savepoint (pgop->priv->cnc, "__gda_blob_read_svp", NULL);
pgop->priv->fd = lo_open (get_pconn (pgop->priv->cnc), pgop->priv->blobid, INV_READ | INV_WRITE);
if (pgop->priv->fd < 0) {
_gda_postgres_make_error (pgop->priv->cnc, get_pconn (pgop->priv->cnc), NULL, NULL);
if (use_svp)
gda_connection_rollback_savepoint (pgop->priv->cnc, "__gda_blob_read_svp", NULL);
return FALSE;
}
if (use_svp)
gda_connection_delete_savepoint (pgop->priv->cnc, "__gda_blob_read_svp", NULL);
return TRUE;
}
static void
blob_op_close (GdaPostgresBlobOp *pgop)
{
lo_close (get_pconn (pgop->priv->cnc), pgop->priv->fd);
pgop->priv->fd = -1;
}
static void
gda_postgres_blob_op_finalize (GObject * object)
{
GdaPostgresBlobOp *pgop = (GdaPostgresBlobOp *) object;
g_return_if_fail (GDA_IS_POSTGRES_BLOB_OP (pgop));
if (pgop->priv->fd >= 0)
lo_close (get_pconn (pgop->priv->cnc), pgop->priv->fd);
g_free (pgop->priv);
pgop->priv = NULL;
parent_class->finalize (object);
}
GdaBlobOp *
gda_postgres_blob_op_new (GdaConnection *cnc)
{
GdaPostgresBlobOp *pgop;
g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL);
pgop = g_object_new (GDA_TYPE_POSTGRES_BLOB_OP, NULL);
pgop->priv->cnc = cnc;
return GDA_BLOB_OP (pgop);
}
GdaBlobOp *
gda_postgres_blob_op_new_with_id (GdaConnection *cnc, const gchar *sql_id)
{
GdaPostgresBlobOp *pgop;
g_return_val_if_fail (GDA_IS_CONNECTION (cnc), NULL);
pgop = g_object_new (GDA_TYPE_POSTGRES_BLOB_OP, NULL);
pgop->priv->blobid = atoi (sql_id);
pgop->priv->cnc = cnc;
return GDA_BLOB_OP (pgop);
}
/**
* gda_postgres_blob_op_declare_blob
*
* Creates a new blob in database if the blob does not yet exist
*/
gboolean
gda_postgres_blob_op_declare_blob (GdaPostgresBlobOp *pgop)
{
g_return_val_if_fail (GDA_IS_POSTGRES_BLOB_OP (pgop), FALSE);
g_return_val_if_fail (pgop->priv, FALSE);
if (pgop->priv->blobid == InvalidOid) {
PGconn *pconn = get_pconn (pgop->priv->cnc);
pgop->priv->blobid = lo_creat (pconn, INV_READ | INV_WRITE);
if (pgop->priv->blobid == InvalidOid) {
_gda_postgres_make_error (pgop->priv->cnc, pconn, NULL, NULL);
return FALSE;
}
}
return TRUE;
}
/**
* gda_postgres_blob_op_get_id
*
* Returns: a new string containing the blob id.
*/
gchar *
gda_postgres_blob_op_get_id (GdaPostgresBlobOp *pgop)
{
g_return_val_if_fail (GDA_IS_POSTGRES_BLOB_OP (pgop), NULL);
g_return_val_if_fail (pgop->priv, NULL);
if (pgop->priv->blobid == InvalidOid)
return NULL;
else
return g_strdup_printf ("%d", pgop->priv->blobid);
}
/**
* gda_postgres_blob_op_set_id
*/
void
gda_postgres_blob_op_set_id (GdaPostgresBlobOp *pgop, const gchar *sql_id)
{
g_return_if_fail (GDA_IS_POSTGRES_BLOB_OP (pgop));
g_return_if_fail (pgop->priv);
g_return_if_fail (sql_id);
if (pgop->priv->fd >= 0)
blob_op_close (pgop);
pgop->priv->blobid = atoi (sql_id);
}
static gboolean
check_transaction_started (GdaConnection *cnc, gboolean *out_started)
{
GdaTransactionStatus *trans;
trans = gda_connection_get_transaction_status (cnc);
if (!trans) {
if (!gda_connection_begin_transaction (cnc, NULL,
GDA_TRANSACTION_ISOLATION_UNKNOWN, NULL))
return FALSE;
else
*out_started = TRUE;
}
return TRUE;
}
/*
* Virtual functions
*/
static glong
gda_postgres_blob_op_get_length (GdaBlobOp *op)
{
GdaPostgresBlobOp *pgop;
PGconn *pconn;
int pos;
gboolean transaction_started = FALSE;
g_return_val_if_fail (GDA_IS_POSTGRES_BLOB_OP (op), -1);
pgop = GDA_POSTGRES_BLOB_OP (op);
g_return_val_if_fail (pgop->priv, -1);
g_return_val_if_fail (GDA_IS_CONNECTION (pgop->priv->cnc), -1);
if (! check_transaction_started (pgop->priv->cnc, &transaction_started))
return -1;
if (!blob_op_open (pgop))
goto out_error;
pconn = get_pconn (pgop->priv->cnc);
pos = lo_lseek (pconn, pgop->priv->fd, 0, SEEK_END);
if (pos < 0) {
_gda_postgres_make_error (pgop->priv->cnc, pconn, NULL, NULL);
goto out_error;
}
blob_op_close (pgop);
if (transaction_started)
gda_connection_rollback_transaction (pgop->priv->cnc, NULL, NULL);
return pos;
out_error:
blob_op_close (pgop);
if (transaction_started)
gda_connection_rollback_transaction (pgop->priv->cnc, NULL, NULL);
return -1;
}
static glong
gda_postgres_blob_op_read (GdaBlobOp *op, GdaBlob *blob, glong offset, glong size)
{
GdaPostgresBlobOp *pgop;
PGconn *pconn;
GdaBinary *bin;
gboolean transaction_started = FALSE;
g_return_val_if_fail (GDA_IS_POSTGRES_BLOB_OP (op), -1);
pgop = GDA_POSTGRES_BLOB_OP (op);
g_return_val_if_fail (pgop->priv, -1);
g_return_val_if_fail (GDA_IS_CONNECTION (pgop->priv->cnc), -1);
if (offset >= G_MAXINT)
return -1;
g_return_val_if_fail (blob, -1);
if (! check_transaction_started (pgop->priv->cnc, &transaction_started))
return -1;
if (!blob_op_open (pgop))
goto out_error;
pconn = get_pconn (pgop->priv->cnc);
if (lo_lseek (pconn, pgop->priv->fd, offset, SEEK_SET) < 0) {
_gda_postgres_make_error (pgop->priv->cnc, pconn, NULL, NULL);
goto out_error;
}
bin = (GdaBinary *) blob;
if (bin->data)
g_free (bin->data);
bin->data = g_new0 (guchar, size);
bin->binary_length = lo_read (pconn, pgop->priv->fd, (char *) (bin->data), size);
blob_op_close (pgop);
if (transaction_started)
gda_connection_rollback_transaction (pgop->priv->cnc, NULL, NULL);
return bin->binary_length;
out_error:
blob_op_close (pgop);
if (transaction_started)
gda_connection_rollback_transaction (pgop->priv->cnc, NULL, NULL);
return -1;
}
static glong
gda_postgres_blob_op_write (GdaBlobOp *op, GdaBlob *blob, glong offset)
{
GdaPostgresBlobOp *pgop;
PGconn *pconn;
glong nbwritten;
gboolean transaction_started = FALSE;
g_return_val_if_fail (GDA_IS_POSTGRES_BLOB_OP (op), -1);
pgop = GDA_POSTGRES_BLOB_OP (op);
g_return_val_if_fail (pgop->priv, -1);
g_return_val_if_fail (GDA_IS_CONNECTION (pgop->priv->cnc), -1);
g_return_val_if_fail (blob, -1);
if (! check_transaction_started (pgop->priv->cnc, &transaction_started))
return -1;
if (!blob_op_open (pgop))
goto out_error;
pconn = get_pconn (pgop->priv->cnc);
if (lo_lseek (pconn, pgop->priv->fd, offset, SEEK_SET) < 0) {
_gda_postgres_make_error (pgop->priv->cnc, pconn, NULL, NULL);
goto out_error;
}
if (blob->op && (blob->op != op)) {
/* use data through blob->op */
#define buf_size 16384
gint nread = 0;
GdaBlob *tmpblob = g_new0 (GdaBlob, 1);
gda_blob_set_op (tmpblob, blob->op);
nbwritten = 0;
for (nread = gda_blob_op_read (tmpblob->op, tmpblob, 0, buf_size);
nread > 0;
nread = gda_blob_op_read (tmpblob->op, tmpblob, nbwritten, buf_size)) {
GdaBinary *bin = (GdaBinary *) tmpblob;
glong tmp_written;
tmp_written = lo_write (pconn, pgop->priv->fd, (char*) bin->data,
bin->binary_length);
if (tmp_written < bin->binary_length) {
_gda_postgres_make_error (pgop->priv->cnc, pconn, NULL, NULL);
gda_blob_free ((gpointer) tmpblob);
goto out_error;
}
nbwritten += tmp_written;
if (nread < buf_size)
/* nothing more to read */
break;
}
gda_blob_free ((gpointer) tmpblob);
}
else {
/* use data in (GdaBinary *) blob */
GdaBinary *bin = (GdaBinary *) blob;
nbwritten = lo_write (pconn, pgop->priv->fd, (char*) bin->data, bin->binary_length);
if (nbwritten == -1) {
_gda_postgres_make_error (pgop->priv->cnc, pconn, NULL, NULL);
goto out_error;
}
}
blob_op_close (pgop);
if (transaction_started)
if (!gda_connection_commit_transaction (pgop->priv->cnc, NULL, NULL))
return -1;
return nbwritten;
out_error:
blob_op_close (pgop);
if (transaction_started)
gda_connection_rollback_transaction (pgop->priv->cnc, NULL, NULL);
return -1;
}
|