lore 0.4.3 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,7 @@ class Table_Accessor
21
21
 
22
22
  attr_reader :constraints, :foreign_keys, :primary_keys, :attributes, :attribute_types
23
23
 
24
- @@logger = Logger.new(Lore.logfile)
24
+ @@logger = Lore.logger
25
25
 
26
26
  def self.log(message, level=:debug)
27
27
  @@logger.debug(message)
@@ -397,7 +397,6 @@ protected
397
397
  ##########################################################################
398
398
  # @has_a getters/setters {{{
399
399
  def self.get_has_a # :nodoc:
400
-
401
400
  if @has_a.nil? then
402
401
  if @has_a_klasses.nil? then
403
402
  @has_a = Array.new
@@ -409,7 +408,6 @@ protected
409
408
  else
410
409
  return @has_a
411
410
  end
412
-
413
411
  end
414
412
  def self.set_has_a(arg) # :nodoc:
415
413
  @has_a = arg
@@ -420,7 +418,6 @@ protected
420
418
  # @has_a getters/setters {{{
421
419
 
422
420
  def self.get_has_n # :nodoc:
423
-
424
421
  if @has_n.nil? then
425
422
  if @has_n_klasses.nil? then
426
423
  @has_n = Array.new
@@ -432,7 +429,6 @@ protected
432
429
  else
433
430
  return @has_n
434
431
  end
435
-
436
432
  end
437
433
  def self.set_has_n(arg) # :nodoc:
438
434
  @has_n = arg
@@ -612,7 +608,6 @@ private
612
608
  #
613
609
  def self.add_foreign_keys(args)
614
610
  # {{{
615
-
616
611
  other_table = args.at(0).to_s # first index is table name (see usage)
617
612
 
618
613
  new_foreign_keys = Hash.new
@@ -620,7 +615,6 @@ private
620
615
  new_foreign_keys[other_table] = args[1...args.length]
621
616
 
622
617
  set_foreign_keys(new_foreign_keys)
623
-
624
618
  end # }}}
625
619
 
626
620
  private
@@ -629,7 +623,6 @@ private
629
623
  # Add single or multiple primary keys to @primary_keys[args.at(0)]:
630
624
  def self.add_primary_keys(args)
631
625
  # {{{
632
-
633
626
  other_table = args.at(0) # first index is table name (see usage)
634
627
 
635
628
  new_primary_keys = Hash.new
@@ -643,9 +636,7 @@ private
643
636
  else
644
637
  new_primary_keys[other_table] += foreign_p_keys
645
638
  end
646
-
647
639
  @primary_keys = new_primary_keys
648
-
649
640
  end # }}}
650
641
 
651
642
  private
@@ -654,17 +645,13 @@ private
654
645
  # Add one sequence to @sequences[table_name]:
655
646
  def self.add_sequence(table_name, field_name, sequence_name)
656
647
  # {{{
657
-
658
- new_sequences = Hash.new
659
- new_sequences.update(get_sequences)
660
-
661
- new_sequences[table_name] = Hash.new
662
- if sequence_name != nil
663
- new_sequences[table_name][field_name] = sequence_name.to_s
664
- end
665
-
666
- set_sequences(new_sequences)
667
-
648
+ new_sequences = Hash.new
649
+ new_sequences.update(get_sequences)
650
+ new_sequences[table_name] = Hash.new
651
+ if sequence_name != nil
652
+ new_sequences[table_name][field_name] = sequence_name.to_s
653
+ end
654
+ set_sequences(new_sequences)
668
655
  end # }}}
669
656
 
670
657
  private
@@ -672,14 +659,10 @@ private
672
659
  ##########################################################################
673
660
  def self.add_attributes(attrib_hash)
674
661
  # {{{
675
-
676
662
  new_attributes = Hash.new
677
663
  new_attributes.update(get_attributes)
678
-
679
664
  new_attributes.update(attrib_hash)
680
-
681
665
  set_attributes(new_attributes)
682
-
683
666
  end # }}}
684
667
 
685
668
  private
@@ -687,14 +670,10 @@ private
687
670
  ##########################################################################
688
671
  def self.add_attribute_types(attrib_hash)
689
672
  # {{{
690
-
691
673
  new_types = Hash.new
692
674
  new_types.update(get_attribute_types)
