File: gda-ldap-connection.c

package info (click to toggle)
libgda5 5.2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 75,256 kB
  • ctags: 49,218
  • sloc: ansic: 461,759; sh: 11,808; xml: 10,466; yacc: 5,160; makefile: 4,327; php: 1,416; java: 1,300; python: 896; sql: 879; perl: 116
file content (1273 lines) | stat: -rw-r--r-- 40,506 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (C) 2011 - 2012 Vivien Malerba <malerba@gnome-db.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <glib/gi18n-lib.h>
#include <string.h>
#include <gda-util.h>
#include "gda-ldap-connection.h"
#include <libgda/gda-connection-private.h>
#include <sql-parser/gda-sql-parser.h>
#include <libgda/gda-debug-macros.h>

/* "inherits" GdaVconnectionDataModelSpec */
typedef struct {
	GdaVconnectionDataModelSpec  spec;
	GdaConnection               *ldap_cnc;
	gchar                       *table_name;
	gchar                       *base_dn;
	gchar                       *filter;
	gchar                       *attributes;
	GList                       *columns;
	GdaLdapSearchScope           scope;

	GHashTable                  *filters_hash; /* key = string; value = a ComputedFilter pointer */
} LdapTableMap;

static void
_ldap_table_map_free (LdapTableMap *map)
{
	if (map->ldap_cnc)
		g_object_unref (map->ldap_cnc);
	g_free (map->table_name);
	g_free (map->base_dn);
	g_free (map->filter);
	g_free (map->attributes);
	if (map->columns) {
		g_list_foreach (map->columns, (GFunc) g_object_unref, NULL);
		g_list_free (map->columns);
	}
	if (map->filters_hash)
		g_hash_table_destroy (map->filters_hash);
	g_free (map);
}

struct _GdaLdapConnectionPrivate {
	GSList   *maps; /* list of #LdapTableMap, no ref held there */
	gchar    *startup_file;
	gboolean  loading_startup_file;
};

static void gda_ldap_connection_class_init (GdaLdapConnectionClass *klass);
static void gda_ldap_connection_init       (GdaLdapConnection *cnc, GdaLdapConnectionClass *klass);
static void gda_ldap_connection_dispose    (GObject *object);
static void gda_ldap_connection_set_property (GObject *object,
					      guint param_id,
					      const GValue *value,
					      GParamSpec *pspec);
static void gda_ldap_connection_get_property (GObject *object,
					      guint param_id,
					      GValue *value,
					      GParamSpec *pspec);

static GObjectClass *parent_class = NULL;

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

static void
update_connection_startup_file (GdaLdapConnection *cnc)
{
	if (! cnc->priv->startup_file || cnc->priv->loading_startup_file)
		return;

	GSList *list;
	GString *string = NULL;
	GError *lerror = NULL;

	string = g_string_new ("");
	for (list = cnc->priv->maps; list; list = list->next) {
		LdapTableMap *map = (LdapTableMap*) list->data;
		g_string_append_printf (string, "CREATE LDAP TABLE %s ", map->table_name);
		if (map->base_dn)
			g_string_append_printf (string, "BASE='%s' ", map->base_dn);
		if (map->filter)
			g_string_append_printf (string, "FILTER='%s' ", map->filter);
		if (map->attributes)
			g_string_append_printf (string, "ATTRIBUTES='%s' ", map->attributes);
		g_string_append (string, "SCOPE=");
		switch (map->scope) {
		case GDA_LDAP_SEARCH_BASE:
			g_string_append (string, "'BASE';\n");
			break;
		case GDA_LDAP_SEARCH_ONELEVEL:
			g_string_append (string, "'ONELEVEL';\n");
			break;
		case GDA_LDAP_SEARCH_SUBTREE:
			g_string_append (string, "'SUBTREE';\n");
			break;
		default:
			g_assert_not_reached ();
		}
	}
	if (! g_file_set_contents (cnc->priv->startup_file, string->str, -1, &lerror)) {
		GdaConnectionEvent *event;
		gchar *msg;
		event = gda_connection_point_available_event (GDA_CONNECTION (cnc),
							      GDA_CONNECTION_EVENT_WARNING);
		msg = g_strdup_printf (_("Error storing list of created LDAP tables: %s"),
				       lerror && lerror->message ? lerror->message : _("No detail"));
		gda_connection_event_set_description (event, msg);
		gda_connection_add_event (GDA_CONNECTION (cnc), event);
		g_free (msg);
		g_clear_error (&lerror);
	}
}

#ifdef GDA_DEBUG_NO
static void
dump_vtables (GdaLdapConnection *cnc)
{
	GSList *list;
	g_print ("LDAP tables: %d\n", g_slist_length (cnc->priv->maps));
	for (list = cnc->priv->maps; list; list = list->next) {
		LdapTableMap *map = (LdapTableMap*) list->data;
		g_print ("    LDAP Vtable: %s (map %p)\n", map->table_name, map);
	}
}
#endif

static void
vtable_created (GdaVconnectionDataModel *cnc, const gchar *table_name)
{
#ifdef GDA_DEBUG_NO
	g_print ("VTable created: %s\n", table_name);
	dump_vtables (GDA_LDAP_CONNECTION (cnc));
#endif
	if (GDA_VCONNECTION_DATA_MODEL_CLASS (parent_class)->vtable_created)
		GDA_VCONNECTION_DATA_MODEL_CLASS (parent_class)->vtable_created (cnc, table_name);
	update_connection_startup_file (GDA_LDAP_CONNECTION (cnc));
}

