Class: Lims::Core::Persistence::Sequel::Session
- Inherits:
-
Lims::Core::Persistence::Session
- Object
- Lims::Core::Persistence::Session
- Lims::Core::Persistence::Sequel::Session
- Includes:
- Uuidable
- Defined in:
- lib/lims-core/persistence/sequel/session.rb
Overview
Sequel specific implementation of a Session.
Instance Attribute Summary
Attributes inherited from Lims::Core::Persistence::Session
Class Method Summary collapse
-
.pack_uuid(uuid) ⇒ Object
Pack if needed an uuid to its store representation.
-
.unpack_uuid(puuid) ⇒ String
Unpac if needed an uuid from its store representation.
Instance Method Summary collapse
- #get_current_session ⇒ Object
- #lock(datasets, unlock = false, &block) ⇒ Object
-
#lock_for_update(datasets, &block) ⇒ Object
this method is to be used when the SQL store doesn’t support LOCK, which is the case for SQLITE It can be used to redefine lock if needed.
- #serialize(object) ⇒ Object
-
#session_object_parameters ⇒ Hash
Return the parameters needed for the creation of a session object.
- #set_current_session(current_session_id = @current_session_id) ⇒ Object
- #transaction ⇒ Object
- #unserialize(object) ⇒ Object
-
#with_session(*params, &block) ⇒ Object
Override with_session to create a session object needed by the database to update revision.
Methods included from Uuidable
#[], #class_for, #delete_resource, #id_for, #model_name_for, #new_uuid_resource_for, #uuid_for, #uuid_for!, #uuid_resource_for
Methods inherited from Lims::Core::Persistence::Session
#<<, #delete, #dirty_key_for, #filter, find_or_create_persistor_for, #id_for, #id_for!, #initialize, #manage_state, #managed?, #method_missing, model_for, model_map, model_to_name, name_to_model, #pack_uuid, persistor_class_for, persistor_class_map, #persistor_for, #persistor_name_for, persistor_name_for, register_model, #save_all, #state_for, #states_for, #unpack_uuid, #with_subsession
Constructor Details
This class inherits a constructor from Lims::Core::Persistence::Session
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Lims::Core::Persistence::Session
Class Method Details
.pack_uuid(uuid) ⇒ Object
Pack if needed an uuid to its store representation
19 20 21 22 23 24 25 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 19 def self.pack_uuid(uuid) # Normal behavior shoulb be pack to binary data UuidResource::pack(uuid) #For now, we just compact it. UuidResource::compact(uuid) end |
.unpack_uuid(puuid) ⇒ String
Unpac if needed an uuid from its store representation
30 31 32 33 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 30 def self.unpack_uuid(puuid) #UuidResource::unpack(puuid) UuidResource::(puuid) end |
Instance Method Details
#get_current_session ⇒ Object
113 114 115 116 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 113 def get_current_session return if database.database_type == :sqlite database.fetch("SELECT @current_session_id AS id").first[:id] end |
#lock(datasets, unlock = false, &block) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 43 def lock(datasets, unlock=false, &block) datasets = [datasets] unless datasets.is_a?(Array) db = datasets.first.db # sqlite3 handles lock differently. # @TODO create Session Subclass for each database type. return lock_for_update(datasets, &block) if db.database_type == :sqlite db.run("LOCK TABLES #{datasets.map { |d| "#{d.first_source} WRITE"}.join(",")}") block.call(*datasets).tap { db.run("UNLOCK TABLES") if unlock } end |
#lock_for_update(datasets, &block) ⇒ Object
this method is to be used when the SQL store doesn’t support LOCK, which is the case for SQLITE It can be used to redefine lock if needed.
58 59 60 61 62 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 58 def lock_for_update(datasets, &block) datasets.first.db.transaction do block.call(*datasets.map(&:for_update)) end end |
#serialize(object) ⇒ Object
35 36 37 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 35 def serialize(object) Lims::Core::Helpers::to_json(object) end |
#session_object_parameters ⇒ Hash
Return the parameters needed for the creation of a session object. It use session attributes which have been set at contruction time. This allow the same session to be reopen multiple times and create each time a new session with the same parameters.
70 71 72 73 74 75 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 70 def session_object_parameters {:user => @user , :backend_application_id => @backend_application_id, :parameters => serialize(@parameters) || nil } end |
#set_current_session(current_session_id = @current_session_id) ⇒ Object
118 119 120 121 122 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 118 def set_current_session(current_session_id=@current_session_id) return if database.database_type == :sqlite database.run "SET @current_session_id = #{current_session_id ? current_session_id : "NULL"};" @current_session_id = current_session_id end |
#transaction ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 124 def transaction super do # Set the current_session_id again # in case it's been overriden by another thread. # Solves bug #64570338 set_current_session yield end end |
#unserialize(object) ⇒ Object
39 40 41 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 39 def unserialize(object) Lims::Core::Helpers::load_json(object) end |
#with_session(*params, &block) ⇒ Object
Override with_session to create a session object needed by the database to update revision. session object are create from the parameters If the session can’t be created due to the lack of parameters. Nothing is created.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/lims-core/persistence/sequel/session.rb', line 82 def with_session(*params, &block) create_session = true success = false # @todo Subclass Session for Sql adapter if database.database_type == :sqlite create_session = false else previous_session_id = database.fetch("SELECT @current_session_id AS id").first[:id] create_session = false if previous_session_id end if create_session session_id = database[:sessions].insert(session_object_parameters) set_current_session(session_id) end begin result = super(*params, &block) success = true ensure if create_session # mark it as finished database[:sessions].where(:id => session_id).update(:end_time => DateTime.now, :success => success) set_current_session(nil) end end return result end |