693
-
694
675
  new_types.update(attrib_hash)
695
-
696
676
  set_attribute_types(new_types)
697
-
698
677
  end # }}}
699
678
 
700
679
  private
@@ -702,12 +681,9 @@ private
702
681
  ##########################################################################
703
682
  def self.add_explicit(explicit_hash)
704
683
  # {{{
705
-
706
684
  new_explicit = explicit_hash
707
685
  new_explicit.update(get_explicit)
708
-
709
686
  set_explicit(new_explicit)
710
-
711
687
  end # }}}
712
688
 
713
689
  private
@@ -715,11 +691,6 @@ private
715
691
  ##########################################################################
716
692
  def self.add_explicit_attributes(attrib_hash)
717
693
  # {{{
718
-
719
- # new_explicit_attributes = attrib_hash
720
- # new_explicit_attributes.update(get_explicit_attributes)
721
- # set_explicit_attributes(new_explicit_attributes)
722
-
723
694
  attrib_hash.each_pair { |table, attribs|
724
695
  if @explicit_attributes and @explicit_attributes[table] then
725
696
  @explicit_attributes[table] = attrib_hash[table] + @explicit_attributes[table]
@@ -728,7 +699,6 @@ private
728
699
  @explicit_attributes[table] = attrib_hash[table]
729
700
  end
730
701
  }
731
-
732
702
  end # }}}
733
703
 
734
704
  private
@@ -736,7 +706,6 @@ private
736
706
  ##########################################################################
737
707
  def self.add_implicit_attributes(attrib_hash)
738
708
  # {{{
739
-
740
709
  attrib_hash.each_pair { |table, attribs|
741
710
  if @implicit_attributes and @implicit_attributes[table] then
742
711
  @implicit_attributes[table] = attrib_hash[table] + @implicit_attributes[table]
@@ -745,11 +714,6 @@ private
745
714
  @implicit_attributes[table] = attrib_hash[table]
746
715
  end
747
716
  }
748
-
749
- # new_implicit_attributes = attrib_hash
750
- # new_implicit_attributes.update(existing_implicit_attributes)
751
-
752
- # set_implicit_attributes(new_implicit_attributes)
753
717
  end # }}}
754
718
 
755
719
  protected
@@ -757,12 +721,9 @@ protected
757
721
  # Demands a value to be set for create and update procedures.
758
722
  def self.expects(attrib_name, klass=nil)
759
723
  # {{{
760
-
761
724
  if klass.nil? then table = get_table_name
762
725
  else table = klass.table end
763
-
764
726
  add_explicit_attribute(table, attrib_name.to_s)
765
-
766
727
  end # }}}
767
728
 
768
729
  def self.hide_attribute(attrib_name)
@@ -783,7 +744,6 @@ private
783
744
  #
784
745
  def self.add_explicit_attribute(table_name, attrib_name)
785
746
  # {{{
786
-
787
747
  table_explicit_attributes = get_explicit_attributes[table_name]
788
748
  if(!table_explicit_attributes.nil?)
789
749
  then table_explicit_attributes.push(attrib_name)
@@ -794,7 +754,6 @@ private
794
754
  get_explicit_attributes.update({table_name => table_explicit_attributes})
795
755
 
796
756
  set_explicit_attributes(new_explicit_attributes)
797
-
798
757
  end # }}}
799
758
 
800
759
  private
@@ -811,7 +770,6 @@ private
811
770
  get_implicit_attributes.update({table_name => table_implicit_attributes})
812
771
 
813
772
  set_implicit_attributes(new_implicit_attributes)
814
-
815
773
  end # }}}
816
774
 
817
775
  ##########################################################################
@@ -836,13 +794,13 @@ public
836
794
  # instance holding it.
837
795
  # Note that this method is operating on a Table_Accessor instance, not
838
796
  # on class Table_Accessor itself.
839
- def initialize(instance_attrib_values, cache=nil)
797
+ def initialize(instance_attrib_values, field_names, joined_models=[], cache=nil)
840
798
  # {{{
841
-
842
799
  @loaded_from_cache = (cache == :cached)
843
800
  # set instance variables.
844
801
  # class instance variables are made accessible
845
802
  # in Table_Instance.setup_instance
803
+ @joined_models = joined_models
846
804
 
847
805
  if @loaded_from_cache then
