Class: HODB

Inherits:
Object
  • Object
show all
Defined in:
lib/hdb/hodb.rb,
lib/hdb/hdb_test.rb

Overview

Object DB

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modelName = nil, parents = [], connectionName: "default", domain: {}, generate: true) ⇒ HODB

Returns a new instance of HODB.



85
86
87
88
89
90
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
# File 'lib/hdb/hodb.rb', line 85

def initialize(modelName = nil, parents = [], connectionName: "default", domain: {}, generate: true)
  p "modelName: #{modelName}, parents: #{parents}, connectionName = #{connectionName}"
  @generate = generate # if true build the table into db

  @visited = []

  @toWrite = []
  @toChange = []
  @toDelete = []

  @vid = 0 # virtual id

  @tmp = {}
  @type = nil 
  @source = nil
  @domain = domain # it's required for example to group the result of oneToMany relation or manyToMany
  @modelName = modelName
  @connectionName = connectionName
  @parents = [] # pairs of type {ptr: 0x..., referenceFieldName: 'modelname_id'}
  @hfields = {} # hypersonic fields
  @tableFields = nil # fields of table
  @modelFields = nil # fields of table + fields of all parents
  @table = []
  @tableCursor = nil 
  @allParents = nil

  self.setParents(parents)
  self.setSystemFields()

  self.init()

end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

[[:a, :b, :c], [1, 2, 3]].transpose

=> [[:a, 1], [:b, 2], [:c, 3]]


334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/hdb/hodb.rb', line 334

def method_missing(sym, *args, &block)
  if sym =~ /^(\w+)=$/
    if args[0].class == HField
      self.addField($1.to_s, args[0])
    elsif self.nodeOfField($1.to_s).any?
      @nodes.each { |node| node.tableCursor[$1.to_s] = args[0] if node.tableCursor }
      @toChange << @tableCursor if (!@toChange.include?(@tableCursor) and @tableCursor) 
    elsif instance_variable_defined?("@#{$1.to_s}") # is required to use attr_reader to setting
      instance_variable_set("@#{$1}", args[0])
    else
      hl.<<("hodb::method_missing: #{$1.to_s} not found", "ERROR")
    end
    return
  elsif sym.to_s =~ /^findBy_(.+)$/
    attrs = $1.split('_and_')
    attrs_with_args = [attrs, args].transpose
    where = Hash[attrs_with_args]
    return self.findBy(where) 
  elsif @hfields[sym.to_s] and @hfields[sym.to_s][:modelNameReference]
    modelName = @hfields[sym.to_s][:modelNameReference]
    type = self.fieldProperty(sym, :type)
    return self.oneToOne(modelName, sym.to_s) if (type == 'oneToOne')
    return self.manyToOne(modelName, sym.to_s) if (type == 'manyToOne')
    referenceFieldName = @hfields[sym.to_s][:referenceFieldName]
    return self.oneToMany(sym.to_s, modelName, referenceFieldName) if (type == 'oneToMany')
    joinTable = @hfields[sym.to_s][:joinTable]
    referenceFieldName2 = @hfields[sym.to_s][:referenceFieldName2]
    return self.manyToMany(modelName, joinTable, referenceFieldName, referenceFieldName2) if (type == 'manyToMany')
  elsif @hfields[sym.to_s] and @hfields[sym.to_s][:type] == 'virtual' 
    return self.virtualField(@hfields[sym.to_s][:object] || self, @hfields[sym.to_s][:functionName], &@hfields[sym.to_s][:block])
  elsif sym =~ /^(\w+)_id$/
    return self.manyToOne($1.to_s, sym.to_s)
  elsif sym =~ /^(\w+)_table$/
    return self.oneToMany(sym.to_s, $1.to_s, "#{@modelName}_id")
  elsif sym =~ /^(\w+)_join$/
    return self.manyToMany($1.to_s, "#{@modelName}_#{$1.to_s}_join", "#{@modelName}_id", "#{$1.to_s}_id")
  elsif @tableCursor and @tableCursor.include?(sym.to_s)
    return @tableCursor[sym.to_s]
  #elsif sym =~ /^(\w+)_virtual$/
  #  return self.virtualField($1.to_s, self, &block)
  else 
    @parents.each do |parent| 
      result = parent[:ptr].method_missing(sym, *args, &block)
      return result if result
    end
  end
  hl.<<("hodb::method_missing: #{sym.to_s} not found", "ERROR")
  return nil
end

Instance Attribute Details

#connectionNameObject (readonly)

Returns the value of attribute connectionName.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def connectionName
  @connectionName
end

#domainObject (readonly)

riga da cancellare



83
84
85
# File 'lib/hdb/hodb.rb', line 83

def domain
  @domain
end

#generateObject (readonly)

Returns the value of attribute generate.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def generate
  @generate
end

#hfields(inheritance = true) ⇒ Object (readonly)

Returns the value of attribute hfields.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def hfields
  @hfields
end

#modelNameObject (readonly)

Returns the value of attribute modelName.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def modelName
  @modelName
end

#parentObject (readonly)

Returns the value of attribute parent.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def parent
  @parent
end

#parentModelObject (readonly)

Returns the value of attribute parentModel.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def parentModel
  @parentModel
end

#sortFieldObject (readonly)

Returns the value of attribute sortField.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def sortField
  @sortField
end

#sourceObject (readonly)

Returns the value of attribute source.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def source
  @source
end

#tableObject (readonly)

Returns the value of attribute table.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def table
  @table
end

#tableCursorObject (readonly)

Returns the value of attribute tableCursor.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def tableCursor
  @tableCursor
end

#tmpObject (readonly)

Returns the value of attribute tmp.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def tmp
  @tmp
end

#toChangeObject (readonly)

Returns the value of attribute toChange.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def toChange
  @toChange
end

#toWriteObject (readonly)

Returns the value of attribute toWrite.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def toWrite
  @toWrite
end

#typeObject (readonly)

Returns the value of attribute type.



