File: mg-db-constraint.c

package info (click to toggle)
mergeant 0.52-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,848 kB
  • ctags: 6,584
  • sloc: ansic: 63,372; xml: 23,218; sh: 8,316; makefile: 613; sql: 237
file content (1561 lines) | stat: -rw-r--r-- 43,066 bytes parent folder | download | duplicates (2)
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
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
/* mg-db-constraint.c
 *
 * Copyright (C) 2003 Vivien Malerba
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include "mg-db-constraint.h"
#include "mg-referer.h"
#include "mg-database.h"
#include "mg-db-table.h"
#include "mg-xml-storage.h"
#include "mg-entity.h"
#include "mg-db-field.h"
#include "mg-field.h"
#include "marshal.h"
#include <string.h>

/* 
 * Main static functions 
 */
static void mg_db_constraint_class_init (MgDbConstraintClass * class);
static void mg_db_constraint_init (MgDbConstraint * srv);
static void mg_db_constraint_dispose (GObject   * object);
static void mg_db_constraint_finalize (GObject   * object);

static void mg_db_constraint_set_property (GObject              *object,
				    guint                 param_id,
				    const GValue         *value,
				    GParamSpec           *pspec);
static void mg_db_constraint_get_property (GObject              *object,
				    guint                 param_id,
				    GValue               *value,
				    GParamSpec           *pspec);

/* XML storage interface */
static void        mg_db_constraint_xml_storage_init (MgXmlStorageIface *iface);
static gchar      *mg_db_constraint_get_xml_id (MgXmlStorage *iface);
static xmlNodePtr  mg_db_constraint_save_to_xml (MgXmlStorage *iface, GError **error);
static gboolean    mg_db_constraint_load_from_xml (MgXmlStorage *iface, xmlNodePtr node, GError **error);


/* MgReferer interface */
static void        mg_db_constraint_referer_init (MgRefererIface *iface);
static gboolean    mg_db_constraint_activate            (MgReferer *iface);
static void        mg_db_constraint_deactivate          (MgReferer *iface);
static gboolean    mg_db_constraint_is_active           (MgReferer *iface);
static GSList     *mg_db_constraint_get_ref_objects     (MgReferer *iface);
static void        mg_db_constraint_replace_refs        (MgReferer *iface, GHashTable *replacaments);

#ifdef debug
static void        mg_db_constraint_dump                (MgDbConstraint *cstr, guint offset);
#endif

/* When a DbTable or MgDbField is nullified */
static void nullified_object_cb (GObject *obj, MgDbConstraint *cstr);

/* get a pointer to the parents to be able to call their destructor */
static GObjectClass  *parent_class = NULL;

/* signals */
enum
{
	TEMPL_SIGNAL,
	LAST_SIGNAL
};

static gint mg_db_constraint_signals[LAST_SIGNAL] = { 0 };

/* properties */
enum
{
	PROP_0,
	PROP_USER_CSTR
};


/* private structure */
struct _MgDbConstraintPrivate
{
	MgDbConstraintType      type;
	MgDbTable              *table;
	gboolean                user_defined; /* FALSE if the constraint is hard coded into the database */
	
	/* Only used if single field constraint */
	MgDbField              *single_field;

	/* Only used if Primary key */
	GSList                 *multiple_fields; 

	/* Only used for foreign keys */
	MgDbTable              *ref_table; 
	GSList                 *fk_pairs;  
	MgDbConstraintFkAction  on_delete;
	MgDbConstraintFkAction  on_update;
	
};


/* module error */
GQuark mg_db_constraint_error_quark (void)
{
	static GQuark quark;
	if (!quark)
		quark = g_quark_from_static_string ("mg_db_constraint_error");
	return quark;
}


guint
mg_db_constraint_get_type (void)
{
	static GType type = 0;

	if (!type) {
		static const GTypeInfo info = {
			sizeof (MgDbConstraintClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) mg_db_constraint_class_init,
			NULL,
			NULL,
			sizeof (MgDbConstraint),
			0,
			(GInstanceInitFunc) mg_db_constraint_init
		};

		static const GInterfaceInfo xml_storage_info = {
			(GInterfaceInitFunc) mg_db_constraint_xml_storage_init,
			NULL,
			NULL
		};

		static const GInterfaceInfo referer_info = {
			(GInterfaceInitFunc) mg_db_constraint_referer_init,
			NULL,
			NULL
		};
		
		type = g_type_register_static (MG_BASE_TYPE, "MgDbConstraint", &info, 0);
		g_type_add_interface_static (type, MG_XML_STORAGE_TYPE, &xml_storage_info);
		g_type_add_interface_static (type, MG_REFERER_TYPE, &referer_info);
	}
	return type;
}

static void 
mg_db_constraint_xml_storage_init (MgXmlStorageIface *iface)
{
	iface->get_xml_id = mg_db_constraint_get_xml_id;
	iface->save_to_xml = mg_db_constraint_save_to_xml;
	iface->load_from_xml = mg_db_constraint_load_from_xml;
}

static void
mg_db_constraint_referer_init (MgRefererIface *iface)
{
	iface->activate = mg_db_constraint_activate;
	iface->deactivate = mg_db_constraint_deactivate;
	iface->is_active = mg_db_constraint_is_active;
	iface->get_ref_objects = mg_db_constraint_get_ref_objects;
	iface->replace_refs = mg_db_constraint_replace_refs;
}

static void
mg_db_constraint_class_init (MgDbConstraintClass * class)
{
	GObjectClass   *object_class = G_OBJECT_CLASS (class);

	parent_class = g_type_class_peek_parent (class);

	mg_db_constraint_signals[TEMPL_SIGNAL] =
		g_signal_new ("templ_signal",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_FIRST,
			      G_STRUCT_OFFSET (MgDbConstraintClass, templ_signal),
			      NULL, NULL,
			      marshal_VOID__VOID, G_TYPE_NONE,
			      0);
	class->templ_signal = NULL;

	object_class->dispose = mg_db_constraint_dispose;
	object_class->finalize = mg_db_constraint_finalize;

	/* Properties */
	object_class->set_property = mg_db_constraint_set_property;
	object_class->get_property = mg_db_constraint_get_property;
	g_object_class_install_property (object_class, PROP_USER_CSTR,
					 g_param_spec_boolean ("user_constraint", NULL, NULL, FALSE,
							       (G_PARAM_READABLE | G_PARAM_WRITABLE)));

	/* virtual functions */
#ifdef debug
        MG_BASE_CLASS (class)->dump = (void (*)(MgBase *, guint)) mg_db_constraint_dump;
#endif
}