848
806
  @attribute_values = instance_attrib_values
@@ -850,19 +808,20 @@ public
850
808
  @attribute_values = Hash.new
851
809
  values = instance_attrib_values
852
810
  field_index = 0
853
- self.class.get_all_table_names.each { |table|
854
- @attribute_values[table] = Hash.new
855
- field_names = self.class.get_attributes[table]
856
- for attr_index in 0...field_names.length do
857
- @attribute_values[table][field_names[attr_index]] = values[field_index]
858
- field_index += 1
859
- end
811
+ models = [ self.class ]
812
+ models += joined_models
813
+ models.each { |model|
814
+ model.get_all_table_names.each { |table|
815
+ @attribute_values[table] = Hash.new
816
+ field_names = model.get_attributes[table]
817
+ for attr_index in 0...field_names.length do
818
+ @attribute_values[table][field_names[attr_index]] = values[field_index]
819
+ field_index += 1
820
+ end
821
+ }
860
822
  }
861
823
  end
862
- # @attribute_values.update(instance_attrib_values)
863
-
864
824
  setup_instance()
865
-
866
825
  end # }}}
867
826
 
868
827
  public
@@ -888,12 +847,9 @@ public
888
847
  # use table :tablename if class != table only.
889
848
  def self.table(_table, _schema=nil)
890
849
  # {{{
891
-
892
850
  set_table_name(_table.to_s)
893
851
  schema(_schema.to_s) unless _schema.nil?
894
-
895
852
  load_attribute_fields();
896
-
897
853
  end # }}}
898
854
 
899
855
  private
@@ -919,7 +875,6 @@ protected
919
875
  # don't extend primary_keys of parent!
920
876
  def self.primary_key(*prim_key)
921
877
  # {{{
922
-
923
878
  keys = Array.new(1,get_table_name) + Array.new(1,prim_key.at(0))
924
879
  add_primary_keys(keys)
925
880
 
@@ -931,7 +886,6 @@ protected
931
886
  else
932
887
  add_implicit_attribute(get_table_name, prim_key.at(0).to_s)
933
888
  end
934
-
935
889
  end # }}}
936
890
 
937
891
  # Called in Cuba.import_imp_model. Defines prepared statements like
@@ -980,7 +934,6 @@ protected
980
934
  # thus allowing key tuples and free naming of table fields.
981
935
  def self.has_a(*args)
982
936
  # {{{
983
-
984
937
  if (args.length < 2) then
985
938
  raise Lore::Exception::Invalid_Usage.new('has_a expects at least Type, :foreign_key_name');
986
939
  end
@@ -1004,7 +957,6 @@ protected
1004
957
  add_has_a_klass(accessor, args[1..-1])
1005
958
 
1006
959
  define_entity_access_methods(accessor, args[1..-1])
1007
-
1008
960
  end # }}}
1009
961
 
1010
962
  def self.maps(*accessors)
@@ -1067,7 +1019,6 @@ private
1067
1019
  # Meta-programs class instance methods for accessing types
1068
1020
  # associated via has_a.
1069
1021
  def self.define_entity_access_methods(accessor, foreign_keys, type_name=nil)
1070
-
1071
1022
  type_name = accessor.to_s.split('::').at(-1).downcase unless type_name
1072
1023
 
1073
1024
  log('defining method '+type_name+'_entity')
@@ -1094,7 +1045,6 @@ private
1094
1045
  self.__send__ 'set_'+type_name+'_entity', other
1095
1046
  self.commit
1096
1047
  }
1097
-
1098
1048
  end
1099
1049
 
1100
1050
  private
@@ -1102,7 +1052,6 @@ private
1102
1052
  # Meta-programs class instance methods for accessing types
1103
1053
  # associated via has_n.
1104
1054
  def self.define_entities_access_methods(accessor, values)
1105
-
1106
1055
  type_name = accessor.to_s.split('::').at(-1).downcase unless type_name
1107
1056
 
1108
1057
  log('defining method add_'+type_name+'_entity')
@@ -1119,59 +1068,8 @@ private
1119
1068
  }
1120
1069
  accessor.all_with(foreign_key_values)
1121
1070
  }
1122
-
1123
1071
  end
1124
1072
 
