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)
40 Debug(LDAP_DEBUG_TRACE,"==>backsql_dn2id(): dn='%s'\n",dn,0,0);
41 backsql_Prepare(dbh,&sth,bi->id_query,0);
42 if ((rc=backsql_BindParamStr(sth,1,dn,BACKSQL_MAX_DN_LEN)) != SQL_SUCCESS)
44 Debug(LDAP_DEBUG_TRACE,"backsql_dn2id(): error binding dn parameter:\n",0,0,0);
45 backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
46 SQLFreeStmt(sth,SQL_DROP);
50 if ((rc=SQLExecute(sth)) != SQL_SUCCESS)
52 Debug(LDAP_DEBUG_TRACE,"backsql_dn2id(): error executing query:\n",0,0,0);
53 backsql_PrintErrors(SQL_NULL_HENV,dbh,sth,rc);
54 SQLFreeStmt(sth,SQL_DROP);
58 backsql_BindRowAsStrings(sth,&row);
59 if ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
63 id=(backsql_entryID*)ch_calloc(1,sizeof(backsql_entryID));
65 id->id=atoi(row.cols[0]);
66 id->keyval=atoi(row.cols[1]);
67 id->oc_id=atoi(row.cols[2]);
73 backsql_FreeRow(&row);
75 SQLFreeStmt(sth, SQL_DROP);
77 Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): id=%d\n",(int)id->id,0,0);
79 Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): no match\n",0,0,0);
84 int backsql_get_attr_vals(backsql_at_map_rec *at,backsql_srch_info *bsi)
91 Debug(LDAP_DEBUG_TRACE,"==>backsql_get_attr_vals(): oc='%s' attr='%s' keyval=%d\n",
92 bsi->oc->name,at->name,bsi->c_eid->keyval);
94 if ((rc=backsql_Prepare(bsi->dbh,&sth,at->query,0)) != SQL_SUCCESS)
96 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error preparing query: %s\n",at->query,0,0);
97 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
101 if (backsql_BindParamID(sth,1,&(bsi->c_eid->keyval)) != SQL_SUCCESS)
103 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error binding key value parameter\n",0,0,0);
107 if ((rc=SQLExecute(sth)) != SQL_SUCCESS && rc!= SQL_SUCCESS_WITH_INFO)
109 Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error executing query\n",0,0,0);
110 backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
111 SQLFreeStmt(sth,SQL_DROP);
115 backsql_BindRowAsStrings(sth,&row);
116 while ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc==SQL_SUCCESS_WITH_INFO)
118 for (i=0;i<row.ncols;i++)
120 if (row.is_null[i]>0)
122 backsql_entry_addattr(bsi->e,row.col_names[i],row.cols[i],/*row.col_prec[i]*/
123 strlen(row.cols[i]));
124 // Debug(LDAP_DEBUG_TRACE,"prec=%d\n",(int)row.col_prec[i],0,0);
127 // Debug(LDAP_DEBUG_TRACE,"NULL value in this row for attribute '%s'\n",row.col_names[i],0,0);
130 backsql_FreeRow(&row);
131 SQLFreeStmt(sth,SQL_DROP);
132 Debug(LDAP_DEBUG_TRACE,"<==backsql_get_attr_vals()\n",0,0,0);
137 Entry* backsql_id2entry(backsql_srch_info *bsi,Entry* e,backsql_entryID* eid)
140 backsql_at_map_rec *at;
142 Debug(LDAP_DEBUG_TRACE,"==>backsql_id2entry()\n",0,0,0);
144 bsi->oc=backsql_oc_with_id(bsi->bi,eid->oc_id);
148 if (bsi->base_dn != NULL)
149 e->e_dn=ch_strdup(bsi->c_eid->dn);
151 if (bsi->attrs!=NULL)
153 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): custom attribute list\n",0,0,0);
154 for(c_at_name=bsi->attrs;*c_at_name!=NULL;c_at_name++)
156 if (!strcasecmp(*c_at_name,"objectclass") || !strcasecmp(*c_at_name,"0.10"))
158 //backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
161 at=backsql_at_with_name(bsi->oc,*c_at_name);
163 backsql_get_attr_vals(at,bsi);
165 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): attribute '%s' is not defined for objectlass '%s'\n",
166 *c_at_name,bsi->oc->name,0);
172 Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): retrieving all attributes\n",0,0,0);
173 avl_apply(bsi->oc->attrs,(AVL_APPLY)backsql_get_attr_vals,bsi,0,AVL_INORDER);
175 backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
177 Debug(LDAP_DEBUG_TRACE,"<==backsql_id2entry()\n",0,0,0);
181 #endif /* SLAPD_SQL */