]> source.dussan.org Git - sonarqube.git/blob
c4fe6f25871c19bb6e3e00cb57e50ae01799284c
[sonarqube.git] /
1 module ::ActiveRecord
2   class Base
3     after_save :write_lobs
4
5   private
6     def write_lobs
7       if connection.is_a?(JdbcSpec::Informix)
8         self.class.columns.each do |c|
9           if [:text, :binary].include? c.type
10             value = self[c.name]
11             value = value.to_yaml if unserializable_attribute?(c.name, c)
12
13             unless value.nil? || (value == '')
14               connection.write_large_object(c.type == :binary,
15                                             c.name,
16                                             self.class.table_name,
17                                             self.class.primary_key,
18                                             quote_value(id),
19                                             value)
20             end
21           end
22         end
23       end
24     end
25   end
26 end
27
28 module ::JdbcSpec
29   module ActiveRecordExtensions
30     def informix_connection(config)
31       config[:port] ||= 9088
32       config[:url] ||= "jdbc:informix-sqli://#{config[:host]}:#{config[:port]}/#{config[:database]}:INFORMIXSERVER=#{config[:servername]}"
33       config[:driver] = 'com.informix.jdbc.IfxDriver'
34       jdbc_connection(config)
35     end
36   end
37
38   module Informix
39     def self.extended(base)
40       @@db_major_version = base.select_one("SELECT dbinfo('version', 'major') version FROM systables WHERE tabid = 1")['version'].to_i
41     end
42
43     def self.column_selector
44       [ /informix/i,
45         lambda { |cfg, column| column.extend(::JdbcSpec::Informix::Column) } ]
46     end
47
48     def self.adapter_selector
49       [ /informix/i,
50         lambda { |cfg, adapter| adapter.extend(::JdbcSpec::Informix) } ]
51     end
52
53     module Column
54     private
55       # TODO: Test all Informix column types.
56       def simplified_type(field_type)
57         if field_type =~ /serial/i
58           :primary_key
59         else
60           super
61         end
62       end
63     end
64
65     def modify_types(tp)
66       tp[:primary_key] = "SERIAL PRIMARY KEY"
67       tp[:string]      = { :name => "VARCHAR", :limit => 255 }
68       tp[:integer]     = { :name => "INTEGER" }
69       tp[:float]       = { :name => "FLOAT" }
70       tp[:decimal]     = { :name => "DECIMAL" }
71       tp[:datetime]    = { :name => "DATETIME YEAR TO FRACTION(5)" }
72       tp[:timestamp]   = { :name => "DATETIME YEAR TO FRACTION(5)" }
73       tp[:time]        = { :name => "DATETIME HOUR TO FRACTION(5)" }
74       tp[:date]        = { :name => "DATE" }
75       tp[:binary]      = { :name => "BYTE" }
76       tp[:boolean]     = { :name => "BOOLEAN" }
77       tp
78     end
79
80     def prefetch_primary_key?(table_name = nil)
81       true
82     end
83
84     def supports_migrations?
85       true
86     end
87
88     def default_sequence_name(table, column)
89       "#{table}_seq"
90     end
91
92     def add_limit_offset!(sql, options)
93       if options[:limit]
94         limit = "FIRST #{options[:limit]}"
95         # SKIP available only in IDS >= 10
96         offset = (@@db_major_version >= 10 && options[:offset]?
97                   "SKIP #{options[:offset]}" : "")
98         sql.sub!(/^select /i, "SELECT #{offset} #{limit} ")
99       end
100       sql
101     end
102
103     def next_sequence_value(sequence_name)
104       select_one("SELECT #{sequence_name}.nextval id FROM systables WHERE tabid=1")['id']
105     end
106
107     # TODO: Add some smart quoting for newlines in string and text fields.
108     def quote_string(string)
109       string.gsub(/\'/, "''")
110     end
111
112     def quote(value, column = nil)
113       if column && [:binary, :text].include?(column.type)
114         # LOBs are updated separately by an after_save trigger.
115         "NULL"
116       elsif column && column.type == :date
117         "'#{value.mon}/#{value.day}/#{value.year}'"
118       else
119         super
120       end
121     end
122
123     def create_table(name, options = {})
124       super(name, options)
125       execute("CREATE SEQUENCE #{name}_seq")
126     end
127  
128     def rename_table(name, new_name)
129       execute("RENAME TABLE #{name} TO #{new_name}")
130       execute("RENAME SEQUENCE #{name}_seq TO #{new_name}_seq")
131     end
132  
133     def drop_table(name)
134       super(name)
135       execute("DROP SEQUENCE #{name}_seq")
136     end
137
138     def remove_index(table_name, options = {})
139       @connection.execute_update("DROP INDEX #{index_name(table_name, options)}")
140     end
141
142   private
143     def select(sql, name = nil)
144       # Informix does not like "= NULL", "!= NULL", or "<> NULL".
145       execute(sql.gsub(/(!=|<>)\s*null/i, "IS NOT NULL").gsub(/=\s*null/i, "IS NULL"), name)
146     end
147   end # module Informix
148 end # module ::JdbcSpec