1125
- protected
1126
-
1127
- ##########################################################################
1128
- # usage in derived classes:
1129
- # has_a_named :table_a, :a_foo, :foreign_key_1, :foreign_key_2, ...
1130
- # has_a_named :table_a, :a_bar, :foreign_key_1, :foreign_key_2, ...
1131
- #
1132
- # This method allows one type to aggregate the same type twice, referencing
1133
- # them under different names, such as (see example) get_a_foo(), get_a_bar().
1134
- def self.has_a_named(*args)
1135
- # {{{
1136
-
1137
- if (args.length < 3) then
1138
- raise Lore::Model_Exception.new(self, 'has_a_named expects at least :Type, :handle_name, :foreign_key_name');
1139
- end
1140
-
1141
- foreign_table_name = args.at(0).get_table_name
1142
- # convert schema.table => table:
1143
- foreign_table = args.at(0).get_table_name.split('.').at(1)
1144
- # convert Module::Other::Klass_Name => klass_name:
1145
- foreign_name = args.at(0).to_s.split('::')[-1].downcase
1146
-
1147
- foreign_keys = Array.new(1,foreign_table_name) + args[2...args.length]
1148
- add_foreign_keys(foreign_keys)
1149
-
1150
- new_has_a = Array.new(1,foreign_table_name)
1151
- new_has_a = get_has_a + new_has_a
1152
- set_has_a(new_has_a)
1153
-
1154
- # TODO: To be wrapped by Table_Instance, feeding this method with
1155
- # primary keys required to get referenced has_a tuple.
1156
- if args.at(2).nil? then type_name = foreign_name
1157
- else type_name = args.at(2).to_s
1158
- end
1159
-
1160
- log('defining method get_'+type_name)
1161
-
1162
- define_method('get_'+type_name) {
1163
- # see self.has_a
1164
- }
1165
-
1166
- log('defining method set_'+type_name)
1167
- # TODO: To be wrapped by Table_Instance, feeding this method with
1168
- # primary keys required to set referenced has_a tuple.
1169
- define_method('set_'+type_name) {|*keys|
1170
- # see self.has_a
1171
- }
1172
-
1173
- end # }}}
1174
-
1175
1073
  protected
1176
1074
 
1177
1075
  def self.belongs_to(*args)
@@ -1337,7 +1235,6 @@ protected
1337
1235
  # is_a.
1338
1236
  def self.aggregates(*args)
1339
1237
  # {{{
1340
-
1341
1238
  parent = args.at(0)
1342
1239
 
1343
1240
  # before: parent_pkeys = Hash { Parent => [id, name] }
@@ -1372,71 +1269,58 @@ protected
1372
1269
  use_label(parent.get_labels)
1373
1270
 
1374
1271
  define_entity_access_methods(parent, args[1..-1])
1375
-
1376
1272
  end # }}}
1377
1273
 
1378
1274
  private
1379
1275
 
1380
1276
  def self.add_is_a(table_name, derive_tree)
1381
1277
  # {{{
1382
-
1383
1278
  new_is_a = Hash.new
1384
1279
  new_is_a[table_name] = derive_tree
1385
1280
  new_is_a.update(get_is_a)
1386
1281
 
1387
1282
  set_is_a(new_is_a)
1388
-
1389
1283
  end # }}}
1390
1284
 
1391
1285
  private
1392
1286
 
1393
1287
  def self.add_is_a_klass(klass, foreign_key)
1394
1288
  # {{{
1395
-
1396
1289
  @is_a_klasses = Hash.new if @is_a_klasses.nil?
1397
1290
  @is_a_klasses[foreign_key] = klass
1398
-
1399
1291
  end # }}}
1400
1292
 
1401
1293
  private
1402
1294
 
1403
1295
  def self.add_aggregate(table_name, derive_tree)
1404
1296
  # {{{
1405
-
1406
1297
  new_aggregates = Hash.new
1407
1298
  new_aggregates[table_name] = derive_tree
1408
1299
  new_aggregates.update(get_aggregates)
1409
1300
 
1410
1301
  set_aggregates(new_aggregates)
1411
-
1412
1302
  end # }}}
1413
1303
 
1414
1304
  private
1415
1305
 
1416
1306
  def self.add_aggregate_klass(klass, attributes)
1417
1307
  # {{{
1418
-
1419
1308
  new_aggregate_klasses = get_aggregate_klasses
