File: sqloinv.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 (623 lines) | stat: -rw-r--r-- 14,961 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
 *  sqloinv.c
 *
 *  $Id$
 *
 *  sql inverse functions
 *
 *  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
 *
 */

#include "libutil.h"
#include "Dk.h"

#include "sqlnode.h"
#include "sqlpar.h"
#include "sqlpfn.h"
#include "sqlcmps.h"
#include "sqlintrp.h"
#include "sqlo.h"
#include "sqloinv.h"
#include "sqlbif.h"
#include "security.h"
#include "xmlnode.h"
static id_hash_t *sinv_func_hash = NULL;

#define REPORT_ERR(err) \
      if ((err)) \
        { \
	  if (IS_BOX_POINTER (err)) \
	    { \
	      log_error ("Internal error reading the SYS_SQL_INVERSE [%.5s] %.1000s", \
		  ERR_STATE (err), ERR_MESSAGE (err)); \
	      dk_free_tree (err); \
	    } \
	  else \
	    log_error ("Internal error reading the SYS_SQL_INVERSE", \
		ERR_STATE (err), ERR_MESSAGE (err)); \
 	  err = NULL; \
	  call_exit (-1); \
	}


static void
sinvm_free (sinv_map_t * map)
{
  dk_free_tree ((box_t) map->sinvm_name);
  dk_free_tree ((box_t) map->sinvm_inverse);
  dk_free (map, sizeof (sinv_map_t));
}


static sinv_map_t *
sinv_find_func_map (const char *function_name, client_connection_t *cli)
{
  sinv_map_t **old_map = NULL;

  if (!sinv_func_hash)
    return NULL;

  old_map =
      (sinv_map_t **) id_hash_get (sinv_func_hash,
				   (caddr_t) & function_name);
  if (old_map)
    return *old_map;

  if (case_mode == CM_MSSQL)
    {
      char nm[3 * MAX_NAME_LEN + 4], *pnm = &nm[0];
      strncpy (nm, function_name, sizeof (nm) - 1);
      nm[sizeof(nm) - 1] = 0;
      strlwr (nm);
      old_map =
	  (sinv_map_t **) id_hash_get (sinv_func_hash,
				       (caddr_t) & pnm);
    }

  if (old_map)
    return *old_map;
  else
    {
      char *proc_name = sch_full_proc_name (isp_schema (NULL), function_name,
	  cli_qual (cli), CLI_OWNER (cli));
      if (proc_name)
	old_map = (sinv_map_t **) id_hash_get (sinv_func_hash,
	    (caddr_t) &proc_name);
    }

  if (old_map)
    return *old_map;
  else
    return NULL;
}


static void
sinv_make_decoy_procs (client_connection_t * cli, sinv_map_t *map)
{
  dk_session_t * ses = strses_allocate ();
  int inx;
  caddr_t str, err = NULL;


  SES_PRINT (ses, "create procedure \"");
  sprintf_escaped_id (map->sinvm_name, NULL, ses);
  SES_PRINT (ses, "\" (");
  _DO_BOX (inx, map->sinvm_inverse)
    {
      char buf[50];
      if (inx > 0)
	SES_PRINT (ses, ", ");

      sprintf (buf, "in P%d any", inx + 1);
      SES_PRINT (ses, buf);
    }
  END_DO_BOX;

  SES_PRINT (ses, ") { signal ('42000', 'INTERNAL error : inverse fwd decoy function'); }");
  str = strses_string (ses);
  sql_compile (str, cli, &err, SQLC_DO_NOT_STORE_PROC);
  dk_free_tree (str);
  if (IS_BOX_POINTER (err))
    {
      log_error ("Internal error making decoys for the SYS_SQL_INVERSE [%.5s] %.1000s",
	  ERR_STATE (err), ERR_MESSAGE (err));
      dk_free_tree (err);
      err = NULL;
    }
  strses_flush (ses);

  DO_BOX (caddr_t, inverse, inx, map->sinvm_inverse)
    {
      SES_PRINT (ses, "create procedure \"");
      sprintf_escaped_id (inverse, NULL, ses);
      SES_PRINT (ses, "\" (in P any) { signal ('42000', 'INTERNAL error : inverse rev decoy function'); }");
      str = strses_string (ses);
      sql_compile (str, cli, &err, SQLC_DO_NOT_STORE_PROC);
      dk_free_tree (str);
      if (err)
	{
	  log_error ("Internal error making rev decoys for the SYS_SQL_INVERSE [%.5s] %.1000s",
	      ERR_STATE (err), ERR_MESSAGE (err));
	  dk_free_tree (err);
	  err = NULL;
	}
      strses_flush (ses);
    }
  END_DO_BOX;
  dk_free_tree ((box_t) ses);
}


