class MysqlBuilder
Public Class Methods
insert(values={})
click to toggle source
# File lib/mdlsql/sockets/mysql.rb, line 88 def insert(values={}) # INSERT INTO table (column1, column2) VALUES (v1c1, v1c2), (v2c1, v2c2) cols = values[:cols] tables = values[:tables] where = values[:where] vals = values[:values] query = String.new query = 'INSERT INTO' if tables tables.each do |tab| query << ' ' << tab.name.to_s << ',' # query << " #{tab.name}" # query << " AS #{tab.as}" if tab.as end query.chop! else raise "No tables at insert query." end if cols && cols.count > 0 query << ' (' cols.each do |key,col| query << "#{col}," end query.chop! << ')' end query << ' VALUES' if vals vals.each do |row| query << ' (' row.each do |val| query << "'#{val}'" << ',' end query.chop! query << '),' end query.chop! else raise 'No values to insert.' end return query end
new()
click to toggle source
# File lib/mdlsql/sockets/mysql.rb, line 25 def initialize end
select(values={})
click to toggle source
# File lib/mdlsql/sockets/mysql.rb, line 29 def select(values={}) cols = values[:cols] tables = values[:tables] where = values[:where] join = values[:join] query = String.new query = "SELECT" # Columns (with alias) if cols cols.each do |key,value| query << " #{key} AS #{value}" << ',' end query.chop! else query << " *" end # From (with possible alias) if tables query << "\nFROM" tables.each do |tab| query << ' ' << tab.to_mysql << ',' # query << " #{tab.name}" # query << " AS #{tab.as}" if tab.as end query.chop! else raise "No table at select query." end # @join, see Join if join && join.length > 0 join.each do |j| query << j.to_mysql # query << ' ' << value[:type] if value[:type] # query << ' ' << value[:table].to_s # query << " ON #{value[:cond1]} #{value[:op]} #{value[:cond2]}" end end # @where = Array if where && where.length > 0 query << "\nWHERE" # where.each do |dec| # query << " #{dec}" # end first = true where.each do |wh| query << " #{wh.concat}" unless first query << " #{wh.cond1} #{wh.op} #{wh.cond2}" first = false end end return query end
update(values={})
click to toggle source
# File lib/mdlsql/sockets/mysql.rb, line 138 def update(values={}) # UPDATE example SET age='22' WHERE age='21' tables = values[:tables] set = values[:values] where = values[:where] query = String.new() if tables query << "UPDATE" tables.each do |tab| query << ' ' << tab.to_mysql << ',' # query << " #{tab.name}" # query << " AS #{tab.as}" if tab.as end query.chop! else raise "No table at update query." end query << "\nSET" if set && set.count > 0 set.each do |key, value| query << " #{key} = '#{value}'," end query.chop! else raise 'Nothing to be set.' end if where && where.length > 0 query << "\nWHERE" # where.each do |dec| # query << " #{dec}" # end first = true where.each do |wh| query << " #{wh.concat}" unless first query << " #{wh.cond1} #{wh.op} #{wh.cond2}" first = false end end return query end