static void
vtable_dropped (GdaVconnectionDataModel *cnc, const gchar *table_name)
{
	GdaLdapConnection *lcnc;
	LdapTableMap *map = NULL;
	GSList *list;

	lcnc = GDA_LDAP_CONNECTION (cnc);
	for (list = lcnc->priv->maps; list; list = list->next) {
		if (!strcmp (((LdapTableMap*)list->data)->table_name, table_name)) {
			map = (LdapTableMap*)list->data;
			break;
		}
	}
	if (map) {
		lcnc->priv->maps = g_slist_remove (lcnc->priv->maps, map);
#ifdef GDA_DEBUG_NO
		g_print ("VTable dropped: %s\n", table_name);
		dump_vtables (lcnc);
#endif
	}
	if (GDA_VCONNECTION_DATA_MODEL_CLASS (parent_class)->vtable_dropped)
		GDA_VCONNECTION_DATA_MODEL_CLASS (parent_class)->vtable_dropped (cnc, table_name);
	update_connection_startup_file (GDA_LDAP_CONNECTION (cnc));
}

/*
 * GdaLdapConnection class implementation
 */
static void
gda_ldap_connection_class_init (GdaLdapConnectionClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	parent_class = g_type_class_peek_parent (klass);
	object_class->dispose = gda_ldap_connection_dispose;
	GDA_VCONNECTION_DATA_MODEL_CLASS (klass)->vtable_created = vtable_created;
	GDA_VCONNECTION_DATA_MODEL_CLASS (klass)->vtable_dropped = vtable_dropped;

	/* Properties */
        object_class->set_property = gda_ldap_connection_set_property;
        object_class->get_property = gda_ldap_connection_get_property;
	g_object_class_install_property (object_class, PROP_STARTUP_FILE,
                                         g_param_spec_string ("startup-file", NULL, _("File used to store startup data"), NULL,
                                                              (G_PARAM_READABLE | G_PARAM_WRITABLE)));
}

static void
dsn_set_cb (GdaLdapConnection *cnc, G_GNUC_UNUSED GParamSpec *pspec, G_GNUC_UNUSED gpointer data)
{
	gchar *fname, *tmp, *dsn;
	g_object_get (cnc, "dsn", &dsn, NULL);
	tmp = g_strdup_printf ("ldap-%s.start", dsn);
	g_free (dsn);
	fname = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
			      "libgda", tmp, NULL);
	g_free (tmp);
	g_free (cnc->priv->startup_file);
	cnc->priv->startup_file = fname;
}

static void
conn_opened_cb (GdaLdapConnection *cnc, G_GNUC_UNUSED gpointer data)
{
	if (!cnc->priv->startup_file)
		return;

	cnc->priv->loading_startup_file = TRUE;

	GdaSqlParser *parser;
	GdaBatch *batch;
	GError *lerror = NULL;
	parser = gda_connection_create_parser (GDA_CONNECTION (cnc));
	if (!parser)
		parser = gda_sql_parser_new ();
	batch = gda_sql_parser_parse_file_as_batch (parser, cnc->priv->startup_file, &lerror);
	if (batch) {
		GSList *list;
		list = gda_connection_batch_execute (GDA_CONNECTION (cnc), batch, NULL, 0, &lerror);
		g_slist_foreach (list, (GFunc) g_object_unref, NULL);
		g_slist_free (list);
		g_object_unref (batch);
	}
	if (lerror) {
		GdaConnectionEvent *event;
		gchar *msg;
		event = gda_connection_point_available_event (GDA_CONNECTION (cnc),
							      GDA_CONNECTION_EVENT_WARNING);
		msg = g_strdup_printf (_("Error recreating LDAP tables: %s"),
				       lerror && lerror->message ? lerror->message : _("No detail"));
		gda_connection_event_set_description (event, msg);
		gda_connection_add_event (GDA_CONNECTION (cnc), event);
		g_free (msg);
		g_clear_error (&lerror);
	}
	g_object_unref (parser);

	cnc->priv->loading_startup_file = FALSE;
}

static void
gda_ldap_connection_init (GdaLdapConnection *cnc, G_GNUC_UNUSED GdaLdapConnectionClass *klass)
{
	cnc->priv = g_new (GdaLdapConnectionPrivate, 1);
	cnc->priv->maps = NULL;
	cnc->priv->startup_file = NULL;
	cnc->priv->loading_startup_file = FALSE;

	g_signal_connect (cnc, "notify::dsn",
			  G_CALLBACK (dsn_set_cb), NULL);
	g_signal_connect (cnc, "conn-opened",
			  G_CALLBACK (conn_opened_cb), NULL);
}

