Skip to content
This repository
Browse code

Recursive queries: 1st step

  • Loading branch information...
commit e8c7fecf174ffcd2f3149e468c700a17845f9f21 1 parent ab0489e
ckroxigor authored December 06, 2012
1  .gitignore
@@ -7,5 +7,6 @@
7 7
 *.cmx
8 8
 *.cmxs
9 9
 *.cmxa
  10
+*.dat
10 11
 *~
11 12
 yadi
38  eval.ml
@@ -352,6 +352,8 @@ let s_generate_select_restrictions cols as_name =
352 352
 	) as_name))
353 353
 ;;
354 354
 
  355
+let with_str = ref "";;
  356
+
355 357
 (* Subquery generator *)
356 358
 let rec get_subquery e name col_names =
357 359
 	let num_queried_columns = List.length col_names in
@@ -378,6 +380,18 @@ let rec get_subquery e name col_names =
378 380
 					fun elem -> match elem with
379 381
 						| Rel l -> (String.compare (relation_head(elem)) name) == 0
380 382
 						| _ -> false ) (get_rel predicate)) predicates in
  383
+		(*Sort the predicates if necessary*)
  384
+		let spredicates = if is_recursive then
  385
+				List.append (List.filter(fun predicate -> not (List.exists (
  386
+						fun elem -> match elem with
  387
+							| Rel l -> (String.compare (relation_head(elem)) name) == 0
  388
+							| _ -> false ) (get_rel predicate))) predicates)
  389
+				(List.filter(fun predicate -> List.exists (
  390
+						fun elem -> match elem with
  391
+							| Rel l -> (String.compare (relation_head(elem)) name) == 0
  392
+							| _ -> false ) (get_rel predicate)) predicates)
  393
+				
  394
+			else predicates in
381 395
 		Printf.printf "Is recursive: %b\n" is_recursive;
382 396
 		let sub_selects = List.map (fun predicate ->
383 397
 			let head_cols = get_head_attibutes predicate in
@@ -392,12 +406,15 @@ let rec get_subquery e name col_names =
392 406
 					"SELECT";
393 407
 					sel;
394 408
 					"FROM";
395  
-					(gen_sub_tables e lrel col_names head_cols);
  409
+					(gen_sub_tables e lrel col_names head_cols name);
396 410
 				])
397  
-		) predicates in
398  
-			"("^(String.concat " UNION " sub_selects)^") AS foo"
399  
-(*gen_sub_tables e: tree l:relation sel:columns to select sel_as: selected attributes name*)
400  
-and gen_sub_tables e l sel sel_as = 
  411
+		) spredicates in
  412
+			if not is_recursive then "("^(String.concat " UNION " sub_selects)^") AS foo"
  413
+			else let vars = (String.concat ", " (List.map(fun x -> param_to_string x)col_names)) in 
  414
+				with_str := with_str.contents^"WITH RECURSIVE "^name^"("^vars^") AS ("^(String.concat " UNION " sub_selects)^")";
  415
+				"(SELECT "^vars^" FROM "^name^") AS foo"
  416
+(*gen_sub_tables e: tree l:relation sel:columns to select sel_as: selected attributes name; name: current relation name*)
  417
+and gen_sub_tables e l sel sel_as name = 
401 418
 	(*Initialize empty lists*)
402 419
 	let from = ref [] in			(*Contains the string queries of the not negated relations*)
403 420
 	let where = ref (generate_select_restrictions sel_as sel) in			(*Contains the string of the restrictions applied to the not negated relations*)
@@ -412,7 +429,9 @@ and gen_sub_tables e l sel sel_as =
412 429
 	for ntable = 0 to List.length tables - 1 do
413 430
 		let table_name = relation_head (List.nth tables ntable) in
414 431
 		let col_names = relation_tail (List.nth tables ntable) in			
415  
-			from := List.append from.contents [(get_subquery e table_name col_names)^(string_of_int num_table.contents)];
  432
+			from := List.append from.contents [
  433
+				if (String.compare name table_name) == 0 then name
  434
+				else (get_subquery e table_name col_names)^(string_of_int num_table.contents)];
416 435
 			num_table := num_table.contents + 1;
417 436
 			vars_from := List.append vars_from.contents (List.filter(fun x -> match x with | ParamS s -> true | ParamI i -> false) col_names);
418 437
 	done;
@@ -471,8 +490,11 @@ and gen_sub_tables e l sel sel_as =
471 490
 
472 491
 
473 492
 (* generate SQL statement from the ast *)
