From e6de9c1911448808b0106dc46c315d9fcbf85eac Mon Sep 17 00:00:00 2001 From: Yessiest Date: Mon, 9 Oct 2023 00:03:20 +0400 Subject: [PATCH] Fixed some issues with the DB initializer and added some stuff to README --- README.md | 20 ++++++++++++++++++++ lib/lpgar.rb | 9 +++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 19eed53..452afde 100644 --- a/README.md +++ b/README.md @@ -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 + +#=>[ 5, ...}>, +# 7, ...}>, +# ...] +``` + And that's really about it. ## Installation diff --git a/lib/lpgar.rb b/lib/lpgar.rb index 9a05dac..d94d666 100644 --- a/lib/lpgar.rb +++ b/lib/lpgar.rb @@ -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 = {}