lore 0.4.5 → 0.4.6

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.
@@ -794,7 +794,7 @@ public
794
794
  # instance holding it.
795
795
  # Note that this method is operating on a Table_Accessor instance, not
796
796
  # on class Table_Accessor itself.
797
- def initialize(instance_attrib_values, field_names, joined_models=[], cache=nil)
797
+ def initialize(instance_attrib_values, joined_models=[], cache=nil)
798
798
  # {{{
799
799
  @loaded_from_cache = (cache == :cached)
800
800
  # set instance variables.
@@ -821,6 +821,7 @@ public
821
821
  }
822
822
  }
823
823
  end
824
+
824
825
  setup_instance()
825
826
  end # }}}
826
827
 
@@ -1544,19 +1545,66 @@ private
1544
1545
 
1545
1546
  end # }}}
1546
1547
 
1548
+ private
1549
+
1550
+ def self.distribute_attrib_values(attrib_values)
1551
+ # {{{
1552
+ values = Hash.new
1553
+ # Predefine
1554
+ attrib_name_array = Array.new
1555
+ # distribute attrib names to tables:
1556
+ get_attributes().each_pair { |table, attribs|
1557
+ table_values = Hash.new
1558
+
1559
+ attrib_name_array = Array.new
1560
+ attrib_values.each_pair { |attrib_name, attrib_value|
1561
+ attrib_name = attrib_name.to_s
1562
+ attrib_name_array = attrib_name.split('.')
1563
+
1564
+ if attribs.include? attrib_name then
1565
+ table_values[attrib_name] = attrib_value
1566
+ elsif attribs.include? attrib_name_array.at(2) and
1567
+ attrib_name_array.at(0)+'.'+attrib_name_array.at(1) == table then
1568
+ table_values[attrib_name_array.at(2)] = attrib_value
1569
+ end
1570
+ }
1571
+ values[table] = table_values
1572
+ }
1573
+ values
1574
+ end # }}}
1575
+
1576
+ public
1577
+
1578
+ # Create a shallow instance, that is: An instance with no reference to
1579
+ # DB model. Attribute values passed to Table_Accessor.create_shallow are
1580
+ # not processed through hooks, filters, and validation.
1581
+ # Values are, however, processed through output filters.
1582
+ # Usage and result is
1583
+ # the same as for Table_Accessor.create, but it only returns
1584
+ # an accessor instance, without storing it in the database.
1585
+ # To commit a shallow copy to database (and thus process given attribute
1586
+ # values through stages mentioned before), call #commit.
1587
+ def self.create_shallow(attrib_values)
1588
+ before_create(attrib_values)
1589
+ values = distribute_attrib_values(attrib_values)
1590
+ flat_attribs = []
1591
+ get_all_table_names.each { |table|
1592
+ get_attributes[table].each { |attrib|
1593
+ flat_attribs << (values[table][attrib])
1594
+ }
1595
+ }
1596
+ instance = self.new(flat_attribs)
1597
+ end
1598
+
1547
1599
  public
1548
1600
 
1549
1601
  ##########################################################################
1550
1602
  # Returns a new Table_Accessor instance by inserting given attribute
1551
1603
  # values into db and returning an instance for further operations.
1552
- def self.create(attrib_values)
1604
+ def self.create(attrib_values={})
1553
1605
  # {{{
1554
1606
  before_create(attrib_values)
1555
1607
 
1556
- values = Hash.new
1557
-
1558
- Lore.log { 'KLASS: ' << attrib_values.class.to_s }
1559
- Lore.log { 'INSERTING VALUES: ' << attrib_values.inspect }
1560
1608
  input_filters = get_input_filters
1561
1609
  attrib_key = ''
1562
1610
  attrib_name = ''
@@ -1574,26 +1622,7 @@ public
1574
1622
  }
1575
1623
  after_filters(attrib_values)
1576
1624
 
1577
- # Predefine
1578
- attrib_name_array = Array.new
1579
- # distribute attrib names to tables:
1580
- get_attributes().each_pair { |table, attribs|
1581
- table_values = Hash.new
1582
-
1583
- attrib_name_array = Array.new
1584
- attrib_values.each_pair { |attrib_name, attrib_value|
1585
- attrib_name = attrib_name.to_s
1586
- attrib_name_array = attrib_name.split('.')
1587
-
1588
- if attribs.include? attrib_name then
1589
- table_values[attrib_name] = attrib_value
1590
- elsif attribs.include? attrib_name_array.at(2) and
1591
- attrib_name_array.at(0)+'.'+attrib_name_array.at(1) == table then
1592
- table_values[attrib_name_array.at(2)] = attrib_value
1593
- end
1594
- }
1595
- values[table] = table_values
1596
- }
1625
+ values = distribute_attrib_values(attrib_values)
1597
1626
 
1598
1627
  begin
1599
1628
  before_validation(values)
@@ -1621,10 +1650,6 @@ public
1621
1650
  }
1622
1651
  end
1623
1652
 
1624
- # Create klass instance via new:
1625
- # Cuba::Module.new(self, attrib_values)
1626
-
1627
- # Create klass instance via load (wrapper of new):
1628
1653
  obj = self.load(select_keys)
1629
1654
  after_create(obj)
1630
1655
 
@@ -1657,6 +1682,10 @@ public
1657
1682
 
1658
1683
  return false if select_keys.empty?
1659
1684
 
1685
+ # We have to perform a select here instead of returning
1686
+ # the instance with given attribute values, as this is the
1687
+ # only way to retreive attribute values set in the DB via
1688
+ # default values, triggers, etc.
1660
1689
  cp = Clause.for(self)