static void
mg_db_constraint_init (MgDbConstraint * mg_db_constraint)
{
	mg_db_constraint->priv = g_new0 (MgDbConstraintPrivate, 1);
	mg_db_constraint->priv->type = CONSTRAINT_PRIMARY_KEY;
	mg_db_constraint->priv->table = NULL;
	mg_db_constraint->priv->user_defined = FALSE;
	mg_db_constraint->priv->single_field = NULL;
	mg_db_constraint->priv->multiple_fields = NULL;
	mg_db_constraint->priv->ref_table = NULL;
	mg_db_constraint->priv->fk_pairs = NULL;
	mg_db_constraint->priv->on_delete = CONSTRAINT_FK_ACTION_NO_ACTION;
	mg_db_constraint->priv->on_update = CONSTRAINT_FK_ACTION_NO_ACTION;
}


/**
 * mg_db_constraint_new
 * @table: the #MgDbTable to which the constraint is attached
 * @type: the type of constraint
 *
 * Creates a new MgDbConstraint object
 *
 * Returns: the new object
 */
GObject*
mg_db_constraint_new (MgDbTable *table, MgDbConstraintType type)
{
	GObject   *obj;
	MgDbConstraint *mg_db_constraint;
	MgConf *conf = NULL;

	g_return_val_if_fail (table && IS_MG_DB_TABLE (table), NULL);
	conf = mg_base_get_conf (MG_BASE (table));
	
	obj = g_object_new (MG_DB_CONSTRAINT_TYPE, "conf", conf, NULL);

	mg_db_constraint = MG_DB_CONSTRAINT (obj);
	mg_base_set_id (MG_BASE (mg_db_constraint), 0);

	mg_db_constraint->priv->type = type;
	mg_db_constraint->priv->table = table;

	g_signal_connect (G_OBJECT (table), "nullified",
			  G_CALLBACK (nullified_object_cb), mg_db_constraint);

	return obj;
}

/**
 * mg_db_constraint_new_with_db
 * @db: a #MgDatabase object
 * 
 * Creates a new #MgDbConstraint object without specifying anything about the
 * constraint except the database it is attached to. This is usefull only
 * when the object is going to be loaded from an XML node.
 *
 * Returns: the new uninitialized object
 */
GObject *
mg_db_constraint_new_with_db (MgDatabase *db)
{
	GObject   *obj;
	MgDbConstraint *mg_db_constraint;
	MgConf *conf = NULL;

	/* here priv->table will be NULL and a "db" data is attached to the object */

	g_return_val_if_fail (db && IS_MG_DATABASE (db), NULL);
	conf = mg_base_get_conf (MG_BASE (db));
	
	obj = g_object_new (MG_DB_CONSTRAINT_TYPE, "conf", conf, NULL);

	mg_db_constraint = MG_DB_CONSTRAINT (obj);
	mg_base_set_id (MG_BASE (mg_db_constraint), 0);
	
	g_object_set_data (obj, "db", db);

	g_signal_connect (G_OBJECT (db), "nullified",
			  G_CALLBACK (nullified_object_cb), mg_db_constraint);
	return obj;
}

static void 
nullified_object_cb (GObject *obj, MgDbConstraint *cstr)
{
	mg_base_nullify (MG_BASE (cstr));
}

static void
mg_db_constraint_dispose (GObject *object)
{
	MgDbConstraint *cstr;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_MG_DB_CONSTRAINT (object));

	cstr = MG_DB_CONSTRAINT (object);
	if (cstr->priv) {
		GSList *list;

		mg_base_nullify_check (MG_BASE (object));

		switch (cstr->priv->type) {
		case CONSTRAINT_PRIMARY_KEY:
		case CONSTRAINT_UNIQUE:
			list = cstr->priv->multiple_fields;
			while (list) {
				g_signal_handlers_disconnect_by_func (G_OBJECT (list->data),
								      G_CALLBACK (nullified_object_cb), cstr);
				list = g_slist_next (list);
			}
			g_slist_free (cstr->priv->multiple_fields);
			cstr->priv->multiple_fields = NULL;
			break;
		case CONSTRAINT_FOREIGN_KEY:
			if (cstr->priv->ref_table)
				g_signal_handlers_disconnect_by_func (G_OBJECT (cstr->priv->ref_table),
								      G_CALLBACK (nullified_object_cb), cstr);
			cstr->priv->ref_table = NULL;

			list = cstr->priv->fk_pairs;
			while (list) {
				MgDbConstraintFkeyPair *pair = MG_DB_CONSTRAINT_FK_PAIR (list->data);

				g_signal_handlers_disconnect_by_func (G_OBJECT (pair->fkey),
								      G_CALLBACK (nullified_object_cb), cstr);
				if (pair->ref_pkey)
					g_signal_handlers_disconnect_by_func (G_OBJECT (pair->ref_pkey),
									      G_CALLBACK (nullified_object_cb), cstr);
				if (pair->ref_pkey_repl)
					g_object_unref (G_OBJECT (pair->ref_pkey_repl));
				g_free (list->data);
				list = g_slist_next (list);
			}
			g_slist_free (cstr->priv->fk_pairs);
			cstr->priv->fk_pairs = NULL;
			break;
		case CONSTRAINT_NOT_NULL:
			if (cstr->priv->single_field)
				g_signal_handlers_disconnect_by_func (G_OBJECT (cstr->priv->single_field),
								      G_CALLBACK (nullified_object_cb), cstr);
			cstr->priv->single_field = NULL;
			break;
		case CONSTRAINT_CHECK_EXPR:
		default:
			TO_IMPLEMENT;
		}

		if (g_object_get_data (G_OBJECT (cstr), "db")) {
			g_signal_handlers_disconnect_by_func (G_OBJECT (g_object_get_data (G_OBJECT (cstr), "db")),
							      G_CALLBACK (nullified_object_cb), cstr);
			g_object_set_data (G_OBJECT (cstr), "db", NULL);
		}
		
		if (cstr->priv->table) {
			g_signal_handlers_disconnect_by_func (G_OBJECT (cstr->priv->table),
							      G_CALLBACK (nullified_object_cb), cstr);
			cstr->priv->table = NULL;
		}
	}

	/* parent class */
	parent_class->dispose (object);
}

static void
mg_db_constraint_finalize (GObject   * object)
{
	MgDbConstraint *mg_db_constraint;

	g_return_if_fail (object != NULL);
	g_return_if_fail (IS_MG_DB_CONSTRAINT (object));

	mg_db_constraint = MG_DB_CONSTRAINT (object);
	if (mg_db_constraint->priv) {

		g_free (mg_db_constraint->priv);
		mg_db_constraint->priv = NULL;
	}

	/* parent class */
	parent_class->finalize (object);
}