void
sinv_read_sql_inverses (const char * function_name, client_connection_t * cli)
{
  query_t *rd;
  caddr_t err = NULL;
  local_cursor_t *lc;
  sinv_map_t *map = NULL;
  dk_set_t inverse_set = NULL;
  caddr_t sinvm_name;
  caddr_t sinvm_inverse;
  unsigned sinvm_flags;

  if (!sinv_func_hash)
    {
      sinv_func_hash = id_str_hash_create (50);
    }

  if (function_name)
    {
      sinv_map_t *old_map = sinv_find_func_map (function_name, cli);
      if (old_map)
	{
	  id_hash_remove (sinv_func_hash, (caddr_t) & old_map->sinvm_name);
	  sinvm_free (old_map);
	}
    }

  if (function_name)
    {
      rd = sql_compile ("select "
	  "  SINV_FUNCTION, "
	  "  SINV_ARGUMENT, "
	  "  SINV_INVERSE, "
	  "  SINV_FLAGS "
	  " from "
	  "  DB.DBA.SYS_SQL_INVERSE "
	  " where SINV_FUNCTION = ? "
	  " order by SINV_FUNCTION, SINV_ARGUMENT", cli, &err, SQLC_DEFAULT);
      REPORT_ERR (err);

      err = qr_quick_exec (rd, cli, "q", &lc, 1,
	  ":0", function_name, QRP_STR);
      REPORT_ERR (err);
    }
  else
    {
      rd = sql_compile ("select "
	  "  SINV_FUNCTION, "
	  "  SINV_ARGUMENT, "
	  "  SINV_INVERSE, "
	  "  SINV_FLAGS "
	  " from "
	  "  DB.DBA.SYS_SQL_INVERSE "
	  " order by SINV_FUNCTION, SINV_ARGUMENT", cli, &err, SQLC_DEFAULT);
      REPORT_ERR (err);

      err = qr_quick_exec (rd, cli, "q", &lc, 0);
      REPORT_ERR (err);
    }

  while (lc_next (lc))
    {
      sinvm_name = lc_nth_col (lc, 0);
      sinvm_inverse = lc_nth_col (lc, 2);
      sinvm_flags = (unsigned) unbox (lc_nth_col (lc, 3));

      if (!map || CASEMODESTRCMP (map->sinvm_name, sinvm_name))
	{
	  if (map)

	    {
	      map->sinvm_inverse =
		  (caddr_t *) list_to_array (dk_set_nreverse (inverse_set));
	      inverse_set = NULL;
#ifndef NDEBUG
	      if (!map->sinvm_inverse
		  || BOX_ELEMENTS (map->sinvm_inverse) < 1)
		GPF_T1 ("no inverse set");
#endif
	      sinv_make_decoy_procs (cli, map);
	    }
	  map = (sinv_map_t *) dk_alloc (sizeof (sinv_map_t));
	  map->sinvm_name = box_dv_short_string (sinvm_name);

	  id_hash_set (sinv_func_hash,
	      (caddr_t) & map->sinvm_name, (caddr_t) & map);
	}

      dk_set_push (&inverse_set,
	  (void *) box_dv_short_string (sinvm_inverse));
      map->sinvm_flags = sinvm_flags;
    }
  if (map)
    {
      map->sinvm_inverse =
	  (caddr_t *) list_to_array (dk_set_nreverse (inverse_set));
      inverse_set = NULL;
#ifndef NDEBUG
      if (!map->sinvm_inverse || BOX_ELEMENTS (map->sinvm_inverse) < 1)
	GPF_T1 ("no inverse set");
#endif
      sinv_make_decoy_procs (cli, map);
    }
#ifndef NDEBUG
  else if (dk_set_length (inverse_set))
    GPF_T1 ("no map have set");
#endif


  REPORT_ERR (lc->lc_error);
  lc_free (lc);
  qr_free (rd);
}