static void
gda_ldap_connection_dispose (GObject *object)
{
	GdaLdapConnection *cnc = (GdaLdapConnection *) object;

	g_return_if_fail (GDA_IS_LDAP_CONNECTION (cnc));

	/* free memory */
	if (cnc->priv) {
		if (cnc->priv->maps)
			g_slist_free (cnc->priv->maps);
		g_free (cnc->priv->startup_file);
		g_free (cnc->priv);
		cnc->priv = NULL;
	}

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

GType
gda_ldap_connection_get_type (void)
{
	static GType type = 0;

	if (G_UNLIKELY (type == 0)) {
		static GMutex registering;
		if (type == 0) {
			static GTypeInfo info = {
				sizeof (GdaLdapConnectionClass),
				(GBaseInitFunc) NULL,
				(GBaseFinalizeFunc) NULL,
				(GClassInitFunc) gda_ldap_connection_class_init,
				NULL, NULL,
				sizeof (GdaLdapConnection),
				0,
				(GInstanceInitFunc) gda_ldap_connection_init,
				0
			};
			
		g_mutex_lock (&registering);
		if (type == 0)
			type = g_type_register_static (GDA_TYPE_VCONNECTION_DATA_MODEL, "GdaLdapConnection", &info, 0);
		g_mutex_unlock (&registering);
		}
	}

	return type;
}

static void
gda_ldap_connection_set_property (GObject *object,
				  guint param_id,
				  const GValue *value,
				  GParamSpec *pspec)
{
        GdaLdapConnection *cnc;
        cnc = GDA_LDAP_CONNECTION (object);
        if (cnc->priv) {
                switch (param_id) {
                case PROP_STARTUP_FILE: {
			if (cnc->priv->startup_file) {
				/* don't override any preexisting setting from a DSN */
				gchar *dsn;
				g_object_get (cnc, "dsn", &dsn, NULL);
				if (dsn)
					g_free (dsn);
				else {
					g_free (cnc->priv->startup_file);
					cnc->priv->startup_file = NULL;
				}
			}
			if (! cnc->priv->startup_file) {
				if (g_value_get_string (value))
					cnc->priv->startup_file = g_value_dup_string (value);
			}
			break;
		}
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
                        break;
                }
	}
}

static void
gda_ldap_connection_get_property (GObject *object,
				  guint param_id,
				  GValue *value,
				  GParamSpec *pspec)
{
	GdaLdapConnection *cnc;
        cnc = GDA_LDAP_CONNECTION (object);
        if (cnc->priv) {
                switch (param_id) {
                case PROP_STARTUP_FILE:
			g_value_set_string (value, cnc->priv->startup_file);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
                        break;
                }
	}	
}

static GList *
_ldap_create_columns_func (GdaVconnectionDataModelSpec *spec, G_GNUC_UNUSED GError **error)
{
	LdapTableMap *map = (LdapTableMap *) spec;
	if (!map->columns)
		map->columns = gda_data_model_ldap_compute_columns (map->ldap_cnc, map->attributes);
	g_list_foreach (map->columns, (GFunc) g_object_ref, NULL);
	return g_list_copy (map->columns);
}

static gchar *
make_string_for_filter (GdaVconnectionDataModelFilter *info)
{
	GString *string;
	gint i;

	string = g_string_new ("");
	for (i = 0; i < info->nConstraint; i++) {
		const struct GdaVirtualConstraint *cons;
		cons = &(info->aConstraint [i]);
		g_string_append_printf (string, "|%d,%d", cons->iColumn, cons->op);
	}
	return g_string_free (string, FALSE);
}

typedef struct {
	gint dn_constindex; /* constraint number to set the DN from when actually executing the LDAP search, or -1 */
	gchar *ldap_filter; /* in LDAP format, with @xxx@ to bind values */
	struct GdaVirtualConstraintUsage *out_const;
} ComputedFilter;

static void
computed_filter_free (ComputedFilter *filter)
{
	g_free (filter->out_const);
	g_free (filter->ldap_filter);
	g_free (filter);
}

#define MARKER_ESCAPE_CHAR 1
#define MARKER_GLOB_CHAR 2
static void
_ldap_table_create_filter (GdaVconnectionDataModelSpec *spec, GdaVconnectionDataModelFilter *info)
{
	/*
	 * REM:
	 *   - LDAP does not allow filtering on the DN => filter only is some cases
	 *   - LDAP does not handle ordering of results => the 'ORDER BY' constraint is ignored
	 *   - the '>' and '<' operators are not allowed in search strings => using '>=' and '<=' and
	 *     have SQLite do the actual check
	 */
	LdapTableMap *map = (LdapTableMap *) spec;
	GString *filter_string = NULL;
	gint i, ncols;
	gint dn_constindex = -1;
	gchar *hash;
	
	info->orderByConsumed = FALSE;
	hash = make_string_for_filter (info);
	if (map->filters_hash) {
		ComputedFilter *filter;
		filter = g_hash_table_lookup (map->filters_hash, hash);
		if (filter) {
			info->idxPointer = (gpointer) filter;
			info->orderByConsumed = FALSE;
			memcpy (info->aConstraintUsage,
				filter->out_const,
				sizeof (struct GdaVirtualConstraintUsage) * info->nConstraint);
			/*g_print ("Reusing filter %p, hash=[%s]\n", filter, hash);*/
			g_free (hash);
			return;
		}
	}

	if (!map->columns)
		map->columns = gda_data_model_ldap_compute_columns (map->ldap_cnc, map->attributes);

	ncols = g_list_length (map->columns);
	for (i = 0; i < info->nConstraint; i++) {
		const struct GdaVirtualConstraint *cons;
		cons = &(info->aConstraint [i]);
		const gchar *attrname;

		info->aConstraintUsage[i].argvIndex = i+1;
		info->aConstraintUsage[i].omit = TRUE;

		if (cons->iColumn < 0) {
			g_warning ("Internal error: negative column number!");
			goto nofilter;
		}
		if (cons->iColumn >= ncols) {
			g_warning ("Internal error: SQLite's virtual table column %d is not known for "
				   "table '%s', which has %d column(s)", cons->iColumn, map->table_name,
				   ncols);
			goto nofilter;
		}
		if (cons->iColumn == 0) {
			/* try to optimize on the DN column */
			if ((map->scope == GDA_LDAP_SEARCH_BASE) ||
			    (map->scope == GDA_LDAP_SEARCH_ONELEVEL))
				goto nofilter;
			if (cons->op != GDA_SQL_OPERATOR_TYPE_EQ)
				goto nofilter;
			if (dn_constindex != -1) /* DN is already filtered by a constraint */
				goto nofilter;
			dn_constindex = i;
			continue;
		}

		attrname = gda_column_get_name (GDA_COLUMN (g_list_nth_data (map->columns, cons->iColumn)));
		if (! filter_string) {
			if ((info->nConstraint > 1) || map->filter)
				filter_string = g_string_new ("(& ");
			else
				filter_string = g_string_new ("");
			if (map->filter)
				g_string_append (filter_string, map->filter);
		}
		switch (cons->op) {
		case GDA_SQL_OPERATOR_TYPE_EQ:
			g_string_append_printf (filter_string, "(%s=%c)", attrname, MARKER_ESCAPE_CHAR);
			break;
		case GDA_SQL_OPERATOR_TYPE_GT:
			g_string_append_printf (filter_string, "(%s>=%c)", attrname, MARKER_ESCAPE_CHAR);
			info->aConstraintUsage[i].omit = FALSE;
			break;
		case GDA_SQL_OPERATOR_TYPE_LEQ:
			g_string_append_printf (filter_string, "(%s<=%c)", attrname, MARKER_ESCAPE_CHAR);
			info->aConstraintUsage[i].omit = FALSE;
			break;
		case GDA_SQL_OPERATOR_TYPE_LT:
			g_string_append_printf (filter_string, "(%s<=%c)", attrname, MARKER_ESCAPE_CHAR);
			break;
		case GDA_SQL_OPERATOR_TYPE_GEQ:
			g_string_append_printf (filter_string, "(%s>=%c)", attrname, MARKER_ESCAPE_CHAR);
			break;
		case GDA_SQL_OPERATOR_TYPE_REGEXP:
			g_string_append_printf (filter_string, "(%s=%c)", attrname, MARKER_GLOB_CHAR);
			break;
		default:
			/* Can't be done with LDAP */
			goto nofilter;
		}
	}

	if (!filter_string && (dn_constindex == -1))
		goto nofilter;

	if (filter_string && ((info->nConstraint > 1) || map->filter))
		g_string_append_c (filter_string, ')');
	/*g_print ("FILTER: [%s]\n", filter_string->str);*/
	
	ComputedFilter *filter;
	filter = g_new0 (ComputedFilter, 1);
	filter->dn_constindex = dn_constindex;
	if (filter_string)
		filter->ldap_filter = g_string_free (filter_string, FALSE);
	else if (map->filter)
		filter->ldap_filter = g_strdup (map->filter);
	filter->out_const = g_new (struct GdaVirtualConstraintUsage,  info->nConstraint);
	memcpy (filter->out_const,
		info->aConstraintUsage,
		sizeof (struct GdaVirtualConstraintUsage) * info->nConstraint);
	
	info->idxPointer = (gpointer) filter;
	if (! map->filters_hash)
		map->filters_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
							   g_free,
							   (GDestroyNotify) computed_filter_free);
	g_hash_table_insert (map->filters_hash, hash, filter);
	/*g_print ("There are now %d statements in store...\n", g_hash_table_size (map->filters_hash));*/
	return;

 nofilter:
	if (filter_string)
		g_string_free (filter_string, TRUE);
	for (i = 0; i < info->nConstraint; i++) {
		info->aConstraintUsage[i].argvIndex = 0;
		info->aConstraintUsage[i].omit = TRUE;
	}
	info->idxPointer = NULL;
}

