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 char backsql_def_oc_query[]="SELECT id,name,keytbl,keycol,create_proc,delete_proc,expect_return FROM ldap_oc_mappings";
25 char backsql_def_at_query[]="SELECT name,sel_expr,from_tbls,join_where,add_proc,delete_proc,param_order,expect_return,sel_expr_u FROM ldap_attr_mappings WHERE oc_map_id=?";
26 char backsql_def_delentry_query[]="DELETE FROM ldap_entries WHERE id=?";
27 char backsql_def_insentry_query[]="INSERT INTO ldap_entries (dn,oc_map_id,parent,keyval) VALUES (?,?,?,?)";
28 char backsql_def_subtree_cond[]="ldap_entries.dn LIKE CONCAT('%',?)";
29 char backsql_id_query[]="SELECT id,keyval,oc_map_id FROM ldap_entries WHERE ";
32 char backsql_check_dn_ru_query[] = "SELECT dn_ru from ldap_entries";
34 char* backsql_strcat(char* dest,int *buflen, ...)
40 /*Debug(LDAP_DEBUG_TRACE,"==>my_strcat()\n");*/
41 va_start(strs,buflen);
42 if (dest==NULL || *buflen<=0)
44 dest=(char*)ch_calloc(BACKSQL_STR_GROW,sizeof(char));
45 *buflen=BACKSQL_STR_GROW;
48 while ((cstr=va_arg(strs,char*)) != NULL)
51 grow=BACKSQL_MAX(BACKSQL_STR_GROW,cslen);
52 if (*buflen-cdlen < cslen)
54 /*Debug(LDAP_DEBUG_TRACE,"my_strcat(): buflen=%d, cdlen=%d, cslen=%d -- reallocating dest\n",
55 *buflen,cdlen,cslen); */
56 dest=(char*)ch_realloc(dest,(*buflen)+grow*sizeof(char));
59 Debug(LDAP_DEBUG_ANY,"my_strcat(): could not reallocate string buffer.\n",0,0,0);
62 /*Debug(LDAP_DEBUG_TRACE,"my_strcat(): new buflen=%d, dest=%p\n",*buflen,dest,0);*/
68 /*Debug(LDAP_DEBUG_TRACE,"<==my_strcat() (dest='%s')\n",dest,0,0);*/
72 int backsql_entry_addattr(Entry *e,char *at_name,char *at_val,unsigned int at_val_len)
74 Attribute *c_at=e->e_attrs;
75 struct berval* add_val[2];
77 AttributeDescription *ad;
81 Debug(LDAP_DEBUG_TRACE,"backsql_entry_addattr(): at_name='%s', at_val='%s'\n",at_name,at_val,0);
83 cval.bv_len=at_val_len;
88 rc = slap_str2ad( at_name, &ad, &text );
89 if( rc != LDAP_SUCCESS )
91 Debug(LDAP_DEBUG_TRACE,"backsql_entry_addattr(): failed to find AttributeDescription for '%s'\n",at_name,0,0);
95 rc = attr_merge(e,ad,add_val);
99 Debug(LDAP_DEBUG_TRACE,"backsql_entry_addattr(): failed to merge value '%s' for attribute '%s'\n",at_val,at_name,0);
103 Debug(LDAP_DEBUG_TRACE,"<==backsql_query_addattr()\n",0,0,0);
107 char* backsql_get_table_spec(char **p)
114 while(**p && **p!=',') (*p)++;
118 #define BACKSQL_NEXT_WORD {while (*s && isspace(*s)) s++; if (!*s) return res; q=s; while (*q && !isspace(*q)) q++; if (*q) *q++='\0';}
120 res=backsql_strcat(res,&res_len,s,NULL);/*table name*/
124 if (!strcasecmp(s,"as"))
129 /*res=backsql_strcat(res,&res_len," AS ",s,NULL);
130 *oracle doesn't understand AS :(
132 res=backsql_strcat(res,&res_len," ",s,NULL);/*table alias*/
136 int backsql_merge_from_clause(char **dest_from,int *dest_len,char *src_from)
138 char *s,*p,*srcc,*pos,e;
140 /*Debug(LDAP_DEBUG_TRACE,"==>backsql_merge_from_clause(): dest_from='%s',src_from='%s'\n",
141 dest_from,src_from,0); */
142 srcc=ch_strdup(src_from);
146 s=backsql_get_table_spec(&p);
147 /* Debug(LDAP_DEBUG_TRACE,"backsql_merge_from_clause(): p='%s' s='%s'\n",p,s,0); */
148 if (*dest_from==NULL)
149 *dest_from=backsql_strcat(*dest_from,dest_len,s,NULL);
151 if((pos=strstr(*dest_from,s))==NULL)
152 *dest_from=backsql_strcat(*dest_from,dest_len,",",s,NULL);
153 else if((e=pos[strlen(s)])!='\0' && e!=',')
154 *dest_from=backsql_strcat(*dest_from,dest_len,",",s,NULL);
158 /* Debug(LDAP_DEBUG_TRACE,"<==backsql_merge_from_clause()\n",0,0,0);*/
163 #endif /* SLAPD_SQL */