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"
24 backsql_entryID* backsql_free_entryID(backsql_entryID* id)
26 backsql_entryID* next=id->next;
33 backsql_entryID* backsql_dn2id(backsql_info *bi,backsql_entryID *id,SQLHDBC dbh,char *dn)
37 /*SQLINTEGER nrows=0;*/
41 char upperdn[BACKSQL_MAX_DN_LEN+1];
45 Debug(LDAP_DEBUG_TRACE,"==>backsql_dn2id(): dn='%s'\n",dn,0,0);
47 Debug(LDAP_DEBUG_TRACE, "id_query '%s'\n", bi->id_query, 0, 0);
48 rc = backsql_Prepare(dbh,&sth,bi->id_query,0);
49 if (rc != SQL_SUCCESS) {
50 Debug(LDAP_DEBUG_TRACE, "backsql_dn2id(): error preparing SQL:\n", 0, 0, 0);
51 Debug(LDAP_DEBUG_TRACE, "%s\n", bi->id_query, 0, 0);
52 backsql_PrintErrors(SQL_NULL_HENV, dbh, sth, rc);
53 SQLFreeStmt(sth, SQL_DROP);
57 if (bi->has_ldapinfo_dn_ru) {
58 /* Prepare an upper cased, byte reversed version that can be
59 searched using indexes */
61 for ((i=0, j=strlen(dn)-1); *(dn+i); (i++, j--)) {
62 *(upperdn+i) = toupper(*(dn+j));
65 Debug(LDAP_DEBUG_TRACE,"==>backsql_dn2id(): upperdn='%s'\n",upperdn,0,0);
70 for (i = 0; *(dn+i); i++) {
71 *(upperdn+i) = toupper(*(dn+i)); /* Copy while upper casing */
74 Debug(LDAP_DEBUG_TRACE,"==>backsql_dn2id(): upperdn='%s'\n",upperdn,0,0);
81 if ((rc=backsql_BindParamStr(sth,1,toBind,
82 BACKSQL_MAX_DN_LEN)) != SQL_SUCCESS)
85 Debug(LDAP_DEBUG_TRACE,"backsql_dn2id(): error binding dn parameter:\n",0,0,0);
86 backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
87 SQLFreeStmt(sth,SQL_DROP);
91 if ((rc=SQLExecute(sth)) != SQL_SUCCESS)
93 Debug(LDAP_DEBUG_TRACE,"backsql_dn2id(): error executing query:\n",0,0,0);
94 backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
95 SQLFreeStmt(sth,SQL_DROP);
99 backsql_BindRowAsStrings(sth,&row);
100 if ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
104 id=(backsql_entryID*)ch_calloc(1,sizeof(backsql_entryID));
106 id->id=atoi(row.cols[0]);
107 id->keyval=atoi(row.cols[1]);
108 id->oc_id=atoi(row.cols[2]);
109 id->dn=ch_strdup(dn);
114 backsql_FreeRow(&row);
116 SQLFreeStmt(sth, SQL_DROP);
118 Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): id=%d\n",(int)id->id,0,0);
120 Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): no match\n",0,0,0);
125 int backsql_get_attr_vals(backsql_at_map_rec *at,backsql_srch_info *bsi)
132 Debug(LDAP_DEBUG_TRACE,"==>backsql_get_attr_vals(): oc='%s' attr='%s' keyval=%d\n",
133 bsi->oc->name,at->name,bsi->c_eid->keyval);
135 if ((rc=backsql_Prepare(bsi->dbh,&sth,at->query,0)) != SQL_SUCCESS)
137 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error preparing query: %s\n",at->query,0,0);
138 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
142 if (backsql_BindParamID(sth,1,&(bsi->c_eid->keyval)) != SQL_SUCCESS)
144 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error binding key value parameter\n",0,0,0);
148 if ((rc=SQLExecute(sth)) != SQL_SUCCESS && rc!= SQL_SUCCESS_WITH_INFO)
150 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error executing attribute query '%s'\n",at->query,0,0);
151 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
152 SQLFreeStmt(sth,SQL_DROP);
156 backsql_BindRowAsStrings(sth,&row);
157 while ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc==SQL_SUCCESS_WITH_INFO)
159 for (i=0;i<row.ncols;i++)
161 if (row.is_null[i]>0)
163 backsql_entry_addattr(bsi->e,row.col_names[i],row.cols[i],/*row.col_prec[i]*/
164 strlen(row.cols[i]));
165 /* Debug(LDAP_DEBUG_TRACE,"prec=%d\n",(int)row.col_prec[i],0,0);*/
168 Debug(LDAP_DEBUG_TRACE,"NULL value in this row for attribute '%s'\n",row.col_names[i],0,0);
172 backsql_FreeRow(&row);
173 SQLFreeStmt(sth,SQL_DROP);
174 Debug(LDAP_DEBUG_TRACE,"<==backsql_get_attr_vals()\n",0,0,0);
179 Entry* backsql_id2entry(backsql_srch_info *bsi,Entry* e,backsql_entryID* eid)
182 backsql_at_map_rec *at;
184 Debug(LDAP_DEBUG_TRACE,"==>backsql_id2entry()\n",0,0,0);
186 bsi->oc=backsql_oc_with_id(bsi->bi,eid->oc_id);
192 /* if (bsi->base_dn != NULL)???*/
195 e->e_dn=ch_strdup(bsi->c_eid->dn);
196 e->e_ndn=dn_normalize(ch_strdup(bsi->c_eid->dn));
198 if (bsi->attrs!=NULL)
200 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): custom attribute list\n",0,0,0);
201 for(c_at_name=bsi->attrs;*c_at_name!=NULL;c_at_name++)
203 if (!strcasecmp(*c_at_name,"objectclass") || !strcasecmp(*c_at_name,"0.10"))
205 /*backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));*/
208 at=backsql_at_with_name(bsi->oc,*c_at_name);
210 backsql_get_attr_vals(at,bsi);
212 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): attribute '%s' is not defined for objectlass '%s'\n",
213 *c_at_name,bsi->oc->name,0);
219 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): retrieving all attributes\n",0,0,0);
220 avl_apply(bsi->oc->attrs,(AVL_APPLY)backsql_get_attr_vals,bsi,0,AVL_INORDER);
222 backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
224 Debug(LDAP_DEBUG_TRACE,"<==backsql_id2entry()\n",0,0,0);
228 #endif /* SLAPD_SQL */