-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableDefinition.ml
84 lines (68 loc) · 2.3 KB
/
TableDefinition.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
module TableDefinition = struct
exception UndefinedType;;
exception UndefinedKeyType;;
open ColumnDefinition.ColumnDefinition;;
type tableStorageType = Temporary | Permanent;;
(*TODO: cleanup this type *)
type tableExists = NOT_EXISTS;;
exception InvalidStatement;;
type table = {
tableName : string;
columns : column list;
constraints : dbConstraint list;
primaryKeys: string list;
indexes : string list;
uniqueKeys: string list
}
type createStatement = {
tableName: string;
columns : column list;
tableExists: tableExists;
tableStorage : tableStorageType
};;
type statement = NoStatement | SelectStatement | InsertStatement | CreateStatement of createStatement;;
let printTableExists a = match a with NOT_EXISTS -> "Not Exists"
let printStorageType a =
match a with
Temporary -> "Temporory"
|Permanent -> "Permanent";;
let printStatement aStatement = match aStatement with
NoStatement -> "NoStatement"
|SelectStatement -> "SelectStatement"
| InsertStatement -> "InsertStatement"
| CreateStatement(c) -> Printf.sprintf "%s -> %s: %s" c.tableName (printTableExists c.tableExists)
(printStorageType c.tableStorage);;
let printStatements aStatementList =
let result = List.fold_left( fun accum x -> accum ^ (printStatement x) ^ "\n") ""
aStatementList in
result;;
let createCreateStatement tableStorage tableExists tableName columns=
let result = CreateStatement({
tableStorage = tableStorage;
tableName = tableName;
columns = columns;
tableExists = tableExists
}) in result;;
let getPrimaryKeyIds aTable =
List.filter (fun x ->
match x.definition with
|ColumnDef(d) ->d.primaryKey
|_ -> false) aTable.columns ;;
(* Ignore primary keys and foreign keys *)
let tableAttributes aKeyColumn aTable =
let aForeignKey = "ForeignKey" in
let primaryKey = "PrimaryKey" in
let uniqueKey = "UniqueKey" in
List.filter (fun aColumn -> not ((aColumn.name = aKeyColumn.name) or
(aColumn.name = aForeignKey) or (aColumn.name = primaryKey)
or(aColumn.name = uniqueKey))) aTable.columns;;
let allTableAttributes aTable =
let aForeignKey = "ForeignKey" in
let primaryKey = "PrimaryKey" in
let uniqueKey = "UniqueKey" in
List.filter( fun aColumn -> not (
(aColumn.name = aForeignKey)
or (aColumn.name = primaryKey)
or (aColumn.name = uniqueKey))
) aTable.columns;;
end