Class: ActiveRecord::ConnectionAdapters::OpenBaseAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::OpenBaseAdapter
- Defined in:
- lib/active_record/connection_adapters/openbase_adapter.rb
Overview
The OpenBase adapter works with the Ruby/Openbase driver by Tetsuya Suzuki. www.spice-of-life.net/ruby-openbase/ (needs version 0.7.3+)
Options:
-
:host
– Defaults to localhost -
:username
– Defaults to nothing -
:password
– Defaults to nothing -
:database
– The name of the database. No default, must be provided.
The OpenBase adapter will make use of OpenBase’s ability to generate unique ids for any column with an unique index applied. Thus, if the value of a primary key is not specified at the time an INSERT is performed, the adapter will prefetch a unique id for the primary key. This prefetching is also necessary in order to return the id after an insert.
Caveat: Operations involving LIMIT and OFFSET do not yet work!
Maintainer: [email protected]
Constant Summary collapse
- DECIMAL_COLUMNS =
{}
- COLUMN_SUPPORT_TABLE =
"rails_openbase_column_support"
Instance Method Summary collapse
- #adapter_name ⇒ Object
- #add_column(table_name, column_name, type, options = {}) ⇒ Object
-
#add_column_options!(sql, options) ⇒ Object
:nodoc:.
- #add_index(table_name, column_name, options = {}) ⇒ Object
-
#add_limit_offset!(sql, options) ⇒ Object
DATABASE STATEMENTS ======================================.
-
#begin_db_transaction ⇒ Object
:nodoc:.
-
#change_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:.
- #change_column_default(table_name, column_name, default) ⇒ Object
-
#column_names(table_name) ⇒ Object
:nodoc:.
-
#columns(table_name, name = nil) ⇒ Object
:nodoc:.
-
#commit_db_transaction ⇒ Object
:nodoc:.
-
#create_table(name, options = {}) {|table_definition| ... } ⇒ Object
:nodoc:.
-
#default_sequence_name(table_name, primary_key) ⇒ Object
:nodoc:.
-
#direct_execute(sql, name = nil) ⇒ Object
:nodoc:.
-
#execute(sql, name = nil) ⇒ Object
:nodoc:.
-
#indexes(table_name, name = nil) ⇒ Object
:nodoc:.
-
#insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object
:nodoc:.
- #native_database_types ⇒ Object
- #next_sequence_value(sequence_name) ⇒ Object
- #prefetch_primary_key?(table_name = nil) ⇒ Boolean
-
#quote(value, column = nil) ⇒ Object
QUOTING ==================================================.
- #quoted_false ⇒ Object
- #quoted_true ⇒ Object
- #remove_column(table_name, column_name) ⇒ Object
- #remove_index(table_name, options = {}) ⇒ Object
- #rename_column(table_name, column_name, new_column_name) ⇒ Object
- #rename_table(name, new_name) ⇒ Object
-
#rollback_db_transaction ⇒ Object
:nodoc:.
- #supports_migrations? ⇒ Boolean
-
#tables(name = nil) ⇒ Object
SCHEMA STATEMENTS ======================================== Return the list of all tables in the schema search path.
-
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
:nodoc:.
-
#update(sql, name = nil) ⇒ Object
(also: #delete)
:nodoc:.
Instance Method Details
#adapter_name ⇒ Object
73 74 75 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 73 def adapter_name 'OpenBase' end |
#add_column(table_name, column_name, type, options = {}) ⇒ Object
270 271 272 273 274 275 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 270 def add_column(table_name, column_name, type, = {}) return_value = super(table_name, "COLUMN " + column_name.to_s, type, ) if type == :decimal record_decimal(table_name, column_name, [:precision], [:scale]) end end |
#add_column_options!(sql, options) ⇒ Object
:nodoc:
285 286 287 288 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 285 def (sql, ) #:nodoc: sql << " NOT NULL" if [:null] == false sql << " DEFAULT #{quote([:default], [:column])}" if () end |
#add_index(table_name, column_name, options = {}) ⇒ Object
304 305 306 307 308 309 310 311 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 304 def add_index(table_name, column_name, = {}) if Hash === # legacy support, since this param was a string index_type = [:unique] ? "UNIQUE" : "" else index_type = end execute "CREATE #{index_type} INDEX #{table_name} #{column_name}" end |
#add_limit_offset!(sql, options) ⇒ Object
DATABASE STATEMENTS ======================================
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 144 def add_limit_offset!(sql, ) #:nodoc: return if [:limit].nil? limit = [:limit] offset = [:offset] if limit == 0 # Mess with the where clause to ensure we get no results if sql =~ /WHERE/i sql.sub!(/WHERE/i, 'WHERE 1 = 2 AND ') elsif sql =~ /ORDER\s+BY/i sql.sub!(/ORDER\s+BY/i, 'WHERE 1 = 2 ORDER BY') else sql << 'WHERE 1 = 2' end elsif offset.nil? sql << " RETURN RESULTS #{limit}" else sql << " RETURN RESULTS #{offset} TO #{limit + offset}" end end |
#begin_db_transaction ⇒ Object
:nodoc:
184 185 186 187 188 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 184 def begin_db_transaction #:nodoc: execute "START TRANSACTION" rescue Exception # Transactions aren't supported end |
#change_column(table_name, column_name, type, options = {}) ⇒ Object
:nodoc:
290 291 292 293 294 295 296 297 298 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 290 def change_column(table_name, column_name, type, = {}) #:nodoc: unless () [:default] = select_one("SELECT * FROM _sys_tables WHERE tablename='#{table_name}' AND fieldname='#{column_name}'")["defaultvalue"] end change_column_sql = "ALTER TABLE #{table_name} ADD COLUMN #{column_name} #{type_to_sql(type, [:limit], [:precision], [:scale])}" (change_column_sql, ) execute(change_column_sql) end |
#change_column_default(table_name, column_name, default) ⇒ Object
300 301 302 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 300 def change_column_default(table_name, column_name, default) execute "ALTER TABLE #{table_name} COLUMN #{column_name} SET DEFAULT #{quote(default)}" end |
#column_names(table_name) ⇒ Object
:nodoc:
225 226 227 228 229 230 231 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 225 def column_names(table_name) #:nodoc: sql = "SELECT fieldname FROM _sys_tables " sql << "WHERE tablename='#{table_name}' AND INDEXOF(fieldname,'_')<>0 " sql << "ORDER BY columnNumber" names = direct_execute(sql).fetch_all names.flatten! || names end |
#columns(table_name, name = nil) ⇒ Object
:nodoc:
210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 210 def columns(table_name, name = nil) #:nodoc: sql = "SELECT * FROM _sys_tables " sql << "WHERE tablename='#{table_name}' AND INDEXOF(fieldname,'_')<>0 " sql << "ORDER BY columnNumber" columns = [] direct_execute(sql, name).each_hash do |row| columns << OpenBaseColumn.new(row["fieldname"], default_value(row["defaultvalue"],row["typename"]), sql_type_name(table_name,row["fieldname"],row["typename"],row["length"]), (row["notnull"] == 1 ? false : true) ) end columns end |
#commit_db_transaction ⇒ Object
:nodoc:
190 191 192 193 194 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 190 def commit_db_transaction #:nodoc: execute "COMMIT" rescue Exception # Transactions aren't supported end |
#create_table(name, options = {}) {|table_definition| ... } ⇒ Object
:nodoc:
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 246 def create_table(name, = {}) #:nodoc: return_value = super # Get my own copy of TableDefinition so that i can detect decimal columns table_definition = TableDefinition.new(self) yield table_definition table_definition.columns.each do |col| if col.type == :decimal record_decimal(name, col.name, col.precision, col.scale) end end unless [:id] == false primary_key = ([:primary_key] || "id") direct_execute("CREATE PRIMARY KEY #{name} (#{primary_key})") end return_value end |
#default_sequence_name(table_name, primary_key) ⇒ Object
:nodoc:
102 103 104 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 102 def default_sequence_name(table_name, primary_key) # :nodoc: "#{table_name} #{primary_key}" end |
#direct_execute(sql, name = nil) ⇒ Object
:nodoc:
174 175 176 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 174 def direct_execute(sql, name = nil) #:nodoc: log(sql, name) { @connection.execute(sql) } end |
#execute(sql, name = nil) ⇒ Object
:nodoc:
170 171 172 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 170 def execute(sql, name = nil) #:nodoc: log(sql, name) { @connection.execute(sql) } end |
#indexes(table_name, name = nil) ⇒ Object
:nodoc:
233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 233 def indexes(table_name, name = nil)#:nodoc: sql = "SELECT fieldname, notnull, searchindex, uniqueindex, clusteredindex FROM _sys_tables " sql << "WHERE tablename='#{table_name}' AND INDEXOF(fieldname,'_')<>0 " sql << "AND primarykey=0 " sql << "AND (searchindex=1 OR uniqueindex=1 OR clusteredindex=1) " sql << "ORDER BY columnNumber" indexes = [] execute(sql, name).each do |row| indexes << IndexDefinition.new(table_name,ob_index_name(row),row[3]==1,[row[0]]) end indexes end |
#insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) ⇒ Object
:nodoc:
164 165 166 167 168 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 164 def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) #:nodoc: execute(sql, name) update_nulls_after_insert(sql, name, pk, id_value, sequence_name) id_value end |
#native_database_types ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 77 def native_database_types { :primary_key => "integer NOT NULL UNIQUE INDEX DEFAULT _rowid", :string => { :name => "char", :limit => 4096 }, :text => { :name => "text" }, :integer => { :name => "integer" }, :float => { :name => "float" }, :decimal => { :name => "decimal" }, :datetime => { :name => "datetime" }, :timestamp => { :name => "timestamp" }, :time => { :name => "time" }, :date => { :name => "date" }, :binary => { :name => "object" }, :boolean => { :name => "boolean" } } end |
#next_sequence_value(sequence_name) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 106 def next_sequence_value(sequence_name) ary = sequence_name.split(' ') if (!ary[1]) then ary[0] =~ /(\w+)_nonstd_seq/ ary[0] = $1 end @connection.unique_row_id(ary[0], ary[1]) end |
#prefetch_primary_key?(table_name = nil) ⇒ Boolean
98 99 100 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 98 def prefetch_primary_key?(table_name = nil) true end |
#quote(value, column = nil) ⇒ Object
QUOTING ==================================================
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 118 def quote(value, column = nil) if value.kind_of?(String) && column && column.type == :binary "'#{@connection.insert_binary(value)}'" elsif value.kind_of?(BigDecimal) return "'#{value.to_s}'" elsif column && column.type == :integer && column.sql_type =~ /decimal/ return "'#{value.to_s}'" elsif [Float,Fixnum,Bignum].include?(value.class) && column && column.type == :string return "'#{value.to_s}'" else super end end |
#quoted_false ⇒ Object
136 137 138 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 136 def quoted_false "0" end |
#quoted_true ⇒ Object
132 133 134 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 132 def quoted_true "1" end |
#remove_column(table_name, column_name) ⇒ Object
277 278 279 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 277 def remove_column(table_name, column_name) execute "ALTER TABLE #{table_name} REMOVE COLUMN #{quote_column_name(column_name)}" end |
#remove_index(table_name, options = {}) ⇒ Object
313 314 315 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 313 def remove_index(table_name, = {}) execute "DROP INDEX #{table_name} #{ === Hash ? [:column] : }" end |
#rename_column(table_name, column_name, new_column_name) ⇒ Object
281 282 283 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 281 def rename_column(table_name, column_name, new_column_name) execute "ALTER TABLE #{table_name} RENAME #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}" end |
#rename_table(name, new_name) ⇒ Object
266 267 268 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 266 def rename_table(name, new_name) execute "RENAME #{name} #{new_name}" end |
#rollback_db_transaction ⇒ Object
:nodoc:
196 197 198 199 200 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 196 def rollback_db_transaction #:nodoc: execute "ROLLBACK" rescue Exception # Transactions aren't supported end |
#supports_migrations? ⇒ Boolean
94 95 96 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 94 def supports_migrations? true end |
#tables(name = nil) ⇒ Object
SCHEMA STATEMENTS ======================================== Return the list of all tables in the schema search path.
205 206 207 208 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 205 def tables(name = nil) #:nodoc: tables = @connection.tables tables.reject! { |t| /\A_SYS_/ === t } end |
#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object
:nodoc:
317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 317 def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc: return super unless type.to_s == 'decimal' if (scale.to_i == 2) return 'money' elsif (scale.to_i == 0) return 'longlong' else return "char(#{precision.to_i + 1})" end end |
#update(sql, name = nil) ⇒ Object Also known as: delete
:nodoc:
178 179 180 |
# File 'lib/active_record/connection_adapters/openbase_adapter.rb', line 178 def update(sql, name = nil) #:nodoc: execute(sql, name).rows_affected end |