82
83
84
# File 'lib/hdb/hodb.rb', line 82

def type
  @type
end

Class Method Details

.amount(this) ⇒ Object

Ci sono diversi modi per creare dei campi virtuali Il primo e’ quello di creare una funzione all’interno nel modello es. per creare il campo amount:

self.amount_virtual = HField.magic()

def amount

return price.to_i * 2

end

In realta’ se togliamo l’istruzione magic funziona lo stesso ma non si capisce che amount e’ un campo del modello

Il secondo e’ quello di creare il campo fuori dal modello in modo da avere piu’ versatilita’ In tal modo posso aggiungere i campi runtime

  • products = Products.new

  • products.amount = HField.virtual(:amount, self) # dove amount e’ la funzione e self e’ l’oggetto contenente la funzione

Il terzo e’ quello di passare direttamente un blocco es.

  • products.amount = HField.virtual { |this| this.price.to_i }



208
209
210
# File 'lib/hdb/hdb_test.rb', line 208

def self.amount(this)
  return this.quantity * this.price.to_i 
end

.manyToManyObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hdb/hdb_test.rb', line 100

def self.manyToMany
  
  models = Models.new
  baby = models.create({name: 'Baby Format', description: 'baby format'})
  family = models.create({name: 'Family Format', description: 'family format'})
  
  categories = Categories.new
  konditorei = categories.create({name: 'KONDITOREI'}).foods_table
  sacher = konditorei.create({name: 'Sacher Torte', description: 'schokolade und marmelade', price: 10})

  sacher_modelds_join = sacher.models_join

  sacher_modelds_join.create({models_id: baby})
  sacher_modelds_join.create({models_id: family})

  categories.writeAll

end

.manyToMany_2Object



119
120
121
122
123
124
# File 'lib/hdb/hdb_test.rb', line 119

def self.manyToMany_2

  sacher = Foods.new.findBy_name("Sacher Torte")
  sacher.models_join.rightModel.show

end

.manyToMany_3Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/hdb/hdb_test.rb', line 126

def self.manyToMany_3

  family = Models.new.findBy_name("Family Format")

  margherita = Foods.new.findBy_name("Margherita Baby")
  margherita_models_join = margherita.models_join
  margherita_models_join.create({models_id: family})

  # margherita.writeAll non funzionerebbe perche' non proviene da una create
  # la writeAll deve quindi essere eseguita dalla create radice in questo caso margherita_models_join
  margherita_models_join.writeAll 

end

.newOdb(modelName, connectionName = "default", domain: {}) ⇒ Object



133
134
135
136
# File 'lib/hdb/hodb.rb', line 133

def self.newOdb(modelName, connectionName = "default", domain: {})
  className = modelName.hcapitalize
  return eval("#{className}.new(connectionName: connectionName, domain: domain)")
end

.oneToManyObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/hdb/hdb_test.rb', line 63

def self.oneToMany
  
  printers = Printers.new
  kitchen = printers.create({name: 'KITCHEN'})
  pizzeria = printers.create({name: 'PIZZERIA'})
  bar = printers.create({name: 'BAR'})
  
  categories = Categories.new
  pizze = categories.create({name: 'PIZZE BABY'}).foods_table
  primi = categories.create({name: 'PRIMI BABY'})
  beverage = categories.create({name: 'BEVERAGE'}).foods_table
  
  pizze.create({name: 'Margherita Baby', description: 'Pomodoro, Mozzarella, Basilico', price: 10, printers_id: pizzeria})
  pizze.create({name: 'Marinara Baby', description: 'Pomodoro, Aglio, Olio', price: 10, printers_id: pizzeria})
  pizze.create({name: 'Capricciosa Baby', description: 'Pomodoro, Mozzarella, Funghi, Olive, Uovo, Piselli, Wurstell', price: 10, printers_id: pizzeria})
  pizze.create({name: 'Capricciosa Baby', description: 'Pomodoro, Mozzarella, Funghi, Olive, Uovo, Piselli, Wurstell', price: 10, printers_id: pizzeria})
  pizze.create({name: 'Napoli Baby', description: 'Pomodoro, Mozzarella, Acciughe', price: 10, printers_id: pizzeria})
  pizze.create({name: 'Taggiasca Baby', description: 'Pomodoro, Mozzarella, Olive', price: 10, printers_id: pizzeria})

  primi.foods_table.create({name: 'Tagliatelle ai Porcini', description: 'Fresh Mushrooms', price: 10, printers_id: kitchen})
  
  primi.name = "PRIMI ITALIANI"
 
  beverage.create({name: 'Coke', description: 'Water and Sugar', price: 2, printers_id: bar})
  
  categories.writeAll

end

.oneToOneObject



92
93
94
95
96
97
98
# File 'lib/hdb/hdb_test.rb', line 92

def self.oneToOne

  margherita = Foods.new.findBy_name("Margherita Baby")
  margherita.printers_id.show   # oneToOne
  margherita.categories_id.show # manyToOne

end

.testObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/hdb/hdb_test.rb', line 223

def self.test
  
  #HDBGenerator.new(true)
  #self.test_save2
  #self.virtualFields 
  #return
  HDBGenerator.new(true)
  self.test_1
  self.test_2
  self.test_3
  self.test_4
  self.oneToMany
  self.oneToOne
  self.manyToMany
  self.manyToMany_2
  self.manyToMany_3
  self.test_showCategories
  self.test_save
  self.test_save2
  self.virtualFields 
  #self.test_delete

end

.test1Object



930
931
932
933
934
935
936
937
# File 'lib/hdb/hodb.rb', line 930

def self.test1
  a = HODB.new({a: 1, b: 2, c: 3})
  a.a = 0
  p a.a
  p a.b
  a.d = 40 # make a variable automatically
  p a.d
end

.test2Object



939
940
941
942
943
944
# File 'lib/hdb/hodb.rb', line 939

