Class: Lutaml::Qea::Infrastructure::TableReader
- Inherits:
-
Object
- Object
- Lutaml::Qea::Infrastructure::TableReader
- Defined in:
- lib/lutaml/qea/infrastructure/table_reader.rb
Overview
TableReader reads data from a single table in a QEA SQLite database.
This class provides methods to query and retrieve records from database tables with filtering and counting capabilities.
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Instance Method Summary collapse
-
#all(limit: nil, offset: nil) ⇒ Array<Hash>
Read all records from the table.
-
#count ⇒ Integer
Count all records in the table.
-
#count_where(conditions, *values) ⇒ Integer
Count records matching a WHERE clause.
-
#execute_query(sql, params = []) ⇒ Array<Hash>
Execute a custom SQL query on this table.
-
#exists?(conditions, *values) ⇒ Boolean
Check if any records match the given conditions.
-
#find_by_pk(primary_key_column, value) ⇒ Hash?
Find a single record by primary key value.
-
#find_first(conditions, *values) ⇒ Hash?
Find first record matching a WHERE clause.
-
#initialize(database, table_name) ⇒ TableReader
constructor
Initialize a new table reader.
-
#select(columns, conditions = nil, *values, limit: nil) ⇒ Array<Hash>
Read records with custom column selection.
-
#where(conditions, *values, limit: nil, offset: nil) ⇒ Array<Hash>
Read records matching a WHERE clause.
Constructor Details
#initialize(database, table_name) ⇒ TableReader
Initialize a new table reader
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 26 def initialize(database, table_name) raise ArgumentError, "database cannot be nil" if database.nil? if table_name.nil? || table_name.empty? raise ArgumentError, "table_name cannot be nil or empty" end @database = database @table_name = table_name end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
19 20 21 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 19 def database @database end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
19 20 21 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 19 def table_name @table_name end |
Instance Method Details
#all(limit: nil, offset: nil) ⇒ Array<Hash>
Read all records from the table
(optional)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 52 def all(limit: nil, offset: nil) # rubocop:disable Metrics/MethodLength query = "SELECT * FROM #{@table_name}" params = [] if limit query += " LIMIT ?" params << limit end if offset query += " OFFSET ?" params << offset end @database.execute(query, params) end |
#count ⇒ Integer
Count all records in the table
107 108 109 110 111 112 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 107 def count result = @database.execute( "SELECT COUNT(*) as count FROM #{@table_name}", ) result.first["count"] end |
#count_where(conditions, *values) ⇒ Integer
Count records matching a WHERE clause
(without the WHERE keyword)
124 125 126 127 128 129 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 124 def count_where(conditions, *values) query = "SELECT COUNT(*) as count FROM #{@table_name} " \ "WHERE #{conditions}" result = @database.execute(query, values.flatten) result.first["count"] end |
#execute_query(sql, params = []) ⇒ Array<Hash>
Execute a custom SQL query on this table
175 176 177 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 175 def execute_query(sql, params = []) @database.execute(sql, params) end |
#exists?(conditions, *values) ⇒ Boolean
Check if any records match the given conditions
(without the WHERE keyword)
189 190 191 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 189 def exists?(conditions, *values) count_where(conditions, *values).positive? end |
#find_by_pk(primary_key_column, value) ⇒ Hash?
Find a single record by primary key value
140 141 142 143 144 145 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 140 def find_by_pk(primary_key_column, value) query = "SELECT * FROM #{@table_name} " \ "WHERE #{primary_key_column} = ? LIMIT 1" result = @database.execute(query, [value]) result.first end |
#find_first(conditions, *values) ⇒ Hash?
Find first record matching a WHERE clause
(without the WHERE keyword) or nil if not found
158 159 160 161 162 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 158 def find_first(conditions, *values) query = "SELECT * FROM #{@table_name} WHERE #{conditions} LIMIT 1" result = @database.execute(query, values.flatten) result.first end |
#select(columns, conditions = nil, *values, limit: nil) ⇒ Array<Hash>
Read records with custom column selection
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 204 def select(columns, conditions = nil, *values, limit: nil) # rubocop:disable Metrics/MethodLength column_list = columns.join(", ") query = "SELECT #{column_list} FROM #{@table_name}" params = [] if conditions query += " WHERE #{conditions}" params = values.flatten end if limit query += " LIMIT ?" params << limit end @database.execute(query, params) end |
#where(conditions, *values, limit: nil, offset: nil) ⇒ Array<Hash>
Read records matching a WHERE clause
(without the WHERE keyword) to return (optional)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/lutaml/qea/infrastructure/table_reader.rb', line 87 def where(conditions, *values, limit: nil, offset: nil) # rubocop:disable Metrics/MethodLength query = "SELECT * FROM #{@table_name} WHERE #{conditions}" params = values.flatten if limit query += " LIMIT ?" params << limit end if offset query += " OFFSET ?" params << offset end @database.execute(query, params) end |