]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/entry-id.c
s/SUBSTRINGS/SUBSTR/
[openldap] / servers / slapd / back-sql / entry-id.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev <mit@openldap.org>, 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_info *bi,backsql_entryID *id,SQLHDBC dbh,char *dn)
34 {
35  SQLHSTMT sth; 
36  BACKSQL_ROW_NTS row;
37  //SQLINTEGER nrows=0;
38  RETCODE rc;
39
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)
43   {
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);
47    return NULL;
48   }
49  
50  if ((rc=SQLExecute(sth)) != SQL_SUCCESS)
51   {
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);
55    return NULL;
56   }
57  
58  backsql_BindRowAsStrings(sth,&row);
59  if ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO)
60   {
61    if (id==NULL)
62     {
63      id=(backsql_entryID*)ch_calloc(1,sizeof(backsql_entryID));
64     }
65    id->id=atoi(row.cols[0]);
66    id->keyval=atoi(row.cols[1]);
67    id->oc_id=atoi(row.cols[2]);
68    id->dn=ch_strdup(dn);
69    id->next=NULL;
70   }
71  else
72   id=NULL;
73  backsql_FreeRow(&row);
74  
75  SQLFreeStmt(sth, SQL_DROP);
76  if (id!=NULL)
77   Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): id=%d\n",(int)id->id,0,0);
78  else
79   Debug(LDAP_DEBUG_TRACE,"<==backsql_dn2id(): no match\n",0,0,0);
80  return id;
81 }
82
83
84 int backsql_get_attr_vals(backsql_at_map_rec *at,backsql_srch_info *bsi)
85 {
86  RETCODE rc;
87  SQLHSTMT sth;
88  BACKSQL_ROW_NTS row;
89  int i;
90  
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);
93
94  if ((rc=backsql_Prepare(bsi->dbh,&sth,at->query,0)) != SQL_SUCCESS)
95   {
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);
98    return 1;
99   }
100
101  if (backsql_BindParamID(sth,1,&(bsi->c_eid->keyval)) != SQL_SUCCESS)
102  {
103   Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error binding key value parameter\n",0,0,0);
104   return 1;
105  }
106
107  if ((rc=SQLExecute(sth)) != SQL_SUCCESS && rc!= SQL_SUCCESS_WITH_INFO)
108   {
109    Debug(LDAP_DEBUG_TRACE,"backsql_get_attr_values(): error executing attribute query '%s'\n",at->query,0,0);
110    backsql_PrintErrors(bsi->bi->db_env,bsi->dbh,sth,rc);
111    SQLFreeStmt(sth,SQL_DROP);
112    return 1;
113   }
114
115  backsql_BindRowAsStrings(sth,&row);
116  while ((rc=SQLFetch(sth)) == SQL_SUCCESS || rc==SQL_SUCCESS_WITH_INFO)
117   {
118    for (i=0;i<row.ncols;i++)
119     {
120      if (row.is_null[i]>0)
121       {
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);
125       }
126     // else
127     //  Debug(LDAP_DEBUG_TRACE,"NULL value in this row for attribute '%s'\n",row.col_names[i],0,0);
128     }
129   }
130  backsql_FreeRow(&row);
131  SQLFreeStmt(sth,SQL_DROP);
132  Debug(LDAP_DEBUG_TRACE,"<==backsql_get_attr_vals()\n",0,0,0);
133  return 1;
134 }
135
136
137 Entry* backsql_id2entry(backsql_srch_info *bsi,Entry* e,backsql_entryID* eid)
138 {
139  char **c_at_name;
140  backsql_at_map_rec *at;
141
142  Debug(LDAP_DEBUG_TRACE,"==>backsql_id2entry()\n",0,0,0);
143
144  bsi->oc=backsql_oc_with_id(bsi->bi,eid->oc_id);
145  bsi->e=e;
146  bsi->c_eid=eid;
147  e->e_attrs=NULL;
148  e->e_private=NULL;
149  
150 // if (bsi->base_dn != NULL)???
151
152  e->e_id=eid->id;
153  e->e_dn=ch_strdup(bsi->c_eid->dn);
154  e->e_ndn=dn_normalize(ch_strdup(bsi->c_eid->dn));
155  
156  if (bsi->attrs!=NULL)
157  {
158   Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): custom attribute list\n",0,0,0);
159   for(c_at_name=bsi->attrs;*c_at_name!=NULL;c_at_name++)
160   {
161    if (!strcasecmp(*c_at_name,"objectclass") || !strcasecmp(*c_at_name,"0.10"))
162    {
163         //backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
164     continue;
165    }
166    at=backsql_at_with_name(bsi->oc,*c_at_name);
167    if (at!=NULL)
168     backsql_get_attr_vals(at,bsi);
169    else
170         Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): attribute '%s' is not defined for objectlass '%s'\n",
171                         *c_at_name,bsi->oc->name,0);
172
173   }
174  }
175  else
176  {
177   Debug(LDAP_DEBUG_TRACE,"backsql_id2entry(): retrieving all attributes\n",0,0,0);
178   avl_apply(bsi->oc->attrs,(AVL_APPLY)backsql_get_attr_vals,bsi,0,AVL_INORDER);
179  }
180  backsql_entry_addattr(bsi->e,"objectclass",bsi->oc->name,strlen(bsi->oc->name));
181
182  Debug(LDAP_DEBUG_TRACE,"<==backsql_id2entry()\n",0,0,0);
183  return e;
184 }
185
186 #endif /* SLAPD_SQL */