def self.test2
  odb = HODB.new
  odb.name = HField.text(caption: 'Name', mandatory: true, readonly: false)
  odb.name = "hypersonic"
  p odb.hfields(:name), odb.name
end

.test_1Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hdb/hdb_test.rb', line 6

def self.test_1
  contacts = Contacts.new
  contacts.create({surname: 'Bonaffini',
                   name: 'Herbert', 
                   address: 'Herbert Strasse', 
                   delivery_address: 'Herbert - Delivery', 
                   invoice_address: 'Herbert - Invoice'})
  contacts.show('hight_red', false)
  contacts.writeAll
  contacts.all
  contacts.show('hight_yellow', false)
end

.test_2Object



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

def self.test_2
  suppliers = Suppliers.new
  suppliers.create({surname: 'Viola', 
                    name: 'Ugo', 
                    address: 'Viola Strasse', 
                    delivery_address: 'Viola - Delivery', 
                    invoice_address: 'Viola - Invoice',
                    company_name: 'VIOLA SPA'})
  suppliers.show('hight_cyan', false)
  suppliers.writeAll
  suppliers.all
  suppliers.show('hight_cyan', false)
end

.test_3Object



33
34
35
36
37
38
39
# File 'lib/hdb/hdb_test.rb', line 33

def self.test_3

  suppliers = Suppliers.new
  supplier = suppliers.findBy_name_and_surname("Mario", "Rossi")
  supplier.show

end

.test_4Object



41
42
43
44
45
46
47
48
49
# File 'lib/hdb/hdb_test.rb', line 41

def self.test_4
  foods = Foods.new
  foods.all
  foods.each do |record|
    p record.name
    p record.categories_id.name
  end 

end

.test_deleteObject



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/hdb/hdb_test.rb', line 173

def self.test_delete

  pizze = Categories.new.findBy_name("PIZZE").foods_table
  
  rustica = pizze.create({name: 'Rustica', description: 'Pomodoro, Mozzarella, Olive', price: 10})
  biancaneve = pizze.create({name: 'Biancaneve', description: 'Aglio, Olio', price: 10})
  pizze.show
  capricciosa = Foods.new.findBy_name("Capricciosa")
  pizze.remove(rustica)
  pizze.remove(capricciosa)
  pizze.writeAll

end

.test_saveObject



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/hdb/hdb_test.rb', line 140

def self.test_save

  pizze = Categories.new.findBy_name("PIZZE").foods_table
  pizze.create({name: 'Diavola', description: 'Pomodoro, Mozzarella, Salamino e Peperoncino', price: 10})
  pizze.each do |pizza|
    pizza.price = 1 #Random.rand(10) 
    pizza.printers_id = 1
  end
  #pizze.show
  pizze.writeAll
  pizze.saveAll

end

.test_save2Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/hdb/hdb_test.rb', line 154

def self.test_save2

  capricciosa = Foods.new.findBy_name("Capricciosa")
  capricciosa.printers_id.name = "pizzeria"
  #capricciosa.show
  capricciosa.saveAll
 

  puts "§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§".red
  pizze = Categories.new.findBy_name("PIZZE")
  pizze.foods_table.each do |pizza|
    pizza.printers_id.name = "pippo"
  end
  #pizze.saveAll
  pizze.ok


end

.test_showCategoriesObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hdb/hdb_test.rb', line 51

def self.test_showCategories
 
  categories = Categories.new
  categories.all
  categories.show
  categories.each do |record|
    p record.name
    record.foods_table.show  # polimorfismo runtime
  end 

end

.virtualFieldsObject



212
213
214
215
216
217
218
219
220
221
# File 'lib/hdb/hdb_test.rb', line 212

def self.virtualFields

  products = Products.new
  products.quantity = HField.virtual { |this| this.categories_id.id.to_i }
  products.amount = HField.virtual(:amount, HODB) # Secondo metodo ma passando il nome della classe essendo statica
  #products.amount = HField.virtual { |this| this.quantity * this.price.to_i } # Terzo metodo
  products.all
  products.show("red", [:id, :quantity, :price, :amount, :score, :random])

end

Instance Method Details

#_remove(record = @tableCursor) ⇒ Object



827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
# File 'lib/hdb/hodb.rb', line 827

def _remove(record = @tableCursor)

  record = record.tableCursor if record.class == HRecordPtr 
  puts "_remove: #{record}".pink
  puts "============================".red
  if (record["id"][0] == "#")
    @parents.each { |parent| record[parent[:referenceFieldName]]._remove }
    @toWrite.delete(record)
    @toChange.delete(record)
  else
    @parents.each do |parent| 
      referenceFieldName = parent[:referenceFieldName]
      id = record[referenceFieldName]
      parent[:ptr].remove(id)
    end
    @toDelete << record   
  end
  @table.delete(record)

end

#addField(fieldName, args) ⇒ Object



237
238
239
240
241
242
243
# File 'lib/hdb/hodb.rb', line 237

def addField(fieldName, args)
  
  field = HField.autoComplete(@modelName, fieldName, args) # For the magic field
  @hfields[fieldName] = field
  hl << "addField: #{fieldName} #{field}"

end

#allObject



679
680
681
# File 'lib/hdb/hodb.rb', line 679

def all
  return self.findBy()
end

#allModelsObject



486
487
488
489
490
# File 'lib/hdb/hodb.rb', line 486

def allModels
  result = []
  self.allParents().each { |parent| result << parent.modelName unless result.include?(parent.modelName) }
  return result
end

#allParentsObject



148
149
150
151
152
153
# File 'lib/hdb/hodb.rb', line 148

def allParents()
  return @allParents if @allParents
  @allParents = [self]
  @parents.each { |parent| @allParents += parent[:ptr].allParents() }
  return @allParents 
end

#allRelationsObject



477
478
479
480
481
482
483
484
# File 'lib/hdb/hodb.rb', line 477

