Class: SqlStmt

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlstmt/sqlstmt.rb

Defined Under Namespace

Classes: Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSqlStmt

Returns a new instance of SqlStmt.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sqlstmt/sqlstmt.rb', line 8

def initialize
  @stmt_type = nil
  @tables = []
  @joins = []
  @wheres = []
  @where_behavior = :require
  @fields = []
  @values = []
  @group_by = nil
  @order_by = nil
  @limit = nil
  @having = []
  @into_table = nil
  @rows = []
  @tables_to_delete = []
  @distinct = nil
  @straight_join = nil
  @replace = nil
  @ignore = ''
  @outfile = ''
  @with_rollup = nil
  # track this explicitly to guarantee get is not used with non-select statements
  @called_get = false
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/sqlstmt/sqlstmt.rb', line 5

def fields
  @fields
end

#joinsObject (readonly)

Returns the value of attribute joins.



5
6
7
# File 'lib/sqlstmt/sqlstmt.rb', line 5

def joins
  @joins
end

#tablesObject (readonly)

Returns the value of attribute tables.



5
6
7
# File 'lib/sqlstmt/sqlstmt.rb', line 5

def tables
  @tables
end

#wheresObject (readonly)

Returns the value of attribute wheres.



5
6
7
# File 'lib/sqlstmt/sqlstmt.rb', line 5

def wheres
  @wheres
end

Instance Method Details

#add_row(row) ⇒ Object

used with INSERT VALUES statements only



157
158
159
# File 'lib/sqlstmt/sqlstmt.rb', line 157

def add_row(row)
  @rows << row
end

#delete(*tables) ⇒ Object



64
65
66
67
68
69
# File 'lib/sqlstmt/sqlstmt.rb', line 64

def delete(*tables)
  ensure_no_statement_type
  @stmt_type = 'delete'
  @tables_to_delete = tables
  return self
end

#distinctObject

less commonly used methods



169
170
171
172
# File 'lib/sqlstmt/sqlstmt.rb', line 169

def distinct
  @distinct = true
  return self
end

#get(*exprs) ⇒ Object



103
104
105
106
107
# File 'lib/sqlstmt/sqlstmt.rb', line 103

def get(*exprs)
  @fields.concat(exprs)
  @called_get = true
  return self
end

#group_by(expr) ⇒ Object



125
126
127
128
# File 'lib/sqlstmt/sqlstmt.rb', line 125

def group_by(expr)
  @group_by = expr
  return self
end

#having(*expr) ⇒ Object



140
141
142
143
# File 'lib/sqlstmt/sqlstmt.rb', line 140

def having(*expr)
  @having.concat(expr)
  return self
end

#ignoreObject



184
185
186
187
# File 'lib/sqlstmt/sqlstmt.rb', line 184

def ignore
  @ignore = 'IGNORE '
  return self
end

#includes_table?(table_to_find) ⇒ Boolean

methods to analyze what the statement contains

Returns:

  • (Boolean)


195
196
197
198
199
# File 'lib/sqlstmt/sqlstmt.rb', line 195

def includes_table?(table_to_find)
  retval = @tables.find { |table| (table.name == table_to_find) || (table.alias == table_to_find) }
  retval ||= @joins.find { |_, table, _| table_names_match?(table, table_to_find) }
  return retval
end

#initialize_copy(orig) ⇒ Object



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

def initialize_copy(orig)
  super
  @tables = @tables.dup
  @joins = @joins.dup
  @wheres = @wheres.dup
  @fields = @fields.dup
  @values = @values.dup
  @having = @having.dup
  @rows = @rows.dup
end

#insertObject



58
59
60
61
62
# File 'lib/sqlstmt/sqlstmt.rb', line 58

def insert
  ensure_no_statement_type
  @stmt_type = 'insert'
  return self
end

#into(into_table) ⇒ Object

used with INSERT statements only



151
152
153
154
# File 'lib/sqlstmt/sqlstmt.rb', line 151

def into(into_table)
  @into_table = into_table
  return self
end

#join(table, *exprs) ⇒ Object



80
81
82
# File 'lib/sqlstmt/sqlstmt.rb', line 80

def join(table, *exprs)
  return add_join('JOIN', table, exprs)
end

#left_join(table, *exprs) ⇒ Object



84
85
86
# File 'lib/sqlstmt/sqlstmt.rb', line 84

def left_join(table, *exprs)
  return add_join('LEFT JOIN', table, exprs)
end

#limit(clause) ⇒ Object



135
136
137
138
# File 'lib/sqlstmt/sqlstmt.rb', line 135

def limit(clause)
  @limit = clause
  return self
end

#no_whereObject



93
94
95
96
# File 'lib/sqlstmt/sqlstmt.rb', line 93

def no_where
  @where_behavior = :exclude
  return self
end

#optional_whereObject



98
99
100
101
# File 'lib/sqlstmt/sqlstmt.rb', line 98

def optional_where
  @where_behavior = :optional
  return self
end

#order_by(expr) ⇒ Object



130
131
132
133
# File 'lib/sqlstmt/sqlstmt.rb', line 130

def order_by(expr)
  @order_by = expr
  return self
end

#outfile(str) ⇒ Object



189
190
191
192
# File 'lib/sqlstmt/sqlstmt.rb', line 189

def outfile(str)
  @outfile = " INTO OUTFILE #{str}"
  return self
end

#replaceObject



179
180
181
182
# File 'lib/sqlstmt/sqlstmt.rb', line 179

def replace
  @replace = true
  return self
end

#selectObject

pick statement type



46
47
48
49
50
# File 'lib/sqlstmt/sqlstmt.rb', line 46

def select
  ensure_no_statement_type
  @stmt_type = 'select'
  return self
end

#set(field, value) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sqlstmt/sqlstmt.rb', line 109

def set(field, value)
  raise "trying to include field #{field} again" if @fields.include?(field)
  # this is to support the special case of INSERT INTO table SELECT * FROM ...
  # where * specified with no matching insert field list specified
  if field
    @fields << field
  end
  value = value.is_a?(String) ? value : value.to_sql
  @values << value
  return self
end

#setq(field, value) ⇒ Object



121
122
123
# File 'lib/sqlstmt/sqlstmt.rb', line 121

def setq(field, value)
  return set(field, value.to_sql)
end

#straight_joinObject



174
175
176
177
# File 'lib/sqlstmt/sqlstmt.rb', line 174

def straight_join
  @straight_join = true
  return self
end

#table(table_str, use_index = nil) ⇒ Object

common operations



73
74
75
76
77
78
# File 'lib/sqlstmt/sqlstmt.rb', line 73

def table(table_str, use_index = nil)
  parts = table_str.split(' ')
  table_obj = Table.new(table_str, parts[0], parts[1], use_index)
  @tables << table_obj
  return self
end

#to_sObject Also known as: to_sql



161
162
163
164
# File 'lib/sqlstmt/sqlstmt.rb', line 161

def to_s
  verify_minimum_requirements
  return build_stmt
end

#updateObject



52
53
54
55
56
# File 'lib/sqlstmt/sqlstmt.rb', line 52

def update
  ensure_no_statement_type
  @stmt_type = 'update'
  return self
end

#where(*expr) ⇒ Object



88
89
90
91
# File 'lib/sqlstmt/sqlstmt.rb', line 88

def where(*expr)
  @wheres.concat(expr)
  return self
end

#with_rollupObject



145
146
147
148
# File 'lib/sqlstmt/sqlstmt.rb', line 145

def with_rollup
  @with_rollup = true
  return self
end