static void 
mg_db_constraint_set_property (GObject              *object,
			       guint                 param_id,
			       const GValue         *value,
			       GParamSpec           *pspec)
{
	MgDbConstraint *mg_db_constraint;

	mg_db_constraint = MG_DB_CONSTRAINT (object);
	if (mg_db_constraint->priv) {
		switch (param_id) {
		case PROP_USER_CSTR:
			mg_db_constraint->priv->user_defined = g_value_get_boolean (value);
			break;
		}
	}
}

static void
mg_db_constraint_get_property (GObject              *object,
			       guint                 param_id,
			       GValue               *value,
			       GParamSpec           *pspec)
{
	MgDbConstraint *mg_db_constraint;
	mg_db_constraint = MG_DB_CONSTRAINT (object);
	
	if (mg_db_constraint->priv) {
		switch (param_id) {
		case PROP_USER_CSTR:
			g_value_set_boolean (value, mg_db_constraint->priv->user_defined);
			break;
		}	
	}
}


/**
 * mg_db_constraint_get_constraint_type
 * @cstr: a #MgDbConstraint object
 *
 * Get the type of constraint the @cstr object represents
 *
 * Returns: the constraint type
 */
MgDbConstraintType
mg_db_constraint_get_constraint_type (MgDbConstraint *cstr)
{
	g_return_val_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr), CONSTRAINT_UNKNOWN);
	g_return_val_if_fail (cstr->priv, CONSTRAINT_UNKNOWN);
	g_return_val_if_fail (cstr->priv->table, CONSTRAINT_UNKNOWN);

	return cstr->priv->type;
}

/**
 * mg_db_constraint_equal
 * @cstr1: the first #MgDbConstraint to compare
 * @cstr2: the second #MgDbConstraint to compare
 *
 * Compares two #MgDbConstraint objects to see if they are equal, without taking into account the
 * name of the constraints or weather they are user or system defined
 *
 * Returns: TRUE if the two constraints are equal and FALSE otherwise
 */
gboolean
mg_db_constraint_equal (MgDbConstraint *cstr1, MgDbConstraint *cstr2)
{
	gboolean equal = TRUE;
	GSList *list1, *list2;

	g_return_val_if_fail (cstr1 && IS_MG_DB_CONSTRAINT (cstr1), FALSE);
	g_return_val_if_fail (cstr1->priv, FALSE);
	g_return_val_if_fail (cstr2 && IS_MG_DB_CONSTRAINT (cstr2), FALSE);
	g_return_val_if_fail (cstr2->priv, FALSE);
	g_return_val_if_fail (cstr1->priv->table, FALSE);
	g_return_val_if_fail (cstr2->priv->table, FALSE);

	if (cstr1->priv->type != cstr2->priv->type)
		return FALSE;

	if (cstr1->priv->table != cstr2->priv->table)
		return FALSE;
	
	mg_db_constraint_activate (MG_REFERER (cstr1));
	mg_db_constraint_activate (MG_REFERER (cstr2));

	switch (cstr1->priv->type) {
	case CONSTRAINT_PRIMARY_KEY:
	case CONSTRAINT_UNIQUE:
		list1 = cstr1->priv->multiple_fields;
		list2 = cstr2->priv->multiple_fields;
		while (list1 && list2 && equal) {
			if (list1->data != list2->data)
				equal = FALSE;
			list1 = g_slist_next (list1);
			list2 = g_slist_next (list2);
		}
		if (list1 || list2)
			equal = FALSE;
		break;
	case CONSTRAINT_FOREIGN_KEY:
		list1 = cstr1->priv->fk_pairs;
		list2 = cstr2->priv->fk_pairs;
		while (list1 && list2 && equal) {
			if (MG_DB_CONSTRAINT_FK_PAIR (list1->data)->fkey != 
			    MG_DB_CONSTRAINT_FK_PAIR (list2->data)->fkey)
				equal = FALSE;
			if (MG_DB_CONSTRAINT_FK_PAIR (list1->data)->ref_pkey != 
			    MG_DB_CONSTRAINT_FK_PAIR (list2->data)->ref_pkey)
				equal = FALSE;

			if (MG_DB_CONSTRAINT_FK_PAIR (list1->data)->ref_pkey_repl &&
			    MG_DB_CONSTRAINT_FK_PAIR (list2->data)->ref_pkey_repl) {
				GType type1, type2;
				MgRefBaseType itype1, itype2;
				const gchar *name1, *name2;
				
				name1 = mg_ref_base_get_ref_name (MG_DB_CONSTRAINT_FK_PAIR (list1->data)->ref_pkey_repl,
								  &type1, &itype1);
				name2 = mg_ref_base_get_ref_name (MG_DB_CONSTRAINT_FK_PAIR (list2->data)->ref_pkey_repl,
								  &type2, &itype2);
				if ((type1 != type2) || (itype1 != itype2) ||
				    strcmp (name1, name2))
					equal = FALSE;
			}
			else {
				if (MG_DB_CONSTRAINT_FK_PAIR (list1->data)->ref_pkey_repl ||
				    MG_DB_CONSTRAINT_FK_PAIR (list2->data)->ref_pkey_repl)
					equal = FALSE;
			}

			list1 = g_slist_next (list1);
			list2 = g_slist_next (list2);
		}
		if (list1 || list2)
			equal = FALSE;
		break;
	case CONSTRAINT_NOT_NULL:
		if (cstr1->priv->single_field != cstr2->priv->single_field)
			equal = FALSE;
		break;
	case CONSTRAINT_CHECK_EXPR:
		TO_IMPLEMENT;
		break;
	default:
		equal = FALSE;
		break;
	}

	return equal;
}


/**
 * mg_db_constraint_get_table
 * @cstr: a #MgDbConstraint object
 *
 * Get the table to which the constraint is attached
 *
 * Returns: the #MgDbTable
 */
MgDbTable *
mg_db_constraint_get_table (MgDbConstraint *cstr)
{
	g_return_val_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr), NULL);
	g_return_val_if_fail (cstr->priv, NULL);
	g_return_val_if_fail (cstr->priv->table, NULL);

	return cstr->priv->table;
}

/**
 * mg_db_constraint_uses_field
 * @cstr: a #MgDbConstraint object
 * @field: a #MgDbField object
 *
 * Tests if @field is part of the @cstr constraint
 *
 * Returns: TRUE if @cstr uses @field
 */