1661
1690
  c = Clause.new
1662
1691
  select_keys.each_pair { |k,v|
@@ -102,6 +102,8 @@ module Table_Deleter # :nodoc:
102
102
  Context.enter(accessor.get_context) unless accessor.get_context.nil?
103
103
  begin
104
104
  Lore::Connection.perform("BEGIN;\n#{query_string}\nCOMMIT;")
105
+ rescue ::Exception => excep
106
+ Lore::Connection.perform("ROLLBACK;")
105
107
  ensure
106
108
  Context.leave unless accessor.get_context.nil?
107
109
  end
@@ -154,6 +154,8 @@ module Table_Inserter # :nodoc:
154
154
 
155
155
  begin
156
156
  Lore::Connection.perform("BEGIN;\n#{query_string}\nCOMMIT;")
157
+ rescue ::Exception => excep
158
+ Lore::Connection.perform("ROLLBACK;")
157
159
  ensure
158
160
  Lore::Context.leave unless accessor.get_context.nil?
159
161
  end
@@ -41,7 +41,6 @@ end # class
41
41
  # This module holds methods provided by Table_Accessor instances.
42
42
  module Table_Instance
43
43
 
44
-
45
44
  # retreive class instance variables from concrete
46
45
  # Table_Accessor this object is an instance of:
47
46
  def setup_instance() # :nodoc:
@@ -70,35 +70,6 @@ public
70
70
  return query_string
71
71
  end # def
72
72
 
73
- public
74
-
75
- # Extracted, recursive method for building the AS-part of
76
- # a SELECT query.
77
- def self.build_as_query(table_fields,
78
- query_string='')
79
-
80
- return '*'
81
-
82
- first = query_string == ''
83
-
84
- table_fields.each_pair { |table_name, fields|
85
-
86
- fields.each { |foreign_field|
87
-
88
- # base.table.foreign_field =
89
- query_string << ', ' unless first
90
- query_string << "\n #{table_name}.#{foreign_field} AS "
91
- query_string << "\"#{table_name}.#{foreign_field}\""
92
-
93
- first = false
94
- }
95
-
96
- }
97
-
98
- query_string
99
-
100
- end #def
101
-
102
73
  protected
103
74
 
104
75
  def self.build_select_query(accessor,
@@ -106,8 +77,7 @@ protected
106
77
  # this is to be wrapped in an adapter to provide DB abstraction:
107
78
 
108
79
  table_name = accessor.get_table_name
109
- query_string = 'SELECT '
110
- query_string << build_as_query(accessor.get_attributes)
80
+ query_string = 'SELECT * '
111
81
  query_string << ' FROM ' << table_name + ' '
112
82
  query_string << build_joined_query(accessor)
113
83
  query_string << "\n WHERE "
@@ -151,7 +121,7 @@ protected
151
121
  end
152
122
 
153
123
  if(what.nil? || what == '*' || what == '') then
154
- query_as_part = build_as_query(accessor.get_attributes)
124
+ query_as_part = '*'
155
125
  else
156
126
  query_as_part = what.to_s
157
127
  end
@@ -232,7 +202,7 @@ protected
232
202
  else
233
203
  db_result = perform_select(accessor, query_string[:query]).get_rows()
234
204
  db_result[:values].each { |row|
235
- result.push(accessor.new(row, db_result[:fields], query_string[:joined_models]))
205
+ result.push(accessor.new(row, query_string[:joined_models]))
236
206
  }
237
207
  if Lore.cache_enabled? && accessor.entity_cache then
238
208
  accessor.entity_cache.create(accessor, query_string, result)
@@ -276,7 +246,7 @@ protected
276
246
  else
277
247
  db_result = perform_select(accessor, query_string).get_rows()
278
248
  db_result[:values].each { |row|
279
- result.push(accessor.new(row, db_result[:fields]))
249
+ result.push(accessor.new(row))
280
250
  }
281
251
  if Lore.cache_enabled? && accessor.entity_cache then
282
252
  accessor.create_entity_cache(query_string, result)
@@ -142,6 +142,8 @@ module Table_Updater # :nodoc:
142
142
  Context.enter(accessor.get_context) unless accessor.get_context.nil?
143
143
  begin
144
144
  Lore::Connection.perform("BEGIN;\n#{query_string}\nCOMMIT;")
145
+ rescue ::Exception => excep
146
+ Lore::Connection.perform("ROLLBACK;")
145
147
  ensure
146
148
  Context.leave unless accessor.get_context.nil?
147
149
  end
data/lib/lore.rb CHANGED
@@ -3,7 +3,7 @@ require('logger')
3
3
 
4
4
  module Lore
5
5
 
6
- VERSION='0.4.5'
6
+ VERSION='0.4.6'
7
7
 
8
8
  @logfile = '/var/log/lore/query.log'
9
9
  def self.logfile
data/lore.gemspec CHANGED
@@ -16,7 +16,7 @@ spec = Gem::Specification.new { |s|
16
16
  high coverage of native SQL functions and features.
17
17
  Lore is currently using PostgreSQL as database backend.
18
18
  EOF
19
- s.version = '0.4.5'
19
+ s.version = '0.4.6'
20
20
  s.author = 'Tobias Fuchs'
21
21
  s.email = 'fuchs@atomnode.net'
22
22
  s.date = Time.now
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.5
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Fuchs
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-12 00:00:00 +01:00
12
+ date: 2008-11-24 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency