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"
24 backsql_entryID* backsql_free_entryID(backsql_entryID* id)
26 backsql_entryID* next=id->next;
33 backsql_entryID* backsql_dn2id(backsql_entryID *id,SQLHDBC dbh,char *dn)
35 static char id_query[]="SELECT id,keyval,objclass FROM ldap_entries WHERE dn=?";
41 Debug(LDAP_DEBUG_TRACE,"==>backsql_dn2id(): dn='%s'\n",dn,0,0);
42 backsql_Prepare(dbh,&sth,id_query,0);
43 if ((rc=backsql_BindParamStr(sth,1,dn,BACKSQL_MAX_DN_LEN)) != SQL_SUCCESS)
45 Debug(LDAP_DEBUG_TRACE,"backsql_dn2id(): error binding dn parameter:\n",0,0,0);
46 backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
47 SQLFreeStmt(sth,SQL_DROP);
51 if ((rc=SQLExecute(sth)) != SQL_SUCCESS)
53 Debug(LDAP_DEBUG_TRACE,"backsql_dn2id(): error executing query:\n",0,0,0);
54 backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
55 SQLFreeStmt(sth,SQL_DROP);
59 backsql_BindRowAsStrings(sth,&row);
60 if ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
64 id=(backsql_entryID*)ch_calloc(1,sizeof(backsql_entryID));
66 id->id=atoi(row.cols[0]);
67 id->keyval=atoi(row.cols[1]);
68 id->oc_id=atoi(row.cols[2]);
74 backsql_FreeRow(&row);
76 SQLFreeStmt(sth, SQL_DROP);
78 Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): id=%d\n",(int)id->id,0,0);
80 Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): no match\n",0,0,0);
85 int backsql_get_attr_vals(backsql_at_map_rec *at,backsql_srch_info *bsi)
92 Debug(LDAP_DEBUG_TRACE,"==>backsql_get_attr_vals(): oc='%s' attr='%s' keyval=%d\n",
93 bsi->oc->name,at->name,bsi->c_eid->keyval);
95 if ((rc=backsql_Prepare(bsi->dbh,&sth,at->query,0)) != SQL_SUCCESS)
97 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error preparing query: %s\n",at->query,0,0);
98 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
102 if (backsql_BindParamID(sth,1,&(bsi->c_eid->keyval)) != SQL_SUCCESS)
104 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error binding key value parameter\n",0,0,0);
108 if ((rc=SQLExecute(sth)) != SQL_SUCCESS && rc!= SQL_SUCCESS_WITH_INFO)
110 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error executing query\n",0,0,0);
111 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
112 SQLFreeStmt(sth,SQL_DROP);
116 backsql_BindRowAsStrings(sth,&row);
117 while ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc==SQL_SUCCESS_WITH_INFO)
119 for (i=0;i<row.ncols;i++)
121 if (row.is_null[i]>0)
123 backsql_entry_addattr(bsi->e,row.col_names[i],row.cols[i],/*row.col_prec[i]*/
124 strlen(row.cols[i]));
125 // Debug(LDAP_DEBUG_TRACE,"prec=%d\n",(int)row.col_prec[i],0,0);
128 // Debug(LDAP_DEBUG_TRACE,"NULL value in this row for attribute '%s'\n",row.col_names[i],0,0);
131 backsql_FreeRow(&row);
132 SQLFreeStmt(sth,SQL_DROP);
133 Debug(LDAP_DEBUG_TRACE,"<==backsql_get_attr_vals()\n",0,0,0);
138 Entry* backsql_id2entry(backsql_srch_info *bsi,Entry* e,backsql_entryID* eid)
141 backsql_at_map_rec *at;
143 Debug(LDAP_DEBUG_TRACE,"==>backsql_id2entry()\n",0,0,0);
145 bsi->oc=backsql_oc_with_id(bsi->bi,eid->oc_id);
149 if (bsi->base_dn != NULL)
150 e->e_dn=strdup(bsi->c_eid->dn);
152 if (bsi->attrs!=NULL)
154 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): custom attribute list\n",0,0,0);
155 for(c_at_name=bsi->attrs;*c_at_name!=NULL;c_at_name++)
157 if (!strcasecmp(*c_at_name,"objectclass") || !strcasecmp(*c_at_name,"0.10"))
159 //backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
162 at=backsql_at_with_name(bsi->oc,*c_at_name);
164 backsql_get_attr_vals(at,bsi);
166 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): attribute '%s' is not defined for objectlass '%s'\n",
167 *c_at_name,bsi->oc->name,0);
173 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): retrieving all attributes\n",0,0,0);
174 avl_apply(bsi->oc->attrs,(AVL_APPLY)backsql_get_attr_vals,bsi,0,AVL_INORDER);
176 backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
178 Debug(LDAP_DEBUG_TRACE,"<==backsql_id2entry()\n",0,0,0);
182 #endif /* SLAPD_SQL */