gboolean
mg_db_constraint_uses_field (MgDbConstraint *cstr, MgDbField *field)
{
	gboolean retval = FALSE;
	GSList *list;

	g_return_val_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr), FALSE);
	g_return_val_if_fail (cstr->priv, FALSE);
	g_return_val_if_fail (field && IS_MG_DB_FIELD (field), FALSE);

	switch (mg_db_constraint_get_constraint_type (cstr)) {
	case CONSTRAINT_PRIMARY_KEY:
	case CONSTRAINT_UNIQUE:
		if (g_slist_find (cstr->priv->multiple_fields, field))
			retval = TRUE;
		break;
	case CONSTRAINT_FOREIGN_KEY:
		list = cstr->priv->fk_pairs;
		while (list && !retval) {
			if (MG_DB_CONSTRAINT_FK_PAIR (list->data)->fkey == field)
				retval = TRUE;
			list = g_slist_next (list);
		}
		break;
	case CONSTRAINT_NOT_NULL:
		if (cstr->priv->single_field == field)
			retval = TRUE;
		break;
	case CONSTRAINT_CHECK_EXPR:
	default:
		TO_IMPLEMENT;
	}
	
	return retval;
}


static void mg_db_constraint_multiple_set_fields (MgDbConstraint *cstr, const GSList *fields);

/**
* mg_db_constraint_pkey_set_fields
* @cstr: a #MgDbConstraint object
* @fields: a list of #MgDbField objects
*
* Sets the fields which make the primary key represented by @cstr. All the fields
* must belong to the same #MgDbTable to which the constraint is attached
*/
void
mg_db_constraint_pkey_set_fields (MgDbConstraint *cstr, const GSList *fields)
{
	g_return_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr));
	g_return_if_fail (cstr->priv);
	g_return_if_fail (cstr->priv->type == CONSTRAINT_PRIMARY_KEY);
	g_return_if_fail (cstr->priv->table);
	g_return_if_fail (fields);

	mg_db_constraint_multiple_set_fields (cstr, fields);
}


static void
mg_db_constraint_multiple_set_fields (MgDbConstraint *cstr, const GSList *fields)
{
	const GSList *list;
	g_return_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr));

	/* testing for errors */
	list = fields;
	while (list) {
		if (!list->data) {
			g_warning ("List contains a NULL value, not a field");
			return;
		}
		if (!IS_MG_DB_FIELD (list->data)) {
			g_warning ("List item %p is not a field\n", list->data);
			return;
		}
		if (mg_field_get_entity (MG_FIELD (list->data)) != MG_ENTITY (cstr->priv->table)) {
			g_warning ("Field %p belongs to a table different from the constraint\n", list->data);
			return;
		}

		list = g_slist_next (list);
	}

	/* if a fields list is already here, get rid of it */
	if (cstr->priv->multiple_fields) {
		list = cstr->priv->multiple_fields;
		while (list) {
			g_signal_handlers_disconnect_by_func (G_OBJECT (list->data),
							      G_CALLBACK (nullified_object_cb), cstr);
			list = g_slist_next (list);
		}
		g_slist_free (cstr->priv->multiple_fields);
		cstr->priv->multiple_fields = NULL;
	}

	/* make a copy of the new fields list */
	list = fields;
	while (list) {
		g_signal_connect (G_OBJECT (list->data), "nullified",
				  G_CALLBACK (nullified_object_cb), cstr);
		cstr->priv->multiple_fields = g_slist_append (cstr->priv->multiple_fields, list->data);
		list = g_slist_next (list);
	}
}

/**
 * mg_db_constraint_pkey_get_fields
 * @cstr: a #MgDbConstraint object
 *
 * Get the list of fields composing the primary key constraint which @cstr represents. The
 * returned list is allocated and must be de-allocated by the caller.
 *
 * Returns: a new list of fields
 */
GSList *
mg_db_constraint_pkey_get_fields (MgDbConstraint *cstr)
{
	g_return_val_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr), NULL);
	g_return_val_if_fail (cstr->priv, NULL);
	g_return_val_if_fail (cstr->priv->type == CONSTRAINT_PRIMARY_KEY, NULL);
	g_return_val_if_fail (cstr->priv->table, NULL);

	return g_slist_copy (cstr->priv->multiple_fields);
}


/** 
 * mg_db_constraint_fkey_set_fields
 * @cstr: a #MgDbConstraint object
 * @pairs: a list of #MgDbField objects
 *
 * Sets the field pairs which make the foreign key represented by @cstr. All the field pairs
 * must list a field which belong to the same #MgDbTable to which the constraint is attached
 * and a field which belongs to a #MgDbTable which is different from the one just mentionned and which
 * is within the same database.
 * The pairs are of type #MgDbConstraintFkeyPair.
 */
void
mg_db_constraint_fkey_set_fields (MgDbConstraint *cstr, const GSList *pairs)
{
	const GSList *list;
	MgDbTable *ref_table = NULL;
	GSList *oldlist;

	g_return_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr));
	g_return_if_fail (cstr->priv);
	g_return_if_fail (cstr->priv->type == CONSTRAINT_FOREIGN_KEY);
	g_return_if_fail (cstr->priv->table);

	/* testing for errors */
	list = pairs;
	while (list) {
		MgDbConstraintFkeyPair *pair;

		if (!list->data) {
			g_warning ("List contains a NULL value, not a pair of fields");
			return;
		}

		pair = MG_DB_CONSTRAINT_FK_PAIR (list->data);
		if (!IS_MG_DB_FIELD (pair->fkey)) {
			g_warning ("Pair item %p has fkey which is not a is not a field", list->data);
			return;
		}
		if (pair->ref_pkey_repl) { /* we have a MgRefBase object */
			if (!IS_MG_REF_BASE (pair->ref_pkey_repl)) {
				g_warning ("Pair item %p has ref_pkey_repl which is not a is not a MgRefBase", list->data);
				return;	
			}
			if (mg_ref_base_get_ref_type (pair->ref_pkey_repl) !=
			    MG_DB_FIELD_TYPE) {
				g_warning ("Pair item %p has ref_pkey_repl which does not reference a field", list->data);
				return;	
			}
		}
		else { /* we have a MgDbField object */
			if (!pair->ref_pkey || !IS_MG_DB_FIELD (pair->ref_pkey)) {
				g_warning ("Pair item %p has ref_pkey which is not a is not a field", list->data);
				return;
			}
			if (!ref_table)
				ref_table = MG_DB_TABLE (mg_field_get_entity (MG_FIELD (pair->ref_pkey)));
			else
				if (mg_field_get_entity (MG_FIELD (pair->ref_pkey)) != 
				    MG_ENTITY (ref_table)) {
					g_warning ("Referenced table is not the same for all pairs");
					return;
				}
		}

		if (mg_field_get_entity (MG_FIELD (pair->fkey)) != 
					 MG_ENTITY (cstr->priv->table)) {
			g_warning ("Field %p belongs to a table different from the constraint", 
				   pair->fkey);
			return;
		}

		list = g_slist_next (list);
	}

	/* if a fields list is already here, we treat in two parts: first (before the new list is analysed)
	 * we diocsonnect signals from objects of the old list, and then (after the new list has been analysed)
	 * we remove any reference to unused objects and clear the list */
	oldlist = cstr->priv->fk_pairs;
	list = cstr->priv->fk_pairs;
	while (list) {
		MgDbConstraintFkeyPair *pair = MG_DB_CONSTRAINT_FK_PAIR (list->data);
		
		g_signal_handlers_disconnect_by_func (G_OBJECT (pair->fkey),
						      G_CALLBACK (nullified_object_cb), cstr);
		if (pair->ref_pkey)
			g_signal_handlers_disconnect_by_func (G_OBJECT (pair->ref_pkey),
							      G_CALLBACK (nullified_object_cb), cstr);
		
		list = g_slist_next (list);
	}
	if (cstr->priv->ref_table) {
		g_signal_handlers_disconnect_by_func (G_OBJECT (cstr->priv->ref_table),
						      G_CALLBACK (nullified_object_cb), cstr);
		cstr->priv->ref_table = NULL;
	}

	/* make a copy of the new fields list */
	cstr->priv->fk_pairs = NULL;
	list = pairs;
	while (list) {
		MgDbConstraintFkeyPair *pair;
		pair = g_new0 (MgDbConstraintFkeyPair, 1);
		*pair = *(MG_DB_CONSTRAINT_FK_PAIR (list->data));

		g_signal_connect (G_OBJECT (pair->fkey), "nullified",
				  G_CALLBACK (nullified_object_cb), cstr);
		
		if (!pair->ref_pkey_repl) 
			/* here we have tested that pair->ref_pkey is OK */
			g_signal_connect (G_OBJECT (pair->ref_pkey), "nullified",
					  G_CALLBACK (nullified_object_cb), cstr);
		else 
			g_object_ref (G_OBJECT (pair->ref_pkey_repl));

		cstr->priv->fk_pairs = g_slist_append (cstr->priv->fk_pairs, pair);
		list = g_slist_next (list);
	}
	cstr->priv->ref_table = ref_table;
	if (ref_table)
		g_signal_connect (G_OBJECT (ref_table), "nullified",
				  G_CALLBACK (nullified_object_cb), cstr);

	/* second part of getting rid of the ols list of fields */
	if (oldlist) {
		list = oldlist;
		while (list) {
			MgDbConstraintFkeyPair *pair = MG_DB_CONSTRAINT_FK_PAIR (list->data);

			if (pair->ref_pkey_repl)
				g_object_unref (G_OBJECT (pair->ref_pkey_repl));
				
			g_free (list->data);
			list = g_slist_next (list);
		}
		g_slist_free (oldlist);
	}


	mg_db_constraint_activate (MG_REFERER (cstr));
}


/**
 * mg_db_constraint_fkey_get_ref_table
 * @cstr: a #MgDbConstraint object
 *
 * Get the #MgDbTable at the other end of the foreign key relation represented by this
 * constraint
 *
 * Returns: the #MgDbTable
 */
MgDbTable *
mg_db_constraint_fkey_get_ref_table (MgDbConstraint *cstr)
{
	g_return_val_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr), NULL);
	g_return_val_if_fail (cstr->priv, NULL);
	g_return_val_if_fail (cstr->priv->type == CONSTRAINT_FOREIGN_KEY, NULL);
	g_return_val_if_fail (cstr->priv->table, NULL);
	
	mg_db_constraint_activate (MG_REFERER (cstr));

	return cstr->priv->ref_table;
}

/**
 * mg_db_constraint_fkey_get_fields
 * @cstr: a #MgDbConstraint object
 *
 * Get the list of field pairs composing the foreign key constraint which @cstr represents. In the returned
 * list, each pair item is allocated and it's up to the caller to free the list and each pair, and the
 * reference count for each pointer to GObjects in each pair is NOT INCREASED, which means the caller of this
 * function DOES NOT hold any reference on the mentionned GObjects (if he needs to, it has to call g_object_ref())
 *
 * Returns: a new list of #MgDbConstraintFkeyPair pairs
 */
GSList *
mg_db_constraint_fkey_get_fields (MgDbConstraint *cstr)
{
	GSList *list, *retval = NULL;

	g_return_val_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr), NULL);
	g_return_val_if_fail (cstr->priv, NULL);
	g_return_val_if_fail (cstr->priv->type == CONSTRAINT_FOREIGN_KEY, NULL);
	g_return_val_if_fail (cstr->priv->table, NULL);

	mg_db_constraint_activate (MG_REFERER (cstr));
	
	/* copy the list of pairs */
	list = cstr->priv->fk_pairs;
	while (list) {
		MgDbConstraintFkeyPair *pair;
		pair = g_new0 (MgDbConstraintFkeyPair, 1);
		*pair = * MG_DB_CONSTRAINT_FK_PAIR (list->data);
		retval = g_slist_append (retval, pair);
		list = g_slist_next (list);
	}

	return retval;
}

/**
 * mg_db_constraint_fkey_set_actions
 * @cstr: a #MgDbConstraint object
 * @on_update: the action undertaken when an UPDATE occurs
 * @on_delete: the action undertaken when a DELETE occurs
 *
 * Sets the actions undertaken by the DBMS when some actions occur on the referenced data
 */
void
mg_db_constraint_fkey_set_actions (MgDbConstraint *cstr, 
				   MgDbConstraintFkAction on_update, MgDbConstraintFkAction on_delete)
{
	g_return_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr));
	g_return_if_fail (cstr->priv);
	g_return_if_fail (cstr->priv->type == CONSTRAINT_FOREIGN_KEY);
	g_return_if_fail (cstr->priv->table);

	cstr->priv->on_update = on_update;
	cstr->priv->on_delete = on_delete;
}

/**
 * mg_db_constraint_fkey_get_actions
 * @cstr: a #MgDbConstraint object
 * @on_update: an address to store the action undertaken when an UPDATE occurs
 * @on_delete: an address to store the action undertaken when a DELETE occurs
 *
 * Get the actions undertaken by the DBMS when some actions occur on the referenced data
 */
void
mg_db_constraint_fkey_get_actions (MgDbConstraint *cstr, 
				   MgDbConstraintFkAction *on_update, MgDbConstraintFkAction *on_delete)
{
	g_return_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr));
	g_return_if_fail (cstr->priv);
	g_return_if_fail (cstr->priv->type == CONSTRAINT_FOREIGN_KEY);
	g_return_if_fail (cstr->priv->table);

	if (on_update)
		*on_update = cstr->priv->on_update;
	if (on_delete)
		*on_delete = cstr->priv->on_delete;
}


/**
 * mg_db_constraint_unique_set_fields
 * @cstr: a #MgDbConstraint object
 * @fields:
 *
 */
