Class: Ea::Qea::Infrastructure::TableReader
- Inherits:
-
Object
- Object
- Ea::Qea::Infrastructure::TableReader
- Defined in:
- lib/ea/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.
-
#table_exists? ⇒ Boolean
True when the table exists in the database.
-
#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/ea/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/ea/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/ea/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)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ea/qea/infrastructure/table_reader.rb', line 61 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
116 117 118 119 120 121 |
# File 'lib/ea/qea/infrastructure/table_reader.rb', line 116 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)
133 134 135 136 137 138 |
# File 'lib/ea/qea/infrastructure/table_reader.rb', line 133 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
184 185 186 |
# File 'lib/ea/qea/infrastructure/table_reader.rb', line 184 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)
198 199 200 |
# File 'lib/ea/qea/infrastructure/table_reader.rb', line 198 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
149 150 151 152 153 154 |
# File 'lib/ea/qea/infrastructure/table_reader.rb', line 149 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
167 168 169 170 171 |
# File 'lib/ea/qea/infrastructure/table_reader.rb', line 167 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
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/ea/qea/infrastructure/table_reader.rb', line 213 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 |
#table_exists? ⇒ Boolean
Returns true when the table exists in the database.
39 40 41 42 43 44 45 |
# File 'lib/ea/qea/infrastructure/table_reader.rb', line 39 def table_exists? row = @database.get_first_row( "SELECT name FROM sqlite_master WHERE type='table' AND name=?", [@table_name] ) !row.nil? end |
#where(conditions, *values, limit: nil, offset: nil) ⇒ Array<Hash>
Read records matching a WHERE clause
(without the WHERE keyword) to return (optional)
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ea/qea/infrastructure/table_reader.rb', line 96 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 |