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);
171 backsql_FreeRow(&row);
172 SQLFreeStmt(sth,SQL_DROP);
173 Debug(LDAP_DEBUG_TRACE,"<==backsql_get_attr_vals()\n",0,0,0);
178 Entry* backsql_id2entry(backsql_srch_info *bsi,Entry* e,backsql_entryID* eid)
181 backsql_at_map_rec *at;
183 Debug(LDAP_DEBUG_TRACE,"==>backsql_id2entry()\n",0,0,0);
185 bsi->oc=backsql_oc_with_id(bsi->bi,eid->oc_id);
191 // if (bsi->base_dn != NULL)???
194 e->e_dn=ch_strdup(bsi->c_eid->dn);
195 e->e_ndn=dn_normalize(ch_strdup(bsi->c_eid->dn));
197 if (bsi->attrs!=NULL)
199 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): custom attribute list\n",0,0,0);
200 for(c_at_name=bsi->attrs;*c_at_name!=NULL;c_at_name++)
202 if (!strcasecmp(*c_at_name,"objectclass") || !strcasecmp(*c_at_name,"0.10"))
204 //backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
207 at=backsql_at_with_name(bsi->oc,*c_at_name);
209 backsql_get_attr_vals(at,bsi);
211 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): attribute '%s' is not defined for objectlass '%s'\n",
212 *c_at_name,bsi->oc->name,0);
218 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): retrieving all attributes\n",0,0,0);
219 avl_apply(bsi->oc->attrs,(AVL_APPLY)backsql_get_attr_vals,bsi,0,AVL_INORDER);
221 backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
223 Debug(LDAP_DEBUG_TRACE,"<==backsql_id2entry()\n",0,0,0);
227 #endif /* SLAPD_SQL */