474  
-let sql_stt e = String.concat "" 
475  
-	["SELECT DISTINCT ";
  493
+let sql_stt e = 
  494
+	with_str := "";
  495
+	String.concat "" 
  496
+	[with_str.contents;
  497
+	"SELECT DISTINCT ";
476 498
 	(String.concat ", " (List.map(fun x -> param_to_string x)(get_varlist(match (get_query e) with 
477 499
 		Query p -> Rel p | _ -> invalid_arg "is_prog"))));
478 500
 	" FROM " ;
25  examples.lang
@@ -319,6 +319,31 @@ Q(x,y,101):-Q(x,y,75).
319 319
 ?-Q(x,y,z).
320 320
 /
321 321
 
  322
+Q(x,y,101):-Q(x,y,75).
  323
+Q(x,y,z):-Marks(x,y,z).
  324
+?-Q(x,y,z).
  325
+/
  326
+
  327
+Q(x,y,z):-Marks(x,y,z).
  328
+Q(x,y,105):-Marks(x,y,z).
  329
+Q(x,y,102):-Q(x,y,75).
  330
+?-Q(x,y,z).
  331
+/
  332
+
  333
+Q(x,y,z):-Marks(x,y,z).
  334
+Q(x,y,101):-Q(x,y,75).
  335
+Q(x,y,102):-Q(x,y,75).
  336
+?-Q(x,y,z).
  337
+/
  338
+
  339
+
  340
+
  341
+Q(x,y,z):-Marks(x,y,z).
  342
+Q(x,y,105):-Marks(x,y,z).
  343
+Q(x,y,102):-Q(x,y,75).
  344
+?-Q(x,y,z).
  345
+/
  346
+
322 347
 (*command examples*)
323 348
 
324 349
 #help select/
219  in.dat
... ...
@@ -1,219 +0,0 @@
1  
-Q(id,descr) :- Test(id,descr).
2  
-?-Q(id,descr).
3  
-/
4  
-
5  
-Q(id,descr) :- Test(id,descr).
6  
-R(id,descr) :- Q(id,descr).
7  
-?-R(id,descr).
8  
-/
9  
-
10  
-Q(id,descr) :- Test(id,descr).
11  
-?-Q(descr, id).
12  
-/
13  
-
14  
-Q(descr,id) :- Test(id,descr).
15  
-?-Q(id).
16  
-/
17  
-
18  
-Q(id,descr) :- Test(id,descr).
19  
-?-Q(id).
20  
-/
21  
-
22  
-Q(id,descr) :- Test(id,descr).
23  
-?-Q(descr).
24  
-/
25  
-
26  
-Q(id,descr) :- Test(id,descr).
27  
-?-Q(descr, id).
28  
-/
29  
-
30  
-Q(y,x) :- Test(x,y).
31  
-?-Q(a,b).
32  
-/
33  
-
34  
-Q(x,y) :- Test(y,x).
35  
-R(x,x) :- Q(x,y).
36  
-?-R(a,b).
37  
-/
38  
-
39  
-Q(x,y) :- Test(y,x).
40  
-R(y,x) :- Q(y,x).
41  
-?-R(a,b).
42  
-/
43  
-
44  
-Q(x,y) :- Test(x,y).
45  
-?-Q(a,b).
46  
-/
47  
-
48  
-Q(x,y) :- Test(x,y).
49  
-?-Q(a,b).
50  
-/
51  
-Q(x,y) :- Test(x,y).
52  
-?-Q(b).
53  
-/
54  
-
55  
-Q(x,y) :- Test(x,y) and x=1.
56  
-?-Q(b,c).
57  
-/
58  
-
59  
-Q(x) :- Test(x,y) and Test(x,e).
60  
-?-Q(b).
61  
-/
62  
-
63  
-Q(x,y,e) :- Test(x,y) and Test(x,e).
64  
-?-Q(b,c,d).
65  
-/
66  
-
67  
-Q(x) :- Test(x,x).
68  
-?-Q(b).
69  
-/
70  
-
71  
-Q(x,y,z,a,b) :- Names(x,y,z) and Marks(x,a,b).
72  
-?-Q(a,b,c,d,e).
73  
-/
74  
-
75  
-Q(x,y,z,a,b,u,i) :- Names(x,y,z) and Marks(x,a,b) and Names(x,u,i).
76  
-?-Q(a,b,c,d,e,f,g).
77  
-/
78  
-
79  
-Q(x,a,b):-Names(x,y,z) and Marks(x,a,b) and b = 75.
80  
-?-Q(x,a,b).
81  
-/
82  
-
83  
-Q(x,a,b):-Marks(x,a,b).
84  
-?-Q(x,a,b).
85  
-/
86  
-
87  
-Q(x,y,z,a,b,u,i) :- Names(x,y,z) and Marks(x,a,b) and Names(x,u,i) and b = 75 and x = 1.
88  
-?-Q(a,b,c,d,e,f,g).
89  
-/
90  
-
91  
-N(x,y,z) :- Names(x,y,z) and x = 1.
92  
-M(x,y,z) :- Marks(x,y,z) and z = 75.
93  
-Q(x,a,b,c,d) :- N(x,a,b) and M(x,c,d).
94  
-?-Q(x,a,b,c,d).
95  
-/
96  
-
97  
-Q(x,a,b):-Names(x,y,z) and Marks(x,a,b) and not b = 75.
98  
-?-Q(x,a,b).
99  
-/
100  
-
101  
-
102  
-Q(y):-Student(x,z,a) and not Enroll(x,y,b) and z=200.
103  
-R(x):-Enroll(a,x,b) and not Q(x).
104  
-?-R(x).
105  
-/
106  
-
107  
-Q(id,descr) :- Test(id,descr).
108  
-R(id,descr) :- Q(id,descr).
109  
-?-R(x,y,x).
110  
-/
111  
-
112  
-Q(id,descr) :- Test(id,descr).
113  
-R(id,descr) :- Q(id,descr,id).
114  
-?-R(x,y).
115  
-/
116  
-Q(y):-Student(x,z,a) and not Enroll(x,y,b) and Enroll(c,y,d) and not b=100.
117  
-R(x):-Enroll(a,x,b) and not Q(x).
118  
-?-R(x).
119  
-/
120  
-
121  
-Q(x,a,b):-Names(x,y,z) and Marks(x,a,b) and b = 75.
122  
-?-Q(x,a,b).
123  
-/
124  
-
125  
-Q(x,a,b):-Names(x,y,z) and Marks(x,a,b) and 75 = b.
126  
-?-Q(x,a,b).
127  
-/
128  
-
129  
-Q(y):-Enroll(a,y,b) and y = 100.
130  
-?-Q(y).
131  
-/
132  
-
133  
-Q(y):-Enroll(a,y,b) and not y = 100.
134  
-?-Q(y).
135  
-/
136  
-
137  
-Q(id,descr) :- Test(id,descr).
138  
-R(id,descr) :- Q(id,descr).
139  
-?-R(x,y,x).
140  
-/
141  
-
142  
-Q(id,descr) :- Test(id,descr).
143  
-R(id,descr) :- Q(id,descr,id).
144  
-?-R(x,y).
145  
-/
146  
-
147  
-Q(a,b,c) :- Test(a,b,c).
148  
-?-Q(id,descr,c).
149  
-/
150  
-
151  
-
152  
-Q(x,y):-Marks(x,y,x).
153  
-?-Q(x,y).
154  
-/
155  
-
156  
-Q(x,y,z):-Marks(x,y,z).
157  
-R(x,z):-Q(x,z,x).
158  
-?-R(x,y).
159  
-/
160  
-
161  
-Q(x,y,z):-Names(x,y,z) and not Marks(x,a,x).
162  
-?-Q(x,a,b).
163  
-/
164  
-
165  
-Q(x,y,z):-Marks(x,y,z).
166  
-?-Q(x,y,x).
167  
-/
168  
-
169  
-Q(x,y,z):-Marks(x,y,z) and x = z.
170  
-?-Q(x,y,z).
171  
-/
172  
-
173  
-Q(x,y,z):-Marks(x,y,z) and not x = z.
174  
-?-Q(x,y,z).
175  
-/
176  
-
177  
-Q(x,y,z):-Names(x,y,z) and not Marks(x,a,b) and x = b.
178  
-?-Q(x,a,b).
179  
-/
180  
-
181  
-Q(x,y,z):-Names(x,y,z) and not Marks(x,a,b) and not x = b.
182  
-?-Q(x,a,b).
183  
-/
184  
-
185  
-Q(x,y,z):-Names(x,y,z) and Marks(x,a,b) and x = b.
186  
-?-Q(x,a,b).
187  
-/
188  
-
189  
-Q(x,y,z):-Names(x,y,z) and Marks(x,a,b) and not x = b.
190  
-?-Q(x,a,b).
191  
-/
192  
-
193  
-Q(x,y,z):-Marks(x,y,z) and z < 50.
194  
-?-Q(x,y,z).
195  
-/
196  
-
197  
-Q(x,y,z):-Marks(x,y,z) and z >= 50.
198  
-?-Q(x,y,z).
199  
-/
200  
-
201  
-Q(x,y,z):-Marks(x,y,z) and x < z.
202  
-?-Q(x,y,z).
203  
-/
204  
-
205  
-Q(x,y,z):-Marks(x,y,z) and not x < z.
206  
-?-Q(x,y,z).
207  
-/
208  
-
209  
-Q(x,y,z):-Marks(x,y,z) and z <> 50.
210  
-?-Q(x,y,z).
211  
-/
212  
-
213  
-Q(x,y,z):-Marks(x,y,z) and not z <> 50.
214  
-?-Q(x,y,z).
215  
-/
216  
-
217  
-Q(x,y,z):-Marks(x,y,z) and z = 50.
218  
-?-Q(x,y,z).
219  
-/

0 notes on commit e8c7fec

Please sign in to comment.
Something went wrong with that request. Please try again.