2 * Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
4 * Redistribution and use in source and binary forms are permitted only
5 * as authorized by the OpenLDAP Public License. A copy of this
6 * license is available at http://www.OpenLDAP.org/license.html or
7 * in file LICENSE in the top-level directory of the distribution.
15 #include <sys/types.h>
20 #include "schema-map.h"
23 int backsql_dummy(void *,void *);
25 int backsql_cmp_oc_name(backsql_oc_map_rec *m1,backsql_oc_map_rec *m2)
27 return strcasecmp(m1->name,m2->name);
30 int backsql_cmp_oc_id(backsql_oc_map_rec *m1,backsql_oc_map_rec *m2)
39 int backsql_cmp_attr(backsql_at_map_rec *m1,backsql_at_map_rec *m2)
41 return strcasecmp(m1->name,m2->name);
44 char* backsql_make_attr_query(backsql_oc_map_rec *oc_map,backsql_at_map_rec *at_map)
50 tmps=backsql_strcat(tmps,&tmpslen,"SELECT ",at_map->sel_expr," AS ",at_map->name,
51 " FROM ",at_map->from_tbls,
52 " WHERE ",oc_map->keytbl,".",oc_map->keycol,"=?",NULL);
53 if (at_map->join_where!=NULL && at_map->join_where[0]!='\0')
54 tmps=backsql_strcat(tmps,&tmpslen," AND ",at_map->join_where,NULL);
55 at_map->query=ch_strdup(tmps);
61 int backsql_add_sysmaps(backsql_oc_map_rec *oc_map)
63 backsql_at_map_rec *at_map;
67 sprintf(s,"%d",oc_map->id);
68 at_map=(backsql_at_map_rec*)ch_calloc(1,sizeof(backsql_at_map_rec));
69 at_map->name=ch_strdup("objectClass");
70 at_map->sel_expr=ch_strdup("ldap_entry_objclasses.oc_name");
71 at_map->from_tbls=ch_strdup("ldap_entry_objclasses,ldap_entries");
72 len=strlen(at_map->from_tbls);
73 backsql_merge_from_clause(&at_map->from_tbls,&len,oc_map->keytbl);
74 at_map->join_where=NULL; len=0;
75 at_map->join_where=backsql_strcat(at_map->join_where,&len,
76 "ldap_entries.id=ldap_entry_objclasses.entry_id and ldap_entries.keyval=",
77 oc_map->keytbl,".",oc_map->keycol," and ldap_entries.oc_map_id=",s,NULL);
78 at_map->add_proc=NULL;
79 at_map->delete_proc=NULL;
80 at_map->param_order=0;
81 at_map->expect_return=0;
82 backsql_make_attr_query(oc_map,at_map);
83 avl_insert(&oc_map->attrs,at_map,(AVL_CMP)backsql_cmp_attr,backsql_dummy);
85 at_map=(backsql_at_map_rec*)ch_calloc(1,sizeof(backsql_at_map_rec));
86 at_map->name=ch_strdup("ref");
87 at_map->sel_expr=ch_strdup("ldap_referrals.url");
88 at_map->from_tbls=ch_strdup("ldap_referrals,ldap_entries");
89 len=strlen(at_map->from_tbls);
90 backsql_merge_from_clause(&at_map->from_tbls,&len,oc_map->keytbl);
91 at_map->join_where=NULL; len=0;
92 at_map->join_where=backsql_strcat(at_map->join_where,&len,
93 "ldap_entries.id=ldap_referrals.entry_id and ldap_entries.keyval=",
94 oc_map->keytbl,".",oc_map->keycol," and ldap_entries.oc_map_id=",s,NULL);
95 at_map->add_proc=NULL;
96 at_map->delete_proc=NULL;
97 at_map->param_order=0;
98 at_map->expect_return=0;
99 backsql_make_attr_query(oc_map,at_map);
100 avl_insert(&oc_map->attrs,at_map,(AVL_CMP)backsql_cmp_attr,backsql_dummy);
105 int backsql_load_schema_map(backsql_info *si,SQLHDBC dbh)
107 SQLHSTMT oc_sth,at_sth;
109 BACKSQL_ROW_NTS oc_row,at_row;
111 backsql_oc_map_rec *oc_map;
112 backsql_at_map_rec *at_map;
116 Debug(LDAP_DEBUG_TRACE,"==>load_schema_map()\n",0,0,0);
118 /* TimesTen : See if the ldap_entries.dn_ru field exists in the schema. */
120 rc = backsql_Prepare(dbh, &oc_sth, backsql_check_dn_ru_query, 0);
121 if (rc == SQL_SUCCESS) {
122 si->has_ldapinfo_dn_ru = 1; /* Yes, the field exists */
123 Debug(LDAP_DEBUG_TRACE, "ldapinfo.dn_ru field exists in the schema\n", 0, 0,0);
126 si->has_ldapinfo_dn_ru = 0; /* No such field exists */
129 SQLFreeStmt(oc_sth, SQL_DROP);
131 rc=backsql_Prepare(dbh,&oc_sth,si->oc_query,0);
132 if (rc != SQL_SUCCESS)
134 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error preparing oc_query: '%s'\n",si->oc_query,0,0);
135 backsql_PrintErrors(si->db_env,dbh,oc_sth,rc);
138 Debug(LDAP_DEBUG_TRACE, "load_schema_map(): at_query '%s'\n", si->at_query,0,0);
140 rc=backsql_Prepare(dbh,&at_sth,si->at_query,0);
141 if (rc != SQL_SUCCESS)
143 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error preparing at_query: '%s'\n",si->at_query,0,0);
144 backsql_PrintErrors(si->db_env,dbh,at_sth,rc);
147 if ((rc=backsql_BindParamID(at_sth,1,&oc_id)) != SQL_SUCCESS)
149 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error binding param for at_query: \n",0,0,0);
150 backsql_PrintErrors(si->db_env,dbh,at_sth,rc);
153 if ((rc=SQLExecute(oc_sth)) != SQL_SUCCESS)
155 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error executing oc_query: \n",0,0,0);
156 backsql_PrintErrors(si->db_env,dbh,oc_sth,rc);
159 backsql_BindRowAsStrings(oc_sth,&oc_row);
160 while ((rc=SQLFetch(oc_sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
162 oc_map=(backsql_oc_map_rec*)ch_calloc(1,sizeof(backsql_oc_map_rec));
163 oc_map->id=atoi(oc_row.cols[0]);
164 oc_map->name=ch_strdup(oc_row.cols[1]);
165 oc_map->keytbl=ch_strdup(oc_row.cols[2]);
166 oc_map->keycol=ch_strdup(oc_row.cols[3]);
167 oc_map->create_proc=(oc_row.is_null[4]<0)?NULL:ch_strdup(oc_row.cols[4]);
168 oc_map->delete_proc=(oc_row.is_null[5]<0)?NULL:ch_strdup(oc_row.cols[5]);
169 oc_map->expect_return=atoi(oc_row.cols[6]);
172 avl_insert(&si->oc_by_name,oc_map,(AVL_CMP)backsql_cmp_oc_name,backsql_dummy);
173 avl_insert(&si->oc_by_id,oc_map,(AVL_CMP)backsql_cmp_oc_id,backsql_dummy);
175 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): objectclass '%s': keytbl='%s' keycol='%s' ",
176 oc_map->name,oc_map->keytbl,oc_map->keycol);
177 if (oc_map->delete_proc) {
178 Debug(LDAP_DEBUG_TRACE,"delete_proc='%s'\n", oc_map->delete_proc, 0, 0);
180 if (oc_map->create_proc) {
181 Debug(LDAP_DEBUG_TRACE,"create_proc='%s'\n", oc_map->create_proc, 0, 0);
183 Debug(LDAP_DEBUG_TRACE,"expect_return=%d; attributes:\n",
184 oc_map->expect_return, 0, 0);
186 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): autoadding 'objectClass' and 'ref' mappings\n",0,0,0);
187 backsql_add_sysmaps(oc_map);
188 if ((rc=SQLExecute(at_sth)) != SQL_SUCCESS)
190 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error executing at_query: \n",0,0,0);
191 backsql_PrintErrors(SQL_NULL_HENV,dbh,at_sth,rc);
194 backsql_BindRowAsStrings(at_sth,&at_row);
195 while ((rc=SQLFetch(at_sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
197 Debug(LDAP_DEBUG_TRACE,"********'%s'\n",at_row.cols[0],0,0);
198 Debug(LDAP_DEBUG_TRACE,"name='%s',sel_expr='%s' from='%s' ",at_row.cols[0],
199 at_row.cols[1],at_row.cols[2]);
200 Debug(LDAP_DEBUG_TRACE,"join_where='%s',add_proc='%s' ",at_row.cols[3],
202 Debug(LDAP_DEBUG_TRACE,"delete_proc='%s'\n",at_row.cols[5],0,0);
203 Debug(LDAP_DEBUG_TRACE,"sel_expr_u='%s'\n", at_row.cols[8],0,0); /* TimesTen*/
204 at_map=(backsql_at_map_rec*)ch_calloc(1,sizeof(backsql_at_map_rec));
205 at_map->name=ch_strdup(at_row.cols[0]);
206 at_map->sel_expr=ch_strdup(at_row.cols[1]);
207 at_map->sel_expr_u = (at_row.is_null[8]<0)?NULL:ch_strdup(at_row.cols[8
210 backsql_merge_from_clause(&tmps,&tmpslen,at_row.cols[2]);
211 at_map->from_tbls=ch_strdup(tmps);
213 at_map->join_where=ch_strdup((at_row.is_null[3]<0)?"":at_row.cols[3]);
214 at_map->add_proc=(at_row.is_null[4]<0)?NULL:ch_strdup(at_row.cols[4]);
215 at_map->delete_proc=(at_row.is_null[5]<0)?NULL:ch_strdup(at_row.cols[5]);
216 at_map->param_order=atoi(at_row.cols[6]);
217 at_map->expect_return=atoi(at_row.cols[7]);
218 backsql_make_attr_query(oc_map,at_map);
219 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): preconstructed query '%s'\n",at_map->query,0,0);
220 avl_insert(&oc_map->attrs,at_map,(AVL_CMP)backsql_cmp_attr,backsql_dummy);
222 backsql_FreeRow(&at_row);
223 SQLFreeStmt(at_sth,SQL_CLOSE);
225 backsql_FreeRow(&oc_row);
226 SQLFreeStmt(at_sth,SQL_DROP);
227 SQLFreeStmt(oc_sth,SQL_DROP);
229 Debug(LDAP_DEBUG_TRACE,"<==load_schema_map()\n",0,0,0);
233 backsql_oc_map_rec* backsql_oc_with_name(backsql_info *si,char* objclass)
235 backsql_oc_map_rec tmp,*res;
237 /* Debug(LDAP_DEBUG_TRACE,"==>oc_with_name(): searching for objectclass with name='%s'\n",objclass,0,0);*/
239 res=(backsql_oc_map_rec*)avl_find(si->oc_by_name,&tmp,(AVL_CMP)backsql_cmp_oc_name);
241 Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): found name='%s', id=%d\n",res->name,res->id,0);
243 Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): not found\n",0,0,0);
248 backsql_oc_map_rec* backsql_oc_with_id(backsql_info *si,unsigned long id)
250 backsql_oc_map_rec tmp,*res;
252 /* Debug(LDAP_DEBUG_TRACE,"==>oc_with_id(): searching for objectclass with id='%d'\n",id,0,0);*/
254 res=(backsql_oc_map_rec*)avl_find(si->oc_by_id,&tmp,(AVL_CMP)backsql_cmp_oc_id);
256 Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): found name='%s', id=%d\n",res->name,res->id,0);
258 Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): not found\n",0,0,0);
263 backsql_at_map_rec* backsql_at_with_name(backsql_oc_map_rec* objclass,char* attr)
265 backsql_at_map_rec tmp,*res;
267 /*Debug(LDAP_DEBUG_TRACE,"==>at_with_name(): searching for attribute with name='%s' (for objectclass '%s')\n",
268 attr,objclass->name,0);
271 res=(backsql_at_map_rec*)avl_find(objclass->attrs,&tmp,(AVL_CMP)backsql_cmp_attr);
273 Debug(LDAP_DEBUG_TRACE,"<==at_with_name(): found name='%s', sel_expr='%s'\n",
274 res->name,res->sel_expr,0);
276 Debug(LDAP_DEBUG_TRACE,"<==at_with_name(): not found\n",0,0,0);
281 int backsql_free_attr(backsql_at_map_rec *at)
283 Debug(LDAP_DEBUG_TRACE,"==>free_attr(): '%s'\n",at->name,0,0);
285 ch_free(at->sel_expr);
286 if (at->from_tbls!=NULL)
287 ch_free(at->from_tbls);
288 if (at->join_where!=NULL)
289 ch_free(at->join_where);
290 if (at->add_proc!=NULL)
291 ch_free(at->add_proc);
292 if (at->delete_proc!=NULL)
293 ch_free(at->delete_proc);
298 ch_free(at->sel_expr_u); /* TimesTen */
299 Debug(LDAP_DEBUG_TRACE,"<==free_attr()\n",0,0,0);
303 int backsql_free_oc(backsql_oc_map_rec *oc)
305 Debug(LDAP_DEBUG_TRACE,"==>free_oc(): '%s'\n",oc->name,0,0);
306 avl_free(oc->attrs,(AVL_FREE)backsql_free_attr);
310 if (oc->create_proc!=NULL)
311 ch_free(oc->create_proc);
312 if (oc->delete_proc!=NULL)
313 ch_free(oc->delete_proc);
315 Debug(LDAP_DEBUG_TRACE,"<==free_oc()\n",0,0,0);
319 int backsql_destroy_schema_map(backsql_info *si)
321 Debug(LDAP_DEBUG_TRACE,"==>destroy_schema_map()\n",0,0,0);
322 avl_free(si->oc_by_id,(AVL_FREE)backsql_free_oc);
323 avl_free(si->oc_by_name,(AVL_FREE)backsql_dummy);
324 Debug(LDAP_DEBUG_TRACE,"<==destroy_schema_map()\n",0,0,0);
328 #endif /* SLAPD_SQL */