static GdaDataModel *
_ldap_table_create_model_func (GdaVconnectionDataModelSpec *spec, G_GNUC_UNUSED int idxNum, const char *idxStr,
			       int argc, GValue **argv)
{
	LdapTableMap *map = (LdapTableMap *) spec;
	GdaDataModel *model;

	if (idxStr) {
		const gchar *ptr;
		gint pos;
		GString *real_filter = NULL;
		ComputedFilter *filter = (ComputedFilter *) idxStr;
		
		for (pos = 0, ptr = filter->ldap_filter ? filter->ldap_filter : ""; *ptr; ptr++) {
			if (pos == filter->dn_constindex)
				pos++; /* skip this constraint position */
			if (! real_filter)
				real_filter = g_string_new ("");
			if ((*ptr == MARKER_ESCAPE_CHAR) || (*ptr == MARKER_GLOB_CHAR)){
				gchar *str, *sptr;
				gchar marker;
				marker = *ptr;
				g_assert (pos < argc);
				str = gda_value_stringify (argv[pos]);
				for (sptr = str; *sptr; sptr++) {
					if ((*sptr == ')') || (*sptr == '(') || (*sptr == '\\') || (*sptr == '*'))
						break;
					if ((marker == MARKER_GLOB_CHAR) && (*sptr == '%'))
						break;
				}
				if (*sptr) {
					/* make the substitutions */
					GString *string;
					string = g_string_new ("");
					for (sptr = str; *sptr; sptr++) {
						if (*sptr == ')')
							g_string_append (string, "\\29");
						else if (*sptr == '(')
							g_string_append (string, "\\28");
						else if (*sptr == '\\')
							g_string_append (string, "\\5c");
						else if (*sptr == '*')
							g_string_append (string, "\\2a");
						else if ((marker == MARKER_GLOB_CHAR) && (*sptr == '%'))
							g_string_append_c (string, '*');
						else
							g_string_append_c (string, *sptr);
					}
					g_free (str);
					str = g_string_free (string, FALSE);
				}
				g_string_append (real_filter, str);
				g_free (str);
				pos++;
			}
			else
				g_string_append_c (real_filter, *ptr);
		}

		gchar *real_dn = NULL;
		GdaLdapSearchScope real_scope = map->scope;
		if (filter->dn_constindex != -1) {
			/* check that the DN is a child of the data model's base DN */
			const gchar *bdn;
			gchar *tmp;
			bdn = map->base_dn;
			if (!bdn)
				bdn = gda_ldap_connection_get_base_dn (GDA_LDAP_CONNECTION (map->ldap_cnc));
			g_assert (bdn);
			tmp = gda_value_stringify (argv[filter->dn_constindex]);
			if (g_str_has_suffix (tmp, bdn)) {
				real_scope = GDA_LDAP_SEARCH_BASE;
				real_dn = gda_value_stringify (argv[filter->dn_constindex]);
			}
			else {
				/* return empty set */
				if (real_filter)
					g_string_free (real_filter, TRUE);
				real_filter = g_string_new ("(objectClass=)");
			}
			g_free (tmp);
		}
			
		/*g_print ("FILTER to use: LDAPFilter=> [%s] LDAPDn => [%s] SCOPE => [%d]\n",
			 real_filter ? real_filter->str : NULL,
			 real_dn ? real_dn : map->base_dn, real_scope);*/
		model = gda_data_model_ldap_new (map->ldap_cnc,
						 real_dn ? real_dn : map->base_dn,
						 real_filter ? real_filter->str : NULL,
						 map->attributes, real_scope);
		if (real_filter)
			g_string_free (real_filter, TRUE);
		g_free (real_dn);
	}
	else
		model = gda_data_model_ldap_new (map->ldap_cnc, map->base_dn, map->filter,
						 map->attributes, map->scope);

	return model;
}

