4 require 'arjdbc/jdbc/quoted_primary_key'
5 ActiveRecord::Base.extend ArJdbc::QuotedPrimaryKeyExtension
9 tp[:primary_key] = "INTEGER NOT NULL PRIMARY KEY"
10 tp[:boolean][:limit] = nil
11 tp[:string][:limit] = 255
12 tp[:binary] = {:name => "BINARY VARYING", :limit => 4096}
13 tp[:text] = {:name => "VARCHAR", :limit => 4096}
14 tp[:datetime] = { :name => "TIMESTAMP" }
15 tp[:timestamp] = { :name => "TIMESTAMP" }
16 tp[:time] = { :name => "TIMESTAMP" }
17 tp[:date] = { :name => "TIMESTAMP" }
21 def default_sequence_name(table, column) #:nodoc:
25 def create_table(name, options = {}) #:nodoc:
27 execute "CREATE SEQUENCE #{name}_seq" unless options[:id] == false
30 def drop_table(name, options = {}) #:nodoc:
31 super(name) rescue nil
32 execute "DROP SEQUENCE #{name}_seq" rescue nil
35 def change_column(table_name, column_name, type, options = {}) #:nodoc:
36 execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} #{type_to_sql(type, options[:limit])}"
39 def change_column_default(table_name, column_name, default) #:nodoc:
40 execute "ALTER TABLE #{table_name} ALTER COLUMN #{column_name} SET DEFAULT #{quote(default)}"
43 def remove_index(table_name, options = {}) #:nodoc:
44 execute "DROP INDEX #{index_name(table_name, options)}"
47 def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc:
48 if pk.nil? # Who called us? What does the sql look like? No idea!
50 elsif id_value # Pre-assigned id
51 log(sql, name) { @connection.execute_insert sql,pk }
52 else # Assume the sql contains a bind-variable for the id
53 id_value = select_one("SELECT NEXT_VALUE OF #{sequence_name} AS val FROM MIMER.ONEROW")['val']
55 execute_prepared_insert(sql,id_value)
61 def execute_prepared_insert(sql, id)
63 @stmts[sql] ||= @connection.ps(sql)
70 def quote(value, column = nil) #:nodoc:
71 return value.quoted_id if value.respond_to?(:quoted_id)
73 if String === value && column && column.type == :binary
74 return "X'#{quote_string(value.unpack("C*").collect {|v| v.to_s(16)}.join)}'"
78 %Q{'#{quote_string(value)}'}
88 %Q{TIMESTAMP '#{value.strftime("%Y-%m-%d %H:%M:%S")}'}
90 %Q{'#{quote_string(value.to_yaml)}'}
102 def add_limit_offset!(sql, options) # :nodoc:
103 @limit = options[:limit]
104 @offset = options[:offset]
107 def select_all(sql, name = nil)
109 if !@limit || @limit == -1
112 range = @offset...(@offset+@limit)
114 select(sql, name)[range]
116 @limit = @offset = nil
119 def select_one(sql, name = nil)
121 select(sql, name)[@offset]
123 @limit = @offset = nil
126 def _execute(sql, name = nil)
129 if !@limit || @limit == -1
132 range = @offset...(@offset+@limit)
134 @connection.execute_query(sql)[range]
136 @connection.execute_update(sql)
139 @limit = @offset = nil