Objectify your SQL scripts. Take blocks of SQL from an .sql script and turn them into class methods on your models/classes.
include Threequel::Commandant
Run a series of co-dependant .sql scripts to perform tasks.
A Hash subclass that maps to Commandant SQL class methods. Keys are the SQL class method names and Values are SQL::Command objects.
A class that can execute a series of SQL blocks on a specified connection.
sql = <<-"SQL"
CREATE TABLE dbo.foo (bar varchar(20) NOT NULL);
GO
DROP TABLE dbo.foo;
GO
SQL
connection = ActiveRecord::Base.connection
cmd = Threequel::SQL::Command.new(sql, "My Command")
cmd.execute_on connection
# SQL (23.6ms) CREATE TABLE dbo.foo (bar varchar(20) NOT NULL);
# SQL (37.1ms) DROP TABLE dbo.foo;
# => {:message=>"Command executed successfully", :status=>:success}
# or
Threequel::SQL::Command.new(sql, "My Command") do |cmd|
cmd.execute_on connection
end
# SQL (23.6ms) CREATE TABLE dbo.foo (bar varchar(20) NOT NULL);
# SQL (37.1ms) DROP TABLE dbo.foo;
# => {:message=>"Command executed successfully", :status=>:success}
Adds logging to any instance method.
sql = <<-"SQL"
CREATE TABLE dbo.foo (bar varchar(20) NOT NULL);
GO
DROP TABLE dbo.foo;
GO
SQL
connection = ActiveRecord::Base.connection
Threequel::SQL::Command.new(sql, "My Command") do |cmd|
cmd.extend Threequel::Logging
cmd.add_logging_to :execute_on, :console
cmd.execute_on connection
end
# -- Starting execution of My Command at 2012-07-25 17:13:43 -0400
# SQL (23.6ms) CREATE TABLE dbo.foo (bar varchar(20) NOT NULL);
# SQL (37.1ms) DROP TABLE dbo.foo;
# -- Finishing execution of My Command at 2012-07-25 17:13:43 -0400 in 0.006583 seconds
# => [#<Threequel::ConsoleLogger:0x007ffc47f95240 @attributes={:sql=>"CREATE TABLE dbo.foo (bar varchar(20) NOT NULL);\nGO\n\nDROP TABLE dbo.foo;\nGO\n", :command=>"My Command", :statement=>nil, :name=>"My Command", :started_at=>2012-07-25 17:13:43 -0400, :finished_at=>2012-07-25 17:13:43 -0400, :duration=>0.006583, :stage=>:finished, :message=>"Command executed successfully", :status=>:success}>]
Wrap code in a Timer.clock block and get timing information about your code block.