Module: Sequel::Plugins::SqlComments

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

Overview

The sql_comments plugin will automatically use SQL comments on queries for the model it is loaded into. These comments will show the related model, what type of method was called, and the method name (or association name for queries to load associations):

album = Album[1]
# SELECT * FROM albums WHERE (id = 1) LIMIT 1
# -- model:Album,method_type:class,method:[]

album.update(name: 'A')
# UPDATE albums SET name = 'baz' WHERE (id = 1)
# -- model:Album,method_type:instance,method:update

album.artist
# SELECT * FROM artists WHERE (artists.id = 1)
# -- model:Album,method_type:association_load,association:artist

Album.eager(:artists).all
# SELECT * FROM albums
# SELECT * FROM artists WHERE (artists.id IN (1))
# -- model:Album,method_type:association_eager_load,association:artist

Album.where(id: 1).delete
# DELETE FROM albums WHERE (id = 1)
# -- model:Album,method_type:dataset,method:delete

This plugin automatically supports the class, instance, and dataset methods are are supported by default in Sequel::Model. To support custom class, instance, and dataset methods, such as those added by other plugins, you can use the appropriate sql_comments_*_methods class method:

Album.sql_comments_class_methods :first_by_name # example from finder plugin, with :mod option
Album.sql_comments_instance_methods :lazy_attribute_lookup # lazy_attributes plugin
Album.sql_comments_dataset_methods :to_csv # csv_serializer plugin

In order for the sql_comments plugin to work, the sql_comments Database extension must be loaded into the model’s database.

Note that in order to make sure SQL comments are included, some optimizations are disabled if this plugin is loaded.

Usage:

# Make all model subclasses support automatic SQL comments
# (called before loading subclasses)
Sequel::Model.plugin :sql_comments

# Make the Album class support automatic SQL comments
Album.plugin :sql_comments

Defined Under Namespace

Modules: ClassMethods, DatasetMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.configure(model) ⇒ Object



70
71
72
# File 'lib/sequel/plugins/sql_comments.rb', line 70

def self.configure(model)
  model.send(:reset_fast_pk_lookup_sql)
end

.def_sql_commend_method(mod, model, method_type, meth) ⇒ Object

Define a method meth on the given module mod that will use automatic SQL comments with the given model, method_type, and method.



59
60
61
62
63
64
65
66
67
68
# File 'lib/sequel/plugins/sql_comments.rb', line 59

def self.def_sql_commend_method(mod, model, method_type, meth)
  mod.send(:define_method, meth) do |*a, &block|
    model.db.with_comments(:model=>model, :method_type=>method_type, :method=>meth) do
      super(*a, &block)
    end
  end
  # :nocov:
  mod.send(:ruby2_keywords, meth) if mod.respond_to?(:ruby2_keywords, true)
  # :nocov:
end