/**
 * gda_ldap_connection_declare_table:
 * @cnc: a #GdaLdapConnection
 * @table_name: a table name, not %NULL
 * @base_dn: (allow-none): the base DN of the LDAP search, or %NULL for @cnc's own base DN
 * @filter: (allow-none): the search filter of the LDAP search, or %NULL for a default filter of "(ObjectClass=*)"
 * @attributes: (allow-none): the search attributes of the LDAP search, or %NULL if only the DN is required
 * @scope: the search scope of the LDAP search
 * @error: a place to store errors, or %NULL
 *
 * Declare a virtual table based on an LDAP search.
 *
 * The @filter argument, if not %NULL, must be a valid LDAP filter string (including the opening and
 * closing parenthesis).
 *
 * The @attribute, if not %NULL, is a list of comma separated LDAP entry attribute names. For each attribute
 * it is also possible to specify a requested #GType, and how to behave in case of multi valued attributes
 * So the general format for an attribute is:
 * "&lt;attribute name&gt;[::&lt;type&gt;][::&lt;muti value handler&gt;]", where:
 * <itemizedlist>
 * <listitem><para>"::&lt;type&gt;" is optional, see gda_g_type_from_string() for more
 *     information about valie values for &lt;type&gt;</para></listitem>
 * <listitem><para>"::&lt;muti value handler&gt;" is also optional and specifies how a multi
 *    values attributed is treated. The possibilities for &lt;muti value handler&gt; are:
 *    <itemizedlist>
 *         <listitem><para>"NULL" or "0": a NULL value will be returned</para></listitem>
 *         <listitem><para>"CSV": a comma separated value with all the values of the attribute will be
 *           returned. This only works for G_TYPE_STRING attribute types.</para></listitem>
 *         <listitem><para>"MULT" of "*": a row will be returned for each value of the attribute, effectively
 *           multiplying the number of returned rows</para></listitem>
 *         <listitem><para>"1": only the first vakue of the attribute will be used, the other values ignored</para></listitem>
 *         <listitem><para>"CONCAT": the attributes' values are concatenated (with a newline char between each value)</para></listitem>
 *         <listitem><para>"ERROR": an error value will be returned (this is the default behaviour)</para></listitem>
 *    </itemizedlist>
 * </para></listitem>
 * </itemizedlist>
 *
 *  After each attribute
 * name (and before the comma for the next attribute if any), it is possible to specify the #GType type using
 * the "::&lt;type&gt;" syntax (see gda_g_type_from_string() for more information). 
 *
 * The following example specifies the "uidNumber" attribute to be returned as a string, the "mail" attribute,
 * and the "objectClass" attribute to be handled as one row per value of that attribute:
 * <programlisting>"uidNumber::string,mail,objectClass::*"</programlisting>
 *
 * Returns: %TRUE if no error occurred
 *
 * Since: 4.2.8
 */
gboolean
gda_ldap_connection_declare_table (GdaLdapConnection *cnc, const gchar *table_name,
				   const gchar *base_dn, const gchar *filter,
				   const gchar *attributes, GdaLdapSearchScope scope,
				   GError **error)
{
	LdapTableMap *map;
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), FALSE);
	g_return_val_if_fail (table_name && *table_name, FALSE);
	
	map = g_new0 (LdapTableMap, 1);
	GDA_VCONNECTION_DATA_MODEL_SPEC (map)->data_model = NULL;
        GDA_VCONNECTION_DATA_MODEL_SPEC (map)->create_columns_func = (GdaVconnectionDataModelCreateColumnsFunc) _ldap_create_columns_func;
        GDA_VCONNECTION_DATA_MODEL_SPEC (map)->create_model_func = NULL;
        GDA_VCONNECTION_DATA_MODEL_SPEC (map)->create_filter_func = _ldap_table_create_filter;
        GDA_VCONNECTION_DATA_MODEL_SPEC (map)->create_filtered_model_func = _ldap_table_create_model_func;
	map->ldap_cnc = g_object_ref (cnc);
	map->table_name = gda_sql_identifier_quote (table_name, GDA_CONNECTION (cnc), NULL, TRUE, FALSE);
	map->filters_hash = NULL;
	if (base_dn)
		map->base_dn = g_strdup (base_dn);
	if (filter)
		map->filter = g_strdup (filter);
	if (attributes)
		map->attributes = g_strdup (attributes);
	map->scope = scope ? scope : GDA_LDAP_SEARCH_BASE;

	cnc->priv->maps = g_slist_append (cnc->priv->maps, map);	
	if (!gda_vconnection_data_model_add (GDA_VCONNECTION_DATA_MODEL (cnc),
					     (GdaVconnectionDataModelSpec*) map,
                                             (GDestroyNotify) _ldap_table_map_free, table_name, error)) {
		cnc->priv->maps = g_slist_remove (cnc->priv->maps, map);
                return FALSE;
	}

	return TRUE;
}

/**
 * gda_ldap_connection_undeclare_table:
 * @cnc: a #GdaLdapConnection
 * @table_name: a table name, not %NULL
 * @error: a place to store errors, or %NULL
 *
 * Remove a table which has been declared using gda_ldap_connection_declare_table().
 *
 * Returns: %TRUE if no error occurred
 *
 * Since: 4.2.8
 */
gboolean
gda_ldap_connection_undeclare_table (GdaLdapConnection *cnc, const gchar *table_name, GError **error)
{
	GdaVconnectionDataModelSpec *specs;
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), FALSE);
	g_return_val_if_fail (table_name && *table_name, FALSE);

	specs =  gda_vconnection_data_model_get (GDA_VCONNECTION_DATA_MODEL (cnc), table_name);
	if (specs && ! g_slist_find (cnc->priv->maps, specs)) {
		g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
			     GDA_SERVER_PROVIDER_MISUSE_ERROR,
			     "%s", _("Can't remove non LDAP virtual table"));
		return FALSE;
	}
	return gda_vconnection_data_model_remove (GDA_VCONNECTION_DATA_MODEL (cnc), table_name, error);
}

