| 1 |
require 'benchmark' |
|---|
| 2 |
require 'date' |
|---|
| 3 |
require 'bigdecimal' |
|---|
| 4 |
require 'bigdecimal/util' |
|---|
| 5 |
|
|---|
| 6 |
require 'active_record/connection_adapters/abstract/schema_definitions' |
|---|
| 7 |
require 'active_record/connection_adapters/abstract/schema_statements' |
|---|
| 8 |
require 'active_record/connection_adapters/abstract/database_statements' |
|---|
| 9 |
require 'active_record/connection_adapters/abstract/quoting' |
|---|
| 10 |
require 'active_record/connection_adapters/abstract/connection_specification' |
|---|
| 11 |
require 'active_record/connection_adapters/abstract/query_cache' |
|---|
| 12 |
|
|---|
| 13 |
module ActiveRecord |
|---|
| 14 |
module ConnectionAdapters |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
class AbstractAdapter |
|---|
| 25 |
include Quoting, DatabaseStatements, SchemaStatements |
|---|
| 26 |
include QueryCache |
|---|
| 27 |
@@row_even = true |
|---|
| 28 |
|
|---|
| 29 |
def initialize(connection, logger = nil) |
|---|
| 30 |
@connection, @logger = connection, logger |
|---|
| 31 |
@runtime = 0 |
|---|
| 32 |
@last_verification = 0 |
|---|
| 33 |
@query_cache_enabled = false |
|---|
| 34 |
end |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
def adapter_name |
|---|
| 39 |
'Abstract' |
|---|
| 40 |
end |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
def supports_migrations? |
|---|
| 45 |
false |
|---|
| 46 |
end |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
def supports_count_distinct? |
|---|
| 51 |
true |
|---|
| 52 |
end |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
def prefetch_primary_key?(table_name = nil) |
|---|
| 59 |
false |
|---|
| 60 |
end |
|---|
| 61 |
|
|---|
| 62 |
def reset_runtime |
|---|
| 63 |
rt, @runtime = @runtime, 0 |
|---|
| 64 |
rt |
|---|
| 65 |
end |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
def quote_table_name(name) |
|---|
| 71 |
quote_column_name(name) |
|---|
| 72 |
end |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
def disable_referential_integrity(&block) |
|---|
| 78 |
yield |
|---|
| 79 |
end |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
def active? |
|---|
| 85 |
@active != false |
|---|
| 86 |
end |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
def reconnect! |
|---|
| 90 |
@active = true |
|---|
| 91 |
end |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
def disconnect! |
|---|
| 95 |
@active = false |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
def requires_reloading? |
|---|
| 101 |
false |
|---|
| 102 |
end |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
def verify!(timeout) |
|---|
| 107 |
now = Time.now.to_i |
|---|
| 108 |
if (now - @last_verification) > timeout |
|---|
| 109 |
reconnect! unless active? |
|---|
| 110 |
@last_verification = now |
|---|
| 111 |
end |
|---|
| 112 |
end |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
def raw_connection |
|---|
| 118 |
@connection |
|---|
| 119 |
end |
|---|
| 120 |
|
|---|
| 121 |
def log_info(sql, name, runtime) |
|---|
| 122 |
if @logger && @logger.debug? |
|---|
| 123 |
name = "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})" |
|---|
| 124 |
@logger.debug format_log_entry(name, sql.squeeze(' ')) |
|---|
| 125 |
end |
|---|
| 126 |
end |
|---|
| 127 |
|
|---|
| 128 |
protected |
|---|
| 129 |
def log(sql, name) |
|---|
| 130 |
if block_given? |
|---|
| 131 |
if @logger and @logger.debug? |
|---|
| 132 |
result = nil |
|---|
| 133 |
seconds = Benchmark.realtime { result = yield } |
|---|
| 134 |
@runtime += seconds |
|---|
| 135 |
log_info(sql, name, seconds) |
|---|
| 136 |
result |
|---|
| 137 |
else |
|---|
| 138 |
yield |
|---|
| 139 |
end |
|---|
| 140 |
else |
|---|
| 141 |
log_info(sql, name, 0) |
|---|
| 142 |
nil |
|---|
| 143 |
end |
|---|
| 144 |
rescue Exception => e |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
@last_verification = 0 |
|---|
| 149 |
message = "#{e.class.name}: #{e.message}: #{sql}" |
|---|
| 150 |
log_info(message, name, 0) |
|---|
| 151 |
raise ActiveRecord::StatementInvalid, message |
|---|
| 152 |
end |
|---|
| 153 |
|
|---|
| 154 |
def format_log_entry(message, dump = nil) |
|---|
| 155 |
if ActiveRecord::Base.colorize_logging |
|---|
| 156 |
if @@row_even |
|---|
| 157 |
@@row_even = false |
|---|
| 158 |
message_color, dump_color = "4;36;1", "0;1" |
|---|
| 159 |
else |
|---|
| 160 |
@@row_even = true |
|---|
| 161 |
message_color, dump_color = "4;35;1", "0" |
|---|
| 162 |
end |
|---|
| 163 |
|
|---|
| 164 |
log_entry = " \e[#{message_color}m#{message}\e[0m " |
|---|
| 165 |
log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump |
|---|
| 166 |
log_entry |
|---|
| 167 |
else |
|---|
| 168 |
"%s %s" % [message, dump] |
|---|
| 169 |
end |
|---|
| 170 |
end |
|---|
| 171 |
end |
|---|
| 172 |
end |
|---|
| 173 |
end |
|---|