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
|
/* GDA common library
* Copyright (C) 1998-2002 The GNOME Foundation.
*
* AUTHORS:
* Rodrigo Moya <rodrigo@gnome-db.org>
*
* This Library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this Library; see the file COPYING.LIB. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <libgda/gda-intl.h>
#include <libgda/gda-log.h>
#include <libgda/gda-util.h>
#include <libgda/gda-xml-database.h>
#include <string.h>
struct _GdaXmlDatabasePrivate {
gchar *uri;
gchar *name;
gchar *user_version;
gchar *version;
GHashTable *tables;
GHashTable *views;
GHashTable *queries;
};
#define OBJECT_DATABASE "database"
#define OBJECT_DATA "data"
#define OBJECT_FIELD "field"
#define OBJECT_QUERIES_NODE "queries"
#define OBJECT_ROW "row"
#define OBJECT_TABLE "table"
#define OBJECT_TABLES_NODE "tables"
#define OBJECT_VIEW "view"
#define OBJECT_VIEWS_NODE "views"
#define PROPERTY_ALLOW_NULL "isnull"
#define PROPERTY_AUTO "auto_increment"
#define PROPERTY_CAPTION "caption"
#define PROPERTY_GDATYPE "gdatype"
#define PROPERTY_NAME "name"
#define PROPERTY_OWNER "owner"
#define PROPERTY_PRIMARY_KEY "pkey"
#define PROPERTY_REFERENCES "references"
#define PROPERTY_SCALE "scale"
#define PROPERTY_SIZE "size"
#define PROPERTY_UNIQUE_KEY "unique"
#define PROPERTY_VERSION "version"
#define PROPERTY_USER_VERSION "user_version"
static void gda_xml_database_class_init (GdaXmlDatabaseClass *klass);
static void gda_xml_database_init (GdaXmlDatabase *xmldb, GdaXmlDatabaseClass *klass);
static void gda_xml_database_finalize (GObject *object);
/*
* GdaXmlDatabase object signals
*/
enum {
GDA_XML_DATABASE_CHANGED,
GDA_XML_DATABASE_LAST_SIGNAL
};
static gint xmldb_signals[GDA_XML_DATABASE_LAST_SIGNAL] = { 0, };
static GObjectClass *parent_class = NULL;
/*
* Private functions
*/
static void
process_queries_node (GdaXmlDatabase *xmldb, xmlNodePtr children)
{
}
static void
process_tables_node (GdaXmlDatabase *xmldb, xmlNodePtr children)
{
xmlNodePtr node;
g_return_if_fail (GDA_IS_XML_DATABASE (xmldb));
for (node = children; node != NULL; node = node->next) {
gda_xml_database_new_table_from_node (xmldb, node);
}
}
static void
process_views_node (GdaXmlDatabase *xmldb, xmlNodePtr children)
{
g_return_if_fail (GDA_IS_XML_DATABASE (xmldb));
g_return_if_fail (children != NULL);
}
/*
* Callbacks
*/
static void
table_changed_cb (GdaDataModel *model, gpointer user_data)
{
GdaXmlDatabase *xmldb = (GdaXmlDatabase *) user_data;
g_return_if_fail (GDA_IS_XML_DATABASE (xmldb));
gda_xml_database_changed (xmldb);
}
static void
table_name_changed_cb (GdaDataModel *model, gchar *old_name, gpointer user_data)
{
GdaXmlDatabase *xmldb = (GdaXmlDatabase *) user_data;
g_return_if_fail (GDA_IS_XML_DATABASE (xmldb));
g_hash_table_remove (xmldb->priv->tables, old_name);
g_hash_table_insert (xmldb->priv->tables, gda_table_get_name (GDA_TABLE (model)), GDA_TABLE (model));
}
/*
* GdaXmlDatabase class interface
*/
static void
gda_xml_database_class_init (GdaXmlDatabaseClass * klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
xmldb_signals[GDA_XML_DATABASE_CHANGED] =
g_signal_new ("changed",
G_TYPE_FROM_CLASS (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdaXmlDatabaseClass, changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
object_class->finalize = gda_xml_database_finalize;
klass->changed = NULL;
}
static void
gda_xml_database_init (GdaXmlDatabase *xmldb, GdaXmlDatabaseClass *klass)
{
g_return_if_fail (GDA_IS_XML_DATABASE (xmldb));
/* allocate private structure */
xmldb->priv = g_new0 (GdaXmlDatabasePrivate, 1);
xmldb->priv->uri = NULL;
xmldb->priv->name = NULL;
xmldb->priv->user_version = NULL;
xmldb->priv->version = NULL;
xmldb->priv->tables = g_hash_table_new (g_str_hash, g_str_equal);
xmldb->priv->views = g_hash_table_new (g_str_hash, g_str_equal);
xmldb->priv->queries = g_hash_table_new (g_str_hash, g_str_equal);
}
static gboolean
remove_table_hash (gpointer key, gpointer value, gpointer user_data)
{
g_free (key);
g_signal_handlers_disconnect_by_func (G_OBJECT (value), table_changed_cb, user_data);
g_object_unref (G_OBJECT (value));
return TRUE;
}
static void
gda_xml_database_finalize (GObject *object)
{
GdaXmlDatabase *xmldb = (GdaXmlDatabase *) object;
g_return_if_fail (GDA_IS_XML_DATABASE (xmldb));
/* free memory */
if (xmldb->priv->uri) {
g_free (xmldb->priv->uri);
xmldb->priv->uri = NULL;
}
if (xmldb->priv->name) {
g_free (xmldb->priv->name);
xmldb->priv->name = NULL;
}
if (xmldb->priv->user_version) {
g_free (xmldb->priv->user_version);
xmldb->priv->user_version = NULL;
}
if (xmldb->priv->version) {
g_free (xmldb->priv->version);
xmldb->priv->version = NULL;
}
g_hash_table_foreach_remove (xmldb->priv->tables, remove_table_hash, xmldb);
g_hash_table_destroy (xmldb->priv->tables);
xmldb->priv->tables = NULL;
g_hash_table_destroy (xmldb->priv->views);
xmldb->priv->views = NULL;
g_hash_table_destroy (xmldb->priv->queries);
xmldb->priv->queries = NULL;
g_free (xmldb->priv);
xmldb->priv = NULL;
/* chain to parent class */
parent_class->finalize (object);
}
GType
gda_xml_database_get_type (void)
{
static GType type = 0;
if (!type) {
static const GTypeInfo info = {
sizeof (GdaXmlDatabaseClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gda_xml_database_class_init,
NULL,
NULL,
sizeof (GdaXmlDatabase),
0,
(GInstanceInitFunc) gda_xml_database_init
};
type = g_type_register_static (G_TYPE_OBJECT,
"GdaXmlDatabase",
&info, 0);
}
return type;
}
/**
* gda_xml_database_new
*
* Creates a new #GdaXmlDatabase object, which can be used to describe
* a database which will then be loaded by a provider to create its
* defined structure.
*
* Returns: the newly created object.
*/
GdaXmlDatabase *
gda_xml_database_new (void)
{
GdaXmlDatabase *xmldb;
xmldb = g_object_new (GDA_TYPE_XML_DATABASE, NULL);
return xmldb;
}
/**
* gda_xml_database_new_from_uri
* @uri: an uniform resource identifier.
*
* Creates a new #GdaXmlDatabase object from a XML representation,
* contained in the given @uri.
*
* Returns: the newly created object.
*/
GdaXmlDatabase *
gda_xml_database_new_from_uri (const gchar *uri)
{
GdaXmlDatabase *xmldb;
gchar *body;
xmlDocPtr doc;
xmlNodePtr root;
xmlNodePtr node;
g_return_val_if_fail (uri != NULL, NULL);
/* load the file from the given URI */
body = gda_file_load (uri);
if (!body) {
gda_log_error (_("Could not load file at %s"), uri);
return NULL;
}
/* parse the loaded XML file */
doc = xmlParseMemory (body, strlen (body));
g_free (body);
if (!doc) {
gda_log_error (_("Could not parse file at %s"), uri);
return NULL;
}
xmldb = g_object_new (GDA_TYPE_XML_DATABASE, NULL);
xmldb->priv->uri = g_strdup (uri);
/* parse the file */
root = xmlDocGetRootElement (doc);
if (strcmp (root->name, OBJECT_DATABASE)) {
gda_log_error (_("Invalid XML database file '%s'"), uri);
g_object_unref (G_OBJECT (xmldb));
return NULL;
}
xmldb->priv->name = g_strdup (xmlGetProp (root, PROPERTY_NAME));
xmldb->priv->user_version = g_strdup (xmlGetProp (root, PROPERTY_USER_VERSION));
xmldb->priv->version = g_strdup (xmlGetProp (root, PROPERTY_VERSION));
node = root->xmlChildrenNode;
while (node) {
xmlNodePtr children;
children = node->xmlChildrenNode;
/* more validation */
if (!strcmp (node->name, OBJECT_TABLES_NODE))
process_tables_node (xmldb, children);
else if (!strcmp (node->name, OBJECT_VIEWS_NODE))
process_views_node (xmldb, children);
else if (!strcmp (node->name, OBJECT_QUERIES_NODE))
process_queries_node (xmldb, children);
else {
/*
gda_log_error (_("Invalid XML database file '%s'"), uri);
g_object_unref (G_OBJECT (xmldb));
return NULL;
*/
}
node = node->next;
}
return xmldb;
}
/**
* gda_xml_database_get_name
* @xmldb: XML database.
*
* Returns: the name of the given XML database.
*/
const gchar *
gda_xml_database_get_name (GdaXmlDatabase *xmldb)
{
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), NULL);
return (const gchar *) xmldb->priv->name;
}
/**
* gda_xml_database_set_name
* @xmldb: XML database.
* @name: new name for the database.
*
* Sets the name of the given XML database object.
*/
void
gda_xml_database_set_name (GdaXmlDatabase *xmldb, const gchar *name)
{
g_return_if_fail (GDA_IS_XML_DATABASE (xmldb));
if (xmldb->priv->name)
g_free (xmldb->priv->name);
xmldb->priv->name = g_strdup (name);
gda_xml_database_changed (xmldb);
}
/**
* gda_xml_database_get_user_version
* @xmldb: XML database.
*
* Returns: the user defined version of the given #GdaXmlDatabase object.
*/
const gchar *
gda_xml_database_get_user_version (GdaXmlDatabase *xmldb)
{
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), NULL);
return (const gchar *) xmldb->priv->user_version;
}
/**
* gda_xml_database_set_user_version
* @xmldb: XML database.
* @user_version: user defined version string.
*
* Sets the user defined version of the given XML database.
*/
void
gda_xml_database_set_user_version (GdaXmlDatabase *xmldb, const gchar *user_version)
{
g_return_if_fail (GDA_IS_XML_DATABASE (xmldb));
g_return_if_fail (user_version != NULL);
if (xmldb->priv->user_version)
g_free (xmldb->priv->user_version);
xmldb->priv->user_version = g_strdup (user_version);
gda_xml_database_changed (xmldb);
}
/**
* gda_xml_database_get_version
* @xmldb: XML database.
*
* Gets the version of libgda used to create the #GdaXmlDatabase object.
* This version is the one that was used for saving the XML file last
* time it was saved. This value can only be "get" as it is an internal
* information related to the creation of the #GdaXmlDatabase object.
* To get the user defined database version, use the function
* gda_xml_database_get_user_version instead.
*
* Returns: the libgda version used to create the database.
*/
const gchar *
gda_xml_database_get_version (GdaXmlDatabase *xmldb)
{
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), NULL);
return (const gchar *) xmldb->priv->version;
}
/**
* gda_xml_database_get_uri
* @xmldb: XML database.
*
* Returns: the URI associated with the given XML database. This URI will
* be used when saving the XML database (#gda_xml_database_save).
*/
const gchar *
gda_xml_database_get_uri (GdaXmlDatabase *xmldb)
{
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), NULL);
return xmldb->priv->uri;
}
/**
* gda_xml_database_set_uri
* @xmldb: XML database.
* @uri: an uniform resource identifier.
*
* Associates an @uri with a given XML database. This URI will
* be used when saving the XML database (#gda_xml_database_save).
*/
void
gda_xml_database_set_uri (GdaXmlDatabase *xmldb, const gchar *uri)
{
g_return_if_fail (GDA_IS_XML_DATABASE (xmldb));
if (xmldb->priv->uri)
g_free (xmldb->priv->uri);
xmldb->priv->uri = g_strdup (uri);
}
/**
* gda_xml_database_changed
* @xmldb: XML database
*
* Emits the "changed" signal for the given XML database.
*/
void
gda_xml_database_changed (GdaXmlDatabase * xmldb)
{
g_return_if_fail (GDA_IS_XML_DATABASE (xmldb));
g_signal_emit (G_OBJECT (xmldb),
xmldb_signals[GDA_XML_DATABASE_CHANGED],
0);
}
/**
* gda_xml_database_reload
* @xmldb: XML database.
*
* Reloads the given XML database from its original place, discarding
* all changes that may have happened.
*/
void
gda_xml_database_reload (GdaXmlDatabase *xmldb)
{
/* FIXME: implement */
}
/**
* gda_xml_database_save
* @xmldb: XML database.
* @uri: URI to save the XML database to.
*
* Saves the given XML database to disk.
*
* Returns: %TRUE if the database was successfully saved, %FALSE otherwise.
*/
gboolean
gda_xml_database_save (GdaXmlDatabase *xmldb, const gchar *uri)
{
gchar*xml;
gboolean result;
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), FALSE);
xml = gda_xml_database_to_string (xmldb);
if (xml) {
result = gda_file_save (uri, xml, strlen (xml));
g_free (xml);
} else
result = FALSE;
return result;
}
/**
* gda_xml_database_to_string
* @xmldb: a #GdaXmlDatabase object.
*
* Returns: the XML string representing the structure and contents of the
* given #GdaXmlDatabase object. The returned value must be freed when no
* longer needed.
*/
gchar *
gda_xml_database_to_string (GdaXmlDatabase *xmldb)
{
xmlDocPtr doc;
xmlNodePtr root;
xmlNodePtr tables_node = NULL;
GList *list, *l;
xmlChar *xml;
gint size;
gchar *retval;
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), NULL);
/* create the top node */
doc = xmlNewDoc ("1.0");
root = xmlNewDocNode (doc, NULL, OBJECT_DATABASE, NULL);
xmlSetProp (root, PROPERTY_NAME, xmldb->priv->name);
xmlSetProp (root, PROPERTY_USER_VERSION, xmldb->priv->user_version);
xmlSetProp (root, PROPERTY_VERSION, PACKAGE_VERSION);
xmlDocSetRootElement (doc, root);
/* add tables */
list = gda_xml_database_get_tables (xmldb);
for (l = list; l != NULL; l = l->next) {
GdaTable *table = gda_xml_database_find_table (xmldb, l->data);
xmlNodePtr node = gda_data_model_to_xml_node (GDA_DATA_MODEL (table), l->data);
if (!node) {
gda_log_error (_("Could not create a XML node from table %s"), l->data);
xmlFreeDoc (doc);
gda_xml_database_free_table_list (list);
return NULL;
}
if (!tables_node)
tables_node = xmlNewChild (root, NULL, OBJECT_TABLES_NODE, NULL);
xmlAddChild (tables_node, node);
}
gda_xml_database_free_table_list (list);
/* save to memory */
xmlDocDumpMemory (doc, &xml, &size);
xmlFreeDoc (doc);
if (!xml) {
gda_log_error (_("Could not dump XML file to memory"));
return NULL;
}
retval = g_strdup (xml);
free (xml);
return retval;
}
static void
add_table_to_list (gpointer key, gpointer value, gpointer user_data)
{
gchar *name = (gchar *) key;
GList **list = (GList **) user_data;
*list = g_list_append (*list, g_strdup (name));
}
/**
* gda_xml_database_get_tables
* @xmldb: XML database.
*
* Returns: a GList of all table names present in the given database.
* You must free the returned GList when you no longer need it, by
* using the #gda_xml_database_free_table_list function.
*/
GList *
gda_xml_database_get_tables (GdaXmlDatabase *xmldb)
{
GList *list = NULL;
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), NULL);
g_hash_table_foreach (xmldb->priv->tables, (GHFunc) add_table_to_list, &list);
return list;
}
/**
* gda_xml_database_free_table_list
* @list: list of table names, as returned by #gda_xml_database_get_tables.
*
* Frees a GList of strings returned by #gda_xml_database_get_tables.
*/
void
gda_xml_database_free_table_list (GList *list)
{
GList *l;
for (l = list; l != NULL; l = l->next)
g_free (l->data);
g_list_free (list);
}
/**
* gda_xml_database_find_table
* @xmldb: XML database.
* @name: name for the table to look for.
*
* Searches the given XML database for a table named @name, and
* returns a pointer to it.
*
* Returns: a pointer to the table, or %NULL if not found.
*/
GdaTable *
gda_xml_database_find_table (GdaXmlDatabase *xmldb, const gchar *name)
{
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), NULL);
g_return_val_if_fail (name != NULL, NULL);
return g_hash_table_lookup (xmldb->priv->tables, name);
}
/**
* gda_xml_database_new_table
* @xmldb: XML database.
* @name: name for the new table.
*
* Creates a new empty table in the given XML database.
*
* Returns: a pointer to the newly created in-memory table.
*/
GdaTable *
gda_xml_database_new_table (GdaXmlDatabase *xmldb, const gchar *name)
{
GdaTable *table;
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), NULL);
g_return_val_if_fail (name != NULL, NULL);
if (g_hash_table_lookup (xmldb->priv->tables, name)) {
gda_log_error (_("Table %s already exists"), name);
return NULL;
}
table = gda_table_new (name);
g_signal_connect (G_OBJECT (table), "changed", G_CALLBACK (table_changed_cb), xmldb);
g_signal_connect (G_OBJECT (table), "name_changed", G_CALLBACK (table_name_changed_cb), xmldb);
g_hash_table_insert (xmldb->priv->tables, g_strdup (name), table);
gda_xml_database_changed (xmldb);
return table;
}
/**
* gda_xml_database_new_table_from_model
* @xmldb: XML database.
* @name: name for the new table.
* @model: model to create the table from.
* @add_data: whether to add model's data or not.
*
* Creates a new table in the given XML database from the given
* #GdaDataModel.
*
* Returns: a pointer to the newly created in-memory table.
*/
GdaTable *
gda_xml_database_new_table_from_model (GdaXmlDatabase *xmldb,
const gchar *name,
const GdaDataModel *model,
gboolean add_data)
{
GdaTable *table;
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), NULL);
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (GDA_IS_DATA_MODEL (model), NULL);
if (g_hash_table_lookup (xmldb->priv->tables, name)) {
gda_log_error (_("Table %s already exists"), name);
return NULL;
}
table = gda_table_new_from_model (name, model, add_data);
g_signal_connect (G_OBJECT (table), "changed", G_CALLBACK (table_changed_cb), xmldb);
g_signal_connect (G_OBJECT (table), "name_changed", G_CALLBACK (table_name_changed_cb), xmldb);
if (GDA_IS_TABLE (table)) {
g_hash_table_insert (xmldb->priv->tables, g_strdup (name), table);
gda_xml_database_changed (xmldb);
}
return table;
}
/**
* gda_xml_database_new_table_from_node
* @xmldb: XML Database.
* @node: a XML node pointer.
*
* Adds a table to the given XML database by parsing the given
* XML node pointer, which usually is obtained from an
* already loaded xmlDocPtr.
*
* Returns: a pointer to the newly created in-memory table.
*/
GdaTable *
gda_xml_database_new_table_from_node (GdaXmlDatabase *xmldb, xmlNodePtr node)
{
GdaTable *table;
gchar *name;
xmlNodePtr children;
xmlNodePtr data = NULL;
g_return_val_if_fail (GDA_IS_XML_DATABASE (xmldb), NULL);
g_return_val_if_fail (node != NULL, NULL);
if (strcmp (node->name, OBJECT_TABLE)) {
gda_log_error (_("Invalid node"));
return NULL;
}
name = xmlGetProp (node, PROPERTY_NAME);
table = gda_table_new (name);
if (!table) {
gda_log_error (_("Table %s already exists"), name);
return NULL;
}
/* parse the node */
for (children = node->xmlChildrenNode; children != NULL; children = children->next) {
if (!strcmp (children->name, OBJECT_FIELD)) {
GdaFieldAttributes *fa;
fa = gda_field_attributes_new ();
gda_field_attributes_set_defined_size (
fa, atoi (xmlGetProp (children, PROPERTY_SIZE)));
gda_field_attributes_set_name (
fa, xmlGetProp (children, PROPERTY_NAME));
gda_field_attributes_set_scale (
fa, atoi (xmlGetProp (children, PROPERTY_SCALE)));
gda_field_attributes_set_gdatype (
fa, gda_type_from_string (xmlGetProp (children, PROPERTY_GDATYPE)));
gda_field_attributes_set_allow_null (
fa, atoi (xmlGetProp (children, PROPERTY_ALLOW_NULL)));
gda_field_attributes_set_primary_key (
fa, atoi (xmlGetProp (children, PROPERTY_PRIMARY_KEY)));
gda_field_attributes_set_unique_key (
fa, atoi (xmlGetProp (children, PROPERTY_UNIQUE_KEY)));
gda_field_attributes_set_references (
fa, xmlGetProp (children, PROPERTY_REFERENCES));
gda_field_attributes_set_caption (
fa, xmlGetProp (children, PROPERTY_CAPTION));
gda_field_attributes_set_auto_increment (
fa, atoi (xmlGetProp (children, PROPERTY_AUTO)));
gda_table_add_field (table, fa);
}
else if (!strcmp (children->name, OBJECT_DATA)) {
if (data) {
gda_log_error (_("Duplicated <data> node for table %s"), name);
g_object_unref (G_OBJECT (table));
return NULL;
}
data = children;
}
else {
gda_log_error (_("Invalid XML node"));
g_object_unref (G_OBJECT (table));
return NULL;
}
}
/* add the data of the table */
if (data) {
if (!gda_data_model_add_data_from_xml_node (GDA_DATA_MODEL (table), data))
g_warning (_("Could not add the data from the XML node"));
}
/* add the table to the XML database object */
g_hash_table_insert (xmldb->priv->tables, g_strdup (name), table);
g_signal_connect (G_OBJECT (table), "changed", G_CALLBACK (table_changed_cb), xmldb);
g_signal_connect (G_OBJECT (table), "name_changed", G_CALLBACK (table_name_changed_cb), xmldb);
gda_xml_database_changed (xmldb);
return table;
}
|