/**
 * gda_ldap_connection_describe_table:
 * @cnc: a #GdaLdapConnection
 * @table_name: a table name, not %NULL
 * @out_base_dn: (allow-none) (transfer none): a place to store the LDAP search base DN, or %NULL
 * @out_filter: (allow-none) (transfer none): a place to store the LDAP search filter, or %NULL
 * @out_attributes: (allow-none) (transfer none): a place to store the LDAP search attributes, or %NULL
 * @out_scope: (allow-none) (transfer none): a place to store the LDAP search scope, or %NULL
 * @error: a place to store errors, or %NULL
 *
 * Get information about a virtual table, the information which has been passed to
 * gda_ldap_connection_declare_table() when the table was created.
 *
 * Returns: %TRUE if no error occurred
 *
 * Since: 4.2.8
 */
gboolean
gda_ldap_connection_describe_table (GdaLdapConnection *cnc, const gchar *table_name,
				    const gchar **out_base_dn, const gchar **out_filter,
				    const gchar **out_attributes,
				    GdaLdapSearchScope *out_scope, GError **error)
{
	GdaVconnectionDataModelSpec *specs;
	LdapTableMap *map;

	if (out_base_dn)
		*out_base_dn = NULL;
	if (out_filter)
		*out_filter = NULL;
	if (out_attributes)
		*out_attributes = NULL;
	if (out_scope)
		*out_scope = GDA_LDAP_SEARCH_BASE;

	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), FALSE);
	g_return_val_if_fail (table_name && *table_name, FALSE);

	specs =  gda_vconnection_data_model_get (GDA_VCONNECTION_DATA_MODEL (cnc), table_name);
	if (specs && ! g_slist_find (cnc->priv->maps, specs)) {
		g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
			     GDA_SERVER_PROVIDER_MISUSE_ERROR,
			     "%s", _("Can't describe non LDAP virtual table"));
		return FALSE;
	}
	
	if (!specs) {
		g_set_error (error, GDA_SERVER_PROVIDER_ERROR,
			     GDA_SERVER_PROVIDER_MISUSE_ERROR,
			     "%s", _("Unknown LDAP virtual table"));
		return FALSE;
	}

	map = (LdapTableMap*) specs;
	if (out_base_dn)
		*out_base_dn = map->base_dn;
	if (out_filter)
		*out_filter = map->filter;
	if (out_attributes)
		*out_attributes = map->attributes;
	if (out_scope)
		*out_scope = map->scope;
	return TRUE;
}

static void
gda_ldap_attribute_free (GdaLdapAttribute *attr)
{
	if (attr) {
		gint i;
		g_free (attr->attr_name);
		for (i = 0; attr->values[i]; i++)
			gda_value_free (attr->values[i]);
		g_free (attr->values);
	}
}

/**
 * gda_ldap_entry_free:
 * @entry: (transfer full): a #GdaLdapEntry pointer
 *
 * Frees @entry
 *
 * Since: 4.2.8
 */
void
gda_ldap_entry_free (GdaLdapEntry *entry)
{
	if (entry) {
		g_free (entry->dn);
		if (entry->attributes) {
			gint i;
			for (i = 0; entry->attributes[i]; i++)
				gda_ldap_attribute_free (entry->attributes[i]);
			g_free (entry->attributes);
		}
		if (entry->attributes_hash)
			g_hash_table_destroy (entry->attributes_hash);
		g_free (entry);
	}
}

/**
 * gda_ldap_entry_new:
 * @dn: (allow-none): a Distinguished name, or %NULL
 *
 * Creates a new #GdaLdapEntry. This function is useful when using gda_ldap_modify_entry()
 *
 * Returns: a new #GdaLdapEntry
 *
 * Since: 5.2.0
 */
GdaLdapEntry *
gda_ldap_entry_new (const gchar *dn)
{
	GdaLdapEntry *entry;
	entry = g_new0 (GdaLdapEntry, 1);
	if (dn)
		entry->dn = g_strdup (dn);
	entry->attributes_hash = g_hash_table_new (g_str_hash, g_str_equal);
	entry->nb_attributes = 0;
	entry->attributes = g_new0 (GdaLdapAttribute*, 1);
	return entry;
}

/**
 * gda_ldap_entry_add_attribute:
 * @entry: a #GdaLdapEntry pointer
 * @merge: set to %TRUE to merge the values in case of an existing attribute in @entry, and %FALSE to replace any existing attribute's values in @entry
 * @attr_name: the name of the attribute to add
 * @nb_values: number of values in @values
 * @values: (array length=nb_values): an array of #GValue (as much values as specified by @nb_values)
 *
 * Add an attribute (ans its values) to @entry. If the attribute is already present in @entry,
 * then the attribute's values are merged or replaced depending on the @merge argument.
 *
 * Since: 5.2.0
 */
void
gda_ldap_entry_add_attribute (GdaLdapEntry *entry, gboolean merge, const gchar *attr_name,
			      guint nb_values, GValue **values)
{
	guint i;
	g_return_if_fail (entry);
	g_return_if_fail (nb_values > 0);
	g_return_if_fail (values);
	g_return_if_fail (attr_name && *attr_name);

	GdaLdapAttribute *att = NULL;
	GdaLdapAttribute *natt = NULL;
	guint replace_pos = G_MAXUINT;

	if (entry->attributes_hash)
		att = g_hash_table_lookup (entry->attributes_hash, attr_name);
	else
		entry->attributes_hash = g_hash_table_new (g_str_hash, g_str_equal);

	if (att) {
		if (merge) {
			TO_IMPLEMENT;
			return;
		}
		else {
			/* get rid of @attr */
			g_hash_table_remove (entry->attributes_hash, att->attr_name);
			for (i = 0; i < entry->nb_attributes; i++) {
				if (entry->attributes [i] == att) {
					replace_pos = i;
					entry->attributes [i] = NULL;
					break;
				}
			}
			gda_ldap_attribute_free (att);
		}
	}

	natt = g_new0 (GdaLdapAttribute, 1);
	natt->attr_name = g_strdup (attr_name);
	natt->nb_values = nb_values;
	if (natt->nb_values > 0) {
		natt->values = g_new0 (GValue *, natt->nb_values + 1);
		for (i = 0; i < natt->nb_values; i++) {
			if (values [i])
				natt->values [i] = gda_value_copy (values [i]);
			else
				natt->values [i] = NULL;
		}
	}
	g_hash_table_insert (entry->attributes_hash, natt->attr_name, natt);
	if (replace_pos != G_MAXUINT)
		entry->attributes [replace_pos] = natt;
	else {
		entry->nb_attributes++;
		entry->attributes = g_renew (GdaLdapAttribute*, entry->attributes, entry->nb_attributes + 1);
		entry->attributes [entry->nb_attributes - 1] = natt;
		entry->attributes [entry->nb_attributes] = NULL;
	}
}