def allRelations
  result = []
  result << hdb(@connectionName).normalizeWhereFormat(@domain) if @domain.class == String or @domain.any?
  @parents.each do |parent|
    result += ["#{@modelName}.#{parent[:referenceFieldName]} = #{parent[:ptr].modelName}.id"] + parent[:ptr].allRelations 
  end
  return result
end

#applyObject



858
859
860
861
862
863
864
865
866
867
868
# File 'lib/hdb/hodb.rb', line 858

def apply
  puts "************************* apply **********************".pink
  self.writeAll
  self.saveAll
  @parents.each { |parent| parent[:ptr].apply }
  @visited.each do |odb|
    puts "####################################=> #{odb.modelName}".pink
    odb.apply
  end
  @visited.clear
end

#columnWidth(fieldName, inheritance = true) ⇒ Object



870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
# File 'lib/hdb/hodb.rb', line 870

def columnWidth(fieldName, inheritance = true)
  
  return @colsWidth[fieldName] if @colsWidth.include?(fieldName)

  maxLength = fieldName.length
  self.each do |row|
    row.eachField(inheritance) do |fieldName, fieldValue|  
      len = fieldValue.to_s.length 
      maxLength = len if maxLength < len and fieldValue.class != HRecordPtr and fieldValue.class > HODB
    end
  end
  
  return @colsWidth[fieldName] = maxLength

end

#create(record = {}) ⇒ Object



699
700
701
702
703
704
705
706
# File 'lib/hdb/hodb.rb', line 699

def create(record = {})
  defaultRecord = self.defaultRecord(record)
  @parents.each { |parent| defaultRecord[parent[:referenceFieldName]] = parent[:ptr].create(record) }
  @toWrite << defaultRecord
  @table << defaultRecord
  self.last
  return self.recordPtr("parent")
end

#data(fieldName) ⇒ Object



507
508
509
510
511
512
513
# File 'lib/hdb/hodb.rb', line 507

def data(fieldName)
  
  fieldName = fieldName.to_s    
  node = self.nodeOfField(fieldName)
  return (node.any?) ? node[0].tableCursor[fieldName] : nil

end

#defaultRecord(record) ⇒ Object



689
690
691
692
693
694
695
696
697
# File 'lib/hdb/hodb.rb', line 689

def defaultRecord(record)
  defaultRecord = Marshal.load(Marshal.dump(self.fields()))
  defaultRecord.each { |fieldName, fieldValue| defaultRecord[fieldName.to_s] = self.defaultValue(fieldName) }
  defaultRecord["id"] = "##{@vid+=1}"
  record.each {|fieldName,fieldValue| defaultRecord[fieldName.to_s] = fieldValue if defaultRecord.include?(fieldName.to_s)}
  @domain.each {|fieldName,fieldValue| defaultRecord[fieldName.to_s] = fieldValue if defaultRecord.include?(fieldName.to_s)} if @domain.class == Hash
  puts "#{defaultRecord}".red
  return defaultRecord
end

#defaultValue(fieldName) ⇒ Object



683
684
685
686
687
# File 'lib/hdb/hodb.rb', line 683

def defaultValue(fieldName)
  field = @hfields[fieldName.to_s]
  return nil unless field
  return (field[:default]) ? field[:default] : field.defaultValue()
end

#delete(where = "TRUE", ids = nil) ⇒ Object



802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
# File 'lib/hdb/hodb.rb', line 802

def delete(where = "TRUE", ids = nil)
  
  where = self.normalizeWhereFormat(where)

  model = (ids == nil) ? self.allModels.join(', ') : @modelName
  where = (self.allRelations << where).join(' and ') unless ids
  
  @tableCursor = nil
  table = hdb(@connectionName).select("#{modelName}.*").from(model).where(where).execute.table
  return self if table.count == 0

  ids = table.column(:id)
  hdb(@connectionName).delete(@modelName, "id IN (#{ids.join(',')})")

  ids.each { |id| @table.delete(self.ptrOfRecord("id", id)) } 
  
  @parents.each do |parent|
    ids = table.column(parent[:referenceFieldName])
    parent[:ptr].delete("id IN (#{ids.join(',')})", ids)
  end
  return self

end

#direction(direction) ⇒ Object



546
547
548
549
550
# File 'lib/hdb/hodb.rb', line 546

def direction(direction)
  @executed = false
  @direction = direction
  return self
end

#eachObject



424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/hdb/hodb.rb', line 424

def each
          
  self.execute unless @executed

  self.reset()
  while(self.next) do
    yield(self)
  end

  @tableCursor = nil
  self.init()

end

#each_with_indexObject



438
439
440
441
442
443
# File 'lib/hdb/hodb.rb', line 438

def each_with_index
  i = -1
  self.each do |record|
    yield(self, i += 1)
  end
end

#eachField(inheritance = true) ⇒ Object



446
447
448
449
450
451
452
# File 'lib/hdb/hodb.rb', line 446

def eachField(inheritance = true)
  self.fields(inheritance).each do |fieldName, fieldValue|
    fieldValue = self.data(fieldName) 
    fieldValue = eval("self.#{fieldName}") unless fieldValue # si tratta di campi virtual runtime
    yield(fieldName, fieldValue)
  end
end

#executeObject



672
673
674
675
676
677
# File 'lib/hdb/hodb.rb', line 672

def execute
  # @executed serve per evitare di chiamare execute nelle istruzioni del tipo:
  # odb.where(filter).orderBy(sortField).direction(sortDirection).pageSize(pageSize).page(page)
  @executed = true
  return self.findBy(@where, nil, orderBy: @orderBy, direction: @direction, pageSize: @pageSize, page: @page)
end

#fieldProperty(fieldName, property) ⇒ Object



322
323
324
325
326
327
328
329
# File 'lib/hdb/hodb.rb', line 322

def fieldProperty(fieldName, property)
  
  return nil unless fieldName
  hfield = self.hfields()[fieldName.to_s] || self.hfields()[fieldName.to_sym]
  return hfield[property.to_s] || hfield[property.to_sym] if hfield
  return nil

end

#fields(inheritance = false) ⇒ Object