static int
sinv_normalize_func_name (const char *fname, client_connection_t *cli,
    char *nm, int sizeof_nm)
{
  if (!bif_find (fname))
    {
      char *proc_name = sch_full_proc_name (isp_schema (NULL), fname,
	  cli_qual (cli), CLI_OWNER (cli));
      if (!proc_name)
	return 0;
      else
	{
	  strncpy (nm, proc_name, sizeof_nm - 1);
	  nm [sizeof_nm - 1] = 0;
	}
    }
  else
    {
      strncpy (nm, fname, sizeof_nm - 1);
      nm [sizeof_nm - 1] = 0;
      if (case_mode == CM_MSSQL)
	strlwr (nm);
    }
  return 1;
}


static caddr_t
bif_sinv_read_invers_sys (caddr_t * qst, caddr_t * err_ret,
    state_slot_t ** args)
{
  caddr_t fname = bif_string_arg (qst, args, 0, "sinv_read_invers_sys");
  query_instance_t *qi = (query_instance_t *) qst;
  caddr_t arr = NULL;
  char nm[3 * MAX_NAME_LEN + 3];

  sec_check_dba (qi, "sinv_read_invers_sys");
  if (!sinv_normalize_func_name (fname, qi->qi_client, nm, sizeof (nm)))
      sqlr_new_error ("22023", "SR461", "Procedure %.*s not found in sinv_read_invers_sys",
	3 * MAX_NAME_LEN, fname);

  sinv_read_sql_inverses (&nm[0], qi->qi_client);

  arr =
      list (2, box_dv_short_string ("sinv_read_invers_sys(?)"),
      box_dv_short_string (&nm[0]));
  log_text_array (qi->qi_trx, arr);
  dk_free_tree (arr);
  return NEW_DB_NULL;
}

#define SKIP_AS(x) \
	while (ST_P (x, BOP_AS)) \
	  (x) = (x)->_.as_exp.left

static int
sinv_call_has_col (ST * tree)
{
  int have_col = 0;
  SKIP_AS (tree);
  if (ST_P (tree, CALL_STMT) && SINV_DV_STRINGP (tree->_.call.name))
    {
      int inx;

      DO_BOX (ST *, arg, inx, tree->_.call.params)
      {
	SKIP_AS (arg);
	if (ST_COLUMN (arg, COL_DOTTED))
	  {
	    have_col = 1;
	    break;
	  }
      }
      END_DO_BOX;

    }
  return have_col;
}

sinv_map_t *
sinv_call_map (ST * tree, client_connection_t * cli)
{
  SKIP_AS (tree);
  if (ST_P (tree, CALL_STMT) && SINV_DV_STRINGP (tree->_.call.name))
    {
      sinv_map_t *pres = NULL;

      pres = sinv_find_func_map (tree->_.call.name, cli);

      if (pres
	  && BOX_ELEMENTS (pres->sinvm_inverse) ==
	  BOX_ELEMENTS (tree->_.call.params))
	return pres;
    }
  return NULL;
}


