Module: SQL::Maker::Util

Included in:
SQL::Maker, Condition, Select, SelectSet, QueryMaker
Defined in:
lib/sql/maker/util.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bind_param(sql, bind) ⇒ Object

Raises:



48
49
50
51
# File 'lib/sql/maker/util.rb', line 48

def bind_param(sql, bind)
  raise SQL::Maker::Error.new('bind arity mismatch') if sql.count('?') != bind.size
  i = -1; sql.gsub('?') { SQL::Maker::Quoting.quote(bind[i+=1]) }
end

.included(klass) ⇒ Object



5
6
7
# File 'lib/sql/maker/util.rb', line 5

def self.included(klass)
  klass.extend(self)
end

.quote_identifier(label, quote_char, name_sep) ⇒ Object



40
41
42
43
44
# File 'lib/sql/maker/util.rb', line 40

def quote_identifier(label, quote_char, name_sep)
  return label if label == '*';
  return label unless name_sep;
  label.to_s.split(/#{Regexp.escape(name_sep)}/).map {|e| e == '*' ? e : "#{quote_char}#{e}#{quote_char}" }.join(name_sep)
end

Instance Method Details

#array_wrap(val) ⇒ Object



13
14
15
# File 'lib/sql/maker/util.rb', line 13

def array_wrap(val)
  val.is_a?(Array) ? val : [val]
end

#bind_param(sql, bind) ⇒ Object

Raises:



48
49
50
51
# File 'lib/sql/maker/util.rb', line 48

def bind_param(sql, bind)
  raise SQL::Maker::Error.new('bind arity mismatch') if sql.count('?') != bind.size
  i = -1; sql.gsub('?') { SQL::Maker::Quoting.quote(bind[i+=1]) }
end

#croak(message) ⇒ Object

Raises:



9
10
11
# File 'lib/sql/maker/util.rb', line 9

def croak(message)
  raise SQL::Maker::Error.new(message)
end

#parse_args(*args) ⇒ Object

perl-like argument parser of my ($a, $b) = @_;



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sql/maker/util.rb', line 18

def parse_args(*args)
  if args.size > 1
    # method('a', 'b') #=> ['a', 'b']
    return args
  else
    args = args.first
    case args
    when Hash
      # method('a' => 'b') #=> ['a', 'b']
      # method('a' => ['b', 'c']) #=> ['a', ['b', 'c']]
      # method('a' => {'b' => 'c'}) #=> ['a', {'b' => 'c'}]
      args.each.first
    when Array
      # method(['a', 'b']) #=> ['a', 'b']
      return args
    else
      # method('a') #=> ['a', nil]
      return [args, nil]
    end
  end
end

#quote_identifier(label, quote_char, name_sep) ⇒ Object



40
41
42
43
44
# File 'lib/sql/maker/util.rb', line 40

def quote_identifier(label, quote_char, name_sep)
  return label if label == '*';
  return label unless name_sep;
  label.to_s.split(/#{Regexp.escape(name_sep)}/).map {|e| e == '*' ? e : "#{quote_char}#{e}#{quote_char}" }.join(name_sep)
end