Module: SQLTree

Defined in:
lib/sql_tree.rb

Overview

The SQLTree module is the basic namespace for the sql_tree gem.

It contains the shorthand parse method (i.e. SQLTree[sql_query]) and some helper methods that are used by the gem. It also requires the necessary files for the gem to function properly.

Defined Under Namespace

Modules: Node Classes: Parser, Token, Tokenizer

Class Method Summary collapse

Class Method Details

.[](query, options = {}) ⇒ Object

The [] method is a shorthand for the SQLTree::Parser.parse method to parse an SQL query and return a SQL syntax tree.



25
26
27
# File 'lib/sql_tree.rb', line 25

def self.[](query, options = {})
  SQLTree::Parser.parse(query)
end

.const_missing(const) ⇒ Object

Loads constants in the SQLTree namespace using self.load_default_class_file(base, const)

const

The constant that is not yet loaded in the SQLTree namespace. This should be passed as a string or symbol.



10
11
12
# File 'lib/sql_tree.rb', line 10

def self.const_missing(const)
  load_default_class_file(SQLTree, const)
end

.load_default_class_file(base, const) ⇒ Object

Loads constants that reside in the SQLTree tree using the constant name and its base constant to determine the filename.

base

The base constant to load the constant from. This should be Foo when the constant Foo::Bar is being loaded.

const

The constant to load from the base constant as a string or symbol. This should be ‘Bar’ or :Bar when the constant Foo::Bar is being loaded.



18
19
20
21
# File 'lib/sql_tree.rb', line 18

def self.load_default_class_file(base, const)
  require "#{to_underscore("#{base.name}::#{const}")}"
  base.const_get(const) if base.const_defined?(const)
end

.to_camelcase(str) ⇒ Object

Convert a string/symbol in underscores (request_log_analyzer/controller) to camelcase (RequestLogAnalyzer::Controller). This can be used to find the class that is defined in a given filename.

str

The string to convert in the following format: module_name/class_name



39
40
41
# File 'lib/sql_tree.rb', line 39

def self.to_camelcase(str)
  str.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
end

.to_underscore(str) ⇒ Object

Convert a string/symbol in camelcase (RequestLogAnalyzer::Controller) to underscores (request_log_analyzer/controller) This function can be used to load the file (using require) in which the given constant is defined.

str

The string to convert in the following format: ModuleName::ClassName



32
33
34
# File 'lib/sql_tree.rb', line 32

def self.to_underscore(str)
  str.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
end