Module: Selekt

Extended by:
Selekt
Included in:
Selekt
Defined in:
lib/selekt.rb,
lib/selekt/sql.rb,
lib/selekt/version.rb

Defined Under Namespace

Modules: SQL Classes: ParseError, Query, SourceStub, StubError

Constant Summary collapse

RESERVED_SQL_KEYWORDS =
[
  'select', 'from', 'where', 'group', 'order', 'having', 'union', 'all',
  'limit', 'offset', 'as', 'by', 'with', 'distinct',
  'left', 'right', 'inner', 'full', 'outer', 'join', 'on', 'using', 'natural',
  'case', 'when', 'then', 'else', 'end', 'interval',
  'over', 'partition', 'range', 'rows', 'window'
]
VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#parse(sql) ⇒ Object



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

def parse(sql)
  Selekt::Query.new(sql)    
end

#parserObject



18
19
20
21
22
23
# File 'lib/selekt.rb', line 18

def parser
  @parser ||= begin
    Treetop.load(File.expand_path('./selekt/sql.treetop', File.dirname(__FILE__)))
    Selekt::SQLParser.new
  end
end

#quote(val) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/selekt.rb', line 33

def quote(val)
  case val
    when NilClass; 'NULL'
    when TrueClass; 'TRUE'
    when FalseClass; 'FALSE'
    when Numeric; val.to_s
    when String; "'" + val.gsub("'", "''") + "'"
    when DateTime, Time; quote(val.strftime('%F %X')) + '::timestamp'
    when Date; quote(val.strftime('%F')) + '::date'
    else raise "Don't know how to quote #{val.inspect}!"
  end
end

#safe_identifier(id) ⇒ Object



29
30
31
# File 'lib/selekt.rb', line 29

def safe_identifier(id)
  id =~ /\A[a-z][a-z0-9_]*\z/i ? id : '"' + id.gsub('"', '""') + '"'
end