Class: SqlStmt

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSqlStmt

Returns a new instance of SqlStmt.



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

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 = ''
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



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

def add_row(row)
  @rows << row
end

#delete(*tables) ⇒ Object



60
61
62
63
64
65
# File 'lib/sqlstmt/sqlstmt.rb', line 60

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

#distinctObject

less commonly used methods



153
154
155
156
# File 'lib/sqlstmt/sqlstmt.rb', line 153

def distinct
  @distinct = true
  return self
end

#field(*args) ⇒ Object

transition only mechanisms



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

def field(*args)
  if ['update','insert'].include?(@stmt_type)
    set(args[0], args[1])
  else
    get(*args)
  end
end

#get(*exprs) ⇒ Object



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

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

#group_by(expr) ⇒ Object



114
115
116
117
# File 'lib/sqlstmt/sqlstmt.rb', line 114

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

#having(*expr) ⇒ Object



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

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

#ignoreObject



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

def ignore
  @ignore = 'IGNORE '
  return self
end

#includes_table?(table_to_find) ⇒ Boolean

methods to analyze what the statement contains

Returns:

  • (Boolean)


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

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

#initialize_copy(orig) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/sqlstmt/sqlstmt.rb', line 29

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



54
55
56
57
58
# File 'lib/sqlstmt/sqlstmt.rb', line 54

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

#into(into_table) ⇒ Object

used with INSERT statements only



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

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

#join(table, *exprs) ⇒ Object



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

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

#left_join(table, *exprs) ⇒ Object



78
79
80
# File 'lib/sqlstmt/sqlstmt.rb', line 78

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

#limit(clause) ⇒ Object



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

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

#no_whereObject



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

def no_where
  @where_behavior = :exclude
  return self
end

#optional_join(table, expr) ⇒ Object



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

def optional_join(table, expr)
  unless includes_table?(table)
    join(table, expr)
  end
end

#optional_whereObject



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

def optional_where
  @where_behavior = :optional
  return self
end

#order_by(expr) ⇒ Object



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

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

#outfile(str) ⇒ Object



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

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

#replaceObject



163
164
165
166
# File 'lib/sqlstmt/sqlstmt.rb', line 163

def replace
  @replace = true
  return self
end

#selectObject

pick statement type



42
43
44
45
46
# File 'lib/sqlstmt/sqlstmt.rb', line 42

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

#set(field, value) ⇒ Object



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

def set(field, value)
  raise "trying to include field #{field} again" if @fields.include?(field)
  @fields << field
  value = value.is_a?(String) ? value : value.to_sql
  @values << value
  return self
end

#setq(field, value) ⇒ Object Also known as: fieldq



110
111
112
# File 'lib/sqlstmt/sqlstmt.rb', line 110

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

#straight_joinObject



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

def straight_join
  @straight_join = true
  return self
end

#table(table) ⇒ Object

common operations



69
70
71
72
# File 'lib/sqlstmt/sqlstmt.rb', line 69

def table(table)
  @tables << table
  return self
end

#to_sObject Also known as: to_sql



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

def to_s
  verify_minimum_requirements
  return build_stmt
end

#updateObject



48
49
50
51
52
# File 'lib/sqlstmt/sqlstmt.rb', line 48

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

#where(*expr) ⇒ Object



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

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