/
database.rb
157 lines (127 loc) · 3.2 KB
/
database.rb
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
module MiniSQL
class Database
require 'minisql/catalog'
require 'minisql/parser'
class Records
require 'minisql/record'
def initialize filename, catalog
@root = Pathname(filename) + 'records'
require 'fileutils'
FileUtils.mkdir_p @root
@catalog = catalog
@records = {}
catalog.tables.each do |table|
add_record(table)
end
end
attr_reader :catalog
def create_table info
add_record info[:name]
[]
end
def select info
records[info[:table]].select(info[:columns], info[:where])
end
def insert_into info
records[info[:table]].insert_record(info[:values])
[]
end
def delete_from info
records[info[:table]].delete_from(info[:where])
[]
end
def execute info
command = info.first
return send(command, info[1]) if respond_to? command
[]
end
protected
attr_reader :records, :root
def add_record table
records[table] = get_record(table)
end
def get_record table
table_info = catalog.table_info(table)
buffer = CachedBuffer.new root + table.to_s
records[table] = Record.new(table_info, buffer,
lambda do |new_table_info|
@catalog.drop_table table
@catalog.create_table new_table_info
end)
end
end
def initialize filename
@catalog = Catalog.new filename
@records = Records.new filename, @catalog
@parser = SQLParser.new
end
attr_reader :catalog
# create_table :table do
# column[:co1].char(16).unique
# column[:co2].int
# column[:co3].float
# primary_key :co2
# end
def create_table name, &block
require 'minisql/dsl/schema'
schema = DSL::Schema.new(name)
schema.instance_eval(&block)
command = schema.dump
execute command
end
def drop_table table
execute "DROP TABLE #{table};"
end
def create_index name, table, column
execute "CREATE INDEX #{name} ON #{table} ( #{column} );"
end
def drop_index name
execute "DROP INDEX #{name};"
end
# rows = select['*'].from(:table)
#
# or
#
# rows = select['*'].from(:table).where do
# column[:co1] == ...
# column[:co2] < ...
# column[:co3] >= ...
# end
#
# rows.each ... # no sql are executed until here
def select
require 'minisql/dsl/selector'
DSL::Selector.new self
end
def insert_into table, values
execute "INSERT INTO #{table} VALUES ( #{values.map(&:inspect).join(', ')} );"
end
# delete_from(:tbl)
#
# OR
#
# delete_from(:tbl) do
# column[:co1] == ...
# ...
# end
def delete_from table, &block
require 'minisql/dsl/deletor'
DSL::Deletor.new self, table, &block
end
def execute command
begin
hash = @parser.parse(command).compile
rescue
raise "Parse error"
end
@catalog.execute hash
@records.execute hash
end
def tables
@catalog.tables
end
def eval &block
instance_eval &block
end
end
end