void
mg_db_constraint_unique_set_fields (MgDbConstraint *cstr, const GSList *fields)
{
	g_return_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr));
	g_return_if_fail (cstr->priv);
	g_return_if_fail (cstr->priv->type == CONSTRAINT_UNIQUE);
	g_return_if_fail (cstr->priv->table);
	g_return_if_fail (fields);

	mg_db_constraint_multiple_set_fields (cstr, fields);
}

/**
 * mg_db_constraint_unique_get_fields
 * @cstr: a #MgDbConstraint object
 *
 * Get the list of fields represented by this UNIQUE constraint. It's up to the caller to free the list.
 *
 * Returns: a new list of fields
 */
GSList *
mg_db_constraint_unique_get_fields (MgDbConstraint *cstr)
{
	g_return_val_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr), NULL);
	g_return_val_if_fail (cstr->priv, NULL);
	g_return_val_if_fail (cstr->priv->type == CONSTRAINT_UNIQUE, NULL);
	g_return_val_if_fail (cstr->priv->table, NULL);

	return g_slist_copy (cstr->priv->multiple_fields);
}

/**
 * mg_db_constraint_non_null_set_field
 * @cstr: a #MgDbConstraint object
 * @field:
 *
 */
void
mg_db_constraint_not_null_set_field (MgDbConstraint *cstr, MgDbField *field)
{
	g_return_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr));
	g_return_if_fail (cstr->priv);
	g_return_if_fail (cstr->priv->type == CONSTRAINT_NOT_NULL);
	g_return_if_fail (cstr->priv->table);
	g_return_if_fail (field && IS_MG_DB_FIELD (field));
	g_return_if_fail (mg_field_get_entity (MG_FIELD (field)) == MG_ENTITY (cstr->priv->table));


	/* if a field  is already here, get rid of it */
	if (cstr->priv->single_field) {
		g_signal_handlers_disconnect_by_func (G_OBJECT (cstr->priv->single_field),
						      G_CALLBACK (nullified_object_cb), cstr);
		cstr->priv->single_field = NULL;
	}

	g_signal_connect (G_OBJECT (field), "nullified",
			  G_CALLBACK (nullified_object_cb), cstr);
	cstr->priv->single_field = field;
}

/**
 * mg_db_constraint_non_null_get_field
 * @cstr: a #MgDbConstraint object
 *
 */
MgDbField *
mg_db_constraint_not_null_get_field (MgDbConstraint *cstr)
{
	g_return_val_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr), NULL);
	g_return_val_if_fail (cstr->priv, NULL);
	g_return_val_if_fail (cstr->priv->type == CONSTRAINT_NOT_NULL, NULL);
	g_return_val_if_fail (cstr->priv->table, NULL);

	return cstr->priv->single_field;
}



#ifdef debug
static void
mg_db_constraint_dump (MgDbConstraint *cstr, guint offset)
{
	gchar *str;
	gint i;

	g_return_if_fail (cstr && IS_MG_DB_CONSTRAINT (cstr));
	
        /* string for the offset */
        str = g_new0 (gchar, offset+1);
        for (i=0; i<offset; i++)
                str[i] = ' ';
        str[offset] = 0;

        /* dump */
        if (cstr->priv && cstr->priv->table)
		g_print ("%s" D_COL_H1 "MgDbConstraint" D_COL_NOR " %p type=%d name=%s\n",
			 str, cstr, cstr->priv->type, mg_base_get_description (MG_BASE (cstr)));
        else
                g_print ("%s" D_COL_ERR "Using finalized object %p" D_COL_NOR, str, cstr);
}
#endif








/* 
 * MgXmlStorage interface implementation
 */
static gchar *
mg_db_constraint_get_xml_id (MgXmlStorage *iface)
{
	gchar *t_xml_id, *xml_id;

	g_return_val_if_fail (iface && IS_MG_DB_CONSTRAINT (iface), NULL);
	g_return_val_if_fail (MG_DB_CONSTRAINT (iface)->priv, NULL);
	g_return_val_if_fail (MG_DB_CONSTRAINT (iface)->priv->table, NULL);

	t_xml_id = mg_xml_storage_get_xml_id (MG_XML_STORAGE (MG_DB_CONSTRAINT (iface)->priv->table));
	xml_id = g_strdup_printf ("%s:FI%s", t_xml_id, mg_base_get_name (MG_BASE (iface)));
	g_free (t_xml_id);
	
	return xml_id;
}

static const gchar *
constraint_type_to_str (MgDbConstraintType type)
{
	switch (type) {
	case CONSTRAINT_PRIMARY_KEY:
		return "PKEY";
		break;
	case CONSTRAINT_FOREIGN_KEY:
		return "FKEY";
		break;
	case CONSTRAINT_UNIQUE:
		return "UNIQ";
		break;
	case CONSTRAINT_NOT_NULL:
		return "NNUL";
		break;
	case CONSTRAINT_CHECK_EXPR:
		return "CHECK";
		break;
	default:
		return "???";
		break;
	}
}

static MgDbConstraintType
constraint_str_to_type (const gchar *str)
{
	g_return_val_if_fail (str, CONSTRAINT_UNKNOWN);

	switch (*str) {
	case 'P':
		return CONSTRAINT_PRIMARY_KEY;
		break;
	case 'F':
		return CONSTRAINT_FOREIGN_KEY;
		break;
	case 'U':
		return CONSTRAINT_UNIQUE;
		break;
	case 'N':
		return CONSTRAINT_NOT_NULL;
		break;
	case 'C':
		return CONSTRAINT_CHECK_EXPR;
		break;
	default:
		return CONSTRAINT_UNKNOWN;
		break;
	}
}


static const gchar *
constraint_action_to_str (MgDbConstraintFkAction action)
{
	switch (action) {
	case CONSTRAINT_FK_ACTION_CASCADE:
		return "CAS";
		break;
	case CONSTRAINT_FK_ACTION_SET_NULL:
		return "NULL";
		break;
	case CONSTRAINT_FK_ACTION_SET_DEFAULT:
		return "DEF";
		break;
	case CONSTRAINT_FK_ACTION_SET_VALUE:
		return "VAL";
		break;
	case CONSTRAINT_FK_ACTION_NO_ACTION:
		return "RESTRICT";
		break;
	default:
		return "???";
		break;	
	}
}

static MgDbConstraintFkAction
constraint_str_to_action (const gchar *str)
{
	g_return_val_if_fail (str, CONSTRAINT_FK_ACTION_NO_ACTION);
	switch (*str) {
	case 'C':
		return CONSTRAINT_FK_ACTION_CASCADE;
		break;
	case 'N':
		return CONSTRAINT_FK_ACTION_SET_NULL;
		break;
	case 'D':
		return CONSTRAINT_FK_ACTION_SET_DEFAULT;
		break;
	case 'V':
		return CONSTRAINT_FK_ACTION_SET_VALUE;
		break;
	case 'R':
		return CONSTRAINT_FK_ACTION_NO_ACTION;
		break;
	default:
		return CONSTRAINT_FK_ACTION_NO_ACTION;
		break;	
	}
}

