Module: JdbcSpec::Mimer

Defined in:
lib/jdbc_adapter/jdbc_mimer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.adapter_matcher(name) ⇒ Object



7
8
9
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 7

def self.adapter_matcher(name, *)
  name =~ /mimer/i ? self : false
end

.extended(mod) ⇒ Object



3
4
5
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 3

def self.extended(mod)
  ActiveRecord::Base.extend JdbcSpec::QuotedPrimaryKeyExtension
end

Instance Method Details

#_execute(sql, name = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 129

def _execute(sql, name = nil)
    if sql =~ /^select/i
      @offset ||= 0
      if !@limit || @limit == -1
        range = @offset..-1
      else
        range = @offset...(@offset+@limit)
      end
      @connection.execute_query(sql)[range]
    else
      @connection.execute_update(sql)
    end
ensure
  @limit = @offset = nil
end

#add_limit_offset!(sql, options) ⇒ Object

:nodoc:



105
106
107
108
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 105

def add_limit_offset!(sql, options) # :nodoc:
  @limit = options[:limit]
  @offset = options[:offset]
end

#change_column(table_name, column_name, type, options = {}) ⇒ Object

:nodoc:



38
39
40
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 38

def change_column(table_name, column_name, type, options = {}) #:nodoc:
  execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} #{type_to_sql(type, options[:limit])}"
end

#change_column_default(table_name, column_name, default) ⇒ Object

:nodoc:



42
43
44
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 42

def change_column_default(table_name, column_name, default) #:nodoc:
  execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET DEFAULT #{quote(default)}"
end

#create_table(name, options = {}) ⇒ Object

:nodoc:



28
29
30
31
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 28

def create_table(name, options = {}) #:nodoc:
  super(name, options)
  execute "CREATE SEQUENCE #{name}_seq" unless options[:id] == false
end

#default_sequence_name(table, column) ⇒ Object

:nodoc:



24
25
26
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 24

def default_sequence_name(table, column) #:nodoc:
  "#{table}_seq"
end

#drop_table(name, options = {}) ⇒ Object

:nodoc:



33
34
35
36
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 33

def drop_table(name, options = {}) #:nodoc:
  super(name) rescue nil
  execute "DROP SEQUENCE #{name}_seq" rescue nil
end

#execute_prepared_insert(sql, id) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 64

def execute_prepared_insert(sql, id)
  @stmts ||= {}
  @stmts[sql] ||= @connection.ps(sql)
  stmt = @stmts[sql]
  stmt.setLong(1,id)
  stmt.executeUpdate
  id
end

#insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object

:nodoc:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 50

def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
  if pk.nil? # Who called us? What does the sql look like? No idea!
    execute sql, name
  elsif id_value # Pre-assigned id
    log(sql, name) { @connection.execute_insert sql,pk }
  else # Assume the sql contains a bind-variable for the id
    id_value = select_one("SELECT NEXT_VALUE OF #{sequence_name} AS val FROM MIMER.ONEROW")['val']
    log(sql, name) { 
      execute_prepared_insert(sql,id_value)
    }
  end
  id_value
end

#modify_types(tp) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 11

def modify_types(tp)
  tp[:primary_key] = "INTEGER NOT NULL PRIMARY KEY"
  tp[:boolean][:limit] = nil
  tp[:string][:limit] = 255
  tp[:binary] = {:name => "BINARY VARYING", :limit => 4096}
  tp[:text] = {:name => "VARCHAR", :limit => 4096}
  tp[:datetime] = { :name => "TIMESTAMP" }
  tp[:timestamp] = { :name => "TIMESTAMP" }
  tp[:time] = { :name => "TIMESTAMP" }
  tp[:date] = { :name => "TIMESTAMP" }
  tp
end

#quote(value, column = nil) ⇒ Object

:nodoc:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 73

def quote(value, column = nil) #:nodoc:
  return value.quoted_id if value.respond_to?(:quoted_id)
  
  if String === value && column && column.type == :binary
    return "X'#{quote_string(value.unpack("C*").collect {|v| v.to_s(16)}.join)}'"
  end
  case value
  when String     
    %Q{'#{quote_string(value)}'}
  when NilClass   
    'NULL'
  when TrueClass  
    '1'
  when FalseClass 
    '0'
  when Numeric    
    value.to_s
  when Date, Time 
    %Q{TIMESTAMP '#{value.strftime("%Y-%m-%d %H:%M:%S")}'}
  else              
    %Q{'#{quote_string(value.to_yaml)}'}
  end
end

#quoted_falseObject



101
102
103
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 101

def quoted_false
  '0'
end

#quoted_trueObject



97
98
99
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 97

def quoted_true
  '1'
end

#remove_index(table_name, options = {}) ⇒ Object

:nodoc:



46
47
48
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 46

def remove_index(table_name, options = {}) #:nodoc:
  execute "DROP INDEX #{index_name(table_name, options)}"
end

#select_all(sql, name = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 110

def select_all(sql, name = nil)
  @offset ||= 0
  if !@limit || @limit == -1
    range = @offset..-1
  else
    range = @offset...(@offset+@limit)
  end
  select(sql, name)[range]
ensure
  @limit = @offset = nil
end

#select_one(sql, name = nil) ⇒ Object



122
123
124
125
126
127
# File 'lib/jdbc_adapter/jdbc_mimer.rb', line 122

def select_one(sql, name = nil)
  @offset ||= 0
  select(sql, name)[@offset]
ensure
  @limit = @offset = nil
end