Module: Riddle::Query
- Defined in:
- lib/riddle/query.rb
Defined Under Namespace
Classes: Delete, Insert, Select
Constant Summary
collapse
- ESCAPE_CHARACTERS =
/[\(\)\|\-!@~\/"\/\^\$\\><&=\?]/
- ESCAPE_WORDS =
/\b(?:MAYBE|NEAR|PARAGRAPH|SENTENCE|ZONE|ZONESPAN)\b/
Class Method Summary
collapse
-
.begin ⇒ Object
-
.collation ⇒ Object
-
.commit ⇒ Object
-
.connection(address = '127.0.0.1', port = 9312) ⇒ Object
-
.create_function(name, type, file) ⇒ Object
-
.describe(index) ⇒ Object
-
.drop_function(name) ⇒ Object
-
.escape(string) ⇒ Object
-
.meta ⇒ Object
-
.quote(string) ⇒ Object
-
.rollback ⇒ Object
-
.set(variable, values, global = true) ⇒ Object
-
.snippets(data, index, query, options = nil) ⇒ Object
-
.sql_escape(string) ⇒ Object
-
.status ⇒ Object
-
.tables ⇒ Object
-
.translate_value(value) ⇒ Object
-
.update(index, id, values = {}) ⇒ Object
-
.variables ⇒ Object
-
.warnings ⇒ Object
Class Method Details
.begin ⇒ Object
47
48
49
|
# File 'lib/riddle/query.rb', line 47
def self.begin
'BEGIN'
end
|
.collation ⇒ Object
39
40
41
|
# File 'lib/riddle/query.rb', line 39
def self.collation
'SHOW COLLATION'
end
|
.commit ⇒ Object
51
52
53
|
# File 'lib/riddle/query.rb', line 51
def self.commit
'COMMIT'
end
|
.connection(address = '127.0.0.1', port = 9312) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/riddle/query.rb', line 6
def self.connection(address = '127.0.0.1', port = 9312)
require 'mysql2'
address = '127.0.0.1' if address == 'localhost'
Mysql2::Client.new(
:host => address,
:port => port
)
end
|
.create_function(name, type, file) ⇒ Object
77
78
79
80
|
# File 'lib/riddle/query.rb', line 77
def self.create_function(name, type, file)
type = type.to_s.upcase
"CREATE FUNCTION #{name} RETURNS #{type} SONAME #{quote file}"
end
|
.describe(index) ⇒ Object
43
44
45
|
# File 'lib/riddle/query.rb', line 43
def self.describe(index)
"DESCRIBE #{index}"
end
|
.drop_function(name) ⇒ Object
82
83
84
|
# File 'lib/riddle/query.rb', line 82
def self.drop_function(name)
"DROP FUNCTION #{name}"
end
|
.escape(string) ⇒ Object
105
106
107
108
|
# File 'lib/riddle/query.rb', line 105
def self.escape(string)
string.gsub(ESCAPE_CHARACTERS) { |match| "\\#{match}" }
.gsub(ESCAPE_WORDS) { |word| "\\#{word}" }
end
|
19
20
21
|
# File 'lib/riddle/query.rb', line 19
def self.meta
'SHOW META'
end
|
.quote(string) ⇒ Object
110
111
112
|
# File 'lib/riddle/query.rb', line 110
def self.quote(string)
"'#{sql_escape string}'"
end
|
.rollback ⇒ Object
55
56
57
|
# File 'lib/riddle/query.rb', line 55
def self.rollback
'ROLLBACK'
end
|
.set(variable, values, global = true) ⇒ Object
59
60
61
62
|
# File 'lib/riddle/query.rb', line 59
def self.set(variable, values, global = true)
values = "(#{values.join(', ')})" if values.is_a?(Array)
"SET#{ ' GLOBAL' if global } #{variable} = #{values}"
end
|
.snippets(data, index, query, options = nil) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/riddle/query.rb', line 64
def self.snippets(data, index, query, options = nil)
data, index, query = quote(data), quote(index), quote(query)
options = ', ' + options.keys.collect { |key|
value = translate_value options[key]
value = quote value if value.is_a?(String)
"#{value} AS #{key}"
}.join(', ') unless options.nil?
"CALL SNIPPETS(#{data}, #{index}, #{query}#{options})"
end
|
.sql_escape(string) ⇒ Object
114
115
116
117
118
|
# File 'lib/riddle/query.rb', line 114
def self.sql_escape(string)
return Mysql2::Client.escape(string) if defined?(Mysql2)
string.gsub(/['"\\]/) { |character| "\\#{character}" }
end
|
.status ⇒ Object
27
28
29
|
# File 'lib/riddle/query.rb', line 27
def self.status
'SHOW STATUS'
end
|
.tables ⇒ Object
31
32
33
|
# File 'lib/riddle/query.rb', line 31
def self.tables
'SHOW TABLES'
end
|
.translate_value(value) ⇒ Object
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/riddle/query.rb', line 94
def self.translate_value(value)
case value
when TrueClass
1
when FalseClass
0
else
value
end
end
|
.update(index, id, values = {}) ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/riddle/query.rb', line 86
def self.update(index, id, values = {})
values = values.keys.collect { |key|
"#{key} = #{translate_value values[key]}"
}.join(', ')
"UPDATE #{index} SET #{values} WHERE id = #{id}"
end
|
.variables ⇒ Object
35
36
37
|
# File 'lib/riddle/query.rb', line 35
def self.variables
'SHOW VARIABLES'
end
|
.warnings ⇒ Object
23
24
25
|
# File 'lib/riddle/query.rb', line 23
def self.warnings
'SHOW WARNINGS'
end
|