-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdb_serializator.rb
42 lines (39 loc) · 1.16 KB
/
db_serializator.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
# DATATYPES = {
# "CHAR" => "string" ,
# "INTEGER" => "integer" ,
# "DATETIME" => "datetime",
# "TEXT" => "text" ,
# "DATE" => "date" ,
# "TIME" => "time" ,
# "BINARY" => "boolean"
# }
def serialize(filepath)
lines = IO.readlines(filepath)
tables = []
table = {}
row = {}
lines.each do |line|
if line =~/<table/
table = { name: line.match(/name=\"(\w+)\"/)[1],
x: line.match(/x=\"(\d+)\"/)[1],
y: line.match(/y=\"(\d+)\"/)[1],
rows: [] }
elsif line =~ /<row/
row_name = line.match(/name=\"(\w+)\"/)[1]
pk = line =~ /autoincrement=\"1\"/ ? true : false
row = { name: row_name, primary_key: pk }
elsif line =~ /<datatypes/ && !(line =~ /db="mysql"/)
row[:datatype] = line.match(/<datatypes>(\w+)<\/datatypes>/)[1]
elsif line =~ /<default/
row[:default_value] = line.match(/<default>(.+)<\/default>/)[1]
elsif line =~ /<relation/
row[:relation] = line.match(/<relation table=\"(\w+)\"/)[1]
elsif line =~ /<\/row>/
table[:rows] << row
elsif line =~ /<\/table>/
tables << table
table = {}
end
end
return tables
end