Class: DbAgile::SequelAdapter::SequelTracer

Inherits:
Object
  • Object
show all
Includes:
Contract::Utils::Delegate
Defined in:
lib/dbagile/adapter/sequel/sequel_tracer.rb

Constant Summary collapse

DEFAULT_OPTIONS =

Default options of this adapter

{
  :trace_sql    => false,
  :trace_only   => false,
  :trace_buffer => STDERR
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate, options) ⇒ SequelTracer

Creates a tracer instance

Raises:

  • (ArgumentError)


20
21
22
23
24
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 20

def initialize(delegate, options)
  raise ArgumentError, "Connection options should be a Hash" unless options.kind_of?(Hash)
  @delegate = delegate
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#delegateObject (readonly)

Adapter delegate



17
18
19
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 17

def delegate
  @delegate
end

Instance Method Details

#add_columns(transaction, table_name, columns) ⇒ Object

Adds some columns to a table



66
67
68
69
70
71
72
73
74
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 66

def add_columns(transaction, table_name, columns)
  if trace?
    trace(alter_table_sql(table_name){ 
      columns.each_pair{|name, type| add_column(name, type)}
    })
  end
  return super unless trace_only?
  nil
end

#alter_table_sql(table_name, &block) ⇒ Object

Returns SQL statement for a given alter table block



44
45
46
47
48
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 44

def alter_table_sql(table_name, &block)
  generator = ::Sequel::Schema::AlterTableGenerator.new(self, &block)
  sqls = delegate.db.send(:alter_table_sql_list, table_name, generator.operations).flatten
  sqls.join("\n")
end

#create_table(transaction, table_name, columns) ⇒ Object

Creates a table with some columns.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 53

def create_table(transaction, table_name, columns)
  if trace?
    gen = ::Sequel::Schema::Generator.new(self){
      columns.each_pair{|name, type| column(name, type)}
    }
    sql = delegate.db.send(:create_table_sql, table_name, gen, {})
    trace(sql)
  end
  return super unless trace_only?
  nil
end

#delete(transaction, table_name, proj = {}) ⇒ Object

Deletes a projection from a given table



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 108

def delete(transaction, table_name, proj = {})
  if trace?
    if proj.empty?
      trace(delegate.db[table_name].delete_sql) 
    else
      trace(delegate.db[table_name].where(proj).delete_sql) 
    end
  end
  return super unless trace_only?
  true
end

#direct_sql(transaction, sql) ⇒ Object

Sends SQL directly to the database SQL server.



121
122
123
124
125
126
127
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 121

def direct_sql(transaction, sql)
  if trace?
    trace(sql)
  end
  return super unless trace_only?
  nil
end

#insert(transaction, table_name, record) ⇒ Object

Inserts a tuple inside a given table



90
91
92
93
94
95
96
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 90

def insert(transaction, table_name, record)
  if trace?
    trace(delegate.db[table_name].insert_sql(record)) 
  end
  return super unless trace_only?
  record
end

#key!(transaction, table_name, columns) ⇒ Object

Make columns be a candidate key for the table.



77
78
79
80
81
82
83
84
85
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 77

def key!(transaction, table_name, columns)
  if trace?
    trace(alter_table_sql(table_name){ 
      add_index(columns, :unique => true)
    })
  end
  return super unless trace_only?
  nil
end

#trace(sql) ⇒ Object

Traces a SQL statement. Returns true if trace_only, false otherwise



133
134
135
136
137
138
139
140
141
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 133

def trace(sql)
  case out = trace_buffer
    when Logger
      out.info(sql)
    else
      out << "#{sql}\n"
  end
  sql
end

#trace?Boolean

Tracing is on?

Returns:

  • (Boolean)


27
28
29
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 27

def trace?
  @trace ||= (@options[:trace_sql] && !trace_buffer.nil?)
end

#trace_bufferObject

Returns the tracing buffer



37
38
39
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 37

def trace_buffer
  @buffer ||= @options[:trace_buffer]
end

#trace_only?Boolean

Only traces, forget about delegation?

Returns:

  • (Boolean)


32
33
34
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 32

def trace_only?
  @trace_only ||= @options[:trace_only]
end

#update(transaction, table_name, proj, update) ⇒ Object

Updates a project of a given table



99
100
101
102
103
104
105
# File 'lib/dbagile/adapter/sequel/sequel_tracer.rb', line 99

def update(transaction, table_name, proj, update)
  if trace?
    trace(delegate.db[table_name].where(proj).update_sql(update)) 
  end
  return super unless trace_only?
  true
end