Class: SqlStmtLib::MysqlBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ MysqlBuilder

Returns a new instance of MysqlBuilder.



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

def initialize(data)
  @data = data
end

Instance Method Details

#build_from_clauseObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sqlstmt/mysql/build.rb', line 57

def build_from_clause
  join_clause = build_join_clause
  group_clause = simple_clause('GROUP BY', @data.group_by)
  if @data.with_rollup
    group_clause += ' WITH ROLLUP'
  end
  order_clause = simple_clause('ORDER BY', @data.order_by)
  limit_clause = simple_clause('LIMIT', @data.limit)
  having_clause = @data.having.empty? ? '' : " HAVING #{@data.having.join(' AND ')}"
  return " FROM #{build_table_list}#{join_clause}#{build_where_clause}#{group_clause}#{having_clause}#{order_clause}#{limit_clause}"
end

#build_join_clauseObject



92
93
94
95
96
97
98
# File 'lib/sqlstmt/mysql/build.rb', line 92

def build_join_clause
  if @data.joins.empty?
    return ''
  else
    return ' ' + @data.joins.map {|ary| ary.join(' ')}.uniq.join(' ')
  end
end

#build_set_clauseObject



69
70
71
72
73
74
75
# File 'lib/sqlstmt/mysql/build.rb', line 69

def build_set_clause
  set_exprs = []
  @data.fields.each_with_index do |field, index|
    set_exprs << "#{field} = #{@data.values[index]}"
  end
  return set_exprs.join(', ')
end

#build_stmtObject



9
10
11
12
# File 'lib/sqlstmt/mysql/build.rb', line 9

def build_stmt
  method_name = "build_stmt_#{@data.stmt_type}"
  return send(method_name)
end

#build_stmt_deleteObject



48
49
50
51
52
53
54
55
# File 'lib/sqlstmt/mysql/build.rb', line 48

def build_stmt_delete
  if @data.tables_to_delete.empty?
    table_clause = ''
  else
    table_clause = ' ' + @data.tables_to_delete.join(',')
  end
  return "DELETE#{table_clause}#{build_from_clause}"
end

#build_stmt_insertObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sqlstmt/mysql/build.rb', line 26

def build_stmt_insert
  if !@data.fields.empty? && !@data.rows.empty?
    raise "unable to use INSERT SELECT and INSERT VALUES together, may only call :set or :add_row, but not both"
  end

  keyword = @data.replace ? 'REPLACE' : 'INSERT'
  value_list = @data.values.join(',')
  start_str = "#{keyword} #{@data.ignore}INTO #{@data.into_table} "
  if !@data.fields.empty?
    field_list = @data.fields.join(',')
    start_str += "(#{field_list}) "
  end

  if @data.rows.empty?
    distinct_str = @data.distinct ? 'DISTINCT ' : ''
    return "#{start_str}SELECT #{distinct_str}#{value_list}#{build_from_clause}"
  else
    raise "DISTINCT not supported when inserting values" if @data.distinct
    return "#{start_str}VALUES (#{value_list})"
  end
end

#build_stmt_selectObject



14
15
16
17
18
19
# File 'lib/sqlstmt/mysql/build.rb', line 14

def build_stmt_select
  straight_join_str = @data.straight_join ? 'STRAIGHT_JOIN ' : ''
  distinct_str = @data.distinct ? 'DISTINCT ' : ''
  select_str = @data.fields.join(',')
  return "SELECT #{straight_join_str}#{distinct_str}#{select_str}#{build_from_clause}#{@data.outfile}"
end

#build_stmt_updateObject



21
22
23
24
# File 'lib/sqlstmt/mysql/build.rb', line 21

def build_stmt_update
  limit_clause = simple_clause('LIMIT', @data.limit)
  return "UPDATE #{build_table_list}#{build_join_clause} SET #{build_set_clause}#{build_where_clause}#{limit_clause}"
end

#build_table_listObject



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

def build_table_list
  return @data.tables.map {|table| table_to_str(table) }.join(',')
end

#build_where_clauseObject



100
101
102
# File 'lib/sqlstmt/mysql/build.rb', line 100

def build_where_clause
  return @data.wheres.empty? ? '' : " WHERE #{@data.wheres.join(' AND ')}"
end

#simple_clause(keywords, value) ⇒ Object



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

def simple_clause(keywords, value)
  return value ? " #{keywords} #{value}" : ''
end

#table_to_str(table) ⇒ Object



77
78
79
80
81
82
# File 'lib/sqlstmt/mysql/build.rb', line 77

def table_to_str(table)
  if table.index
    return "#{table.str} USE INDEX (#{table.index})"
  end
  return table.str
end