ST *
sinv_check_inverses (ST *tree, client_connection_t *cli)
{
  ST *otree = tree;
  sinv_map_t *map;

  SKIP_AS (tree);
  map = sinv_call_map (tree, cli);
  if (map)
    {
      if (BOX_ELEMENTS (tree->_.call.params) == 1)
	{
	  ST *arg;
	  arg = tree->_.call.params[0];
	  SKIP_AS (arg);
	  if (ST_P (arg, CALL_STMT) && SINV_DV_STRINGP (arg->_.call.name) &&
	      BOX_ELEMENTS (arg->_.call.params) == 1)
	    {
	      char nm[3 * MAX_NAME_LEN + 3];
	      if (sinv_normalize_func_name (arg->_.call.name, cli, nm, sizeof (nm)) &&
		  !CASEMODESTRCMP (nm, map->sinvm_inverse[0]))
		return sinv_check_inverses (arg->_.call.params[0], cli);
	    }
	}
    }
  else if (ST_P (tree, CALL_STMT) &&
      SINV_DV_STRINGP (tree->_.call.name) &&
      BOX_ELEMENTS (tree->_.call.params) == 1)
    {
      char nm[3 * MAX_NAME_LEN + 3];

      if (sinv_normalize_func_name (tree->_.call.name, cli, nm, sizeof (nm)))
	{
	  ST *arg = tree->_.call.params[0];
	  SKIP_AS (arg);
	  map = sinv_call_map (arg, cli);

	  if (map)
	    {
	      int iinx;
	      _DO_BOX (iinx, map->sinvm_inverse)
		{
		  if (0 == CASEMODESTRCMP (map->sinvm_inverse[iinx], nm))
		    return sinv_check_inverses (arg->_.call.params[iinx], cli);
		}
	      END_DO_BOX;
	    }
	}
    }
  return otree;
}


