]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/entry-id.c
wrap sql *.c files with #ifdef SLAPD_SQL to facilate NT builds
[openldap] / servers / slapd / back-sql / entry-id.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev (zmit@mail.ru), All rights reserved.
3  *
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.
8  */
9
10 #include "portable.h"
11
12 #ifdef SLAPD_SQL
13
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include <string.h>
17 #include "slap.h"
18 #include "back-sql.h"
19 #include "sql-wrap.h"
20 #include "schema-map.h"
21 #include "entry-id.h"
22 #include "util.h"
23
24 backsql_entryID* backsql_free_entryID(backsql_entryID* id)
25 {
26  backsql_entryID* next=id->next;
27  if (id->dn!=NULL)
28   free(id->dn);
29  free(id);
30  return next;
31 }
32
33 backsql_entryID* backsql_dn2id(backsql_entryID *id,SQLHDBC dbh,char *dn)
34 {
35  static char id_query[]="SELECT id,keyval,objclass FROM ldap_entries WHERE dn=?";
36  SQLHSTMT sth; 
37  BACKSQL_ROW_NTS row;
38  //SQLINTEGER nrows=0;
39  RETCODE rc;
40
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)
44   {
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);
48    return NULL;
49   }
50  
51  if ((rc=SQLExecute(sth)) != SQL_SUCCESS)
52   {
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);
56    return NULL;
57   }
58  
59  backsql_BindRowAsStrings(sth,&row);
60  if ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
61   {
62    if (id==NULL)
63     {
64      id=(backsql_entryID*)ch_calloc(1,sizeof(backsql_entryID));
65     }
66    id->id=atoi(row.cols[0]);
67    id->keyval=atoi(row.cols[1]);
68    id->oc_id=atoi(row.cols[2]);
69    id->dn=strdup(dn);
70    id->next=NULL;
71   }
72  else
73   id=NULL;
74  backsql_FreeRow(&row);
75  
76  SQLFreeStmt(sth, SQL_DROP);
77  if (id!=NULL)
78   Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): id=%d\n",(int)id->id,0,0);
79  else
80   Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): no match\n",0,0,0);
81  return id;
82 }
83
84
85 int backsql_get_attr_vals(backsql_at_map_rec *at,backsql_srch_info *bsi)
86 {
87  RETCODE rc;
88  SQLHSTMT sth;
89  BACKSQL_ROW_NTS row;
90  int i;
91  
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);
94
95  if ((rc=backsql_Prepare(bsi->dbh,&sth,at->query,0)) != SQL_SUCCESS)
96   {
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);
99    return 1;
100   }
101
102  if (backsql_BindParamID(sth,1,&(bsi->c_eid->keyval)) != SQL_SUCCESS)
103  {
104   Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error binding key value parameter\n",0,0,0);
105   return 1;
106  }
107
108  if ((rc=SQLExecute(sth)) != SQL_SUCCESS && rc!= SQL_SUCCESS_WITH_INFO)
109   {
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);
113    return 1;
114   }
115
116  backsql_BindRowAsStrings(sth,&row);
117  while ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc==SQL_SUCCESS_WITH_INFO)
118   {
119    for (i=0;i<row.ncols;i++)
120     {
121      if (row.is_null[i]>0)
122       {
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);
126       }
127     // else
128     //  Debug(LDAP_DEBUG_TRACE,"NULL value in this row for attribute '%s'\n",row.col_names[i],0,0);
129     }
130   }
131  backsql_FreeRow(&row);
132  SQLFreeStmt(sth,SQL_DROP);
133  Debug(LDAP_DEBUG_TRACE,"<==backsql_get_attr_vals()\n",0,0,0);
134  return 1;
135 }
136
137
138 Entry* backsql_id2entry(backsql_srch_info *bsi,Entry* e,backsql_entryID* eid)
139 {
140  char **c_at_name;
141  backsql_at_map_rec *at;
142
143  Debug(LDAP_DEBUG_TRACE,"==>backsql_id2entry()\n",0,0,0);
144
145  bsi->oc=backsql_oc_with_id(bsi->bi,eid->oc_id);
146  bsi->e=e;
147  bsi->c_eid=eid;
148  e->e_attrs=NULL;
149  if (bsi->base_dn != NULL)
150   e->e_dn=strdup(bsi->c_eid->dn);
151  
152  if (bsi->attrs!=NULL)
153  {
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++)
156   {
157    if (!strcasecmp(*c_at_name,"objectclass") || !strcasecmp(*c_at_name,"0.10"))
158    {
159         //backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
160     continue;
161    }
162    at=backsql_at_with_name(bsi->oc,*c_at_name);
163    if (at!=NULL)
164     backsql_get_attr_vals(at,bsi);
165    else
166         Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): attribute '%s' is not defined for objectlass '%s'\n",
167                         *c_at_name,bsi->oc->name,0);
168
169   }
170  }
171  else
172  {
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);
175  }
176  backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
177
178  Debug(LDAP_DEBUG_TRACE,"<==backsql_id2entry()\n",0,0,0);
179  return e;
180 }
181
182 #endif /* SLAPD_SQL */