Class: MysqlFramework::SqlQuery

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

Overview

This class is used to represent and build a sql query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSqlQuery

Returns a new instance of SqlQuery.



9
10
11
12
# File 'lib/mysql_framework/sql_query.rb', line 9

def initialize
  @sql = ''
  @params = []
end

Instance Attribute Details

#paramsObject (readonly)

This method is called to get any params required to execute this query as a prepared statement.



7
8
9
# File 'lib/mysql_framework/sql_query.rb', line 7

def params
  @params
end

Instance Method Details

#andObject

This method is called to add an ‘and` keyword to a query to provide additional where clauses.



89
90
91
92
# File 'lib/mysql_framework/sql_query.rb', line 89

def and
  @sql += 'and'
  self
end

#deleteObject

This method is called to start a delete query



26
27
28
29
# File 'lib/mysql_framework/sql_query.rb', line 26

def delete
  @sql = 'delete'
  self
end

#from(table, partition = nil) ⇒ Object

This method is called to specify the table/partition a select/delete query is for.



72
73
74
75
76
# File 'lib/mysql_framework/sql_query.rb', line 72

def from(table, partition = nil)
  @sql += " from #{table}"
  @sql += " partition(p#{partition})" unless partition.nil?
  self
end

#insert(table, partition = nil) ⇒ Object

This method is called to start an insert query



39
40
41
42
43
# File 'lib/mysql_framework/sql_query.rb', line 39

def insert(table, partition = nil)
  @sql += "insert into #{table}"
  @sql += " partition(p#{partition})" unless partition.nil?
  self
end

#into(*columns) ⇒ Object

This method is called to specify the columns to insert into.



46
47
48
49
# File 'lib/mysql_framework/sql_query.rb', line 46

def into(*columns)
  @sql += " (#{columns.join(',')})"
  self
end

#join(table) ⇒ Object

This method is called to add a join statement to a query.



120
121
122
123
# File 'lib/mysql_framework/sql_query.rb', line 120

def join(table)
  @sql += " join #{table}"
  self
end

#limit(count) ⇒ Object

This method is called to add a limit to a query



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

def limit(count)
  @sql += " limit #{count}"
  self
end

#on(column_1, column_2) ⇒ Object

This method is called to add the ‘on` detail to a join statement.



126
127
128
129
# File 'lib/mysql_framework/sql_query.rb', line 126

def on(column_1, column_2)
  @sql += " on #{column_1} = #{column_2}"
  self
end

#orObject

This method is called to add an ‘or` keyword to a query to provide alternate where clauses.



95
96
97
98
# File 'lib/mysql_framework/sql_query.rb', line 95

def or
  @sql += 'or'
  self
end

#order(*columns) ⇒ Object

This method is called to add an ‘order by` statement to a query



101
102
103
104
# File 'lib/mysql_framework/sql_query.rb', line 101

def order(*columns)
  @sql += " order by #{columns.join(',')}"
  self
end

#order_desc(*columns) ⇒ Object

This method is called to add an ‘order by … desc` statement to a query



107
108
109
110
111
# File 'lib/mysql_framework/sql_query.rb', line 107

def order_desc(*columns)
  order(*columns)
  @sql += ' desc'
  self
end

#select(*columns) ⇒ Object

This method is called to start a select query



20
21
22
23
# File 'lib/mysql_framework/sql_query.rb', line 20

def select(*columns)
  @sql = "select #{columns.join(',')}"
  self
end

#set(values) ⇒ Object

This method is called to specify the columns to update.



61
62
63
64
65
66
67
68
69
# File 'lib/mysql_framework/sql_query.rb', line 61

def set(values)
  @sql += ' set '
  values.each do |k, p|
    @sql += "`#{k}` = ?, "
    @params << p
  end
  @sql = @sql[0...-2]
  self
end

#sqlObject

This method is called to access the sql string for this query.



15
16
17
# File 'lib/mysql_framework/sql_query.rb', line 15

def sql
  @sql.strip
end

#update(table, partition = nil) ⇒ Object

This method is called to start an update query



32
33
34
35
36
# File 'lib/mysql_framework/sql_query.rb', line 32

def update(table, partition = nil)
  @sql = "update #{table}"
  @sql += " partition(p#{partition})" unless partition.nil?
  self
end

#values(*values) ⇒ Object

This method is called to specify the values to insert.



52
53
54
55
56
57
58
# File 'lib/mysql_framework/sql_query.rb', line 52

def values(*values)
  @sql += " values (#{values.map { |_v| '?' }.join(',')})"
  values.each do |v|
    @params << v
  end
  self
end

#where(*conditions) ⇒ Object

This method is called to specify a where clause for a query.



79
80
81
82
83
84
85
86
# File 'lib/mysql_framework/sql_query.rb', line 79

def where(*conditions)
  @sql += ' where' unless @sql.include?('where')
  @sql += " (#{conditions.join(' and ')}) "
  conditions.each do |c|
    @params << c.value
  end
  self
end