-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuery.ml
127 lines (104 loc) · 3.31 KB
/
Query.ml
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
open Sexplib.Std
type file_name = string
[@@deriving sexp]
type equality_operator =
| Eq_equals
| Eq_not_equals
[@@deriving sexp]
type comparison_operator =
| Comp_greater_than
| Comp_greater_than_equals
| Comp_less_than_equals
| Comp_less_than
[@@deriving sexp]
type term_operator =
| Term_plus
| Term_minus
[@@deriving sexp]
type factor_operator =
| Factor_mult
| Factor_div
[@@deriving sexp]
type binary_operator =
| Eq of equality_operator
| Comp of comparison_operator
| Term of term_operator
| Factor of factor_operator
[@@deriving sexp]
type unary_operator =
| Unary_minus
[@@deriving sexp]
type column_name = string
[@@deriving sexp]
type table_name = string
[@@deriving sexp]
(*type row = 'a 'b map*)
type value =
| Bool of bool
| Number of float
| String of string
[@@deriving sexp]
type table =
Table of file_name
[@@deriving sexp]
type column =
Column_reference of (table_name option * column_name)
[@@deriving sexp]
type projection =
| All
| Columns of column list
[@@deriving sexp]
type expression =
| Nil
| Boolean_literal of bool
| String_literal of string
| Numeric_literal of float
| Reference of (table_name option * column_name)
| Binary of (binary_operator * expression * expression)
| Unary of (unary_operator * expression)
[@@deriving sexp]
type query =
Select of (projection * table * expression option)
[@@deriving sexp]
(*
example query: select e.name, d.name from employees.csv as e join departments.csv as d where d.name = accounting
*)
let print_table = function
| Table table_name -> table_name
let print_column = function
| Column_reference (Some table_name, column_name) -> table_name ^ "." ^ column_name
| Column_reference (None, column_name) -> column_name
let print_projection = function
| All -> "*"
| Columns columns ->
let column_names = List.map print_column columns in
String.concat ", " column_names
let escape_string = String.escaped
let print_binary_operator = function
| Eq Eq_equals -> "="
| Eq Eq_not_equals -> "!="
| Comp Comp_greater_than -> ">"
| Comp Comp_greater_than_equals -> ">="
| Comp Comp_less_than -> "<"
| Comp Comp_less_than_equals -> "<="
| Term Term_plus -> "+"
| Term Term_minus -> "-"
| Factor Factor_mult -> "*"
| Factor Factor_div -> "/"
let rec print_expression = function
| Nil -> "nil"
| Boolean_literal b -> string_of_bool b
| String_literal s -> "\"" ^ (escape_string s) ^ "\""
| Numeric_literal n -> string_of_float n
| Reference (None, cn) -> cn
| Reference (Some table_name, cn) -> table_name ^ "." ^ cn
| Binary (operator, e1, e2) -> "(" ^ print_expression e1 ^ " " ^ (print_binary_operator operator) ^ " " ^ (print_expression e2) ^ ")"
| Unary (_, e) -> "-(" ^ (print_expression e) ^ ")"
let to_query_string = function
| Select (projection, table, option_expression) ->
let projection_string = print_projection projection in
let table_string = print_table table in
let where_string = match option_expression with
| None -> ""
| Some e -> " where " ^ print_expression e in
"select " ^ projection_string ^ " from " ^ table_string ^ where_string