File: name.c

package info (click to toggle)
virtuoso-opensource 6.1.6+dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 260,992 kB
  • ctags: 125,220
  • sloc: ansic: 652,748; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (220 lines) | stat: -rw-r--r-- 5,308 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 *  name.c
 *
 *  $Id$
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

int stricmp (const char *s1, const char *s2); /* from libutil */

col_ref_rec_t *
ct_col_crr (comp_table_t * ct, ST * ref)
{
  DO_SET (col_ref_rec_t *, crr, &ct->ct_out_crrs)
  {
    if (0 == CASEMODESTRCMP (ref->_.col_ref.name, crr->crr_col_ref->_.col_ref.name))
      return crr;
  }
  END_DO_SET ();
  return NULL;
}


void
col_ref_error (sql_comp_t * sc, ST * ref)
{
  if (ref->_.col_ref.prefix)
    sqlc_new_error (sc->sc_cc, "42S22", "SQ062", "No column %s.%s.",
	ref->_.col_ref.prefix, ref->_.col_ref.name);
  else
    sqlc_new_error (sc->sc_cc, "42S22", "SQ063", "No column %s.", ref->_.col_ref.name);
}


int
ct_col_ref (sql_comp_t * sc, comp_table_t * ct, ST * ref,
    dbe_column_t ** col_ret, col_ref_rec_t ** crr_ret, int err_if_not)
{
  char *col_name = ref->_.col_ref.name;
  if (!ct->ct_derived)
    {
      dbe_column_t *dbe_col = tb_name_to_column (ct->ct_table, col_name);
#ifdef BIF_XML
      if (!dbe_col
	  && ct_is_entity (sc, ct))
	dbe_col = lt_xml_col (NULL, col_name);
#endif
      if (dbe_col)
	{
	  *col_ret = dbe_col;
	  *crr_ret = NULL;
	  DO_SET (col_ref_rec_t *, crr, &ct->ct_out_crrs)
	  {
	    if (0 == CASEMODESTRCMP (crr->crr_col_ref->_.col_ref.name,
		ref->_.col_ref.name))
	      {
		*crr_ret = crr;
		break;
	      }
	  }
	  END_DO_SET ();
	  return 1;
	}
      else
	{
	  if (err_if_not)
	    col_ref_error (sc, ref);
	  else
	    return 0;
	}
    }
  else
    {
      *col_ret = NULL;
      *crr_ret = NULL;
      DO_SET (col_ref_rec_t *, crr, &ct->ct_out_crrs)
      {
	if (0 == CASEMODESTRCMP (crr->crr_col_ref->_.col_ref.name, ref->_.col_ref.name))
	  {
	    *crr_ret = crr;
	    return 1;
	  }
      }
      END_DO_SET ();
      if (err_if_not)
	col_ref_error (sc, ref);
    }
  return 0;
}

comp_table_t *
sqlc_col_table_1 (sql_comp_t * sc, ST * col_ref, dbe_column_t ** col_ret,
    col_ref_rec_t ** crr_ret, int err_if_not)
{
  /* Find a table with the col. Error if 2 found. */
  col_ref_rec_t *crr_found = NULL;
  int n_found = 0;
  dbe_table_t *prefix_table;
  comp_table_t *ct_found = NULL;
  dbe_column_t *col_found = NULL;
  char *col_name;
  char *col_prefix;
  int inx;

  if (!ST_COLUMN (col_ref, COL_DOTTED))
    return NULL;

  col_prefix = col_ref->_.col_ref.prefix;
  col_name = col_ref->_.col_ref.name;
  if (col_name == STAR)
    sqlc_new_error (sc->sc_cc, "42000", "SQ064", "Illegal use of '*'.");

  if (col_ref->_.col_ref.prefix)
    {
      comp_table_t *ct_found = NULL;
      int n_found = 0;
      DO_BOX (comp_table_t *, ct, inx, sc->sc_tables)
      {
	if (ct->ct_prefix && col_prefix)
	  {
	    int res = sqlc_pref_match (ct->ct_prefix, col_prefix);
	    if (res == P_EXACT)
	      {
		if (ct_col_ref (sc, ct, col_ref, col_ret, crr_ret, err_if_not))
		  return ct;
	      }
	    else if (res == P_PARTIAL)
	      {
		n_found++;
		ct_found = ct;
	      }
	    /* cr.table and cr.col */
	  }
      }
      END_DO_BOX;
      if (ct_found && n_found == 1 && ct_col_ref (sc, ct_found, col_ref, col_ret, crr_ret, err_if_not))
	return ct_found;

      prefix_table = sch_name_to_table (sc->sc_cc->cc_schema,
	  col_ref->_.col_ref.prefix);
      DO_BOX (comp_table_t *, ct, inx, sc->sc_tables)
      {
	if (!ct->ct_prefix)
	  {
	    if (ct->ct_table && ct->ct_table == prefix_table)
	      {
		if (ct_col_ref (sc, ct, col_ref, col_ret, crr_ret, err_if_not))
		  return ct;
	      }
	  }
      }
      END_DO_BOX;
      if (err_if_not)
	col_ref_error (sc, col_ref);
      else
	return NULL;
    }
  else
    {
      DO_BOX (comp_table_t *, ct, inx, sc->sc_tables)
      {
	if (ct_col_ref (sc, ct, col_ref, col_ret, crr_ret, 0))
	  {
	    if (!ct_found)
	      {
		n_found++;
		crr_found = *crr_ret;
		col_found = *col_ret;
		ct_found = ct;
	      }
	    else
	      {
		/* second table w/ this column */
		if (!ct_found->ct_prefix && !ct->ct_prefix)
		  {
		    n_found++;
		    break;
		  }
		if (!ct_found->ct_prefix && ct->ct_prefix)
		  continue;
		if (ct_found->ct_prefix && !ct->ct_prefix)
		  {
		    n_found = 1;
		    crr_found = *crr_ret;
		    col_found = *col_ret;
		    ct_found = ct;
		  }
		if (ct_found->ct_prefix && ct->ct_prefix)
		  n_found++;
	      }
	  }
      }
      END_DO_BOX;
      if (n_found > 1)
	sqlc_new_error (sc->sc_cc, "42S22", "SQ065", "Col ref ambiguous %s.", col_name);
      if (err_if_not && !n_found)
	col_ref_error (sc, col_ref);
      *crr_ret = crr_found;
      *col_ret = col_found;
      return ct_found;
    }
  return NULL;
}