static xmlNodePtr
mg_db_constraint_save_to_xml (MgXmlStorage *iface, GError **error)
{
	xmlNodePtr node = NULL;
	xmlNodePtr child;
	MgDbConstraint *cstr;
	gchar *str;
	GSList *list;

	g_return_val_if_fail (iface && IS_MG_DB_CONSTRAINT (iface), NULL);
	g_return_val_if_fail (MG_DB_CONSTRAINT (iface)->priv, NULL);
	g_return_val_if_fail (MG_DB_CONSTRAINT (iface)->priv->table, NULL);

	cstr = MG_DB_CONSTRAINT (iface);
	mg_db_constraint_activate (MG_REFERER (cstr));

	if (! mg_db_constraint_is_active (MG_REFERER (cstr))) {
		g_set_error (error,
			     MG_DB_CONSTRAINT_ERROR,
			     MG_DB_CONSTRAINT_XML_SAVE_ERROR,
			     _("Constraint cannot be activated!"));
		return NULL;
	}
	node = xmlNewNode (NULL, "MG_CONSTRAINT");
	
	xmlSetProp (node, "name", mg_base_get_name (MG_BASE (cstr)));
	xmlSetProp (node, "user_defined", cstr->priv->user_defined ? "t" : "f");
	xmlSetProp (node, "type", constraint_type_to_str (cstr->priv->type));

	str = mg_xml_storage_get_xml_id (MG_XML_STORAGE (cstr->priv->table));
	xmlSetProp (node, "table", str);
	g_free (str);

	switch (cstr->priv->type) {
	case CONSTRAINT_UNIQUE:
	case CONSTRAINT_PRIMARY_KEY:
		list = cstr->priv->multiple_fields;
		while (list) {
			child = xmlNewChild (node, NULL, "MG_CONSTRAINT_FIELD", NULL);
			str = mg_xml_storage_get_xml_id (MG_XML_STORAGE (list->data));
			xmlSetProp (child, "field", str);
			g_free (str);
			list = g_slist_next (list);
		}
		break;
	case CONSTRAINT_FOREIGN_KEY:
		list = cstr->priv->fk_pairs;
		while (list) {
			child = xmlNewChild (node, NULL, "MG_CONSTRAINT_PAIR", NULL);
			str = mg_xml_storage_get_xml_id (MG_XML_STORAGE (MG_DB_CONSTRAINT_FK_PAIR (list->data)->fkey));
			xmlSetProp (child, "field", str);
			g_free (str);
			str = mg_xml_storage_get_xml_id (MG_XML_STORAGE (MG_DB_CONSTRAINT_FK_PAIR (list->data)->ref_pkey));
			xmlSetProp (child, "ref", str);
			g_free (str);
			list = g_slist_next (list);
		}

		xmlSetProp (node, "on_update", constraint_action_to_str (cstr->priv->on_update));
		xmlSetProp (node, "on_delete", constraint_action_to_str (cstr->priv->on_delete));
		break;
	case CONSTRAINT_NOT_NULL:
		child = xmlNewChild (node, NULL, "MG_CONSTRAINT_FIELD", NULL);
		if (cstr->priv->single_field)
			str = mg_xml_storage_get_xml_id (MG_XML_STORAGE (cstr->priv->single_field));
		xmlSetProp (child, "field", str);
		g_free (str);
		break;
	case CONSTRAINT_CHECK_EXPR:
		TO_IMPLEMENT;
		break;
	default:
		break;
	}

	return node;
}

