Class: Tina4::DatabaseResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tina4/database_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_rowsObject (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

#columnsObject (readonly)

Returns the value of attribute columns.



8
9
10
# File 'lib/tina4/database_result.rb', line 8

def columns
  @columns
end

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/tina4/database_result.rb', line 8

def count
  @count
end

#errorObject (readonly)

Returns the value of attribute error.



8
9
10
# File 'lib/tina4/database_result.rb', line 8

def error
  @error
end

#last_idObject (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

#limitObject (readonly)

Returns the value of attribute limit.



8
9
10
# File 'lib/tina4/database_result.rb', line 8

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



8
9
10
# File 'lib/tina4/database_result.rb', line 8

def offset
  @offset
end

#recordsObject (readonly)

Returns the value of attribute records.



8
9
10
# File 'lib/tina4/database_result.rb', line 8

def records
  @records
end

#sqlObject (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

#[](*args) ⇒ Object

Index / slice access into the result rows.

result“ is documented (book ch5 §4 “Index Access”). Delegating straight to the materialised rows means every Array subscript form works — “result“, “result“, “result[1, 2]“ and “result“ — and matches Python’s “DatabaseResult.__getitem__“, which forwards to its records list.



49
50
51
# File 'lib/tina4/database_result.rb', line 49

def [](*args)
  @records[*args]
end

#column_infoObject

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


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/tina4/database_result.rb', line 129

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

Returns:

  • (Boolean)


38
39
40
# File 'lib/tina4/database_result.rb', line 38

def empty?
  @records.empty?
end

#firstObject



30
31
32
# File 'lib/tina4/database_result.rb', line 30

def first
  @records.first
end

#lastObject



34
35
36
# File 'lib/tina4/database_result.rb', line 34

def last
  @records.last
end

#lengthObject



59
60
61
# File 'lib/tina4/database_result.rb', line 59

def length
  @count
end

#sizeObject



63
64
65
# File 'lib/tina4/database_result.rb', line 63

def size
  @count
end

#success?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/tina4/database_result.rb', line 67

def success?
  @error.nil?
end

#to_arrayObject Also known as: to_a



71
72
73
74
75
# File 'lib/tina4/database_result.rb', line 71

def to_array
  @records.map do |record|
    record.is_a?(Hash) ? record : record.to_h
  end
end

#to_aryObject

Implicit array coercion, so a DatabaseResult can be splatted and used anywhere an Array is expected (“a, b = result“, “[*result]“).



55
56
57
# File 'lib/tina4/database_result.rb', line 55

def to_ary
  @records.dup
end

#to_crud(table_name: "data", primary_key: "id", editable: true) ⇒ Object



117
118
119
120
# File 'lib/tina4/database_result.rb', line 117

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



83
84
85
86
87
88
89
90
91
92
# File 'lib/tina4/database_result.rb', line 83

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



79
80
81
# File 'lib/tina4/database_result.rb', line 79

def to_json(*_args)
  JSON.generate(to_array)
end

#to_paginate(page: nil, per_page: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tina4/database_result.rb', line 94

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