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.
13 #include <sys/types.h>
18 #include "schema-map.h"
21 int backsql_dummy(void *,void *);
23 int backsql_cmp_oc_name(backsql_oc_map_rec *m1,backsql_oc_map_rec *m2)
25 return strcasecmp(m1->name,m2->name);
28 int backsql_cmp_oc_id(backsql_oc_map_rec *m1,backsql_oc_map_rec *m2)
37 int backsql_cmp_attr(backsql_at_map_rec *m1,backsql_at_map_rec *m2)
39 return strcasecmp(m1->name,m2->name);
42 int backsql_load_schema_map(backsql_info *si,SQLHDBC dbh)
44 SQLHSTMT oc_sth,at_sth;
46 BACKSQL_ROW_NTS oc_row,at_row;
48 backsql_oc_map_rec *oc_map;
49 backsql_at_map_rec *at_map;
53 Debug(LDAP_DEBUG_TRACE,"==>load_schema_map()\n",0,0,0);
54 rc=backsql_Prepare(dbh,&oc_sth,si->oc_query,0);
55 if (rc != SQL_SUCCESS)
57 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error preparing oc_query: '%s'\n",si->oc_query,0,0);
58 backsql_PrintErrors(si->db_env,dbh,oc_sth,rc);
61 rc=backsql_Prepare(dbh,&at_sth,si->at_query,0);
62 if (rc != SQL_SUCCESS)
64 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error preparing at_query: '%s'\n",si->at_query,0,0);
65 backsql_PrintErrors(si->db_env,dbh,at_sth,rc);
68 if ((rc=backsql_BindParamID(at_sth,1,&oc_id)) != SQL_SUCCESS)
70 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error binding param for at_query: \n",0,0,0);
71 backsql_PrintErrors(si->db_env,dbh,at_sth,rc);
74 if ((rc=SQLExecute(oc_sth)) != SQL_SUCCESS)
76 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error executing oc_query: \n",0,0,0);
77 backsql_PrintErrors(si->db_env,dbh,oc_sth,rc);
80 backsql_BindRowAsStrings(oc_sth,&oc_row);
81 while ((rc=SQLFetch(oc_sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
83 oc_map=(backsql_oc_map_rec*)ch_calloc(1,sizeof(backsql_oc_map_rec));
84 oc_map->id=atoi(oc_row.cols[0]);
85 oc_map->name=strdup(oc_row.cols[1]);
86 oc_map->keytbl=strdup(oc_row.cols[2]);
87 oc_map->keycol=strdup(oc_row.cols[3]);
88 oc_map->create_proc=(oc_row.is_null[4]<0)?NULL:strdup(oc_row.cols[4]);
89 oc_map->delete_proc=(oc_row.is_null[5]<0)?NULL:strdup(oc_row.cols[5]);
91 avl_insert(&si->oc_by_name,oc_map,(AVL_CMP)backsql_cmp_oc_name,backsql_dummy);
92 avl_insert(&si->oc_by_id,oc_map,(AVL_CMP)backsql_cmp_oc_id,backsql_dummy);
94 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): objectclass '%s': keytbl='%s' keycol='%s' ",
95 oc_row.cols[1],oc_row.cols[2],oc_row.cols[3]);
96 Debug(LDAP_DEBUG_TRACE,"create_proc='%s' delete_proc='%s' ; attributes:\n",oc_row.cols[4],
98 if ((rc=SQLExecute(at_sth)) != SQL_SUCCESS)
100 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): error executing at_query: \n",0,0,0);
101 backsql_PrintErrors(SQL_NULL_HENV,dbh,at_sth,rc);
104 backsql_BindRowAsStrings(at_sth,&at_row);
105 while ((rc=SQLFetch(at_sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
107 Debug(LDAP_DEBUG_TRACE,"********'%s'\n",at_row.cols[0],0,0);
108 Debug(LDAP_DEBUG_TRACE,"name='%s',sel_expr='%s' from='%s' ",at_row.cols[0],
109 at_row.cols[1],at_row.cols[2]);
110 Debug(LDAP_DEBUG_TRACE,"join_where='%s',add_proc='%s' modify_proc='%s' ",at_row.cols[3],
111 at_row.cols[4],at_row.cols[5]);
112 Debug(LDAP_DEBUG_TRACE,"delete_proc='%s'\n",at_row.cols[6],0,0);
113 at_map=(backsql_at_map_rec*)ch_calloc(1,sizeof(backsql_at_map_rec));
114 at_map->name=strdup(at_row.cols[0]);
115 at_map->sel_expr=strdup(at_row.cols[1]);
117 backsql_merge_from_clause(&tmps,&tmpslen,at_row.cols[2]);
118 at_map->from_tbls=strdup(tmps);
120 at_map->join_where=strdup((at_row.is_null[3]<0)?"":at_row.cols[3]);
121 at_map->add_proc=(at_row.is_null[4]<0)?NULL:strdup(at_row.cols[4]);
122 at_map->modify_proc=(at_row.is_null[5]<0)?NULL:strdup(at_row.cols[5]);
123 at_map->delete_proc=(at_row.is_null[6]<0)?NULL:strdup(at_row.cols[6]);
125 tmps=backsql_strcat(tmps,&tmpslen,"SELECT ",at_map->sel_expr," AS ",at_map->name,
126 " FROM ",at_map->from_tbls,
127 " WHERE ",oc_map->keytbl,".",oc_map->keycol,"=?",NULL);
128 if (at_map->join_where!=NULL && at_map->join_where[0]!='\0')
129 tmps=backsql_strcat(tmps,&tmpslen," AND ",at_map->join_where,NULL);
130 at_map->query=strdup(tmps);
132 Debug(LDAP_DEBUG_TRACE,"load_schema_map(): preconstructed query '%s'\n",at_map->query,0,0);
133 avl_insert(&oc_map->attrs,at_map,(AVL_CMP)backsql_cmp_attr,backsql_dummy);
135 backsql_FreeRow(&at_row);
136 SQLFreeStmt(at_sth,SQL_CLOSE);
138 backsql_FreeRow(&oc_row);
139 SQLFreeStmt(at_sth,SQL_DROP);
140 SQLFreeStmt(oc_sth,SQL_DROP);
142 Debug(LDAP_DEBUG_TRACE,"<==load_schema_map()\n",0,0,0);
146 backsql_oc_map_rec* backsql_oc_with_name(backsql_info *si,char* objclass)
148 backsql_oc_map_rec tmp,*res;
150 // Debug(LDAP_DEBUG_TRACE,"==>oc_with_name(): searching for objectclass with name='%s'\n",objclass,0,0);
152 res=(backsql_oc_map_rec*)avl_find(si->oc_by_name,&tmp,(AVL_CMP)backsql_cmp_oc_name);
154 // Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): found name='%s', id=%d\n",res->name,res->id,0);
156 // Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): not found\n",0,0,0);
160 backsql_oc_map_rec* backsql_oc_with_id(backsql_info *si,unsigned long id)
162 backsql_oc_map_rec tmp,*res;
164 // Debug(LDAP_DEBUG_TRACE,"==>oc_with_id(): searching for objectclass with id='%d'\n",id,0,0);
166 res=(backsql_oc_map_rec*)avl_find(si->oc_by_id,&tmp,(AVL_CMP)backsql_cmp_oc_id);
168 // Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): found name='%s', id=%d\n",res->name,res->id,0);
170 // Debug(LDAP_DEBUG_TRACE,"<==oc_with_name(): not found\n",0,0,0);
174 backsql_at_map_rec* backsql_at_with_name(backsql_oc_map_rec* objclass,char* attr)
176 backsql_at_map_rec tmp,*res;
178 //Debug(LDAP_DEBUG_TRACE,"==>at_with_name(): searching for attribute with name='%s' (for objectclass '%s')\n",
179 // attr,objclass->name,0);
181 res=(backsql_at_map_rec*)avl_find(objclass->attrs,&tmp,(AVL_CMP)backsql_cmp_attr);
183 //Debug(LDAP_DEBUG_TRACE,"<==at_with_name(): found name='%s', sel_expr='%s'\n",
184 // res->name,res->sel_expr,0);
186 // Debug(LDAP_DEBUG_TRACE,"<==at_with_name(): not found\n",0,0,0);
190 int backsql_free_attr(backsql_at_map_rec *at)
192 Debug(LDAP_DEBUG_TRACE,"==>free_attr(): '%s'\n",at->name,0,0);
194 ch_free(at->sel_expr);
195 if (at->from_tbls!=NULL)
196 ch_free(at->from_tbls);
197 if (at->join_where!=NULL)
198 ch_free(at->join_where);
199 if (at->add_proc!=NULL)
200 ch_free(at->add_proc);
201 if (at->modify_proc!=NULL)
202 ch_free(at->modify_proc);
203 if (at->delete_proc!=NULL)
204 ch_free(at->delete_proc);
208 Debug(LDAP_DEBUG_TRACE,"<==free_attr()\n",0,0,0);
212 int backsql_free_oc(backsql_oc_map_rec *oc)
214 Debug(LDAP_DEBUG_TRACE,"==>free_oc(): '%s'\n",oc->name,0,0);
215 avl_free(oc->attrs,(AVL_FREE)backsql_free_attr);
219 if (oc->create_proc!=NULL)
220 ch_free(oc->create_proc);
221 if (oc->delete_proc!=NULL)
222 ch_free(oc->delete_proc);
224 Debug(LDAP_DEBUG_TRACE,"<==free_oc()\n",0,0,0);
228 int backsql_destroy_schema_map(backsql_info *si)
230 Debug(LDAP_DEBUG_TRACE,"==>destroy_schema_map()\n",0,0,0);
231 avl_free(si->oc_by_id,(AVL_FREE)backsql_free_oc);
232 avl_free(si->oc_by_name,(AVL_FREE)backsql_dummy);
233 Debug(LDAP_DEBUG_TRACE,"<==destroy_schema_map()\n",0,0,0);