static gboolean
mg_db_constraint_load_from_xml (MgXmlStorage *iface, xmlNodePtr node, GError **error)
{
	MgDbConstraint *cstr;
	gchar *prop;
	gboolean type = FALSE, field_error = FALSE;
	MgDbTable *table = NULL;
	MgDatabase *db;
	gchar *ref_pb = NULL;
	xmlNodePtr subnode;
	GSList *contents, *list;

	g_return_val_if_fail (iface && IS_MG_DB_CONSTRAINT (iface), FALSE);
	g_return_val_if_fail (MG_DB_CONSTRAINT (iface)->priv, FALSE);
	g_return_val_if_fail (node, FALSE);
	g_return_val_if_fail (!MG_DB_CONSTRAINT (iface)->priv->table, FALSE);
	db = g_object_get_data (G_OBJECT (iface), "db");
	g_return_val_if_fail (db, FALSE);

	cstr = MG_DB_CONSTRAINT (iface);
	if (strcmp (node->name, "MG_CONSTRAINT")) {
		g_set_error (error,
			     MG_DB_CONSTRAINT_ERROR,
			     MG_DB_CONSTRAINT_XML_LOAD_ERROR,
			     _("XML Tag is not <MG_CONSTRAINT>"));
		return FALSE;
	}

	prop = xmlGetProp (node, "name");
	if (prop) {
		mg_base_set_name (MG_BASE (cstr), prop);
		g_free (prop);
	}

	prop = xmlGetProp (node, "table");
	if (prop) {
		table = mg_database_get_table_by_xml_id (db, prop);
		if (table) {
			g_object_set_data (G_OBJECT (iface), "db", NULL);
			g_signal_handlers_disconnect_by_func (G_OBJECT (db),
							      G_CALLBACK (nullified_object_cb), cstr);
			cstr->priv->table = table;
			g_signal_connect (G_OBJECT (table), "nullified",
					  G_CALLBACK (nullified_object_cb), cstr);
			g_free (prop);
		}
		else 
			ref_pb = prop;
	}
	
	prop = xmlGetProp (node, "user_defined");
	if (prop) {
		cstr->priv->user_defined = (*prop && (*prop == 't')) ? TRUE : FALSE;
		g_free (prop);
	}

	prop = xmlGetProp (node, "type");
	if (prop) {
		cstr->priv->type = constraint_str_to_type (prop);
		g_free (prop);
		type = TRUE;
	}
	
	prop = xmlGetProp (node, "on_update");
	if (prop) {
		cstr->priv->on_update = constraint_str_to_action (prop);
		g_free (prop);
		type = TRUE;
	}

	prop = xmlGetProp (node, "on_delete");
	if (prop) {
		cstr->priv->on_delete = constraint_str_to_action (prop);
		g_free (prop);
		type = TRUE;
	}

	/* contents of the constraint if applicable */
	contents = NULL;
	subnode = node->children;
	while (subnode) {
		if (!strcmp (subnode->name, "MG_CONSTRAINT_FIELD")) {
			MgDbField *field = NULL;
			prop = xmlGetProp (subnode, "field");
			if (prop) {
				field = mg_database_get_field_by_xml_id (db, prop);
				g_free (prop);
			}
			if (field)
				contents = g_slist_append (contents, field);
			else {
				field_error = TRUE;
				ref_pb = xmlGetProp (subnode, "field");
			}
		}
		
		if (!strcmp (subnode->name, "MG_CONSTRAINT_PAIR")) {
			MgDbField *field = NULL, *ref = NULL;
			prop = xmlGetProp (subnode, "field");
			if (prop) {
				field = mg_database_get_field_by_xml_id (db, prop);
				g_free (prop);
			}
			prop = xmlGetProp (subnode, "ref");
			if (prop) {
				ref = mg_database_get_field_by_xml_id (db, prop);
				g_free (prop);
			}
			if (field && ref) {
				MgDbConstraintFkeyPair *pair;

				pair = g_new0 (MgDbConstraintFkeyPair, 1);
				pair->fkey = field;
				pair->ref_pkey = ref;
				pair->ref_pkey_repl = NULL;
				contents = g_slist_append (contents, pair);
			}
			else {
				field_error = TRUE;
				if (!field)
					ref_pb = xmlGetProp (subnode, "field");
				else
					ref_pb = xmlGetProp (subnode, "ref");
			}
		}
		subnode = subnode->next;
	}

	switch (cstr->priv->type) {
	case CONSTRAINT_PRIMARY_KEY:
		mg_db_constraint_pkey_set_fields (cstr, contents);
		g_slist_free (contents);
		break;
        case CONSTRAINT_FOREIGN_KEY:
		mg_db_constraint_fkey_set_fields (cstr, contents);
		list = contents;
		while (list) {
			g_free (list->data);
			list = g_slist_next (list);
		}
		g_slist_free (contents);
		break;
        case CONSTRAINT_UNIQUE:
		mg_db_constraint_unique_set_fields (cstr, contents);
		g_slist_free (contents);
		break;
        case CONSTRAINT_NOT_NULL:
		if (contents) {
			mg_db_constraint_not_null_set_field (cstr, contents->data);
			g_slist_free (contents);
		}
		else
			field_error = TRUE;
		break;
        case CONSTRAINT_CHECK_EXPR:
		TO_IMPLEMENT;
		break;
	default:
		break;
	}

	if (type && table && !field_error)
		return TRUE;
	else {
		if (!type)
			g_set_error (error,
				     MG_DB_CONSTRAINT_ERROR,
				     MG_DB_CONSTRAINT_XML_LOAD_ERROR,
				     _("Missing required attributes for <MG_CONSTRAINT>"));
		if (!table) {
			g_set_error (error,
				     MG_DB_CONSTRAINT_ERROR,
				     MG_DB_CONSTRAINT_XML_LOAD_ERROR,
				     _("Referenced table (%s) not found"), ref_pb);
			if (ref_pb)
				g_free (ref_pb);
		}
		if (field_error) {
			g_set_error (error,
				     MG_DB_CONSTRAINT_ERROR,
				     MG_DB_CONSTRAINT_XML_LOAD_ERROR,
				     _("Referenced field in constraint (%s) not found"), ref_pb);
			if (ref_pb)
				g_free (ref_pb);
		}
		return FALSE;
	}
}




/*
 * MgReferer interface implementation
 */

/*
 * mg_db_constraint_activate
 *
 * Tries to convert any MgRefBase object to the pointed MgDbField instead
 */
static gboolean
mg_db_constraint_activate (MgReferer *iface)
{
	gboolean activated = TRUE;
	MgDbConstraint *cstr;
	
	g_return_val_if_fail (iface && IS_MG_DB_CONSTRAINT (iface), FALSE);
	cstr = MG_DB_CONSTRAINT (iface);
	g_return_val_if_fail (cstr->priv, FALSE);
	g_return_val_if_fail (cstr->priv->table, FALSE);

	if (mg_db_constraint_is_active (MG_REFERER (cstr)))
		return TRUE;

	if (cstr->priv->type == CONSTRAINT_FOREIGN_KEY) {
		GSList *list;
		MgDbConstraintFkeyPair *pair;
		MgBase *ref;

		list = cstr->priv->fk_pairs;
		while (list) {
			pair = MG_DB_CONSTRAINT_FK_PAIR (list->data);
			if (!pair->ref_pkey) {
				g_assert (pair->ref_pkey_repl);
				ref = mg_ref_base_get_ref_object (pair->ref_pkey_repl);
				if (ref) {
					pair->ref_pkey = MG_DB_FIELD (ref);
					g_object_unref (G_OBJECT (pair->ref_pkey_repl));
					pair->ref_pkey_repl = NULL;
					g_signal_connect (G_OBJECT (pair->ref_pkey), "nullified",
							  G_CALLBACK (nullified_object_cb), cstr);
				}
			}

			if (!pair->ref_pkey)
				activated = FALSE;
			
			list = g_slist_next (list);
		}
	}

	return activated;
}

static void
mg_db_constraint_deactivate (MgReferer *iface)
{
	g_return_if_fail (iface && IS_MG_DB_CONSTRAINT (iface));
	/* do nothing */
}

/*
 * mg_db_constraint_is_active
 *
 * Tells wether the MgDbConstraint object has found all the
 * MgDbField objects it needs
 */
static gboolean
mg_db_constraint_is_active (MgReferer *iface)
{
	gboolean activated = TRUE;
	MgDbConstraint *cstr;
	
	g_return_val_if_fail (iface && IS_MG_DB_CONSTRAINT (iface), FALSE);
	cstr = MG_DB_CONSTRAINT (iface);
	g_return_val_if_fail (cstr->priv, FALSE);
	g_return_val_if_fail (cstr->priv->table, FALSE);

	if (cstr->priv->type == CONSTRAINT_FOREIGN_KEY) {
		GSList *list = cstr->priv->fk_pairs;
		while (list && activated) {
			MgDbConstraintFkeyPair *pair = MG_DB_CONSTRAINT_FK_PAIR (list->data);
			if (pair->ref_pkey_repl)
				activated = FALSE;
			list = g_slist_next (list);
		}
	}

	return activated;
}

static GSList *
mg_db_constraint_get_ref_objects (MgReferer *iface)
{
	g_return_val_if_fail (iface && IS_MG_DB_CONSTRAINT (iface), NULL);
	/* do nothing */
	return NULL;
}

static void mg_db_constraint_replace_refs (MgReferer *iface, GHashTable *replacements)
{
	g_return_if_fail (iface && IS_MG_DB_CONSTRAINT (iface));
	/* do nothing */
}