false: return only fields of table true: return the fields of table + parents fields :false: return only hfields of table :true: return only hfields of table + parents hfields



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/hdb/hodb.rb', line 185

def fields(inheritance = false)

  return inheritance if inheritance.class == Array

  case inheritance
  when true 
    return self.modelFields() 
  when false 
    return self.tableFields()
  when :true
    return self.hfields(:true)
  when :false
    return self.hfields(:false)
  end

end

#findBy(where = 'true', ids = nil, orderBy: nil, direction: nil, pageSize: "all", page: 0) ⇒ Object

example: findBy(1)

findBy({'c_tables.id': 1}) - I can specify the field of the another table 
findBy({id: 1, name: 'ciao'}) -> c_tables.id = '1' AND c_tables.name = 'ciao' 
The follow statement isn't safe. You can use it, only if the values don't come from forms but they are statics
findBy(["c_tables.id > 0", "c_tables.name = 'ciao'"]) 
The follow statement is safe as the ? syntax and you can use the values from forms
findBy(["c_tables.id > 0", "c_tables.name = #{q(form_field)}"])


642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
# File 'lib/hdb/hodb.rb', line 642

def findBy(where = 'true', ids = nil, orderBy: nil, direction: nil, pageSize: "all", page: 0)

  return self unless where 
  where = self.normalizeWhereFormat(where)
      
  model = (ids == nil) ? self.allModels.join(', ') : @modelName
  where = (self.allRelations << where).join(' and ') if !ids and where
 
  hdb = hdb(@connectionName).
    select("#{modelName}.*").
    from(model).
    where(where).
    orderBy(orderBy).
    direction(direction).
    execute(pageSize: pageSize, page: page)

  @tableFields = Hash[hdb.fieldNameList()]
  @table = hdb.table.sortByList("id", ids)
  @tableCursor = nil

  return self if @table.count == 0

  @parents.each do |parent|
    ids = @table.column(parent[:referenceFieldName])
    parent[:ptr].findBy("id IN (#{ids.join(',')})", ids)
  end
  return self.next().recordPtr('manyToOne')

end

#findByOffLine(where) ⇒ Object

findByOffLine(1, name: ‘margherita’)



620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/hdb/hodb.rb', line 620

def findByOffLine(where)

  tableCursor = @tableCursor 
  self.each do |record|
    return self if where.each do |fieldName, fieldValue| 
      #puts "#{record.valueOfField(fieldName.to_s)} -  #{fieldValue}"
      break if record.valueOfField(fieldName.to_s) != fieldValue 
    end
  end
  self.setTableCursor(tableCursor) # if not found restore the tableCursor value
  return self

end

#firstObject



390
391
392
393
# File 'lib/hdb/hodb.rb', line 390

def first
  self.reset
  return self.next
end

#initObject



528
529
530
531
532
533
# File 'lib/hdb/hodb.rb', line 528

def init()
  @where = "true"
  @orderBy = "id"
  @pageSize = "all"
  @page = 0
end

#inspection(str) ⇒ Object



317
318
319
320
# File 'lib/hdb/hodb.rb', line 317

def inspection(str)
  puts "=============> #{str}"
  return false
end

#lastObject



395
396
397
398
# File 'lib/hdb/hodb.rb', line 395

def last
  @tableCursor = @table.count > 0 ? @table[@table.count - 1] : nil
  return self
end

#manyToMany(modelName, joinTable, referenceFieldName, referenceFieldName2) ⇒ Object



283
284
285
286
287
288
289
290
291
# File 'lib/hdb/hodb.rb', line 283

def manyToMany(modelName, joinTable, referenceFieldName, referenceFieldName2)

  odb = self.oneToMany(joinTable, joinTable, referenceFieldName)
  odb.type = 'manyToMany'
  odb.tmp[:referenceFieldName] = referenceFieldName2
  odb.tmp[:rightModelName] = modelName
  return odb

end

#manyToOne(modelName, referenceFieldName, type = 'manyToOne') ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/hdb/hodb.rb', line 251

def manyToOne(modelName, referenceFieldName, type = 'manyToOne')
 
  return @tableCursor[modelName] if @tableCursor[modelName]
  id = self.valueOfField(referenceFieldName)
  @tableCursor[modelName] = odb = self.newHODB(modelName, type: type)
  @visited.addIfNotPresent(odb)
  puts "modelName: #{referenceFieldName}".red
  @tableCursor.hprint
  unless id 
    self.setValueOfField(referenceFieldName, odb.recordPtr(type))
    return odb
  end
  @toChange << @tableCursor unless @toChange.include?(@tableCursor)
  return odb.findBy_id(id)
 
end

#modelFieldsObject

return all fields



160
161
162
163
164
165
# File 'lib/hdb/hodb.rb', line 160

def modelFields # return all fields 
  return @modelFields if @modelFields
  @modelFields = Marshal.load(Marshal.dump(self.tableFields()))
  @parents.each { |parent| @modelFields.merge!(parent[:ptr].modelFields()) }
  return @modelFields
end

#modelNameAndFieldName(fieldName) ⇒ Object



500
501
502
503
504
505
# File 'lib/hdb/hodb.rb', line 500

def modelNameAndFieldName(fieldName)

  modelName = self.modelNameOfField(fieldName)
  return (modelName) ? "#{modelName}.#{fieldName}" : fieldName

end

#modelNameOfField(fieldName) ⇒ Object



492
493
494
495
496
497
498
# File 'lib/hdb/hodb.rb', line 492

def modelNameOfField(fieldName)

  fieldName = fieldName.to_s    
  node = self.nodeOfField(fieldName)
  return (node.any?) ? node[0].modelName : nil

end

#newHODB(modelName, type: nil, domain: {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/hdb/hodb.rb', line 122

