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"
22 backsql_entryID* backsql_free_entryID(backsql_entryID* id)
24 backsql_entryID* next=id->next;
31 backsql_entryID* backsql_dn2id(backsql_entryID *id,SQLHDBC dbh,char *dn)
33 static char id_query[]="SELECT id,keyval,objclass FROM ldap_entries WHERE dn=?";
39 Debug(LDAP_DEBUG_TRACE,"==>backsql_dn2id(): dn='%s'\n",dn,0,0);
40 backsql_Prepare(dbh,&sth,id_query,0);
41 if ((rc=backsql_BindParamStr(sth,1,dn,BACKSQL_MAX_DN_LEN)) != SQL_SUCCESS)
43 Debug(LDAP_DEBUG_TRACE,"backsql_dn2id(): error binding dn parameter:\n",0,0,0);
44 backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
45 SQLFreeStmt(sth,SQL_DROP);
49 if ((rc=SQLExecute(sth)) != SQL_SUCCESS)
51 Debug(LDAP_DEBUG_TRACE,"backsql_dn2id(): error executing query:\n",0,0,0);
52 backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
53 SQLFreeStmt(sth,SQL_DROP);
57 backsql_BindRowAsStrings(sth,&row);
58 if ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
62 id=(backsql_entryID*)ch_calloc(1,sizeof(backsql_entryID));
64 id->id=atoi(row.cols[0]);
65 id->keyval=atoi(row.cols[1]);
66 id->oc_id=atoi(row.cols[2]);
72 backsql_FreeRow(&row);
74 SQLFreeStmt(sth, SQL_DROP);
76 Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): id=%d\n",(int)id->id,0,0);
78 Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): no match\n",0,0,0);
83 int backsql_get_attr_vals(backsql_at_map_rec *at,backsql_srch_info *bsi)
90 Debug(LDAP_DEBUG_TRACE,"==>backsql_get_attr_vals(): oc='%s' attr='%s' keyval=%d\n",
91 bsi->oc->name,at->name,bsi->c_eid->keyval);
93 if ((rc=backsql_Prepare(bsi->dbh,&sth,at->query,0)) != SQL_SUCCESS)
95 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error preparing query: %s\n",at->query,0,0);
96 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
100 if (backsql_BindParamID(sth,1,&(bsi->c_eid->keyval)) != SQL_SUCCESS)
102 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error binding key value parameter\n",0,0,0);
106 if ((rc=SQLExecute(sth)) != SQL_SUCCESS && rc!= SQL_SUCCESS_WITH_INFO)
108 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error executing query\n",0,0,0);
109 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
110 SQLFreeStmt(sth,SQL_DROP);
114 backsql_BindRowAsStrings(sth,&row);
115 while ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc==SQL_SUCCESS_WITH_INFO)
117 for (i=0;i<row.ncols;i++)
119 if (row.is_null[i]>0)
121 backsql_entry_addattr(bsi->e,row.col_names[i],row.cols[i],/*row.col_prec[i]*/
122 strlen(row.cols[i]));
123 // Debug(LDAP_DEBUG_TRACE,"prec=%d\n",(int)row.col_prec[i],0,0);
126 // Debug(LDAP_DEBUG_TRACE,"NULL value in this row for attribute '%s'\n",row.col_names[i],0,0);
129 backsql_FreeRow(&row);
130 SQLFreeStmt(sth,SQL_DROP);
131 Debug(LDAP_DEBUG_TRACE,"<==backsql_get_attr_vals()\n",0,0,0);
136 Entry* backsql_id2entry(backsql_srch_info *bsi,Entry* e,backsql_entryID* eid)
139 backsql_at_map_rec *at;
141 Debug(LDAP_DEBUG_TRACE,"==>backsql_id2entry()\n",0,0,0);
143 bsi->oc=backsql_oc_with_id(bsi->bi,eid->oc_id);
147 if (bsi->base_dn != NULL)
148 e->e_dn=strdup(bsi->c_eid->dn);
150 if (bsi->attrs!=NULL)
152 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): custom attribute list\n",0,0,0);
153 for(c_at_name=bsi->attrs;*c_at_name!=NULL;c_at_name++)
155 if (!strcasecmp(*c_at_name,"objectclass") || !strcasecmp(*c_at_name,"0.10"))
157 //backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
160 at=backsql_at_with_name(bsi->oc,*c_at_name);
162 backsql_get_attr_vals(at,bsi);
164 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): attribute '%s' is not defined for objectlass '%s'\n",
165 *c_at_name,bsi->oc->name,0);
171 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): retrieving all attributes\n",0,0,0);
172 avl_apply(bsi->oc->attrs,(AVL_APPLY)backsql_get_attr_vals,bsi,0,AVL_INORDER);
174 backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
176 Debug(LDAP_DEBUG_TRACE,"<==backsql_id2entry()\n",0,0,0);