Module: ArJdbc::Mimer

Defined in:
lib/arjdbc/mimer/adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object



3
4
5
6
# File 'lib/arjdbc/mimer/adapter.rb', line 3

def self.extended(mod)
  require 'arjdbc/jdbc/quoted_primary_key'
  ActiveRecord::Base.extend ArJdbc::QuotedPrimaryKeyExtension
end

Instance Method Details

#_execute(sql, name = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/arjdbc/mimer/adapter.rb', line 126

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:



102
103
104
105
# File 'lib/arjdbc/mimer/adapter.rb', line 102

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

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

:nodoc:



35
36
37
# File 'lib/arjdbc/mimer/adapter.rb', line 35

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:



39
40
41
# File 'lib/arjdbc/mimer/adapter.rb', line 39

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:



25
26
27
28
# File 'lib/arjdbc/mimer/adapter.rb', line 25

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:



21
22
23
# File 'lib/arjdbc/mimer/adapter.rb', line 21

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

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

:nodoc:



30
31
32
33
# File 'lib/arjdbc/mimer/adapter.rb', line 30

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

#execute_prepared_insert(sql, id) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/arjdbc/mimer/adapter.rb', line 61

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, binds = []) ⇒ Object

:nodoc:



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/arjdbc/mimer/adapter.rb', line 47

def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = []) #:nodoc:
  if pk.nil? # Who called us? What does the sql look like? No idea!
    execute sql, name, binds
  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



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/arjdbc/mimer/adapter.rb', line 8

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:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/arjdbc/mimer/adapter.rb', line 70

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



98
99
100
# File 'lib/arjdbc/mimer/adapter.rb', line 98

def quoted_false
  '0'
end

#quoted_trueObject



94
95
96
# File 'lib/arjdbc/mimer/adapter.rb', line 94

def quoted_true
  '1'
end

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

:nodoc:



43
44
45
# File 'lib/arjdbc/mimer/adapter.rb', line 43

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

#select_all(sql, name = nil, binds = []) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/arjdbc/mimer/adapter.rb', line 107

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

#select_one(sql, name = nil) ⇒ Object



119
120
121
122
123
124
# File 'lib/arjdbc/mimer/adapter.rb', line 119

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