def newHODB(modelName, type: nil, domain: {})
  _modelName = modelName.to_s.hcapitalize
  #puts "newHODB: #{modelName}"
  p "#{_modelName}.new(connectionName: @connectionName, domain: domain)"
  odb = (Object.const_defined?(_modelName)) ? eval("#{_modelName}.new(connectionName: @connectionName, domain: domain)") : HODB.new(modelName, connectionName: @connectionName, domain: domain)
  odb.source = self
  odb.type = type if type
  return odb

end

#nextObject



411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/hdb/hodb.rb', line 411

def next()
  
  index = @tableCursor ? @table.index(@tableCursor) + 1 : 0

  if (index < @table.count)
    @tableCursor = @table[index]
    self.setTableCursor 
    return self
  end
  return nil

end

#nodeOfField(fieldName) ⇒ Object



202
203
204
205
206
# File 'lib/hdb/hodb.rb', line 202

def nodeOfField(fieldName)
  result = self.fields().include?(fieldName) ? [self] : []
  @parents.each { |parent| result += parent[:ptr].nodeOfField(fieldName) }
  return @nodes = result
end

#normalizeWhereFormat(where) ⇒ Object



516
517
518
519
520
521
522
523
524
525
526
# File 'lib/hdb/hodb.rb', line 516

def normalizeWhereFormat(where)
  
  if where.class == Hash # findBy {'c_tables.id': 1}
    newWhere = {} 
    where.each { |fieldName, fieldValue| newWhere[self.modelNameAndFieldName(fieldName)] = fieldValue }
    where = newWhere
  end
  
  return hdb(@connectionName).normalizeWhereFormat(where)

end

#oneToMany(fieldName, modelName, referenceFieldName) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/hdb/hodb.rb', line 268

def oneToMany(fieldName, modelName, referenceFieldName)
  
  return @tableCursor[fieldName] if @tableCursor[fieldName]
  if(self.id[0] != "#")
    domain = {"#{referenceFieldName}": self.id}
    odb = self.newHODB(modelName, type: 'oneToMany', domain: domain).findBy()
    return  @visited.addIfNotPresent(odb)
  end
  domain = {"#{referenceFieldName}": nil}
  odb = self.newHODB(modelName, type: 'oneToMany', domain: domain)
  @tableCursor[fieldName] = odb
  @visited.addIfNotPresent(odb)
  return odb
end

#oneToOne(modelName, referenceFieldName) ⇒ Object



245
246
247
248
249
# File 'lib/hdb/hodb.rb', line 245

def oneToOne(modelName, referenceFieldName)
 
  return self.manyToOne(modelName, referenceFieldName, 'oneToOne')

end

#orderBy(orderBy) ⇒ Object



540
541
542
543
544
545
# File 'lib/hdb/hodb.rb', line 540

def orderBy(orderBy)
  @executed = false
  orderBy = orderBy.hjoin(', ') if(orderBy.class == Array)
  @orderBy = orderBy
  return self
end

#page(page) ⇒ Object



556
557
558
559
560
# File 'lib/hdb/hodb.rb', line 556

def page(page)
  @executed = false
  @page = page
  return self
end

#pageSize(pageSize) ⇒ Object



551
552
553
554
555
# File 'lib/hdb/hodb.rb', line 551

def pageSize(pageSize)
  @executed = false
  @pageSize = pageSize
  return self
end

#ptrOfRecord(fieldName, fieldValue) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'lib/hdb/hodb.rb', line 138

def ptrOfRecord(fieldName, fieldValue)
  
  return fieldValue.tableCursor if fieldValue.class == HRecordPtr # Se si tratta gia' di un ptr a un record lo restituisce
  @table.each do |record|
    return record if record[fieldName.to_s] == fieldValue
  end  
  return nil

end

#recordPtr(type = nil) ⇒ Object



118
119
120
# File 'lib/hdb/hodb.rb', line 118

def recordPtr(type = nil)
  return HRecordPtr.new(self, type)
end

#remove(recordId = ) ⇒ Object

virtual remove => the real removal happens with apply



849
850
851
852
853
854
855
# File 'lib/hdb/hodb.rb', line 849

def remove(recordId = @tableCursor["id"])

  puts "remove: #{@modelName} - #{recordId}".yellow
  self.findByOffLine({id: recordId})
  self._remove(@tableCursor)

end

#resetObject



385
386
387
388
# File 'lib/hdb/hodb.rb', line 385

def reset
  @parents.each { |parent| parent[:ptr].reset }
  @tableCursor = nil
end

#rightModelObject

a_table <–> manyToMany <–> b_table To access to b_table from a_table -> a_table.a_table_b_table_join.rightModel Moreover on right model is unsense call the create function



296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/hdb/hodb.rb', line 296

def rightModel
  
  rightModelName = @tmp[:rightModelName]
  return @tableCursor[rightModelName] if @tableCursor[rightModelName]

  ids = @table.column(@tmp[:referenceFieldName]) 
  where = "id IN (#{ids.join(',')})"
  odb = self.newHODB(rightModelName, type: 'rightTable').findBy(where) if (@type == 'manyToMany')
  @visited.addIfNotPresent(odb)
  return @tableCursor[rightModelName] = odb

end

#save(values = {}) ⇒ Object



766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/hdb/hodb.rb', line 766

def save(values = {})
  hl << "********************************* save(#{@type} - #{@modelName}) *********************************"
  #return self unless @toChange.include?(@tableCursor)
  puts "#{values}".red
  @tableCursor.hprint
  @tableCursor.merge!(values)
  @tableCursor["id"] = "#default" if @tableCursor["id"][0] == "#"
  self.setTableCursor(@tableCursor)
  @tableCursor.each do |fieldName, fieldValue| 
    if fieldValue.class <= HODB and (fieldValue.type == 'manyToOne' or fieldValue.type == 'oneToOne')
      puts "call: #{fieldName} - #{fieldValue.domain}".red
      @tableCursor[fieldName] = fieldValue.saveAll().id 
    end
  end
  @parents.each { |parent| parent[:ptr].save(values) }
  # intersect rimuove ad es. i campy oneToMany e i campi inseriti dalla merge! sopra se non fanno parte del @tableCursor
  hdb(@connectionName).update(@modelName, @tableCursor.intersect(self.fields), {id: @tableCursor["id"]})
  @tableCursor.each do |fieldName, fieldValue| 
    if fieldValue.class <= HODB and (fieldValue.type == 'oneToMany' or fieldValue.type == 'manyToMany' or fieldValue.type == 'rightTable')
      p "saveAll".red
      fieldValue.saveAll()
    end
  end
  @toChange.delete(@tableCursor) 
  return self
