2017-06-29 15:01:32 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
|
|
|
module DatabaseHelper
|
|
|
|
class SQLColumn
|
|
|
|
def initialize(row, name)
|
|
|
|
@row = row
|
|
|
|
@name = name
|
|
|
|
end
|
|
|
|
|
|
|
|
def value
|
2018-03-01 19:30:07 +00:00
|
|
|
@row.nil? ? '' : @row[@name.downcase]
|
2017-06-29 15:01:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
'SQL Column'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SQLRow
|
|
|
|
def initialize(query, row)
|
|
|
|
@query = query
|
|
|
|
@row = row
|
|
|
|
end
|
|
|
|
|
|
|
|
def column(column)
|
|
|
|
SQLColumn.new(@row, column)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
'SQL Row'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SQLQueryResult
|
|
|
|
attr_reader :error
|
|
|
|
def initialize(cmd, results)
|
|
|
|
@cmd = cmd
|
|
|
|
@results = results
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty?
|
|
|
|
@results.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def successful?
|
|
|
|
@cmd.exit_status == 0 && @error.nil?
|
|
|
|
end
|
|
|
|
|
2018-07-09 17:57:45 +00:00
|
|
|
def rows
|
|
|
|
@results
|
|
|
|
end
|
|
|
|
|
2017-06-29 15:01:32 +00:00
|
|
|
def row(id)
|
|
|
|
SQLRow.new(self, @results[id])
|
|
|
|
end
|
|
|
|
|
2018-07-09 17:57:45 +00:00
|
|
|
def column(column)
|
|
|
|
result = []
|
|
|
|
@results.each do |row|
|
|
|
|
result << row[column]
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2017-06-29 15:01:32 +00:00
|
|
|
def size
|
|
|
|
@results.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
'SQL ResultSet'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|