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/
- MYSQL2_ESCAPE =
defined?(Mysql2) && defined?(Mysql::Client)
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
50
51
52
|
# File 'lib/riddle/query.rb', line 50
def self.begin
'BEGIN'
end
|
.collation ⇒ Object
42
43
44
|
# File 'lib/riddle/query.rb', line 42
def self.collation
'SHOW COLLATION'
end
|
.commit ⇒ Object
54
55
56
|
# File 'lib/riddle/query.rb', line 54
def self.commit
'COMMIT'
end
|
.connection(address = '127.0.0.1', port = 9312) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/riddle/query.rb', line 9
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
80
81
82
83
|
# File 'lib/riddle/query.rb', line 80
def self.create_function(name, type, file)
type = type.to_s.upcase
"CREATE FUNCTION #{name} RETURNS #{type} SONAME #{quote file}"
end
|
.describe(index) ⇒ Object
46
47
48
|
# File 'lib/riddle/query.rb', line 46
def self.describe(index)
"DESCRIBE #{index}"
end
|
.drop_function(name) ⇒ Object
85
86
87
|
# File 'lib/riddle/query.rb', line 85
def self.drop_function(name)
"DROP FUNCTION #{name}"
end
|
.escape(string) ⇒ Object
108
109
110
111
|
# File 'lib/riddle/query.rb', line 108
def self.escape(string)
string.gsub(ESCAPE_CHARACTERS) { |match| "\\#{match}" }
.gsub(ESCAPE_WORDS) { |word| "\\#{word}" }
end
|
22
23
24
|
# File 'lib/riddle/query.rb', line 22
def self.meta
'SHOW META'
end
|
.quote(string) ⇒ Object
113
114
115
|
# File 'lib/riddle/query.rb', line 113
def self.quote(string)
"'#{sql_escape string}'"
end
|
.rollback ⇒ Object
58
59
60
|
# File 'lib/riddle/query.rb', line 58
def self.rollback
'ROLLBACK'
end
|
.set(variable, values, global = true) ⇒ Object
62
63
64
65
|
# File 'lib/riddle/query.rb', line 62
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
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/riddle/query.rb', line 67
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
117
118
119
120
121
|
# File 'lib/riddle/query.rb', line 117
def self.sql_escape(string)
return Mysql2::Client.escape(string) if MYSQL2_ESCAPE
string.gsub(/['"\\]/) { |character| "\\#{character}" }
end
|
.status ⇒ Object
30
31
32
|
# File 'lib/riddle/query.rb', line 30
def self.status
'SHOW STATUS'
end
|
.tables ⇒ Object
34
35
36
|
# File 'lib/riddle/query.rb', line 34
def self.tables
'SHOW TABLES'
end
|
.translate_value(value) ⇒ Object
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/riddle/query.rb', line 97
def self.translate_value(value)
case value
when TrueClass
1
when FalseClass
0
else
value
end
end
|
.update(index, id, values = {}) ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/riddle/query.rb', line 89
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
38
39
40
|
# File 'lib/riddle/query.rb', line 38
def self.variables
'SHOW VARIABLES'
end
|
.warnings ⇒ Object
26
27
28
|
# File 'lib/riddle/query.rb', line 26
def self.warnings
'SHOW WARNINGS'
end
|