module Riddle::Query
Constants
- ESCAPE_CHARACTERS
- ESCAPE_WORDS
- MYSQL2_ESCAPE
Public Class Methods
begin()
click to toggle source
# File lib/riddle/query.rb, line 49 def self.begin 'BEGIN' end
collation()
click to toggle source
# File lib/riddle/query.rb, line 41 def self.collation 'SHOW COLLATION' end
commit()
click to toggle source
# File lib/riddle/query.rb, line 53 def self.commit 'COMMIT' end
connection(address = '127.0.0.1', port = 9312)
click to toggle source
# File lib/riddle/query.rb, line 8 def self.connection(address = '127.0.0.1', port = 9312) require 'mysql2' # If you use localhost, MySQL insists on a socket connection, but Sphinx # requires a TCP connection. Using 127.0.0.1 fixes that. address = '127.0.0.1' if address == 'localhost' Mysql2::Client.new( :host => address, :port => port ) end
create_function(name, type, file)
click to toggle source
# File lib/riddle/query.rb, line 79 def self.create_function(name, type, file) type = type.to_s.upcase "CREATE FUNCTION #{name} RETURNS #{type} SONAME #{quote file}" end
describe(index)
click to toggle source
# File lib/riddle/query.rb, line 45 def self.describe(index) "DESCRIBE #{index}" end
drop_function(name)
click to toggle source
# File lib/riddle/query.rb, line 84 def self.drop_function(name) "DROP FUNCTION #{name}" end
escape(string)
click to toggle source
# File lib/riddle/query.rb, line 107 def self.escape(string) string.gsub(ESCAPE_CHARACTERS) { |match| "\\#{match}" } .gsub(ESCAPE_WORDS) { |word| "\\#{word}" } end
meta()
click to toggle source
# File lib/riddle/query.rb, line 21 def self.meta 'SHOW META' end
quote(string)
click to toggle source
# File lib/riddle/query.rb, line 112 def self.quote(string) "'#{sql_escape string}'" end
rollback()
click to toggle source
# File lib/riddle/query.rb, line 57 def self.rollback 'ROLLBACK' end
set(variable, values, global = true)
click to toggle source
# File lib/riddle/query.rb, line 61 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)
click to toggle source
# File lib/riddle/query.rb, line 66 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)
click to toggle source
# File lib/riddle/query.rb, line 116 def self.sql_escape(string) return Mysql2::Client.escape(string) if MYSQL2_ESCAPE string.gsub(/['"\\]/) { |character| "\\#{character}" } end
status()
click to toggle source
# File lib/riddle/query.rb, line 29 def self.status 'SHOW STATUS' end
tables()
click to toggle source
# File lib/riddle/query.rb, line 33 def self.tables 'SHOW TABLES' end
translate_value(value)
click to toggle source
# File lib/riddle/query.rb, line 96 def self.translate_value(value) case value when TrueClass 1 when FalseClass 0 else value end end
update(index, id, values = {})
click to toggle source
# File lib/riddle/query.rb, line 88 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()
click to toggle source
# File lib/riddle/query.rb, line 37 def self.variables 'SHOW VARIABLES' end
warnings()
click to toggle source
# File lib/riddle/query.rb, line 25 def self.warnings 'SHOW WARNINGS' end