-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.rb
188 lines (152 loc) · 5.09 KB
/
sqlite.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# encoding: UTF-8
#
# sqlite.rb - SQLite module for GLaDZIOS - IRC bot for HSCracow
#
#
# (C) 2012 Wiktor Przybylski
# (C) 2012 Jakub Skrzypnik
#
# This program is licensed under GNU General Public License
#
# ProTip(tm) - Jak robisz większy kod, to dawaj komentarze, do cholery!
require 'sqlite3'
require 'time' #i highly require some time...
class Sqlite
def exe(file, query)
begin
database = SQLite3::Database.open file # Self explanatory
database.execute(query) #
puts "Executing that query" #
rescue SQLite3::Exception => e # Exception catching
puts "Exception occured" #
puts e # Which exception is that
ensure
database.close if database
puts "Database closed after executing"
end
end
def sel(file, query)
begin
table = []
database = SQLite3::Database.open file
request = database.prepare(query)
returned = request.execute
returned.each do |row|
row.join "\s"
table += row
end
puts "#{table} - 43"
return table # Problematic piece of code, Skrzyp help meh
rescue SQLite3::Exception => e # But i tried to get exception outside rescue, and it didn't work well
puts "#{e} - line 61"
ensure
if request then request.close end
if database then database.close end
end
end
def memo_table_generate(file)
database = SQLite3::Database.open file
database.execute "CREATE TABLE 'memo' ('id' int(11) PRIMARY KEY,'time' int(11) NOT NULL,'sender' varchar(10) NOT NULL,'receiver' varchar(10) NOT NULL,'memo' varchar(255) NOT NULL);"
puts "Memo table generated!"
#< if database then database.close puts "m-OK." end >#
end
def seen_table_generate(file)
database = SQLite3::Database.open file
database.execute "CREATE TABLE 'seen' ('who' varchar(10) NOT NULL,'time' int(11) NOT NULL,'content' varchar(255) NOT NULL);"
puts "Seen table generated!"
#< if database then database.close puts "s-OK." end >#
end
def describe_table_generate(file)
database = SQLite3::Database.open file
database.execute "CREATE TABLE 'desc' ('command' varchar(20) NOT NULL,'content' varchar(255) NOT NULL);"
puts "Seen table generated!"
#< if database then database.close puts "s-OK." end >#
end
def check(file)
begin
if !File.exists?(file) then
database = SQLite3::Database.new file
puts "Database generated!"
end
database = SQLite3::Database.open file
puts "Database opened!"
query = sel(file, 'SELECT * FROM sqlite_master WHERE type = "table" ; ')
#puts "line 93 - #{query}" # cuz it's only for debugging, i don't want crap in my log
if query.kind_of?(Array) == true then # sel() should return an array, if it doesn't, it's exception return that i want to mess with
puts "Checked for tables, it has them" #
return "OK" #cuz it's only for debugging, i don't want crap in my log "
#puts "Here you are" + query.to_s # cuz it's only for debugging, i don't want crap in my log
else
if query == 0 then
puts "No tables, i will make both"
memo_table_generate(file)
seen_table_generate(file)
return "GEN"
end
#if query =~ /notable_memo/ then
# puts "No memo table around, i should make it"
# memo_table_generate(file)
#end
#if query =~ /notable_seen/ then
# puts "No seen table around, i should make it"
# seen_table_generate(file)
#end
end
ensure
database.close if database
puts "Database closed!"
end
end
def seen_check_base(arg)
base = "base.db"
base_check = "WTF"
until base_check == "OK" # until base check don't return that it's generated and working keep checking&generating it
base_check = check(base)
puts "Zwrocila #{base_check}" # and inform me about it
end
ask = "SELECT * FROM seen WHERE who=\"#{arg}\""
checked = sel(base,ask)
return checked
end
def seen_check(who)
if seen_check_base(who) == 0 then return "Never seen before"
else
table = seen_check_base(who)
return table
end
end
def seen_add(who,what,time)
if seen_check_base(who) == 0
query = "INSERT INTO seen VALUES (\"#{who}\", \"#{what}\", #{time})"
else
query = "UPDATE seen SET time=\"#{time}\", content=\"#{what}\" WHERE who=\"#{who}\""
end
exe("base.db", query)
end
def seen_check_base(arg)
base = "base.db"
base_check = "WTF"
until base_check == "OK" # until base check don't return that it's generated and working keep checking&generating it
base_check = check(base)
puts "Zwrocila #{base_check}" # and inform me about it
end
ask = "SELECT * FROM desc WHERE command=\"#{arg}\""
checked = sel(base,ask)
return checked
end
def describe_check(what)
if describe_check_base(what) == 0 then return "No such command"
else
table = describe_check_base(what)
return table
end
end
def describe_add(command,content)
if describe_check_base(who) == 0
query = "INSERT INTO desc VALUES (\"#{command}\", \"#{content}\")"
else
query = "UPDATE desc SET content=\"#{content}\" WHERE command=\"#{command}\""
end
exe("base.db", query)
end
end