Module: Sequel::Database::SQLComments

Defined in:
lib/sequel/extensions/sql_comments.rb

Defined Under Namespace

Modules: DatasetSQLComments

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#comment_hashesObject (readonly)

A map of threads to comment hashes, used for correctly setting comments for all queries inside #with_comments blocks.



150
151
152
# File 'lib/sequel/extensions/sql_comments.rb', line 150

def comment_hashes
  @comment_hashes
end

Class Method Details

.extended(db) ⇒ Object



143
144
145
146
# File 'lib/sequel/extensions/sql_comments.rb', line 143

def self.extended(db)
  db.instance_variable_set(:@comment_hashes, {})
  db.extend_datasets DatasetSQLComments
end

Instance Method Details

#with_comments(comment_hash) ⇒ Object

Store the comment hash and use it to create comments inside the block



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/sequel/extensions/sql_comments.rb', line 153

def with_comments(comment_hash)
  hashes = @comment_hashes
  t = Sequel.current
  new_hash = if hash = Sequel.synchronize{hashes[t]}
    hash.merge(comment_hash)
  else
    comment_hash.dup
  end
  yield Sequel.synchronize{hashes[t] = new_hash}
ensure
  if hash
    Sequel.synchronize{hashes[t] = hash}
  else
    t && Sequel.synchronize{hashes.delete(t)}
  end
end