1420
1309
  attributes.each { |attribute|
1421
1310
  new_aggregate_klasses[get_table_name+'.' << attribute.to_s] = klass
1422
1311
  }
1423
-
1424
1312
  set_aggregate_klasses(new_aggregate_klasses)
1425
-
1426
1313
  end # }}}
1427
1314
 
1428
1315
  private
1429
1316
 
1430
1317
  def self.add_has_a_klass(klass, attributes)
1431
1318
  # {{{
1432
-
1433
1319
  new_has_a_klasses = get_has_a_klasses
1434
1320
  attributes.each { |attribute|
1435
1321
  new_has_a_klasses[get_table_name+'.' << attribute.to_s] = klass
1436
1322
  }
1437
-
1438
1323
  set_has_a_klasses(new_has_a_klasses)
1439
-
1440
1324
  end # }}}
1441
1325
 
1442
1326
  protected
@@ -1444,17 +1328,16 @@ protected
1444
1328
  # unless it has a non-empty value.
1445
1329
  def self.explicit(*args)
1446
1330
  # {{{
1447
-
1448
1331
  new_explicit = get_explicit
1449
1332
  new_explicit[get_table_name] = args.map!{|elem| elem.to_s}
1450
-
1451
1333
  add_explicit(new_explicit)
1452
-
1453
1334
  end # }}}
1454
1335
 
1455
1336
  private
1456
1337
 
1457
1338
  ##########################################################################
1339
+ # DELETE ME
1340
+ #
1458
1341
  # Usage:
1459
1342
  # Cuba::Admin::Container_Local.select({'primary_key_1'=>'2',
1460
1343
  # 'primary_key_2'=>'38',
@@ -1462,7 +1345,6 @@ private
1462
1345
  # })
1463
1346
  def self.select_row_by_key(*keys)
1464
1347
  # {{{
1465
-
1466
1348
  # Aggregate klasses this klass is derived from and
1467
1349
  # those it aggregates explicitly:
1468
1350
  begin
@@ -1471,30 +1353,23 @@ private
1471
1353
  raise excep
1472
1354
  raise ::Exception.new('Unable to load instance from DB (Reason: ' << excep.message << ')')
1473
1355
  end
1474
- field_hash = result.get_row()
1475
-
1356
+ return result.get_row_with_field_names()
1476
1357
  end # }}}
1477
1358
 
1478
1359
  public
1479
1360
 
1480
- # Same as select, but with predefined primary key values as
1481
- # hash.
1361
+ # DELETE ME
1482
1362
  def self.select_by_key(*keys)
1483
1363
  # {{{
1484
-
1485
1364
  result = Lore::Table_Selector.select_on_keys(self, keys)
1486
-
1487
1365
  field_array = result.get_rows()
1488
-
1489
1366
  end # }}}
1490
1367
 
1491
1368
  public
1492
1369
 
1493
1370
  def self.update(&block)
1494
1371
  # {{{
1495
-
1496
1372
  query_string = Lore::Table_Updater.block_update(self, &block)
1497
-
1498
1373
  end # def }}}
1499
1374
 
1500
1375
  public
@@ -1503,22 +1378,17 @@ public
1503
1378
  # {{{
1504
1379
  GC.disable
1505
1380
  if(!clause.nil? && !clause.to_s.include?('*,')) then
1506
-
1507
1381
  query_string = Lore::Table_Selector.select_query(clause.to_s, self, &block)
1508
- return Clause.new(query_string)
1509
-
1382
+ return Clause.new(query_string[:query])
1510
1383
  else
1511
-
1512
1384
  what = clause.to_s
1513
1385
  result = Lore::Table_Selector.select_cached(what, self, &block)
1514
-
1515
1386
  # result = Array.new
1516
1387
  # db_result.get_rows.each { |row|
1517
1388
  # result.push(self.new(self, row))
1518
1389
  # }
1519
1390
  GC.enable
1520
1391
  return result
1521
-
1522
1392
  end
1523
1393
 
1524
1394
  end # }}}
@@ -1558,6 +1428,7 @@ public
1558
1428
  # EXPLAIN EXECUTE. "
1559
1429
  #
1560
1430
  def self.prepare(plan_name, *args, &block)
1431
+ # {{{
1561
1432
  log('PREPARE: TRYING CLASS METHOD ' << plan_name.to_s)
