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,
127 si->has_ldapinfo_dn_ru = 0; // No such field exists
130 SQLFreeStmt(oc_sth, SQL_DROP);
132 rc=backsql_Prepare(dbh,&oc_sth,si->oc_query,0);
133 if (rc != SQL_SUCCESS)
135 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error preparing oc_query: '%s'\n",si->oc_query,0,0);
136 backsql_PrintErrors(si->db_env,dbh,oc_sth,rc);
139 Debug(LDAP_DEBUG_TRACE, "load_schema_map(): at_query '%s'\n", si->at_query,0,0);
141 rc=backsql_Prepare(dbh,&at_sth,si->at_query,0);
142 if (rc != SQL_SUCCESS)
144 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error preparing at_query: '%s'\n",si->at_query,0,0);
145 backsql_PrintErrors(si->db_env,dbh,at_sth,rc);
148 if ((rc=backsql_BindParamID(at_sth,1,&oc_id)) != SQL_SUCCESS)
150 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error binding param for at_query: \n",0,0,0);
151 backsql_PrintErrors(si->db_env,dbh,at_sth,rc);
154 if ((rc=SQLExecute(oc_sth)) != SQL_SUCCESS)
156 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error executing oc_query: \n",0,0,0);
157 backsql_PrintErrors(si->db_env,dbh,oc_sth,rc);
160 backsql_BindRowAsStrings(oc_sth,&oc_row);
161 while ((rc=SQLFetch(oc_sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
163 oc_map=(backsql_oc_map_rec*)ch_calloc(1,sizeof(backsql_oc_map_rec));
164 oc_map->id=atoi(oc_row.cols[0]);
165 oc_map->name=ch_strdup(oc_row.cols[1]);
166 oc_map->keytbl=ch_strdup(oc_row.cols[2]);
167 oc_map->keycol=ch_strdup(oc_row.cols[3]);
168 oc_map->create_proc=(oc_row.is_null[4]<0)?NULL:ch_strdup(oc_row.cols[4]);
169 oc_map->delete_proc=(oc_row.is_null[5]<0)?NULL:ch_strdup(oc_row.cols[5]);
170 oc_map->expect_return=atoi(oc_row.cols[6]);
173 avl_insert(&si->oc_by_name,oc_map,(AVL_CMP)backsql_cmp_oc_name,backsql_dummy);
174 avl_insert(&si->oc_by_id,oc_map,(AVL_CMP)backsql_cmp_oc_id,backsql_dummy);
176 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): objectclass '%s': keytbl='%s' keycol='%s' ",
177 oc_map->name,oc_map->keytbl,oc_map->keycol);
178 if (oc_map->delete_proc) {
179 Debug(LDAP_DEBUG_TRACE,"delete_proc='%s'\n", oc_map->delete_proc, 0, 0);
181 if (oc_map->create_proc) {
182 Debug(LDAP_DEBUG_TRACE,"create_proc='%s'\n", oc_map->create_proc, 0, 0);
184 Debug(LDAP_DEBUG_TRACE,"expect_return=%d; attributes:\n",
185 oc_map->expect_return, 0, 0);
187 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): autoadding 'objectClass' and 'ref' mappings\n",0,0,0);
188 backsql_add_sysmaps(oc_map);
189 if ((rc=SQLExecute(at_sth)) != SQL_SUCCESS)
191 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error executing at_query: \n",0,0,0);
192 backsql_PrintErrors(SQL_NULL_HENV,dbh,at_sth,rc);
195 backsql_BindRowAsStrings(at_sth,&at_row);
196 while ((rc=SQLFetch(at_sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
198 Debug(LDAP_DEBUG_TRACE,"********'%s'\n",at_row.cols[0],0,0);
199 Debug(LDAP_DEBUG_TRACE,"name='%s',sel_expr='%s' from='%s' ",at_row.cols[0],
200 at_row.cols[1],at_row.cols[2]);
201 Debug(LDAP_DEBUG_TRACE,"join_where='%s',add_proc='%s' ",at_row.cols[3],
203 Debug(LDAP_DEBUG_TRACE,"delete_proc='%s'\n",at_row.cols[5],0,0);
204 Debug(LDAP_DEBUG_TRACE,"sel_expr_u='%s'\n", at_row.cols[8],0,0); // TimesTen
205 at_map=(backsql_at_map_rec*)ch_calloc(1,sizeof(backsql_at_map_rec));
206 at_map->name=ch_strdup(at_row.cols[0]);
207 at_map->sel_expr=ch_strdup(at_row.cols[1]);
208 at_map->sel_expr_u = (at_row.is_null[8]<0)?NULL:ch_strdup(at_row.cols[8
211 backsql_merge_from_clause(&tmps,&tmpslen,at_row.cols[2]);
212 at_map->from_tbls=ch_strdup(tmps);
214 at_map->join_where=ch_strdup((at_row.is_null[3]<0)?"":at_row.cols[3]);
215 at_map->add_proc=(at_row.is_null[4]<0)?NULL:ch_strdup(at_row.cols[4]);
216 at_map->delete_proc=(at_row.is_null[5]<0)?NULL:ch_strdup(at_row.cols[5]);
217 at_map->param_order=atoi(at_row.cols[6]);
218 at_map->expect_return=atoi(at_row.cols[7]);
219 backsql_make_attr_query(oc_map,at_map);
220 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): preconstructed query '%s'\n",at_map->query,0,0);
221 avl_insert(&oc_map->attrs,at_map,(AVL_CMP)backsql_cmp_attr,backsql_dummy);
223 backsql_FreeRow(&at_row);
224 SQLFreeStmt(at_sth,SQL_CLOSE);
226 backsql_FreeRow(&oc_row);
227 SQLFreeStmt(at_sth,SQL_DROP);
228 SQLFreeStmt(oc_sth,SQL_DROP);
230 Debug(LDAP_DEBUG_TRACE,"<==load_schema_map()\n",0,0,0);
234 backsql_oc_map_rec* backsql_oc_with_name(backsql_info *si,char* objclass)
236 backsql_oc_map_rec tmp,*res;
238 // Debug(LDAP_DEBUG_TRACE,"==>oc_with_name(): searching for objectclass with name='%s'\n",objclass,0,0);
240 res=(backsql_oc_map_rec*)avl_find(si->oc_by_name,&tmp,(AVL_CMP)backsql_cmp_oc_name);
242 // Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): found name='%s', id=%d\n",res->name,res->id,0);
244 // 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);
262 backsql_at_map_rec* backsql_at_with_name(backsql_oc_map_rec* objclass,char* attr)
264 backsql_at_map_rec tmp,*res;
266 //Debug(LDAP_DEBUG_TRACE,"==>at_with_name(): searching for attribute with name='%s' (for objectclass '%s')\n",
267 // attr,objclass->name,0);
269 res=(backsql_at_map_rec*)avl_find(objclass->attrs,&tmp,(AVL_CMP)backsql_cmp_attr);
271 //Debug(LDAP_DEBUG_TRACE,"<==at_with_name(): found name='%s', sel_expr='%s'\n",
272 // res->name,res->sel_expr,0);
274 // Debug(LDAP_DEBUG_TRACE,"<==at_with_name(): not found\n",0,0,0);
278 int backsql_free_attr(backsql_at_map_rec *at)
280 Debug(LDAP_DEBUG_TRACE,"==>free_attr(): '%s'\n",at->name,0,0);
282 ch_free(at->sel_expr);
283 if (at->from_tbls!=NULL)
284 ch_free(at->from_tbls);
285 if (at->join_where!=NULL)
286 ch_free(at->join_where);
287 if (at->add_proc!=NULL)
288 ch_free(at->add_proc);
289 if (at->delete_proc!=NULL)
290 ch_free(at->delete_proc);
295 ch_free(at->sel_expr_u); // TimesTen
296 Debug(LDAP_DEBUG_TRACE,"<==free_attr()\n",0,0,0);
300 int backsql_free_oc(backsql_oc_map_rec *oc)
302 Debug(LDAP_DEBUG_TRACE,"==>free_oc(): '%s'\n",oc->name,0,0);
303 avl_free(oc->attrs,(AVL_FREE)backsql_free_attr);
307 if (oc->create_proc!=NULL)
308 ch_free(oc->create_proc);
309 if (oc->delete_proc!=NULL)
310 ch_free(oc->delete_proc);
312 Debug(LDAP_DEBUG_TRACE,"<==free_oc()\n",0,0,0);
316 int backsql_destroy_schema_map(backsql_info *si)
318 Debug(LDAP_DEBUG_TRACE,"==>destroy_schema_map()\n",0,0,0);
319 avl_free(si->oc_by_id,(AVL_FREE)backsql_free_oc);
320 avl_free(si->oc_by_name,(AVL_FREE)backsql_dummy);
321 Debug(LDAP_DEBUG_TRACE,"<==destroy_schema_map()\n",0,0,0);
325 #endif /* SLAPD_SQL */