Module: ArPerfToolkit::Insert

Defined in:
lib/insert.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/insert.rb', line 20

def self.included(base) #:nodoc:
  base.extend(ClassMethods)
  
  #alias methods
  base.class_eval do
    alias_method_chain_arperf(%w(save update save! create_or_update))
    class << self
       alias_method_chain_arperf(%w(create create!))
    end
  end
end

Instance Method Details

#create_or_update_x(options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/insert.rb', line 93

def create_or_update_x options={}
  return false if callback(:before_save) == false
  options[:keywords] = 'IGNORE' if options.delete(:ignore).kind_of?(TrueClass)
  result = 
    if new_record? then 
      create_x(options) 
    else 
      options.delete(:duplicate) 
      update_x(options) 
    end
  callback(:after_save)
  result
end

#create_x(options = {}) ⇒ Object

Creates a new record with values matching those of the instance attributes.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/insert.rb', line 126

def create_x options={}
  return false if callback(:before_create) == false
  insert_with_timestamps(true)

  if self.id.nil? && connection.prefetch_primary_key?(self.class.table_name)
    self.id = connection.next_sequence_value(self.class.sequence_name)
  end
  
  options[:command]||='INSERT'
  sql = self.class.construct_sql(options) do |sql, options| 
    sql << "INTO #{self.class.table_name} (#{quoted_column_names.join(', ')}) "
    sql << "VALUES(#{attributes_with_quotes.values.join(', ')})"
  end
  
  self.id = connection.insert(sql, "#{self.class.name} CreateX ", 
                  self.class.primary_key, self.id, self.class.sequence_name)

  @new_record = false
  
  callback(:after_create)   
  return self.id != 0
end

#replace_x(options = {}) ⇒ Object

Replace deletes the existing duplicate if it exists then adds this



151
152
153
# File 'lib/insert.rb', line 151

def replace_x options={}
  create_x(options.merge(:command => 'REPLACE'))
end

#save_ignoreObject

ClassMethods



66
67
68
# File 'lib/insert.rb', line 66

def save_ignore
  save_x :keywords => 'IGNORE'
end

#save_ignore!Object



70
71
72
# File 'lib/insert.rb', line 70

def save_ignore!
  save_ignore || raise(ActiveRecord::RecordNotSaved)
end

#save_x(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/insert.rb', line 74

def save_x(options={})
  #invoke save_with_validation if the argument is not a hash
  return save_without_arperf_options(options) unless options.is_a?(Hash)
  
  perform_validation = options.delete(:perform_validation)
  if (perform_validation.is_a?(FalseClass)) || valid? 
    raise ActiveRecord::ReadOnlyRecord if readonly?
    create_or_update_x options
  else
    raise ActiveRecord::RecordInvalid.new(self) if perform_validation == :exception
    false
  end
end

#save_x!(options = {}) ⇒ Object



88
89
90
91
# File 'lib/insert.rb', line 88

def save_x!(options={})
  options[:perform_validation] = :exception
  save_x(options) || raise(ActiveRecord::RecordNotSaved)
end

#update_x(options = {}) ⇒ Object

Updates the associated record with values matching those of the instance attributes.

Raises:

  • (ArgumentError)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/insert.rb', line 110

def update_x options={}
  return false if callback(:before_update) == false
  insert_with_timestamps(false)
  options[:command]||='UPDATE'
  raise(ArgumentError, "Unknown key: duplicate") if options[:duplicate]
  sql = self.class.construct_sql(options) do |sql, options|  
    sql << "#{self.class.table_name} "
    sql << "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " +
           "WHERE (#{self.class.primary_key} = #{quote_value(id)}) "      
  end
  connection.update(sql, "#{self.class.name} UpdateX")
  callback(:after_update)
  return true
end