2 * Copyright 1999, Dmitry Kovalev (zmit@mail.ru), 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 int backsql_load_schema_map(backsql_info *si,SQLHDBC dbh)
46 SQLHSTMT oc_sth,at_sth;
48 BACKSQL_ROW_NTS oc_row,at_row;
50 backsql_oc_map_rec *oc_map;
51 backsql_at_map_rec *at_map;
55 Debug(LDAP_DEBUG_TRACE,"==>load_schema_map()\n",0,0,0);
56 rc=backsql_Prepare(dbh,&oc_sth,si->oc_query,0);
57 if (rc != SQL_SUCCESS)
59 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error preparing oc_query: '%s'\n",si->oc_query,0,0);
60 backsql_PrintErrors(si->db_env,dbh,oc_sth,rc);
63 rc=backsql_Prepare(dbh,&at_sth,si->at_query,0);
64 if (rc != SQL_SUCCESS)
66 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error preparing at_query: '%s'\n",si->at_query,0,0);
67 backsql_PrintErrors(si->db_env,dbh,at_sth,rc);
70 if ((rc=backsql_BindParamID(at_sth,1,&oc_id)) != SQL_SUCCESS)
72 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error binding param for at_query: \n",0,0,0);
73 backsql_PrintErrors(si->db_env,dbh,at_sth,rc);
76 if ((rc=SQLExecute(oc_sth)) != SQL_SUCCESS)
78 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error executing oc_query: \n",0,0,0);
79 backsql_PrintErrors(si->db_env,dbh,oc_sth,rc);
82 backsql_BindRowAsStrings(oc_sth,&oc_row);
83 while ((rc=SQLFetch(oc_sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
85 oc_map=(backsql_oc_map_rec*)ch_calloc(1,sizeof(backsql_oc_map_rec));
86 oc_map->id=atoi(oc_row.cols[0]);
87 oc_map->name=strdup(oc_row.cols[1]);
88 oc_map->keytbl=strdup(oc_row.cols[2]);
89 oc_map->keycol=strdup(oc_row.cols[3]);
90 oc_map->create_proc=(oc_row.is_null[4]<0)?NULL:strdup(oc_row.cols[4]);
91 oc_map->delete_proc=(oc_row.is_null[5]<0)?NULL:strdup(oc_row.cols[5]);
93 avl_insert(&si->oc_by_name,oc_map,(AVL_CMP)backsql_cmp_oc_name,backsql_dummy);
94 avl_insert(&si->oc_by_id,oc_map,(AVL_CMP)backsql_cmp_oc_id,backsql_dummy);
96 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): objectclass '%s': keytbl='%s' keycol='%s' ",
97 oc_row.cols[1],oc_row.cols[2],oc_row.cols[3]);
98 Debug(LDAP_DEBUG_TRACE,"create_proc='%s' delete_proc='%s' ; attributes:\n",oc_row.cols[4],
100 if ((rc=SQLExecute(at_sth)) != SQL_SUCCESS)
102 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error executing at_query: \n",0,0,0);
103 backsql_PrintErrors(SQL_NULL_HENV,dbh,at_sth,rc);
106 backsql_BindRowAsStrings(at_sth,&at_row);
107 while ((rc=SQLFetch(at_sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
109 Debug(LDAP_DEBUG_TRACE,"********'%s'\n",at_row.cols[0],0,0);
110 Debug(LDAP_DEBUG_TRACE,"name='%s',sel_expr='%s' from='%s' ",at_row.cols[0],
111 at_row.cols[1],at_row.cols[2]);
112 Debug(LDAP_DEBUG_TRACE,"join_where='%s',add_proc='%s' modify_proc='%s' ",at_row.cols[3],
113 at_row.cols[4],at_row.cols[5]);
114 Debug(LDAP_DEBUG_TRACE,"delete_proc='%s'\n",at_row.cols[6],0,0);
115 at_map=(backsql_at_map_rec*)ch_calloc(1,sizeof(backsql_at_map_rec));
116 at_map->name=strdup(at_row.cols[0]);
117 at_map->sel_expr=strdup(at_row.cols[1]);
119 backsql_merge_from_clause(&tmps,&tmpslen,at_row.cols[2]);
120 at_map->from_tbls=strdup(tmps);
122 at_map->join_where=strdup((at_row.is_null[3]<0)?"":at_row.cols[3]);
123 at_map->add_proc=(at_row.is_null[4]<0)?NULL:strdup(at_row.cols[4]);
124 at_map->modify_proc=(at_row.is_null[5]<0)?NULL:strdup(at_row.cols[5]);
125 at_map->delete_proc=(at_row.is_null[6]<0)?NULL:strdup(at_row.cols[6]);
127 tmps=backsql_strcat(tmps,&tmpslen,"SELECT ",at_map->sel_expr," AS ",at_map->name,
128 " FROM ",at_map->from_tbls,
129 " WHERE ",oc_map->keytbl,".",oc_map->keycol,"=?",NULL);
130 if (at_map->join_where!=NULL && at_map->join_where[0]!='\0')
131 tmps=backsql_strcat(tmps,&tmpslen," AND ",at_map->join_where,NULL);
132 at_map->query=strdup(tmps);
134 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): preconstructed query '%s'\n",at_map->query,0,0);
135 avl_insert(&oc_map->attrs,at_map,(AVL_CMP)backsql_cmp_attr,backsql_dummy);
137 backsql_FreeRow(&at_row);
138 SQLFreeStmt(at_sth,SQL_CLOSE);
140 backsql_FreeRow(&oc_row);
141 SQLFreeStmt(at_sth,SQL_DROP);
142 SQLFreeStmt(oc_sth,SQL_DROP);
144 Debug(LDAP_DEBUG_TRACE,"<==load_schema_map()\n",0,0,0);
148 backsql_oc_map_rec* backsql_oc_with_name(backsql_info *si,char* objclass)
150 backsql_oc_map_rec tmp,*res;
152 // Debug(LDAP_DEBUG_TRACE,"==>oc_with_name(): searching for objectclass with name='%s'\n",objclass,0,0);
154 res=(backsql_oc_map_rec*)avl_find(si->oc_by_name,&tmp,(AVL_CMP)backsql_cmp_oc_name);
156 // Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): found name='%s', id=%d\n",res->name,res->id,0);
158 // Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): not found\n",0,0,0);
162 backsql_oc_map_rec* backsql_oc_with_id(backsql_info *si,unsigned long id)
164 backsql_oc_map_rec tmp,*res;
166 // Debug(LDAP_DEBUG_TRACE,"==>oc_with_id(): searching for objectclass with id='%d'\n",id,0,0);
168 res=(backsql_oc_map_rec*)avl_find(si->oc_by_id,&tmp,(AVL_CMP)backsql_cmp_oc_id);
170 // Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): found name='%s', id=%d\n",res->name,res->id,0);
172 // Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): not found\n",0,0,0);
176 backsql_at_map_rec* backsql_at_with_name(backsql_oc_map_rec* objclass,char* attr)
178 backsql_at_map_rec tmp,*res;
180 //Debug(LDAP_DEBUG_TRACE,"==>at_with_name(): searching for attribute with name='%s' (for objectclass '%s')\n",
181 // attr,objclass->name,0);
183 res=(backsql_at_map_rec*)avl_find(objclass->attrs,&tmp,(AVL_CMP)backsql_cmp_attr);
185 //Debug(LDAP_DEBUG_TRACE,"<==at_with_name(): found name='%s', sel_expr='%s'\n",
186 // res->name,res->sel_expr,0);
188 // Debug(LDAP_DEBUG_TRACE,"<==at_with_name(): not found\n",0,0,0);
192 int backsql_free_attr(backsql_at_map_rec *at)
194 Debug(LDAP_DEBUG_TRACE,"==>free_attr(): '%s'\n",at->name,0,0);
196 ch_free(at->sel_expr);
197 if (at->from_tbls!=NULL)
198 ch_free(at->from_tbls);
199 if (at->join_where!=NULL)
200 ch_free(at->join_where);
201 if (at->add_proc!=NULL)
202 ch_free(at->add_proc);
203 if (at->modify_proc!=NULL)
204 ch_free(at->modify_proc);
205 if (at->delete_proc!=NULL)
206 ch_free(at->delete_proc);
210 Debug(LDAP_DEBUG_TRACE,"<==free_attr()\n",0,0,0);
214 int backsql_free_oc(backsql_oc_map_rec *oc)
216 Debug(LDAP_DEBUG_TRACE,"==>free_oc(): '%s'\n",oc->name,0,0);
217 avl_free(oc->attrs,(AVL_FREE)backsql_free_attr);
221 if (oc->create_proc!=NULL)
222 ch_free(oc->create_proc);
223 if (oc->delete_proc!=NULL)
224 ch_free(oc->delete_proc);
226 Debug(LDAP_DEBUG_TRACE,"<==free_oc()\n",0,0,0);
230 int backsql_destroy_schema_map(backsql_info *si)
232 Debug(LDAP_DEBUG_TRACE,"==>destroy_schema_map()\n",0,0,0);
233 avl_free(si->oc_by_id,(AVL_FREE)backsql_free_oc);
234 avl_free(si->oc_by_name,(AVL_FREE)backsql_dummy);
235 Debug(LDAP_DEBUG_TRACE,"<==destroy_schema_map()\n",0,0,0);
239 #endif /* SLAPD_SQL */