1562
1433
  if !@@prepared_statements[plan_name] then
1563
1434
  Table_Selector.prepare(plan_name, self, args, &block)
@@ -1570,7 +1441,7 @@ public
1570
1441
  @@prepared_statements[plan_name] = true
1571
1442
  log('PREPARE: CREATED CLASS METHOD ' << plan_name.to_s)
1572
1443
  end
1573
- end
1444
+ end # }}}
1574
1445
 
1575
1446
  def self.execute_prepared(plan_name, *args)
1576
1447
  plan_name = "#{table_name.gsub('.','_')}__#{plan_name.to_s}"
@@ -1635,9 +1506,6 @@ private
1635
1506
 
1636
1507
  key_hash = Hash.new
1637
1508
 
1638
- log('ATTRIBUTES TOTAL: ')
1639
- log(get_attributes.inspect)
1640
-
1641
1509
  attribute_name_array = Array.new
1642
1510
  attribute_table = ''
1643
1511
  get_attributes.each_pair { |table, fields|
@@ -1665,8 +1533,6 @@ private
1665
1533
  }
1666
1534
 
1667
1535
  }
1668
-
1669
- log(key_hash.inspect)
1670
1536
 
1671
1537
  # sequence values only are known after insert operation,
1672
1538
  # so we have to retreive the complete key_hash back from
@@ -1674,7 +1540,6 @@ private
1674
1540
  key_hash = Lore::Table_Inserter.perform_insert(self, key_hash)
1675
1541
  # key_hash has been extended by sequence_values now, so we
1676
1542
  # return it:
1677
-
1678
1543
  key_hash
1679
1544
 
1680
1545
  end # }}}
@@ -1707,7 +1572,6 @@ public
1707
1572
  attrib_values[attrib_name] = input_filters[attrib_key].call(attrib_value)
1708
1573
  end
1709
1574
  }
1710
- log('FILTERED VALUES: ' << attrib_values.inspect)
1711
1575
  after_filters(attrib_values)
1712
1576
 
1713
1577
  # Predefine
@@ -1717,15 +1581,12 @@ public
1717
1581
  table_values = Hash.new
1718
1582
 
1719
1583
  attrib_name_array = Array.new
1720
- log('ALL ATTRIBS: ' << table.to_s + ' <-- ' << attribs.inspect)
1721
1584
  attrib_values.each_pair { |attrib_name, attrib_value|
1722
1585
  attrib_name = attrib_name.to_s
1723
1586
  attrib_name_array = attrib_name.split('.')
1724
1587
 
1725
1588
  if attribs.include? attrib_name then
1726
-
1727
1589
  table_values[attrib_name] = attrib_value
1728
-
1729
1590
  elsif attribs.include? attrib_name_array.at(2) and
1730
1591
  attrib_name_array.at(0)+'.'+attrib_name_array.at(1) == table then
1731
1592
  table_values[attrib_name_array.at(2)] = attrib_value
@@ -1735,17 +1596,13 @@ public
1735
1596
  }
1736
1597
 
1737
1598
  begin
1738
-
1739
1599
  before_validation(values)
1740
1600
  Lore::Validation::Parameter_Validator.invalid_params(self,
1741
1601
  values)
1742
-
1743
1602
  rescue Lore::Exception::Invalid_Klass_Parameters => ikp
1744
-
1745
1603
  # log'n'throw
1746
1604
  ikp.log
1747
1605
  raise ikp
1748
-
1749
1606
  end
1750
1607
 
1751
1608
  before_insert(attrib_values)
@@ -1755,6 +1612,8 @@ public
1755
1612
  #
1756
1613
  attrib_values = insert(attrib_values)
1757
1614
 
1615
+ # This would be a double check, as self.load already filters
1616
+ # non-primary key attributes
1758
1617
  select_keys = Hash.new
1759
1618
  if @primary_keys then
