Class: Vorpal::Driver::Postgresql
- Inherits:
-
Object
- Object
- Vorpal::Driver::Postgresql
- Defined in:
- lib/vorpal/driver/postgresql.rb
Overview
Interface between the database and Vorpal for PostgreSQL using ActiveRecord.
Instance Method Summary collapse
-
#build_db_class(model_class, table_name) ⇒ Class
Builds an ORM Class for accessing data in the given DB table.
- #destroy(db_class, ids) ⇒ Object
-
#get_primary_keys(db_class, count) ⇒ [Integer]
Fetches primary key values to be used for new entities.
-
#initialize ⇒ Postgresql
constructor
A new instance of Postgresql.
- #insert(db_class, db_objects) ⇒ Object
-
#load_by_foreign_key(db_class, id, foreign_key_info) ⇒ [Object]
Loads instances of the given class whose foreign key has the given value.
-
#load_by_id(db_class, ids) ⇒ [Object]
Loads instances of the given class by primary key.
-
#query(db_class, aggregate_mapper) ⇒ Object
Builds a composable query object (e.g. ActiveRecord::Relation) with Vorpal methods mixed in for querying for instances of the given AR::Base class.
- #update(db_class, db_objects) ⇒ Object
Constructor Details
#initialize ⇒ Postgresql
Returns a new instance of Postgresql.
7 8 9 |
# File 'lib/vorpal/driver/postgresql.rb', line 7 def initialize @sequence_names = {} end |
Instance Method Details
#build_db_class(model_class, table_name) ⇒ Class
Builds an ORM Class for accessing data in the given DB table.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/vorpal/driver/postgresql.rb', line 65 def build_db_class(model_class, table_name) db_class = Class.new(ActiveRecord::Base) do class << self # This is overridden for two reasons: # 1) For anonymous classes, #name normally returns nil. Class names in Ruby come from the # name of the constant they are assigned to. # 2) Because the default implementation for Class#name for anonymous classes is very, very # slow. https://bugs.ruby-lang.org/issues/11119 # Remove this override once #2 has been fixed! def name @name ||= "Vorpal_generated_ActiveRecord__Base_class_for_#{vorpal_model_class_name}" end # Overridden because, like #name, the default implementation for anonymous classes is very, # very slow. def to_s name end attr_accessor :vorpal_model_class_name end end db_class.vorpal_model_class_name = Util::StringUtils.escape_class_name(model_class.name) db_class.table_name = table_name db_class end |
#destroy(db_class, ids) ⇒ Object
27 28 29 |
# File 'lib/vorpal/driver/postgresql.rb', line 27 def destroy(db_class, ids) db_class.where(id: ids).delete_all end |
#get_primary_keys(db_class, count) ⇒ [Integer]
Fetches primary key values to be used for new entities.
55 56 57 58 |
# File 'lib/vorpal/driver/postgresql.rb', line 55 def get_primary_keys(db_class, count) result = execute("select nextval($1) from generate_series(1,$2);", [sequence_name(db_class), count]) result.rows.map(&:first).map(&:to_i) end |
#insert(db_class, db_objects) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/vorpal/driver/postgresql.rb', line 11 def insert(db_class, db_objects) if defined? ActiveRecord::Import db_class.import(db_objects, validate: false) else db_objects.each do |db_object| db_object.save!(validate: false) end end end |
#load_by_foreign_key(db_class, id, foreign_key_info) ⇒ [Object]
Loads instances of the given class whose foreign key has the given value.
45 46 47 48 49 |
# File 'lib/vorpal/driver/postgresql.rb', line 45 def load_by_foreign_key(db_class, id, foreign_key_info) arel = db_class.where(foreign_key_info.fk_column => id) arel = arel.where(foreign_key_info.fk_type_column => foreign_key_info.fk_type) if foreign_key_info.polymorphic? arel.to_a end |
#load_by_id(db_class, ids) ⇒ [Object]
Loads instances of the given class by primary key.
35 36 37 |
# File 'lib/vorpal/driver/postgresql.rb', line 35 def load_by_id(db_class, ids) db_class.where(id: ids).to_a end |
#query(db_class, aggregate_mapper) ⇒ Object
Builds a composable query object (e.g. ActiveRecord::Relation) with Vorpal methods mixed in for querying for instances of the given AR::Base class.
97 98 99 |
# File 'lib/vorpal/driver/postgresql.rb', line 97 def query(db_class, aggregate_mapper) db_class.unscoped.extending(ArelQueryMethods.new(aggregate_mapper)) end |
#update(db_class, db_objects) ⇒ Object
21 22 23 24 25 |
# File 'lib/vorpal/driver/postgresql.rb', line 21 def update(db_class, db_objects) db_objects.each do |db_object| db_object.save!(validate: false) end end |