Class: MoSQL::SQLAdapter
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Class Method Summary collapse
Instance Method Summary collapse
- #connect_db(uri, pgschema) ⇒ Object
-
#delete_ns(ns, obj) ⇒ Object
obj must contain an _id field.
-
#initialize(schema, uri, pgschema = nil) ⇒ SQLAdapter
constructor
A new instance of SQLAdapter.
- #table_for_ns(ns) ⇒ Object
- #transform_one_ns(ns, obj) ⇒ Object
- #upsert!(table, table_primary_key, item) ⇒ Object
- #upsert_ns(ns, obj) ⇒ Object
Methods included from Logging
Constructor Details
#initialize(schema, uri, pgschema = nil) ⇒ SQLAdapter
Returns a new instance of SQLAdapter.
7 8 9 10 11 |
# File 'lib/mosql/sql.rb', line 7 def initialize(schema, uri, pgschema=nil) @schema = schema connect_db(uri, pgschema) @db.extension :pg_array end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
5 6 7 |
# File 'lib/mosql/sql.rb', line 5 def db @db end |
Class Method Details
.duplicate_key_error?(e) ⇒ Boolean
64 65 66 67 68 69 70 71 |
# File 'lib/mosql/sql.rb', line 64 def self.duplicate_key_error?(e) # c.f. http://www.postgresql.org/docs/9.2/static/errcodes-appendix.html # for the list of error codes. # # No thanks to Sequel and pg for making it easy to figure out # how to get at this error code.... e.wrapped_exception.result.error_field(PG::Result::PG_DIAG_SQLSTATE) == "23505" end |
Instance Method Details
#connect_db(uri, pgschema) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/mosql/sql.rb', line 13 def connect_db(uri, pgschema) @db = Sequel.connect(uri, :after_connect => proc do |conn| if pgschema begin conn.execute("CREATE SCHEMA \"#{pgschema}\"") rescue PG::Error end conn.execute("SET search_path TO \"#{pgschema}\"") end end) end |
#delete_ns(ns, obj) ⇒ Object
obj must contain an _id field. All other fields will be ignored.
43 44 45 46 47 48 |
# File 'lib/mosql/sql.rb', line 43 def delete_ns(ns, obj) primary_sql_key = @schema.primary_sql_key_for_ns(ns) h = transform_one_ns(ns, obj) raise "No #{primary_sql_key} found in transform of #{obj.inspect}" if h[primary_sql_key].nil? table_for_ns(ns).where(primary_sql_key.to_sym => h[primary_sql_key]).delete end |
#table_for_ns(ns) ⇒ Object
25 26 27 |
# File 'lib/mosql/sql.rb', line 25 def table_for_ns(ns) @db[@schema.table_for_ns(ns).intern] end |
#transform_one_ns(ns, obj) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/mosql/sql.rb', line 29 def transform_one_ns(ns, obj) h = {} cols = @schema.all_columns(@schema.find_ns(ns)) row = @schema.transform(ns, obj) cols.zip(row).each { |k,v| h[k] = v } h end |
#upsert!(table, table_primary_key, item) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mosql/sql.rb', line 50 def upsert!(table, table_primary_key, item) rows = table.where(table_primary_key.to_sym => item[table_primary_key]).update(item) if rows == 0 begin table.insert(item) rescue Sequel::DatabaseError => e raise e unless self.class.duplicate_key_error?(e) log.info("RACE during upsert: Upserting #{item} into #{table}: #{e}") end elsif rows > 1 log.warn("Huh? Updated #{rows} > 1 rows: upsert(#{table}, #{item})") end end |
#upsert_ns(ns, obj) ⇒ Object
37 38 39 40 |
# File 'lib/mosql/sql.rb', line 37 def upsert_ns(ns, obj) h = transform_one_ns(ns, obj) upsert!(table_for_ns(ns), @schema.primary_sql_key_for_ns(ns), h) end |