Class: DataMapper::Adapters::Sql::Commands::LoadCommand
- Defined in:
- lib/data_mapper/adapters/sql/commands/load_command.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#instance_id ⇒ Object
readonly
Returns the value of attribute instance_id.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
Instance Method Summary collapse
- #call ⇒ Object
-
#create_instance(instance_class, instance_id) ⇒ Object
Create an instance for the specified Class and id in preparation for loading.
- #escape(conditions) ⇒ Object
- #include?(association_name) ⇒ Boolean
- #includes ⇒ Object
-
#initialize(adapter, session, klass, options) ⇒ LoadCommand
constructor
A new instance of LoadCommand.
- #inspect ⇒ Object
- #load(hash, set = []) ⇒ Object
- #load_instance(instance, columns, values, set = []) ⇒ Object
- #load_instances(fields, rows) ⇒ Object
- #reload? ⇒ Boolean
- #select ⇒ Object
- #table_name ⇒ Object
- #to_sql ⇒ Object
Constructor Details
#initialize(adapter, session, klass, options) ⇒ LoadCommand
Returns a new instance of LoadCommand.
12 13 14 15 16 17 18 19 20 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 12 def initialize(adapter, session, klass, ) @adapter, @session, @klass, @options = adapter, session, klass, @order = @options[:order] @limit = @options[:limit] @reload = @options[:reload] @instance_id = @options[:id] @conditions = Conditions.new(@adapter, self) end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
10 11 12 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 10 def conditions @conditions end |
#instance_id ⇒ Object (readonly)
Returns the value of attribute instance_id.
10 11 12 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 10 def instance_id @instance_id end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
10 11 12 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 10 def klass @klass end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
10 11 12 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 10 def limit @limit end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 10 def @options end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
10 11 12 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 10 def order @order end |
Instance Method Details
#call ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 91 def call if instance_id && !reload? if instance_id.kind_of?(Array) instances = instance_id.map do |id| @session.identity_map.get(klass, id) end.compact return instances if instances.size == instance_id.size else instance = @session.identity_map.get(klass, instance_id) return instance unless instance.nil? end end reader = execute(to_sql) results = if eof?(reader) nil elsif limit == 1 || ( instance_id && !instance_id.kind_of?(Array) ) fetch_one(reader) else fetch_all(reader) end close_reader(reader) return results end |
#create_instance(instance_class, instance_id) ⇒ Object
Create an instance for the specified Class and id in preparation for loading. This method first checks to see if the instance is in the IdentityMap. If not, then a new class is created, it’s marked as not-new, the key is set and it’s added to the IdentityMap. Afterwards the instance’s Session is updated to the current session, and the instance returned.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 233 def create_instance(instance_class, instance_id) instance = @session.identity_map.get(instance_class, instance_id) if instance.nil? || reload? instance = instance_class.new() instance.instance_variable_set(:@__key, instance_id) instance.instance_variable_set(:@new_record, false) @session.identity_map.set(instance) end instance.session = @session return instance end |
#escape(conditions) ⇒ Object
26 27 28 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 26 def escape(conditions) @adapter.escape(conditions) end |
#include?(association_name) ⇒ Boolean
34 35 36 37 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 34 def include?(association_name) return false if includes.empty? includes.include?(association_name) end |
#includes ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 39 def includes @includes || @includes = begin list = @options[:include] || [] list.kind_of?(Array) ? list : [list] list end end |
#inspect ⇒ Object
30 31 32 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 30 def inspect @options.inspect end |
#load(hash, set = []) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 120 def load(hash, set = []) instance_class = unless hash['type'].nil? Kernel::const_get(hash['type']) else klass end mapping = @adapter[instance_class] instance_id = mapping.key.type_cast_value(hash['id']) instance = @session.identity_map.get(instance_class, instance_id) if instance.nil? || reload? instance ||= instance_class.new instance.class.callbacks.execute(:before_materialize, instance) instance.instance_variable_set(:@new_record, false) hash.each_pair do |name_as_string,raw_value| name = name_as_string.to_sym if column = mapping.find_by_column_name(name) value = column.type_cast_value(raw_value) instance.instance_variable_set(column.instance_variable_name, value) else instance.instance_variable_set("@#{name}", value) end instance.original_hashes[name] = value.hash end instance.instance_variable_set(:@__key, instance_id) instance.class.callbacks.execute(:after_materialize, instance) @session.identity_map.set(instance) end instance.instance_variable_set(:@loaded_set, set) instance.session = @session set << instance return instance end |
#load_instance(instance, columns, values, set = []) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 248 def load_instance(instance, columns, values, set = []) instance.class.callbacks.execute(:before_materialize, instance) hashes = {} columns.each_pair do |column, i| hashes[column.name] = instance.instance_variable_set( column.instance_variable_name, column.type_cast_value(values[i]) ).hash end instance.instance_variable_set(:@original_hashes, hashes) instance.instance_variable_set(:@loaded_set, set) set << instance instance.class.callbacks.execute(:after_materialize, instance) return instance end |
#load_instances(fields, rows) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 162 def load_instances(fields, rows) table = @adapter[klass] set = [] columns = {} key_ordinal = nil key_column = table.key type_ordinal = nil type_column = nil fields.each_with_index do |field, i| column = table.find_by_column_name(field.to_sym) key_ordinal = i if column.key? type_ordinal, type_column = i, column if column.name == :type columns[column] = i end if type_ordinal tables = Hash.new() do |h,k| table_for_row = @adapter[k.blank? ? klass : type_column.type_cast_value(k)] key_ordinal_for_row = nil columns_for_row = {} fields.each_with_index do |field, i| column = table_for_row.find_by_column_name(field.to_sym) key_ordinal_for_row = i if column.key? columns_for_row[column] = i end h[k] = [ table_for_row.klass, table_for_row.key, key_ordinal_for_row, columns_for_row ] end rows.each do |row| klass_for_row, key_column_for_row, key_ordinal_for_row, columns_for_row = *tables[row[type_ordinal]] load_instance( create_instance( klass_for_row, key_column_for_row.type_cast_value(row[key_ordinal_for_row]) ), columns_for_row, row, set ) end else rows.each do |row| load_instance( create_instance( klass, key_column.type_cast_value(row[key_ordinal]) ), columns, row, set ) end end set.dup end |
#reload? ⇒ Boolean
22 23 24 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 22 def reload? @reload end |
#select ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 47 def select @select_columns || @select_columns = begin select_columns = @options[:select] unless select_columns.nil? select_columns = select_columns.kind_of?(Array) ? select_columns : [select_columns] select_columns.map { |column| @adapter.quote_column_name(column.to_s) } else @options[:select] = @adapter[klass].columns.select do |column| include?(column.name) || !column.lazy? end.map { |column| column.to_sql } end end end |
#table_name ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 61 def table_name @table_name || @table_name = if @options.has_key?(:table) @adapter.quote_table_name(@options[:table]) else @adapter[klass].to_sql end end |
#to_sql ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/data_mapper/adapters/sql/commands/load_command.rb', line 69 def to_sql sql = 'SELECT ' << select.join(', ') << ' FROM ' << table_name where = [] where += conditions.to_a unless conditions.empty? unless where.empty? sql << ' WHERE (' << where.join(') AND (') << ')' end unless order.nil? sql << ' ORDER BY ' << order.to_s end unless limit.nil? sql << ' LIMIT ' << limit.to_s end return sql end |