ST *
sinv_check_exp (sqlo_t * so, ST * tree)
{
  ST *res = NULL;
  ST *otree = tree;
  client_connection_t *cli = NULL;

  sinv_map_t *lmap, *rmap, *map;
  ST *left, *right;
  ptrlong new_op;
  int inx_inv;
  int do_it = 0;
  int lmap_col = 0, rmap_col = 0, map_col = 0;

  if (!sinv_func_hash)
    return otree;

  if (tree->type != BOP_PLUS &&
      tree->type != BOP_MINUS &&
      tree->type != BOP_TIMES &&
      tree->type != BOP_DIV &&
      tree->type != BOP_EQ &&
      tree->type != BOP_NEQ &&
      tree->type != BOP_LT &&
      tree->type != BOP_LTE &&
      tree->type != BOP_GT &&
      tree->type != BOP_GTE)
    return otree;

  res = NULL;

  cli = sqlc_client();
  lmap = sinv_call_map (tree->_.bin_exp.left, cli);
  if (lmap)
    lmap_col = sinv_call_has_col (tree->_.bin_exp.left);
  rmap = sinv_call_map (tree->_.bin_exp.right, cli);
  if (rmap)
    rmap_col = sinv_call_has_col (tree->_.bin_exp.right);


  map = (lmap && (lmap_col || !rmap)) ? lmap : rmap;
  if (!map)
    return otree;
  /* printf ("\n*** sqloinv: "); sqlo_box_print (tree);
     printf ("\n   becomes:"); */
  map_col = (lmap == map) ? lmap_col : rmap_col;

  left = lmap == map ? tree->_.bin_exp.left : tree->_.bin_exp.right;
  right = lmap == map ? tree->_.bin_exp.right : tree->_.bin_exp.left;
  new_op = lmap == map ? tree->type : cmp_op_inverse (tree->type);
  SKIP_AS (left);
  SKIP_AS (right);

  if (!IS_ORDER_PRESERVING (map) && !ST_P (tree, BOP_EQ))
    return otree;

  if (lmap && rmap && lmap == rmap && lmap_col)
    {
      _DO_BOX (inx_inv, map->sinvm_inverse)
        {
	  ST *clause;
	  ST *new_left, *new_right;
	  new_left =
	      (ST *) t_box_copy_tree ((caddr_t) left->_.call.params[inx_inv]);
	  new_right =
	      (ST *) t_box_copy_tree ((caddr_t) right->_.call.params[inx_inv]);

	  new_left = sinv_check_inverses (new_left, cli);
	  new_right = sinv_check_inverses (new_right, cli);

	  BIN_OP (clause, tree->type, new_left, new_right);
	  clause = sinv_check_exp (so, clause);
	  t_st_and (&res, clause);
	}
      END_DO_BOX;
    }
  else if (map_col)
    {
      if (!ST_COLUMN (right, COL_DOTTED))
	do_it = 1;
      else
	{
	  if (!right->_.col_ref.prefix)	/* parameter ? */
	    do_it = 1;
	  else if (so || lmap_col)
	    {
	      int dont_do_it = 0;
	      if (0 /* !so->so_scope || !so->so_scope->sco_tables */)
		dont_do_it = 1;
	      else if (so)
		{
		  DO_SET (op_table_t *, ot, &so->so_this_dt->ot_from_ots)
		    {
		      if (!strcmp (ot->ot_new_prefix, right->_.col_ref.prefix))
			{
			  dont_do_it = 1;
			  break;
			}
		    }
		  END_DO_SET ();
		}
	      if (!dont_do_it)
		do_it = 1;
	    }
	}

      if (do_it)
	{
	  DO_BOX (caddr_t, inverse, inx_inv, map->sinvm_inverse)
	  {
	    ST *clause;
	    ST *new_left, *new_right;
	    new_left =
		(ST *) t_box_copy_tree ((caddr_t) left->_.call.
		params[inx_inv]);
	    new_right =
		t_listst (3, CALL_STMT, t_sqlp_box_id_upcase (inverse),
		t_list (1, right));

	    new_left = sinv_check_inverses (new_left, cli);
	    new_right = sinv_check_inverses (new_right, cli);

	    BIN_OP (clause, new_op, new_left, new_right);
	    clause = sinv_check_exp (so, clause);
	    t_st_and (&res, clause);
	  }
	  END_DO_BOX;
	}
    }
  /* sqlo_box_print (res ? res : otree); */

  return res ? res : otree;
}


void
sinv_sqlo_check_col_val (ST **pcol, ST **pval, dk_set_t *acol, dk_set_t *aval)
{
  ST *op, *res;
  ST *col = *pcol;
  ST *val = *pval;

  BIN_OP (op, BOP_EQ, col, val);
  res = sinv_check_exp (NULL, op);
  if (res != op && ST_P (res, BOP_AND))
    {
      int outs_done = 0;
      dk_set_t and_set = NULL;

      sqlc_make_and_list (res, &and_set);
      DO_SET (predicate_t *, new_tree_pred, &and_set)
	{
	  if (!outs_done)
	    {
	      outs_done = 1;
	      *pcol = new_tree_pred->pred_text->_.bin_exp.left;
	      *pval = new_tree_pred->pred_text->_.bin_exp.right;
	    }
	  else
	    {
	      t_set_push (acol, new_tree_pred->pred_text->_.bin_exp.left);
	      t_set_push (aval, new_tree_pred->pred_text->_.bin_exp.right);
	    }
	}
      END_DO_SET ();
    }
  else
    {
      *pcol = res->_.bin_exp.left;
      *pval = res->_.bin_exp.right;
    }
}




int
sqlo_solve (sqlo_t * so, df_elt_t * tb_dfe, df_elt_t * cond, dk_set_t * cond_ret, dk_set_t * after_preds)
{
  /* put one or more conds into cond ret such that they are equivalent to cond and each has dependent of ot on the left and no dependent of ot on the right.  */
  /* more cases here */
  return 0;
}


void
sqlo_inv_bif_int (void)
{
  bif_define ("sinv_read_invers_sys", bif_sinv_read_invers_sys);
}