Class: Tina4::DatabaseResult
- Inherits:
-
Object
- Object
- Tina4::DatabaseResult
- Includes:
- Enumerable
- Defined in:
- lib/tina4/database_result.rb
Instance Attribute Summary collapse
-
#affected_rows ⇒ Object
readonly
Returns the value of attribute affected_rows.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#last_id ⇒ Object
readonly
Returns the value of attribute last_id.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
-
#sql ⇒ Object
readonly
Returns the value of attribute sql.
Instance Method Summary collapse
- #[](index) ⇒ Object
-
#column_info ⇒ Object
Return column metadata for the query’s table.
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #first ⇒ Object
-
#initialize(records = [], sql: "", columns: [], count: nil, limit: 10, offset: 0, affected_rows: 0, last_id: nil, error: nil, db: nil) ⇒ DatabaseResult
constructor
A new instance of DatabaseResult.
- #last ⇒ Object
- #length ⇒ Object
- #size ⇒ Object
- #success? ⇒ Boolean
- #to_array ⇒ Object (also: #to_a)
- #to_crud(table_name: "data", primary_key: "id", editable: true) ⇒ Object
- #to_csv(separator: ",", headers: true) ⇒ Object
- #to_json(*_args) ⇒ Object
- #to_paginate(page: nil, per_page: nil) ⇒ Object
Constructor Details
#initialize(records = [], sql: "", columns: [], count: nil, limit: 10, offset: 0, affected_rows: 0, last_id: nil, error: nil, db: nil) ⇒ DatabaseResult
Returns a new instance of DatabaseResult.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tina4/database_result.rb', line 11 def initialize(records = [], sql: "", columns: [], count: nil, limit: 10, offset: 0, affected_rows: 0, last_id: nil, error: nil, db: nil) @records = records || [] @sql = sql @columns = columns.empty? && !@records.empty? ? @records.first.keys : columns @count = count || @records.length @limit = limit @offset = offset @affected_rows = affected_rows @last_id = last_id @error = error @db = db @column_info_cache = nil end |
Instance Attribute Details
#affected_rows ⇒ Object (readonly)
Returns the value of attribute affected_rows.
8 9 10 |
# File 'lib/tina4/database_result.rb', line 8 def affected_rows @affected_rows end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
8 9 10 |
# File 'lib/tina4/database_result.rb', line 8 def columns @columns end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
8 9 10 |
# File 'lib/tina4/database_result.rb', line 8 def count @count end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
8 9 10 |
# File 'lib/tina4/database_result.rb', line 8 def error @error end |
#last_id ⇒ Object (readonly)
Returns the value of attribute last_id.
8 9 10 |
# File 'lib/tina4/database_result.rb', line 8 def last_id @last_id end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
8 9 10 |
# File 'lib/tina4/database_result.rb', line 8 def limit @limit end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
8 9 10 |
# File 'lib/tina4/database_result.rb', line 8 def offset @offset end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
8 9 10 |
# File 'lib/tina4/database_result.rb', line 8 def records @records end |
#sql ⇒ Object (readonly)
Returns the value of attribute sql.
8 9 10 |
# File 'lib/tina4/database_result.rb', line 8 def sql @sql end |
Instance Method Details
#[](index) ⇒ Object
42 43 44 |
# File 'lib/tina4/database_result.rb', line 42 def [](index) @records[index] end |
#column_info ⇒ Object
Return column metadata for the query’s table.
Lazy — only queries the database when explicitly called. Caches the result so subsequent calls return immediately without re-querying.
Returns an array of hashes with keys:
name, type, size, decimals, nullable, primary_key
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/tina4/database_result.rb', line 116 def column_info return @column_info_cache if @column_info_cache table = extract_table_from_sql if @db && table begin @column_info_cache = (table) return @column_info_cache rescue StandardError # Fall through to fallback end end @column_info_cache = fallback_column_info @column_info_cache end |
#each(&block) ⇒ Object
26 27 28 |
# File 'lib/tina4/database_result.rb', line 26 def each(&block) @records.each(&block) end |
#empty? ⇒ Boolean
38 39 40 |
# File 'lib/tina4/database_result.rb', line 38 def empty? @records.empty? end |
#first ⇒ Object
30 31 32 |
# File 'lib/tina4/database_result.rb', line 30 def first @records.first end |
#last ⇒ Object
34 35 36 |
# File 'lib/tina4/database_result.rb', line 34 def last @records.last end |
#length ⇒ Object
46 47 48 |
# File 'lib/tina4/database_result.rb', line 46 def length @count end |
#size ⇒ Object
50 51 52 |
# File 'lib/tina4/database_result.rb', line 50 def size @count end |
#success? ⇒ Boolean
54 55 56 |
# File 'lib/tina4/database_result.rb', line 54 def success? @error.nil? end |
#to_array ⇒ Object Also known as: to_a
58 59 60 61 62 |
# File 'lib/tina4/database_result.rb', line 58 def to_array @records.map do |record| record.is_a?(Hash) ? record : record.to_h end end |
#to_crud(table_name: "data", primary_key: "id", editable: true) ⇒ Object
104 105 106 107 |
# File 'lib/tina4/database_result.rb', line 104 def to_crud(table_name: "data", primary_key: "id", editable: true) Tina4::Crud.generate_table(@records, table_name: table_name, primary_key: primary_key, editable: editable) end |
#to_csv(separator: ",", headers: true) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tina4/database_result.rb', line 70 def to_csv(separator: ",", headers: true) return "" if @records.empty? lines = [] cols = @records.first.keys lines << cols.join(separator) if headers @records.each do |row| lines << cols.map { |c| escape_csv(row[c], separator) }.join(separator) end lines.join("\n") end |
#to_json(*_args) ⇒ Object
66 67 68 |
# File 'lib/tina4/database_result.rb', line 66 def to_json(*_args) JSON.generate(to_array) end |
#to_paginate(page: nil, per_page: nil) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/tina4/database_result.rb', line 81 def to_paginate(page: nil, per_page: nil) per_page ||= @limit > 0 ? @limit : 10 page ||= @offset > 0 ? (@offset / per_page) + 1 : 1 total = @count total_pages = [1, (total.to_f / per_page).ceil].max slice_offset = (page - 1) * per_page page_records = @records[slice_offset, per_page] || [] { records: page_records, data: page_records, count: total, total: total, limit: per_page, offset: (page - 1) * per_page, page: page, per_page: per_page, totalPages: total_pages, total_pages: total_pages, has_next: page < total_pages, has_prev: page > 1 } end |