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 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 460 461 462 463 464 465 466 467 468 469 470 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 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
|
/*
* Copyright (C) 2007 - 2011 Vivien Malerba <malerba@gnome-db.org>
* Copyright (C) 2008 Murray Cumming <murrayc@murrayc.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 <string.h>
#include <glib/gi18n-lib.h>
#include <libgda/gda-data-model.h>
#include <libgda/gda-data-model-bdb.h>
#include <db.h>
#include <gmodule.h>
#include <libgda/gda-server-provider.h>
#include <libgda/gda-debug-macros.h>
#define BDB_VERSION (10000*DB_VERSION_MAJOR+100*DB_VERSION_MINOR+DB_VERSION_PATCH)
static int (*libdb_db_create) (DB **, DB_ENV *, u_int32_t) = NULL;
static char *(*libdb_db_strerror) (int) = NULL;
static char *_db_strerror (int i);
struct _GdaDataModelBdbPrivate {
gchar *filename;
gchar *db_name;
gboolean db_name_set;
DB *dbp;
DBC *dbpc; /* cursor */
int cursor_pos; /* <0 if @dbpc is invalid */
GSList *errors; /* list of errors as GError structures*/
GSList *columns;
gint n_columns; /* length of @columns */
gint n_rows;
gint n_key_columns; /* > 0 if custom number of columns */
gint n_data_columns;/* > 0 if custom number of columns */
GSList *cursor_values; /* list of GValues for the current row */
};
/* properties */
enum
{
PROP_0,
PROP_FILENAME,
PROP_DB_NAME
};
static void gda_data_model_bdb_class_init (GdaDataModelBdbClass *klass);
static void gda_data_model_bdb_init (GdaDataModelBdb *model,
GdaDataModelBdbClass *klass);
static void gda_data_model_bdb_dispose (GObject *object);
static void gda_data_model_bdb_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec);
static void gda_data_model_bdb_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec);
/* GdaDataModel interface */
static void gda_data_model_bdb_data_model_init (GdaDataModelIface *iface);
static gint gda_data_model_bdb_get_n_rows (GdaDataModel *model);
static gint gda_data_model_bdb_get_n_columns (GdaDataModel *model);
static GdaColumn *gda_data_model_bdb_describe_column (GdaDataModel *model, gint col);
static GdaDataModelAccessFlags gda_data_model_bdb_get_access_flags(GdaDataModel *model);
static const GValue *gda_data_model_bdb_get_value_at (GdaDataModel *model, gint col, gint row, GError **error);
static GdaValueAttribute gda_data_model_bdb_get_attributes_at (GdaDataModel *model, gint col, gint row);
static gboolean gda_data_model_bdb_set_value_at (GdaDataModel *model, gint col, gint row, const GValue *value, GError **error);
static gboolean gda_data_model_bdb_set_values (GdaDataModel *model, gint row, GList *values, GError **error);
static gint gda_data_model_bdb_append_values (GdaDataModel *model, const GList *values, GError **error);
static gint gda_data_model_bdb_append_row (GdaDataModel *model, GError **error);
static gboolean gda_data_model_bdb_remove_row (GdaDataModel *model, gint row, GError **error);
static void add_error (GdaDataModelBdb *model, const gchar *err);
static void free_current_cursor_values (GdaDataModelBdb *model);
static GObjectClass *parent_class = NULL;
#define CLASS(model) (GDA_DATA_MODEL_BDB_CLASS (G_OBJECT_GET_CLASS (model)))
/*
* Object init and dispose
*/
static void
gda_data_model_bdb_data_model_init (GdaDataModelIface *iface)
{
iface->i_get_n_rows = gda_data_model_bdb_get_n_rows;
iface->i_get_n_columns = gda_data_model_bdb_get_n_columns;
iface->i_describe_column = gda_data_model_bdb_describe_column;
iface->i_get_access_flags = gda_data_model_bdb_get_access_flags;
iface->i_get_value_at = gda_data_model_bdb_get_value_at;
iface->i_get_attributes_at = gda_data_model_bdb_get_attributes_at;
iface->i_create_iter = NULL;
iface->i_iter_at_row = NULL;
iface->i_iter_next = NULL;
iface->i_iter_prev = NULL;
iface->i_set_value_at = gda_data_model_bdb_set_value_at;
iface->i_iter_set_value = NULL;
iface->i_set_values = gda_data_model_bdb_set_values;
iface->i_append_values = gda_data_model_bdb_append_values;
iface->i_append_row = gda_data_model_bdb_append_row;
iface->i_remove_row = gda_data_model_bdb_remove_row;
iface->i_find_row = NULL;
iface->i_set_notify = NULL;
iface->i_get_notify = NULL;
iface->i_send_hint = NULL;
}
static void
gda_data_model_bdb_init (GdaDataModelBdb *model,
G_GNUC_UNUSED GdaDataModelBdbClass *klass)
{
g_return_if_fail (GDA_IS_DATA_MODEL_BDB (model));
model->priv = g_new0 (GdaDataModelBdbPrivate, 1);
model->priv->filename = NULL;
model->priv->db_name = NULL;
model->priv->columns = NULL;
model->priv->n_columns = 0;
model->priv->n_rows = 0;
model->priv->cursor_values = NULL;
model->priv->cursor_pos = -1;
model->priv->db_name_set = FALSE;
}
static void
gda_data_model_bdb_class_init (GdaDataModelBdbClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
/* properties */
object_class->set_property = gda_data_model_bdb_set_property;
object_class->get_property = gda_data_model_bdb_get_property;
g_object_class_install_property (object_class, PROP_FILENAME,
g_param_spec_string ("filename", NULL, "DB file", NULL,
G_PARAM_READABLE | G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (object_class, PROP_DB_NAME,
g_param_spec_string ("db-name", NULL, "Name of the database", NULL,
G_PARAM_READABLE | G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
/* virtual functions */
object_class->dispose = gda_data_model_bdb_dispose;
}
static void
gda_data_model_bdb_dispose (GObject * object)
{
GdaDataModelBdb *model = (GdaDataModelBdb *) object;
g_return_if_fail (GDA_IS_DATA_MODEL_BDB (model));
if (model->priv) {
if (model->priv->dbp) {
model->priv->dbp->close (model->priv->dbp, 0);
model->priv->dbp = NULL;
}
if (model->priv->errors) {
g_slist_foreach (model->priv->errors, (GFunc) g_error_free, NULL);
g_slist_free (model->priv->errors);
}
if (model->priv->columns) {
g_slist_foreach (model->priv->columns, (GFunc) g_object_unref, NULL);
g_slist_free (model->priv->columns);
model->priv->columns = NULL;
}
if (model->priv->cursor_values)
free_current_cursor_values (model);
g_free (model->priv->filename);
g_free (model->priv->db_name);
g_free (model->priv);
model->priv = NULL;
}
parent_class->dispose (object);
}
static void
free_current_cursor_values (GdaDataModelBdb *model)
{
g_slist_foreach (model->priv->cursor_values, (GFunc) gda_value_free, NULL);
g_slist_free (model->priv->cursor_values);
model->priv->cursor_values = NULL;
}
static void
add_error (GdaDataModelBdb *model, const gchar *err)
{
GError *error = NULL;
g_set_error (&error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
"%s", err);
model->priv->errors = g_slist_append (model->priv->errors, error);
}
/*
* Public functions
*/
GType
gda_data_model_bdb_get_type (void)
{
static GType type = 0;
if (G_UNLIKELY (type == 0)) {
static GMutex registering;
static const GTypeInfo info = {
sizeof (GdaDataModelBdbClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gda_data_model_bdb_class_init,
NULL,
NULL,
sizeof (GdaDataModelBdb),
0,
(GInstanceInitFunc) gda_data_model_bdb_init,
0
};
static const GInterfaceInfo data_model_info = {
(GInterfaceInitFunc) gda_data_model_bdb_data_model_init,
NULL,
NULL
};
g_mutex_lock (®istering);
if (type == 0) {
type = g_type_register_static (G_TYPE_OBJECT, "GdaDataModelBdb", &info, 0);
g_type_add_interface_static (type, GDA_TYPE_DATA_MODEL, &data_model_info);
GModule *module;
gchar *err = NULL;
module = g_module_open (BDB_LIB, G_MODULE_BIND_LAZY);
if (!module)
err = g_strdup_printf (_("Could not load the Berkeley DB library: %s"),
g_module_error ());
else {
if (!g_module_symbol (module, "db_create", (gpointer *) &libdb_db_create))
err = g_strdup_printf (_("Could not load the '%s' symbol from the "
"Berkeley DB library"),
"db_create");
else if (!g_module_symbol (module, "db_strerror", (gpointer *) &libdb_db_strerror))
err = g_strdup_printf (_("Could not load the '%s' symbol from the "
"Berkeley DB library"),
"db_strerror");
}
if (err) {
g_warning ("%s", err);
g_free (err);
libdb_db_create = NULL;
libdb_db_strerror = NULL;
}
else
g_module_make_resident (module);
}
g_mutex_unlock (®istering);
}
return type;
}
static char *_db_strerror (int i)
{
if (libdb_db_strerror)
return libdb_db_strerror (i);
else
return _("Berkeley DB library not loaded");
}
static void
update_number_of_rows (GdaDataModelBdb *model)
{
int ret;
DB_BTREE_STAT *statp;
ret = model->priv->dbp->stat (model->priv->dbp,
#if BDB_VERSION > 40300
NULL,
#endif
&statp,
#if BDB_VERSION < 40000
NULL,
#endif
0);
if (ret) {
add_error (model, _db_strerror (ret));
model->priv->n_rows = 0;
}
else {
model->priv->n_rows = (int) statp->bt_ndata;
free (statp);
}
}
static void
gda_data_model_bdb_set_property (GObject *object,
guint param_id,
const GValue *value,
GParamSpec *pspec)
{
GdaDataModelBdb *model;
const gchar *string;
model = GDA_DATA_MODEL_BDB (object);
if (model->priv) {
switch (param_id) {
case PROP_FILENAME:
if (model->priv->filename) {
g_free (model->priv->filename);
model->priv->filename = NULL;
}
string = g_value_get_string (value);
if (string)
model->priv->filename = g_strdup (string);
break;
case PROP_DB_NAME:
if (model->priv->db_name) {
g_free (model->priv->db_name);
model->priv->db_name = NULL;
}
string = g_value_get_string (value);
if (string)
model->priv->db_name = g_strdup (string);
model->priv->db_name_set = TRUE;
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
if (model->priv->db_name_set && model->priv->filename) {
/* open the DB file */
int ret;
DBC *dbpc;
DB *dbp;
/* open database */
if (!libdb_db_create) {
add_error (model, _("Berkeley DB library not loaded"));
goto out;
}
ret = libdb_db_create (&dbp, NULL, 0);
if (ret) {
add_error (model, _db_strerror (ret));
goto out;
}
model->priv->dbp = dbp;
ret = dbp->open (dbp, /* Flawfinder: ignore */
#if BDB_VERSION >= 40124
NULL,
#endif
model->priv->filename,
model->priv->db_name,
DB_UNKNOWN, /* autodetect DBTYPE */
0, 0);
if (ret) {
add_error (model, _db_strerror (ret));
goto out;
}
/* get cursor */
ret = dbp->cursor (dbp, NULL, &dbpc, 0);
if (ret) {
add_error (model, _db_strerror (ret));
goto out;
}
model->priv->dbpc = dbpc;
/* create columns */
model->priv->columns = NULL;
model->priv->n_key_columns = 0;
if (CLASS (model)->create_key_columns) {
model->priv->columns = CLASS (model)->create_key_columns (model);
model->priv->n_key_columns = g_slist_length (model->priv->columns);
}
if (model->priv->n_key_columns == 0) {
GdaColumn *column;
column = gda_column_new ();
model->priv->columns = g_slist_append (model->priv->columns , column);
gda_column_set_name (column, "key");
gda_column_set_description (column, "key");
gda_column_set_g_type (column, GDA_TYPE_BINARY);
}
model->priv->n_data_columns = 0;
if (CLASS (model)->create_data_columns) {
GSList *list;
list = CLASS (model)->create_data_columns (model);
model->priv->columns = g_slist_concat (model->priv->columns, list);
model->priv->n_data_columns = g_slist_length (list);
}
if (model->priv->n_data_columns == 0) {
GdaColumn *column;
column = gda_column_new ();
model->priv->columns = g_slist_append (model->priv->columns , column);
gda_column_set_name (column, "data");
gda_column_set_description (column, "data");
gda_column_set_g_type (column, GDA_TYPE_BINARY);
}
model->priv->n_columns = g_slist_length (model->priv->columns);
/* number of rows */
update_number_of_rows (model);
}
out:
return;
}
static void
gda_data_model_bdb_get_property (GObject *object,
guint param_id,
GValue *value,
GParamSpec *pspec)
{
GdaDataModelBdb *model;
model = GDA_DATA_MODEL_BDB (object);
if (model->priv) {
switch (param_id) {
case PROP_FILENAME:
g_value_set_string (value, model->priv->filename);
break;
case PROP_DB_NAME:
g_value_set_string (value, model->priv->db_name);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
break;
}
}
}
/**
* gda_data_model_bdb_new:
* @filename: name of the file containing the database
* @db_name: (allow-none): the name of the database within @filename, or %NULL
*
* Creates a new #GdaDataModel object to access the contents of the Berkeley DB file @file,
* for the database @db_name if not %NULL
*
* Returns: (transfer full): a new #GdaDataModel
*/
GdaDataModel *
gda_data_model_bdb_new (const gchar *filename, const gchar *db_name)
{
GdaDataModel *model;
g_return_val_if_fail (filename && *filename, NULL);
model = (GdaDataModel *) g_object_new (GDA_TYPE_DATA_MODEL_BDB, "db-name", db_name,
"filename", filename, NULL);
return model;
}
/**
* gda_data_model_bdb_get_errors:
* @model: a #GdaDataModelBdb object
*
* Get the list of errors which have occurred while using @model
*
* Returns: (element-type GError) (transfer none): a read-only list of #GError pointers, or %NULL if no error has occurred
*/
const GSList *
gda_data_model_bdb_get_errors (GdaDataModelBdb *model)
{
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), NULL);
g_return_val_if_fail (model->priv, NULL);
return model->priv->errors;
}
/**
* gda_data_model_bdb_clean_errors:
* @model: a #GdaDataModelBdb object
*
* Reset the list of errors which have occurred while using @model
*/
void
gda_data_model_bdb_clean_errors (GdaDataModelBdb *model)
{
g_return_if_fail (GDA_IS_DATA_MODEL_BDB (model));
g_return_if_fail (model->priv);
if (model->priv->errors) {
g_slist_foreach (model->priv->errors, (GFunc) g_error_free, NULL);
g_slist_free (model->priv->errors);
model->priv->errors = NULL;
}
}
static gint
gda_data_model_bdb_get_n_rows (GdaDataModel *model)
{
GdaDataModelBdb *imodel = (GdaDataModelBdb *) model;
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (imodel), 0);
g_return_val_if_fail (imodel->priv != NULL, 0);
return imodel->priv->n_rows;
}
static gint
gda_data_model_bdb_get_n_columns (GdaDataModel *model)
{
GdaDataModelBdb *imodel;
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), 0);
imodel = GDA_DATA_MODEL_BDB (model);
g_return_val_if_fail (imodel->priv, 0);
if (imodel->priv->columns)
return imodel->priv->n_columns;
else
return 0;
}
static GdaColumn *
gda_data_model_bdb_describe_column (GdaDataModel *model, gint col)
{
GdaDataModelBdb *imodel;
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), NULL);
imodel = GDA_DATA_MODEL_BDB (model);
g_return_val_if_fail (imodel->priv, NULL);
if (imodel->priv->columns)
return g_slist_nth_data (imodel->priv->columns, col);
else
return NULL;
}
static GdaDataModelAccessFlags
gda_data_model_bdb_get_access_flags (GdaDataModel *model)
{
GdaDataModelBdb *imodel;
GdaDataModelAccessFlags flags;
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), 0);
imodel = GDA_DATA_MODEL_BDB (model);
g_return_val_if_fail (imodel->priv, 0);
flags = GDA_DATA_MODEL_ACCESS_CURSOR_FORWARD |
GDA_DATA_MODEL_ACCESS_CURSOR_BACKWARD |
GDA_DATA_MODEL_ACCESS_RANDOM |
GDA_DATA_MODEL_ACCESS_WRITE;
return flags;
}
static gboolean
move_cursor_at (GdaDataModelBdb *model, gint row)
{
/*
* REM: there is no general rwo numbering in BDB, so the current code to set a cursor at a
* row is to iterate back and forth which is time and memory I/O consuming. Solutions to
* solve this problem would be:
* - for a general solution, create another database where the key is the row number, and
* the data is either the current key if no duplicates are allowed, or the current key
* and the current data otherwise; the problem is that this index may require large amounts
* of disk space
* - implement the solution only for databases which support logical record numbers (to investigate
* further)
*/
DBC *dbpc;
DBT key, data;
int ret, i;
dbpc = model->priv->dbpc;
if (model->priv->cursor_pos < 0) {
memset (&key, 0, sizeof key);
memset (&data, 0, sizeof data);
ret = dbpc->c_get (dbpc, &key, &data, DB_FIRST);
if (ret) {
add_error (model, _db_strerror (ret));
return FALSE;
}
model->priv->cursor_pos = 0;
}
row = model->priv->n_rows - 1 - row;
if (row > model->priv->cursor_pos) {
for (i = model->priv->cursor_pos; i < row; i++) {
memset (&key, 0, sizeof key);
memset (&data, 0, sizeof data);
ret = dbpc->c_get (dbpc, &key, &data, DB_NEXT);
if (ret) {
add_error (model, _db_strerror (ret));
return FALSE;
}
model->priv->cursor_pos ++;
}
}
else if (row < model->priv->cursor_pos) {
for (i = model->priv->cursor_pos; i > row; i--) {
memset (&key, 0, sizeof key);
memset (&data, 0, sizeof data);
ret = dbpc->c_get (dbpc, &key, &data, DB_PREV);
if (ret) {
add_error (model, _db_strerror (ret));
return FALSE;
}
model->priv->cursor_pos --;
}
}
return TRUE;
}
static const GValue *
gda_data_model_bdb_get_value_at (GdaDataModel *model, gint col, gint row, GError **error)
{
GdaDataModelBdb *imodel;
DBT key, data;
GdaBinary bin;
GValue *value;
int ret;
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), NULL);
imodel = GDA_DATA_MODEL_BDB (model);
g_return_val_if_fail (imodel->priv, NULL);
if ((col < 0) || (col > imodel->priv->n_columns)) {
gchar *tmp;
tmp = g_strdup_printf (_("Column %d out of range (0-%d)"), col, imodel->priv->n_columns - 1);
add_error (imodel, tmp);
g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_COLUMN_OUT_OF_RANGE_ERROR,
"%s", tmp);
g_free (tmp);
return NULL;
}
if (! move_cursor_at (imodel, row))
return NULL;
memset (&key, 0, sizeof key);
memset (&data, 0, sizeof data);
ret = imodel->priv->dbpc->c_get (imodel->priv->dbpc, &key, &data, DB_CURRENT);
if (ret) {
add_error (imodel, _db_strerror (ret));
g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
"%s", _db_strerror (ret));
return NULL;
}
if (imodel->priv->cursor_values)
free_current_cursor_values (imodel);
/* key part */
if (imodel->priv->n_key_columns > 0) {
int c;
for (c = 0; c < imodel->priv->n_key_columns; c++) {
if (CLASS (model)->get_key_part) {
value = CLASS (model)->get_key_part (imodel, key.data, key.size, c);
if (!value)
value = gda_value_new_null ();
imodel->priv->cursor_values = g_slist_append (imodel->priv->cursor_values,
value);
}
else {
if (c == 0) {
gchar *str;
str = g_strdup_printf (_("Custom BDB model implementation is not complete: "
"the '%s' method is missing"), "get_key_part");
g_warning ("%s", str);
g_free (str);
}
value = gda_value_new_null ();
imodel->priv->cursor_values = g_slist_append (imodel->priv->cursor_values,
value);
}
}
}
else {
value = gda_value_new (GDA_TYPE_BINARY);
bin.data = key.data;
bin.binary_length = key.size;
gda_value_set_binary (value, &bin);
imodel->priv->cursor_values = g_slist_append (imodel->priv->cursor_values, value);
}
/* data part */
if (imodel->priv->n_data_columns > 0) {
int c;
for (c = 0; c < imodel->priv->n_data_columns; c++) {
if (CLASS (model)->get_data_part) {
value = CLASS (model)->get_data_part (imodel, data.data, data.size, c);
if (!value)
value = gda_value_new_null ();
imodel->priv->cursor_values = g_slist_append (imodel->priv->cursor_values,
value);
}
else {
if (c == 0) {
gchar *str;
str = g_strdup_printf (_("Custom BDB model implementation is not complete: "
"the '%s' method is missing"), "get_data_part");
g_warning ("%s", str);
g_free (str);
}
value = gda_value_new_null ();
imodel->priv->cursor_values = g_slist_append (imodel->priv->cursor_values,
value);
}
}
}
else {
value = gda_value_new (GDA_TYPE_BINARY);
bin.data = data.data;
bin.binary_length = data.size;
gda_value_set_binary (value, &bin);
imodel->priv->cursor_values = g_slist_append (imodel->priv->cursor_values, value);
}
return g_slist_nth_data (imodel->priv->cursor_values, col);
}
static GdaValueAttribute
gda_data_model_bdb_get_attributes_at (GdaDataModel *model, gint col, G_GNUC_UNUSED gint row)
{
GdaDataModelBdb *imodel;
GdaValueAttribute flags;
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), 0);
imodel = GDA_DATA_MODEL_BDB (model);
g_return_val_if_fail (imodel->priv, 0);
if ((col < 0) || (col > imodel->priv->n_columns)) {
gchar *tmp;
tmp = g_strdup_printf (_("Column %d out of range (0-%d)"), col, imodel->priv->n_columns - 1);
add_error (imodel, tmp);
g_free (tmp);
return 0;
}
if (((imodel->priv->n_key_columns > 0) && (col >= imodel->priv->n_key_columns)) ||
((imodel->priv->n_key_columns <= 0) && (col >= 1)))
flags = GDA_VALUE_ATTR_CAN_BE_NULL;
else
flags = GDA_VALUE_ATTR_CAN_BE_NULL | GDA_VALUE_ATTR_NO_MODIF;
return flags;
}
static gboolean
gda_data_model_bdb_set_value_at (GdaDataModel *model, gint col, gint row, const GValue *value, GError **error)
{
GList *values = NULL;
gint i;
gboolean retval;
GdaDataModelBdb *imodel;
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), FALSE);
imodel = GDA_DATA_MODEL_BDB (model);
g_return_val_if_fail (imodel->priv, FALSE);
if ((col < 0) || (col > imodel->priv->n_columns)) {
gchar *tmp;
tmp = g_strdup_printf (_("Column %d out of range (0-%d)"), col, imodel->priv->n_columns - 1);
add_error (imodel, tmp);
g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_COLUMN_OUT_OF_RANGE_ERROR,
"%s", tmp);
g_free (tmp);
return FALSE;
}
/* start padding with default values */
for (i = 0; i < col; i++)
values = g_list_append (values, NULL);
values = g_list_append (values, (gpointer) value);
/* add extra padding */
for (i++; i < imodel->priv->n_columns; i++)
values = g_list_append (values, NULL);
retval = gda_data_model_bdb_set_values (model, row, values, error);
g_list_free (values);
return retval;
}
static gboolean
alter_key_value (GdaDataModelBdb *model, DBT *key, GList **values, gboolean *has_modifications, GError **error)
{
if (has_modifications)
*has_modifications = FALSE;
if (model->priv->n_key_columns > 0) {
gint i;
for (i = 0; i < model->priv->n_key_columns; i++) {
if ((*values)->data) {
if (CLASS (model)->update_key_part) {
if (! CLASS (model)->update_key_part (model, key->data, key->size, i,
(const GValue *) (*values)->data, error))
return FALSE;
if (has_modifications)
*has_modifications = TRUE;
}
else {
g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
GDA_SERVER_PROVIDER_METHOD_NON_IMPLEMENTED_ERROR,
_("Custom BDB model implementation is not complete: "
"the '%s' method is missing"),
"update_key_part");
return FALSE;
}
}
(*values) = (*values)->next;
}
}
else {
if ((*values)->data) {
GValue *v = (GValue *) (*values)->data;
if (gda_value_is_null (v)) {
memset (&key, 0, sizeof key);
key->size = sizeof (int);
}
else if (gda_value_isa (v, GDA_TYPE_BINARY)) {
GdaBinary *bin;
bin = (GdaBinary *) gda_value_get_binary ((GValue *) (*values)->data);
memset (&key, 0, sizeof key);
key->size = bin->binary_length;
key->data = bin->data;
if (has_modifications)
*has_modifications = TRUE;
}
else {
g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
_("Expected GdaBinary value, got %s"), g_type_name (G_VALUE_TYPE (v)));
return FALSE;
}
}
(*values) = (*values)->next;
}
return TRUE;
}
static gboolean
gda_data_model_bdb_set_values (GdaDataModel *model, gint row, GList *values, GError **error)
{
GdaDataModelBdb *imodel;
DBT key, data;
int ret;
GList *ptr = values;
gboolean key_modified = FALSE;
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), FALSE);
imodel = (GdaDataModelBdb *) model;
g_return_val_if_fail (imodel->priv, FALSE);
if (!values)
return TRUE;
/* set sursor at the correct position */
if (! move_cursor_at (imodel, row))
return FALSE;
/* fetch current values */
memset (&key, 0, sizeof key);
memset (&data, 0, sizeof data);
ret = imodel->priv->dbpc->c_get (imodel->priv->dbpc, &key, &data, DB_CURRENT);
if (ret) {
add_error (imodel, _db_strerror (ret));
g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
"%s", _db_strerror (ret));
return FALSE;
}
/* update key value */
if (!alter_key_value (imodel, &key, &ptr, &key_modified, error))
return FALSE;
if (key_modified) {
g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_FEATURE_NON_SUPPORTED_ERROR,
"%s", _("Key modification is not supported"));
return FALSE;
}
/* update data value */
if (imodel->priv->n_data_columns > 0) {
gint i;
for (i = 0; i < imodel->priv->n_data_columns; i++) {
if (ptr->data) {
if (CLASS (model)->update_data_part) {
if (! CLASS (model)->update_data_part (imodel, data.data, data.size, i,
(const GValue *) ptr->data, error))
return FALSE;
}
else {
g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
+ GDA_SERVER_PROVIDER_METHOD_NON_IMPLEMENTED_ERROR,
_("Custom BDB model implementation is not complete: "
"the '%s' method is missing"),
"update_data_part");
return FALSE;
}
}
ptr = ptr->next;
}
}
else {
if (ptr->data) {
GValue *v = (GValue *) ptr->data;
if (gda_value_is_null (v)) {
memset (&data, 0, sizeof data);
data.size = sizeof (int);
}
else if (gda_value_isa (v, GDA_TYPE_BINARY)) {
GdaBinary *bin;
bin = (GdaBinary *) gda_value_get_binary ((GValue *) ptr->data);
memset (&data, 0, sizeof data);
data.size = bin->binary_length;
data.data = bin->data;
}
else {
g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
GDA_SERVER_PROVIDER_DATA_ERROR,
_("Expected GdaBinary value, got %s"),
g_type_name (G_VALUE_TYPE (v)));
return FALSE;
}
}
ptr = ptr->next;
}
/* write back */
if (!key_modified) {
ret = imodel->priv->dbpc->c_put (imodel->priv->dbpc, &key, &data, DB_CURRENT);
if (ret) {
add_error (imodel, _db_strerror (ret));
g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
"%s", _db_strerror (ret));
return FALSE;
}
}
else {
TO_IMPLEMENT;
}
return TRUE;
}
static gint
gda_data_model_bdb_append_values (GdaDataModel *model, G_GNUC_UNUSED const GList *values,
G_GNUC_UNUSED GError **error)
{
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), -1);
return -1;
}
static gint
gda_data_model_bdb_append_row (GdaDataModel *model, GError **error)
{
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), -1);
GdaDataModelBdb *imodel;
DBT key, data;
int ret;
int def = 0;
imodel = GDA_DATA_MODEL_BDB (model);
g_return_val_if_fail (imodel->priv, -1);
memset (&key, 0, sizeof (key));
key.size = sizeof (int);
key.data = &def;
memset (&data, 0, sizeof (data));
data.size = sizeof (int);
data.data = &def;
ret = imodel->priv->dbp->put (imodel->priv->dbp, NULL, &key, &data, 0);
if (ret) {
add_error (imodel, _db_strerror (ret));
g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
"%s", _db_strerror (ret));
return -1;
}
imodel->priv->cursor_pos = -1;
imodel->priv->n_rows ++;
return imodel->priv->n_rows - 1;
}
static gboolean
gda_data_model_bdb_remove_row (GdaDataModel *model, gint row, GError **error)
{
GdaDataModelBdb *imodel;
int ret;
g_return_val_if_fail (GDA_IS_DATA_MODEL_BDB (model), FALSE);
imodel = (GdaDataModelBdb *) model;
g_return_val_if_fail (imodel->priv, FALSE);
if (! move_cursor_at (imodel, row))
return FALSE;
ret = imodel->priv->dbpc->c_del (imodel->priv->dbpc, 0);
if (ret) {
add_error (imodel, _db_strerror (ret));
g_set_error (error, GDA_DATA_MODEL_ERROR, GDA_DATA_MODEL_ACCESS_ERROR,
"%s", _db_strerror (ret));
return FALSE;
}
imodel->priv->cursor_pos = -1;
imodel->priv->n_rows --;
return TRUE;
}
|