1760
1619
  @primary_keys[table_name].each { |key|
@@ -1777,34 +1636,41 @@ public
1777
1636
 
1778
1637
  ##########################################################################
1779
1638
  # Return new Table_Accessor instance by loading an existing entry from
1780
- # table.
1639
+ # table if present, or false if no entry has been found.
1640
+ # Accepts any combination of :primary_key => 'value'
1641
+ # Also allows inherited primary keys.
1781
1642
  def self.load(keys)
1782
1643
  # {{{
1783
1644
 
1784
1645
  before_load(keys)
1785
1646
 
1786
- select_keys = Hash.new
1787
-
1788
- get_primary_keys[table_name].each { |attrib_name|
1789
- value = keys[attrib_name.intern]
1790
- value = keys[attrib_name.to_s] if value.nil?
1791
- value = keys[table_name+'.'+attrib_name.to_s] if value.nil?
1792
- select_keys[table_name+'.'+attrib_name.to_s] = value unless value.nil?
1647
+ select_keys = {}
1648
+ value = false
1649
+ get_primary_keys.each_pair { |table, pkeys|
1650
+ pkeys.each { |attrib_name|
1651
+ value = keys[table+'.'+attrib_name.to_s] # The more explicit, the better.
1652
+ value = keys[attrib_name.intern] if value.nil? # Symbols are supposed to be more frequent than strings
1653
+ value = keys[attrib_name.to_s] if value.nil?
1654
+ select_keys[table+'.'+attrib_name.to_s] = value unless value.nil?
1655
+ }
1793
1656
  }
1794
1657
 
1795
- return false if select_keys.empty?
1796
-
1797
- log('Loading with prim keys: ' << select_keys.inspect)
1798
- instance_attribs_hash = select_row_by_key(select_keys)
1799
-
1800
- return false if instance_attribs_hash.nil?
1801
-
1802
- instance = new(instance_attribs_hash)
1658
+ return false if select_keys.empty?
1803
1659
 
1804
- #TODO:
1805
- # after_load(instance)
1660
+ cp = Clause.for(self)
1661
+ c = Clause.new
1662
+ select_keys.each_pair { |k,v|
1663
+ c & (Clause.new(k.to_s.dup) == v.to_s)
1664
+ }
1665
+
1666
+ instance = self.select { |inst|
1667
+ inst.where(c)
1668
+ inst.limit(1)
1669
+ }.first
1806
1670
 
1671
+ return false unless instance
1807
1672
  return instance
1673
+
1808
1674
  end # }}}
1809
1675
 
1810
1676
  public
@@ -1886,41 +1752,10 @@ public
1886
1752
  # Inspect method
1887
1753
  def self.inspect
1888
1754
  # {{{
1889
-
1890
- # return self.to_s + ' : Table_Accessor'
1891
-
1892
- dump = 'Accessor: ' << self.to_s
1893
- # dump << "\n\t" << 'own table name: '+get_table_name
1894
- # dump << "\n\t" << 'attributes: '
1895
- # dump << "\n\t" << @attributes.inspect
1896
- # dump << "\n\t" << 'explicit attributes: '
1897
- # dump << "\n\t" << @explicit_attributes.inspect
1898
- # dump << "\n\t" << 'implicit attributes: '
1899
- # dump << "\n\t" << @implicit_attributes.inspect
1900
- # dump << "\n\t" << 'constraints: '
1901
- # dump << "\n\t" << @constraints.inspect
1902
- # dump << "\n\t" << 'attribute types: '
1903
- # dump << "\n\t" << @attribute_types.inspect
1904
- # dump << "\n\t" << 'primary keys: '
1905
- # dump << "\n\t" << @primary_keys.inspect
1906
- # dump << "\n\t" << 'sequences: '
1907
- # dump << "\n\t" << @sequences.inspect
1908
- # dump << "\n\t" << 'foreign keys: '
1909
- # dump << "\n\t" << @foreign_keys.inspect
1910
- # dump << "\n\t" << 'has_a: '
1911
- # dump << "\n\t" << @has_a.inspect
1912
- # dump << "\n\t" << 'has_n: '
1913
- # dump << "\n\t" << @has_n.inspect
1914
- # dump << "\n\t" << 'is_a: '
1915
- # dump << "\n\t" << @is_a.inspect
1916
- # dump << "\n\t" << 'explicit: '
1917
- # dump << "\n\t" << @explicit.inspect
1918
- # dump << "\n"
1919
-
1755
+ 'Lore::Table_Accessor: ' << self.to_s
1920
1756
  end # }}}
1921
1757
 
1922
1758
  end # class
1923
1759
 
1924
-
1925
1760
  end # module
1926
1761