end

#saveAll(values = {}) ⇒ Object



793
794
795
796
797
798
799
800
# File 'lib/hdb/hodb.rb', line 793

def saveAll(values = {})
  toChange = @toChange.dup
  toChange.each do |record| 
    @tableCursor = record
    self.save(values)
  end
  return self
end

#setParents(parents = []) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/hdb/hodb.rb', line 218

def setParents(parents = [])
  parents.each do |modelName|
    referenceFieldName = "#{modelName}_id"
    parent = {modelName: modelName, referenceFieldName: referenceFieldName}
    self.addField(referenceFieldName, HField.oneToOne(modelName))  
    #parent[:ptr] = self.newHODB(modelName, domain: @domain)
    parent[:ptr] = self.newHODB(modelName)
    @parents << parent
  end

end

#setRecord(record, tableCursor = @tableCursor) ⇒ Object



708
709
710
711
712
713
714
715
716
717
# File 'lib/hdb/hodb.rb', line 708

def setRecord(record, tableCursor = @tableCursor)
  @toChange << tableCursor unless @toChange.include?(tableCursor)
  record.each do |fieldName, fieldValue| 
    unless self.fields(true).include?(fieldName.to_s)
      hl.<<("hodb::setRecord: #{fieldName.to_s} not found", "ERROR")
      next
    end
    setValueOfField(fieldName.to_s, fieldValue) 
  end
end

#setSystemFieldsObject



230
231
232
233
234
235
# File 'lib/hdb/hodb.rb', line 230

def setSystemFields()
  self.id = HField.primaryKey
  self.created_date = HField.dateTime(default: '#CURRENT_TIMESTAMP', system: true)
  self.updated_date = HField.dateTime(default: '#CURRENT_TIMESTAMP', system: true)
  self.ordering = HField.integer(system: true)
end

#setTableCursor(tableCursor = @tableCursor) ⇒ Object



400
401
402
403
404
405
406
407
408
409
# File 'lib/hdb/hodb.rb', line 400

def setTableCursor(tableCursor = @tableCursor)
  @tableCursor = tableCursor
  @id = @tableCursor["id"]
  @parents.each do |parent|
    parentTableCursor = @tableCursor[parent[:referenceFieldName]]
    parentTableCursor = parent[:ptr].ptrOfRecord("id", parentTableCursor)
    parent[:ptr].setTableCursor(parentTableCursor)
  end
  return self
end

#setValueOfField(fieldName, fieldValue) ⇒ Object



213
214
215
216
# File 'lib/hdb/hodb.rb', line 213

def setValueOfField(fieldName, fieldValue)
  nodes = self.nodeOfField(fieldName)
  nodes.each { |node| node.tableCursor[fieldName] = fieldValue }
end

#show(color = 'hight_cyan', inheritance = true) ⇒ Object



916
917
918
919
920
921
922
923
924
925
926
927
928
# File 'lib/hdb/hodb.rb', line 916

def show(color = 'hight_cyan', inheritance = true)
  @colsWidth = {}
  @parents.each do |parent|
    parent[:ptr].show(color, inheritance) unless (inheritance)
  end
  self.showHeader(color, inheritance) 
  self.each do |row|
    row.eachField(inheritance) do |fieldName, fieldValue|  
      self.showValue(fieldName, fieldValue, 'white')
    end
    puts 
  end
end

#showHeader(color, inheritance = true) ⇒ Object



897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
# File 'lib/hdb/hodb.rb', line 897

def showHeader(color, inheritance = true)

  puts
  print eval "' >> #{@modelName.to_s} <<'.#{color}"
  puts
  self.fields(inheritance).each do |fieldName, fieldValue|
    self.showValue(fieldName, "-" * self.columnWidth(fieldName, inheritance), color, '+' )
  end
  puts
  self.fields(inheritance).each do |fieldName, fieldValue|
    self.showValue(fieldName, fieldName, color, '|')
  end
  puts
  self.fields(inheritance).each do |fieldName, fieldValue|
    self.showValue(fieldName, "-" * self.columnWidth(fieldName, inheritance), color, '+' )
  end
  puts
end

#showValue(fieldName, fieldValue, color = nil, separator = '|') ⇒ Object



886
887
888
889
890
891
892
893
894
895
# File 'lib/hdb/hodb.rb', line 886

def showValue(fieldName, fieldValue, color = nil, separator = '|')
  
  fieldValue = fieldValue.object_id if fieldValue.class == HRecordPtr or fieldValue.class <= HODB
  value = " #{fieldValue.to_s.center(self.columnWidth(fieldName))} #{separator}"
  unless color
    print value
  else
    print eval "'#{value}'.#{color}"
  end
end

#sortOffLine(fieldName = "id", sortDirection = 'desc', sortMap = []) ⇒ Object



562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/hdb/hodb.rb', line 562

