Fixed some issues with the DB initializer and added some stuff to README

This commit is contained in:
Yessiest 2023-10-09 00:03:20 +04:00
parent b35d4926b1
commit e6de9c1911
2 changed files with 27 additions and 2 deletions

View File

@ -59,6 +59,26 @@ row.commit(transaction)
row.sync # true if synchronization successful
```
### Explicitly request an existing row or explicitly creating a new row
```ruby
#...
row = Table.create({"pk1" => "new", "pk2" => "something", ...}) # returns nil if row exists
row = Table.get({"pk1" => "new", "pk2" => "something", ...}) # returns nil if row does not exist
```
### Mapping results of a query to objects of a table
```ruby
rows = Table.map("SELECT * FROM table_name WHERE row > 4 # AND ...")
pp rows
#=>[<Table:0x000055da208232c8 @data={"row" => 5, ...}>,
# <Table:0x00001238989cc89d @data={"row" => 7, ...}>,
# ...]
```
And that's really about it.
## Installation

View File

@ -132,8 +132,9 @@ module LPGAR
@sync_query = <<~QUERY
SELECT * FROM #{@table_name}
WHERE #{selector}
LIMIT 1;
LIMIT 1
QUERY
puts @sync_query
end
# Returns transaction class for this record.
@ -363,10 +364,14 @@ module LPGAR
@cols = conn.exec_prepared('lpgar_get_columns', [name]).map do |row|
row['column_name']
end
raise StandardError, "table #{name} doesn't exist" if @cols.empty?
@cols_pk = conn.exec_prepared('lpgar_get_pk', [name]).map do |row|
row['attname']
end
raise StandardError, "table #{name} doesn't exist" if @cols.empty?
if @cols_pk.empty?
raise StandardError, "table #{name} has no primary keys"
end
@conn = conn
@instances = {}