module Sequel::Plugins::SqlComments

  1. lib/sequel/plugins/sql_comments.rb

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

Methods

Public Class

  1. configure
  2. def_sql_commend_method

Public Class methods

configure(model)
[show source]
   # File lib/sequel/plugins/sql_comments.rb
70 def self.configure(model)
71   model.send(:reset_fast_pk_lookup_sql)
72 end
def_sql_commend_method(mod, model, method_type, meth)

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

[show source]
   # File lib/sequel/plugins/sql_comments.rb
59 def self.def_sql_commend_method(mod, model, method_type, meth)
60   mod.send(:define_method, meth) do |*a, &block|
61     model.db.with_comments(:model=>model, :method_type=>method_type, :method=>meth) do
62       super(*a, &block)
63     end
64   end
65   # :nocov:
66   mod.send(:ruby2_keywords, meth) if mod.respond_to?(:ruby2_keywords, true)
67   # :nocov:
68 end