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