/* proxy declaration */
GdaLdapEntry *_gda_ldap_describe_entry (GdaLdapConnection *cnc, const gchar *dn, GError **error);
GdaLdapEntry **_gda_ldap_get_entry_children (GdaLdapConnection *cnc, const gchar *dn, gchar **attributes, GError **error);

/**
 * gda_ldap_describe_entry:
 * @cnc: a #GdaLdapConnection connection
 * @dn: (allow-none): the Distinguished Name of the LDAP entry to describe
 * @error: (allow-none): a place to store errors, or %NULL
 *
 * Describes the LDAP entry which DN is @dn. If @dn is %NULL, then the top entry (as specified when 
 * the LDAP connection was opened) is described.
 *
 * Returns: (transfer full) (Free-function: gda_ldap_entry_free): a new #GdaLdapEntry, or %NULL if an error occurred or if the @dn entry does not exist
 *
 * Since: 4.2.8
 */
GdaLdapEntry *
gda_ldap_describe_entry (GdaLdapConnection *cnc, const gchar *dn, GError **error)
{
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), FALSE);

	return _gda_ldap_describe_entry (cnc, dn, error);
}

/**
 * gda_ldap_get_entry_children:
 * @cnc: a #GdaLdapConnection connection
 * @dn: (allow-none): the Distinguished Name of the LDAP entry to get children from
 * @attributes: (allow-none) (array zero-terminated=1) (element-type gchar*): a %NULL terminated array of attributes to fetch for each child, or %NULL if no attribute is requested
 * @error: (allow-none): a place to store errors, or %NULL
 *
 * Get the list of children entries for the LDAP entry which DN is @dn. If the @dn entry does not have any
 * child, then this function returns an array which first element is %NULL.
 *
 * If @dn is %NULL, then the top entry (as specified when the LDAP connection was opened) is used.
 *
 * Returns: (transfer full) (element_type GdaLdapEntry) (array zero-terminated=1): a %NULL terminated array of #GdaLdapEntry for each child entry, or %NULL if an error occurred or if the @dn entry does not exist
 *
 * Since: 4.2.8
 */
GdaLdapEntry **
gda_ldap_get_entry_children (GdaLdapConnection *cnc, const gchar *dn, gchar **attributes, GError **error)
{
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), FALSE);

	return _gda_ldap_get_entry_children (cnc, dn, attributes, error);
}

gchar **_gda_ldap_dn_split (const gchar *dn, gboolean all);

/**
 * gda_ldap_dn_split
 * @dn: a Distinguished Name string
 * @all: set to %FALSE to split @dn into its RND and its parent DN, or %TRUE to completely split @dn
 *
 * Splits @dn into its components.
 *
 * Returns: (transfer full) (Free-function: g_strfreev): a %NULL terminated array containing the DN parts (free using g_strfreev()), or %NULL if an error occurred because @dn is not a valid DN expression
 *
 * Since: 4.2.8
 */
gchar **
gda_ldap_dn_split (const gchar *dn, gboolean all)
{
	return _gda_ldap_dn_split (dn, all);
}

gboolean _gda_ldap_is_dn (const gchar *dn);

/**
 * gda_ldap_is_dn:
 * @dn: a Distinguished Name string
 *
 * Tells if @dn represents a distinguished name (it only checks for the syntax, not for
 * the actual existence of the entry with that distinguished name).
 *
 * Returns: %TRUE if @dn is a valid representation of a distinguished name
 *
 * Since: 4.2.8
 */
gboolean
gda_ldap_is_dn (const gchar *dn)
{
	return _gda_ldap_is_dn (dn);
}

const gchar *_gda_ldap_get_base_dn (GdaLdapConnection *cnc);

/**
 * gda_ldap_connection_get_base_dn:
 * @cnc: a #GdaLdapConnection
 *
 * Get the base DN which was used when the LDAP connection was opened
 *
 * Returns: the base DN, or %NULL
 *
 * Since: 4.2.8
 */
const gchar *
gda_ldap_connection_get_base_dn (GdaLdapConnection *cnc)
{
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), NULL);

	return _gda_ldap_get_base_dn (cnc);
}

GdaLdapClass *_gda_ldap_get_class_info (GdaLdapConnection *cnc, const gchar *classname);
/**
 * gda_ldap_get_class_info:
 * @cnc: a #GdaLdapConnection
 * @classname: an LDAP class name
 *
 * Get information about an LDAP class
 *
 * Returns: (transfer none): a #GdaLdapClass
 *
 * Since: 4.2.8
 */
GdaLdapClass*
gda_ldap_get_class_info (GdaLdapConnection *cnc, const gchar *classname)
{
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), NULL);

	return _gda_ldap_get_class_info (cnc, classname);
}

