1
|
|
|
import {DatabaseInterface, ModelInterface, ModelStaticInterface} from "../JeloquentInterfaces"; |
2
|
|
|
import Collection from "./Collection"; |
3
|
|
|
import Table from "./Table"; |
4
|
|
|
|
5
|
|
|
export default class Database implements DatabaseInterface { |
6
|
|
|
|
7
|
|
|
private _name: string; |
8
|
|
|
|
9
|
|
|
private _tables: Map<string, Table>; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* @param name |
14
|
|
|
* @param models |
15
|
|
|
*/ |
16
|
|
|
constructor(name, models:Array<ModelStaticInterface>) { |
17
|
|
|
this._name = name; |
18
|
|
|
this._tables = new Map(); |
19
|
|
|
|
20
|
|
|
models.forEach((model: ModelStaticInterface) => { |
21
|
|
|
this.register(model); |
22
|
|
|
}); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
get name(): string { |
26
|
|
|
return this._name; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
addIndex(table:string, indexName:string, lookUpKey:string, id:string|number): void { |
30
|
|
|
this.table(table).addIndex(indexName, lookUpKey, id) |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
all(table: string): Collection { |
34
|
|
|
return this.table(table).all(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
allModels(table): Map<string|number, ModelInterface> { |
38
|
|
|
return this.table(table).allModels(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
delete(table:string, id:number|string): void { |
42
|
|
|
this.table(table).delete(id); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
drop(table: string): void { |
46
|
|
|
this._tables.delete(table); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
find(table:string, id:number|string|object|Array<string|number|object>): Collection|ModelInterface|null { |
50
|
|
|
return this.table(table).find(id); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
getIndexByKey(table: string, indexName: string): Map<string|number, Set<string|number>> { |
54
|
|
|
return this.table(table).getIndexByKey(indexName); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
ids(table: string): Array<string|number> { |
58
|
|
|
return this.table(table).ids; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
indexes(table: string): Map<string, Map<string|number, Set<string|number>>> { |
62
|
|
|
return this.table(table).indexes; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
insert(table: string, model: ModelInterface): void { |
66
|
|
|
this.table(table).insert(model); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @todo Build better way of parsing queries; |
71
|
|
|
*/ |
72
|
|
|
query(sql) { |
73
|
|
|
const sqlParts = sql.match(/^((SELECT)|(INSERT)|(DELETE))\s+(.*)\s+FROM\s+([^\s]+)(\s+WHERE\s+([^\s]+)\s+(=)\s+([^\s+]))?((\s+)|;)?$/i); |
74
|
|
|
|
75
|
|
|
if (sqlParts.length === 0) { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
const action = sqlParts[1]; |
80
|
|
|
//const fields = sqlParts[5].split(','); |
81
|
|
|
const table = sqlParts[6] |
82
|
|
|
const matchField = sqlParts[8]; |
83
|
|
|
const matchValue = sqlParts[10]; |
84
|
|
|
|
85
|
|
|
if (matchField === 'id') { |
86
|
|
|
return this[action.toLowerCase()](table, matchValue); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (matchField === undefined && action === 'SELECT') { |
90
|
|
|
return this.all(table); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
register(model: ModelStaticInterface) { |
97
|
|
|
const table = new Table(model); |
98
|
|
|
this._tables.set(table.name, table); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
registerIndex(table: string, name: string): void { |
102
|
|
|
this.table(table).registerIndex(name); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
removeIndex(table: string, indexName: string, lookUpKey: string, id:string|number): void { |
106
|
|
|
this.table(table).removeIndex(indexName, lookUpKey, id); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
save(table: string, model: ModelInterface): void { |
110
|
|
|
this.table(table).save(model); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
setIndexes(): void { |
114
|
|
|
this._tables.forEach((table) => { |
115
|
|
|
table.setupIndexes(); |
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
showTables(): Array<string> { |
120
|
|
|
return [...this._tables.keys()]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
truncate(table: string): void { |
124
|
|
|
this.table(table).truncate(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
update(table: string, model: ModelInterface): void { |
128
|
|
|
this.table(table).update(model); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
private table(name:string): Table { |
132
|
|
|
return this._tables.get(name); |
133
|
|
|
} |
134
|
|
|
} |