Module: ArJdbc::FireBird

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/arjdbc/firebird/adapter.rb', line 4

def self.extended(mod)
  unless @lob_callback_added
    ActiveRecord::Base.class_eval do
      def after_save_with_firebird_blob
        self.class.columns.select { |c| c.sql_type =~ /blob/i }.each do |c|
          value = self[c.name]
          value = value.to_yaml if unserializable_attribute?(c.name, c)
          next if value.nil?
          connection.write_large_object(c.type == :binary, c.name, self.class.table_name, self.class.primary_key, quote_value(id), value)
        end
      end
    end

    ActiveRecord::Base.after_save :after_save_with_firebird_blob
    @lob_callback_added = true
  end
end

Instance Method Details

#adapter_nameObject



22
23
24
# File 'lib/arjdbc/firebird/adapter.rb', line 22

def adapter_name
  'Firebird'
end

#add_limit_offset!(sql, options) ⇒ Object

:nodoc:



38
39
40
41
42
43
44
# File 'lib/arjdbc/firebird/adapter.rb', line 38

def add_limit_offset!(sql, options) # :nodoc:
  if options[:limit]
    limit_string = "FIRST #{options[:limit]}"
    limit_string << " SKIP #{options[:offset]}" if options[:offset]
    sql.sub!(/\A(\s*SELECT\s)/i, '\&' + limit_string + ' ')
  end
end

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

:nodoc:



73
74
75
# File 'lib/arjdbc/firebird/adapter.rb', line 73

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

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

:nodoc:



58
59
60
61
# File 'lib/arjdbc/firebird/adapter.rb', line 58

def create_table(name, options = {}) #:nodoc:
  super(name, options)
  execute "CREATE GENERATOR #{name}_seq"
end

#default_sequence_name(table_name, primary_key) ⇒ Object

:nodoc:



50
51
52
# File 'lib/arjdbc/firebird/adapter.rb', line 50

def default_sequence_name(table_name, primary_key) # :nodoc:
  "#{table_name}_seq"
end

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

:nodoc:



68
69
70
71
# File 'lib/arjdbc/firebird/adapter.rb', line 68

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

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

:nodoc:



33
34
35
36
# File 'lib/arjdbc/firebird/adapter.rb', line 33

def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) # :nodoc:
  execute(sql, name)
  id_value
end

#modify_types(tp) ⇒ Object



26
27
28
29
30
31
# File 'lib/arjdbc/firebird/adapter.rb', line 26

def modify_types(tp)
  tp[:primary_key] = 'INTEGER NOT NULL PRIMARY KEY'
  tp[:string][:limit] = 252
  tp[:integer][:limit] = nil
  tp
end

#next_sequence_value(sequence_name) ⇒ Object



54
55
56
# File 'lib/arjdbc/firebird/adapter.rb', line 54

def next_sequence_value(sequence_name)
  select_one("SELECT GEN_ID(#{sequence_name}, 1 ) FROM RDB$DATABASE;")["gen_id"]
end

#prefetch_primary_key?(table_name = nil) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/arjdbc/firebird/adapter.rb', line 46

def prefetch_primary_key?(table_name = nil)
  true
end

#quote(value, column = nil) ⇒ Object

:nodoc:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/arjdbc/firebird/adapter.rb', line 85

def quote(value, column = nil) # :nodoc:
  return value.quoted_id if value.respond_to?(:quoted_id)

  # BLOBs are updated separately by an after_save trigger.
  return value.nil? ? "NULL" : "'#{quote_string(value[0..1])}'" if column && [:binary, :text].include?(column.type)

  if [Time, DateTime].include?(value.class)
    "CAST('#{value.strftime("%Y-%m-%d %H:%M:%S")}' AS TIMESTAMP)"
  else
    if column && column.type == :primary_key
      return value.to_s
    end
    super
  end
end

#quote_column_name(column_name) ⇒ Object

:nodoc:



105
106
107
# File 'lib/arjdbc/firebird/adapter.rb', line 105

def quote_column_name(column_name) # :nodoc:
  %Q("#{ar_to_fb_case(column_name)}")
end

#quote_string(string) ⇒ Object

:nodoc:



101
102
103
# File 'lib/arjdbc/firebird/adapter.rb', line 101

def quote_string(string) # :nodoc:
  string.gsub(/'/, "''")
end

#quoted_falseObject

:nodoc:



113
114
115
# File 'lib/arjdbc/firebird/adapter.rb', line 113

def quoted_false # :nodoc:
  quote(0)
end

#quoted_trueObject

:nodoc:



109
110
111
# File 'lib/arjdbc/firebird/adapter.rb', line 109

def quoted_true # :nodoc:
  quote(1)
end

#remove_index(table_name, options) ⇒ Object

:nodoc:



81
82
83
# File 'lib/arjdbc/firebird/adapter.rb', line 81

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

#rename_column(table_name, column_name, new_column_name) ⇒ Object



77
78
79
# File 'lib/arjdbc/firebird/adapter.rb', line 77

def rename_column(table_name, column_name, new_column_name)
  execute "ALTER TABLE #{table_name} ALTER  #{column_name} TO #{new_column_name}"
end

#rename_table(name, new_name) ⇒ Object

:nodoc:



63
64
65
66
# File 'lib/arjdbc/firebird/adapter.rb', line 63

def rename_table(name, new_name) #:nodoc:
  execute "RENAME #{name} TO #{new_name}"
  execute "UPDATE RDB$GENERATORS SET RDB$GENERATOR_NAME='#{new_name}_seq' WHERE RDB$GENERATOR_NAME='#{name}_seq'" rescue nil
end