const GSList *_gda_ldap_get_top_classes (GdaLdapConnection *cnc);
/**
 * gda_ldap_get_top_classes:
 * @cnc: a #GdaLdapConnection
 *
 * get a list of the top level LDAP classes (ie. classes which don't have any parent)
 *
 * Returns: (transfer none) (element-type GdaLdapClass): a list of #GdaLdapClass pointers (don't modify it)
 *
 * Since: 4.2.8
 */
const GSList *
gda_ldap_get_top_classes (GdaLdapConnection *cnc)
{
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), NULL);

	return _gda_ldap_get_top_classes (cnc);
}

GSList *_gda_ldap_entry_get_attributes_list (GdaLdapConnection *cnc, GdaLdapEntry *entry, GdaLdapAttribute *object_class_attr);
/**
 * gda_ldap_entry_get_attributes_list:
 * @cnc: a #GdaLdapConnection
 * @entry: a #GdaLdapEntry
 *
 * Get a list of all the possible attributes which @entry can have. Each possible attribute is represented
 * by a #GdaLdapAttributeDefinition strunture.
 *
 * Returns: (transfer full) (element-type GdaLdapAttributeDefinition): a #GSList of #GdaLdapAttributeDefinition pointers, free the list using gda_ldap_attributes_list_free()
 *
 * Since: 5.2.0
 */
GSList *
gda_ldap_entry_get_attributes_list (GdaLdapConnection *cnc, GdaLdapEntry *entry)
{
	g_return_val_if_fail (entry, NULL);

	return _gda_ldap_entry_get_attributes_list (cnc, entry, NULL);
}

/**
 * gda_ldap_attributes_list_free:
 * @list: (allow-none): a #GSList of #GdaLdapAttributeDefinition pointers, or %NULL
 *
 * Frees the list returned by gda_ldap_entry_get_attributes_list().
 *
 * Since: 5.2.0
 */
void
gda_ldap_attributes_list_free (GSList *list)
{
	GSList *l;
	if (!list)
		return;
	for (l = list; l; l = l->next) {
		GdaLdapAttributeDefinition *def;
		def = (GdaLdapAttributeDefinition*) l->data;
		if (def) {
			g_free (def->name);
			g_free (def);
		}
	}
	g_slist_free (list);
}


gboolean
_gda_ldap_modify (GdaLdapConnection *cnc, GdaLdapModificationType modtype,
		  GdaLdapEntry *entry, GdaLdapEntry *ref_entry, GError **error);

/**
 * gda_ldap_add_entry:
 * @cnc: a #GdaLdapConnection
 * @entry: a #GdaLDapEntry describing the LDAP entry to add
 * @error: (allow-none): a place to store an error, or %NULL
 *
 * Creates a new LDAP entry.
 *
 * Returns: %TRUE if no error occurred
 *
 * Since: 5.2.0
 */
gboolean
gda_ldap_add_entry (GdaLdapConnection *cnc, GdaLdapEntry *entry, GError **error)
{
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), FALSE);
	g_return_val_if_fail (entry, FALSE);
	g_return_val_if_fail (entry->dn && *(entry->dn), FALSE);

	return _gda_ldap_modify (cnc, GDA_LDAP_MODIFICATION_INSERT, entry, NULL, error);
}

/**
 * gda_ldap_remove_entry:
 * @cnc: a #GdaLdapConnection
 * @entry: a #GdaLDapEntry describing the LDAP entry to remove
 * @error: (allow-none): a place to store an error, or %NULL
 *
 * Delete an LDAP entry.
 *
 * Returns: %TRUE if no error occurred
 *
 * Since: 5.2.0
 */
gboolean
gda_ldap_remove_entry (GdaLdapConnection *cnc, const gchar *dn, GError **error)
{
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), FALSE);
	g_return_val_if_fail (dn && *dn, FALSE);

	GdaLdapEntry entry;
	memset (&entry, 0, sizeof (GdaLdapEntry));
	entry.dn = (gchar*) dn;

	return _gda_ldap_modify (cnc, GDA_LDAP_MODIFICATION_DELETE, &entry, NULL, error);
}

gboolean
_gda_ldap_rename_entry (GdaLdapConnection *cnc, const gchar *current_dn, const gchar *new_dn, GError **error);

/**
 * gda_ldap_rename_entry:
 * @cnc: a #GdaLdapConnection
 * @current_dn: the current DN of the entry
 * @new_dn: the new DN of the entry
 * @error: (allow-none): a place to store an error, or %NULL
 *
 * Renames an LDAP entry.
 *
 * Returns: %TRUE if no error occurred
 *
 * Since: 5.2.0
 */
gboolean
gda_ldap_rename_entry (GdaLdapConnection *cnc, const gchar *current_dn, const gchar *new_dn, GError **error)
{
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), FALSE);
	g_return_val_if_fail (current_dn && *current_dn, FALSE);
	g_return_val_if_fail (new_dn && *new_dn, FALSE);

	return _gda_ldap_rename_entry (cnc, current_dn, new_dn, error);
}

/**
 * gda_ldap_modify_entry:
 * @cnc: a #GdaLdapConnection
 * @modtype: the type of modification to perform
 * @entry: a #GdaLDapEntry describing the LDAP entry to apply modifications, along with the attributes which will be modified
 * @ref_entry: (allow-none): a #GdaLDapEntry describing the reference LDAP entry, if @modtype is %GDA_LDAP_MODIFICATION_ATTR_DIFF
 * @error: (allow-none): a place to store an error, or %NULL
 *
 * Modifies an LDAP entry.
 *
 * Returns: %TRUE if no error occurred
 *
 * Since: 5.2.0
 */
gboolean
gda_ldap_modify_entry (GdaLdapConnection *cnc, GdaLdapModificationType modtype,
		       GdaLdapEntry *entry, GdaLdapEntry *ref_entry, GError **error)
{
	g_return_val_if_fail (GDA_IS_LDAP_CONNECTION (cnc), FALSE);
	g_return_val_if_fail (entry, FALSE);
	g_return_val_if_fail (entry->dn && *(entry->dn), FALSE);

	return _gda_ldap_modify (cnc, modtype, entry, ref_entry, error);
}