def sortOffLine(fieldName = "id", sortDirection = 'desc', sortMap = [])
  
  # table = [{id:7, name:'valerio', surname:'bianchi'}, 
  #          {id:8, name:'mario', surname:'rossi'},
  #          {id:6, name:'laura', surname:'neri'}]
  #
  # table.sort_by { |row| row["name"] } =>
  #          {id:6, name:'laura', surname:'neri'},
  #          {id:8, name:'mario', surname:'rossi'},
  #          {id:7, name:'valerio', surname:'bianchi'} 
  # columnSorted = tableSorted.column("id") => [1, 3, 2]
  # idColumn = @table.column("id") => [2, 3, 1]
  # sortMap => [2, 1, 0]
  # A this time to print the records it is enough to scan sortMap
  # The first element to print is @table[2] => @table[1] => @table[0]

  puts "========================================================".red
  puts "=======                sortOffLine               =======".red
  puts "========================================================".red
  puts "currentModelName: #{@modelName}".red
  if sortMap.empty?
    node = self.nodeOfField(fieldName)[0]
    return unless node
    puts "modelName of #{fieldName}: #{node.modelName}".red

    p node.hfields()
    fieldType = node.hfields()[fieldName] ? node.hfields()[fieldName][:type] : nil
    puts "type of #{fieldName}: #{fieldType}" 
    tableSorted = nil
    if fieldType == "integer"
      tableSorted = node.table.sort_by { |row| row[fieldName].to_i }
    elsif fieldType == "float"
      tableSorted = node.table.sort_by { |row| row[fieldName].to_f }
    elsif fieldType == "time"
      tableSorted = node.table.sort_by { |row| row[fieldName].to_time }
    elsif fieldType == "date_time"
      tableSorted = node.table.sort_by { |row| row[fieldName].to_datetime }
    else
      tableSorted = node.table.sort_by { |row| row[fieldName] }
    end

    tableSorted.reverse! if sortDirection == 'asc'

    columnSorted = tableSorted.column("id")
    idColumn = node.table.column("id")
    columnSorted.each_with_index { |value, i| sortMap[i] = idColumn.index(value) }
    p sortMap
  end
    
  @table.sortByPositionList(sortMap)

  @parents.each do |parent|
    parent[:ptr].sortOffLine(nil, sortDirection, sortMap)
  end

end

#systemFields(inheritance = false) ⇒ Object



167
168
169
# File 'lib/hdb/hodb.rb', line 167

def systemFields(inheritance = false) 
  return self.hfields(inheritance).select { |fieldName, field| field[:system] }
end

#tableFieldsObject

return only the fields of table



155
156
157
158
# File 'lib/hdb/hodb.rb', line 155

def tableFields # return only the fields of table
  self.findBy(:false, []) unless @tableFields # poputate @tableFields
  return @tableFields
end

#toFieldTable(inheritance = true) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/hdb/hodb.rb', line 455

def toFieldTable(inheritance = true)
  
  fieldTable = HSpreadFieldTable.new
  fieldTable.tableName = @modelName

  self.fields(inheritance).each do |fieldName, fieldValue|
    fieldTable.addFieldName(fieldName)
    fieldTable.setFieldCaption(fieldName, fieldName.to_s.hcapitalize)
    fieldType = self.fieldProperty(fieldName.to_s, :type) 
    fieldTable.setFieldType(fieldName, fieldType) if fieldType
  end

  self.each_with_index do |row, i|
    row.eachField(inheritance) do |fieldName, fieldValue|  
      fieldTable.setIntoRecordByFieldName(i, fieldName, fieldValue)
    end
  end

  return fieldTable

end

#valueOfField(fieldName) ⇒ Object



208
209
210
211
# File 'lib/hdb/hodb.rb', line 208

def valueOfField(fieldName)
  nodes = self.nodeOfField(fieldName)
  return (nodes.any?) ? nodes[0].tableCursor[fieldName] : nil
end

#virtualField(obj, functionName, &block) ⇒ Object



309
310
311
312
313
314
315
# File 'lib/hdb/hodb.rb', line 309

def virtualField(obj, functionName, &block)

  return block.call(self) if block
  #hl <<  "obj.#{functionName.to_s}(self)"
  return eval("obj.#{functionName.to_s}(self)") if obj and functionName

end

#where(where) ⇒ Object



535
536
537
538
539
# File 'lib/hdb/hodb.rb', line 535

def where(where)
  @executed = false
  @where = where
  return self
end

#write(values = {}) ⇒ Object



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
# File 'lib/hdb/hodb.rb', line 720

def write(values = {})
  hl << "********************************* write(#{@type}) *********************************"
  return self unless @toWrite.include?(@tableCursor)
  return self unless values  
  puts "#{values}".red
  @tableCursor.merge!(values)
  @tableCursor["id"] = "#default" if @tableCursor["id"][0] == "#"
  @tableCursor.each do |fieldName, fieldValue| 
    if fieldValue.class == HRecordPtr 
      if fieldValue.type == "parent"
        @tableCursor[fieldName] = fieldValue.write(values).id 
      elsif fieldValue.type == "manyToOne" or fieldValue.type == "oneToOne"
        @tableCursor[fieldName] = fieldValue.write().id 
      elsif fieldValue.type == nil # questo caso non deve verificarsi
        hl.<<("hodb::write: ", "ERROR")
        fieldValue.tableCursor.hprint
      end
    end
  end
  # intersect rimuove ad es. i campy oneToMany e i campi inseriti dalla merge! sopra se non fanno parte del @tableCursor
  @tableCursor["id"] = hdb(@connectionName).insert(@modelName, @tableCursor.intersect(self.fields))
  @tableCursor.each do |fieldName, fieldValue| 
    if fieldValue.class <= HODB and (fieldValue.type == 'oneToMany' or fieldValue.type == 'manyToMany' or fieldValue.type == 'rightTable')
      referenceFieldName = fieldValue.domain.keys[0].to_s
      fieldValue.writeAll({"#{referenceFieldName}" => @tableCursor["id"]})
    end
  end
  @toWrite.delete(@tableCursor) 
  return self
end

#writeAll(values = {}) ⇒ Object



751
752
753
754
755
756
757
758
759
760
761
762
763
764
# File 'lib/hdb/hodb.rb', line 751

def writeAll(values = {})

  puts "********************* writeAll ********************".pink

  @toDelete.each { |record| self.delete({id: record["id"]}) }
  @toDelete.clear

  toWrite = @toWrite.dup
  toWrite.each do |record| 
    @tableCursor = record
    self.write(values)
  end
  return self
end