Class: HDBGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/hdb/hdbgenerator.rb

Instance Method Summary collapse

Constructor Details

#initialize(dropTables = false, connectionName = "default") ⇒ HDBGenerator

Returns a new instance of HDBGenerator.



11
12
13
14
15
16
17
# File 'lib/hdb/hdbgenerator.rb', line 11

def initialize(dropTables = false, connectionName = "default")
  self.initModule("quickmanager", dropTables, connectionName)
  return
  HDir.new("#{Dir.pwd}/app/modules/*").onlyDirectories().each do |moduleName|
    self.initModule(moduleName, dropTables, connectionName)
  end
end

Instance Method Details

#addField(modelName, fieldName, type, constraint = nil, default = nil, reference = nil, onDelete = nil, onUpdate = nil) ⇒ Object



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

def addField(modelName, fieldName, type, constraint = nil, default = nil, reference = nil, onDelete = nil, onUpdate = nil)
  onDelete = " ON DELETE #{onDelete}" if onDelete
  onUpdate = " ON UPDATE #{onUpdate}" if onUpdate
  reference = " REFERENCES #{reference}(id)" if reference
  constraint = " #{constraint}" if constraint != nil
  default = " DEFAULT #{hdb.q(default)}" if default != nil # default could be false and so is correct default != nil
  hdb(@connectionName).execute("ALTER TABLE #{modelName} ADD COLUMN #{fieldName} #{type}#{constraint}#{reference}#{onDelete}#{onUpdate}#{default}") if type 
end

#addSystemFields(modelName) ⇒ Object



117
118
119
# File 'lib/hdb/hdbgenerator.rb', line 117

def addSystemFields(modelName)
  self.addField(modelName, :id, 'SERIAL', 'PRIMARY KEY')
end

#after(modelName) ⇒ Object



124
125
# File 'lib/hdb/hdbgenerator.rb', line 124

def after(modelName)
end

#before(modelName) ⇒ Object



121
122
# File 'lib/hdb/hdbgenerator.rb', line 121

def before(modelName)
end

#createScaffoldTable(modelName) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/hdb/hdbgenerator.rb', line 81

def createScaffoldTable(modelName)
  unless @dbTables.include?(modelName)
    self.createTable(modelName)
    self.addSystemFields(modelName)  
    @htables[modelName][:generated] = true
  end
end

#createTable(modelName) ⇒ Object



110
111
112
113
114
# File 'lib/hdb/hdbgenerator.rb', line 110

def createTable(modelName)
  return if @dbTables.include?(modelName)
  hdb(@connectionName).execute("CREATE TABLE #{modelName} ()") 
  @dbTables << modelName
end

#createTableManyToMany(modelName, fieldAttrs) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/hdb/hdbgenerator.rb', line 89

def createTableManyToMany(modelName, fieldAttrs)
  p fieldAttrs
  type = self.toType(fieldAttrs[:type])
  _modelName = fieldAttrs[:joinTable]
  return if !@dropTables and @dbTables.include?(_modelName)
  self.dropTable(_modelName) 
  self.createTable(_modelName) 
  self.addSystemFields(_modelName)  
  self.addField(_modelName, fieldAttrs[:referenceFieldName], type, 
                fieldAttrs[:constraint], fieldAttrs[:default], modelName)
  self.addField(_modelName, fieldAttrs[:referenceFieldName2], type, 
                fieldAttrs[:constraint], fieldAttrs[:default], fieldAttrs[:modelNameReference])
end

#databasesObject



192
193
194
195
# File 'lib/hdb/hdbgenerator.rb', line 192

def databases
  result = hdb(@connectionName).select(:datname).from(:pg_database).where({datistemplate: 'false'}).execute
  return result.table.column(:datname)
end

#dataLoader(modelName) ⇒ Object



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

def dataLoader(modelName)

  self.disableTrigger(modelName)
  filename = "modules/#{@moduleName}/models/#{modelName}/default_data.yml"  
  HDataLoader.new(filename, @connectionName).load()
  self.enableTrigger(modelName)

end

#disableTrigger(modelName) ⇒ Object



127
128
129
130
131
# File 'lib/hdb/hdbgenerator.rb', line 127

def disableTrigger(modelName)
  @htables[modelName][:oodb].allParents().each do |parent|
    hdb(@connectionName).execute("ALTER TABLE #{parent.modelName} DISABLE TRIGGER ALL") 
  end
end

#dropField(modelName, fieldName) ⇒ Object



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

def dropField(modelName, fieldName)
  hdb(@connectionName).execute("ALTER TABLE #{modelName} DROP COLUMN #{fieldName}") 
end

#dropFields(modelName, fields) ⇒ Object



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

def dropFields(modelName, fields)

  fields.each do |field|
    self.dropField(modelName, field)
  end

end

#dropTable(modelName) ⇒ Object



103
104
105
106
107
108
# File 'lib/hdb/hdbgenerator.rb', line 103

def dropTable(modelName)
  return unless @dbTables.include?(modelName)
  return unless @dropTables
  hdb(@connectionName).execute("DROP TABLE #{modelName} CASCADE") 
  @dbTables.delete(modelName)
end

#enableTrigger(modelName) ⇒ Object



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

def enableTrigger(modelName)
  @htables[modelName][:oodb].allParents().each do |parent|
    hdb(@connectionName).execute("ALTER TABLE #{parent.modelName} ENABLE TRIGGER ALL")
  end 
end

#fields(modelName, fieldName = "column_name") ⇒ Object

column_name, data_type, column_default, ordinal_position



203
204
205
206
207
# File 'lib/hdb/hdbgenerator.rb', line 203

def fields(modelName, fieldName = "column_name")
  result = hdb(@connectionName).select(fieldName).from('information_schema.columns').where({table_name: modelName}).execute
  return result.table if fieldName == "*"
  return result.table.column(fieldName)
end

#initModule(moduleName, dropTables = false, connectionName = "default") ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/hdb/hdbgenerator.rb', line 20

def initModule(moduleName, dropTables = false, connectionName = "default")
  @moduleName = moduleName
  @connectionName = connectionName
  @dropTables = dropTables

  databases = self.databases()
  @dbTables = self.tables()
  
  @htables = {} # hypersonic tables
  for filename in Dir.glob("#{Dir.pwd}/app/modules/#{@moduleName}/*/*.rb") do
    modelName = File.basename(filename).chomp('.rb')
    @htables[modelName] = {}
    self.createScaffoldTable(modelName)
  end

  @htables.each do |modelName, emptyHash|
    oodb = self.newOdb(modelName)
    emptyHash[:oodb] = oodb if oodb.generate
  end

  @htables.each { |modelName, oodb| self.dropTable(modelName) }

  @dbTables = self.tables()
  @htables.each  { |modelName, oodb| self.createScaffoldTable(modelName) }
  @htables.each do |modelName, oodb|
    fields = self.fields(modelName)
    hfields = oodb[:oodb].hfields(false)

    fieldsToDrop = fields - hfields.keys - oodb[:oodb].systemFields().keys()
    self.dropFields(modelName, fieldsToDrop)

    self.before(modelName)
    hfields.each do |fieldName, fieldAttrs|
      hl << "#{modelName}.#{fieldName} #{fieldAttrs}"

      next if fields.include?(fieldName)
      next if (fieldAttrs[:type] == "oneToMany")
      if (fieldAttrs[:type] == "manyToMany")
        self.createTableManyToMany(modelName, fieldAttrs)
        next
      end
      
      type = self.toType(fieldAttrs[:type])
      self.addField(modelName, 
                    fieldName, 
                    type, 
                    fieldAttrs[:constraint],
                    fieldAttrs[:default], 
                    fieldAttrs[:modelNameReference],
                    fieldAttrs[:onDelete],
                    fieldAttrs[:onUpdate])
    end
    self.after(modelName)
  end
  
  @htables.each do |modelName, oodb| 
    self.dataLoader(modelName) if oodb[:generated]
  end

end

#newOdb(modelName) ⇒ Object



6
7
8
9
# File 'lib/hdb/hdbgenerator.rb', line 6

def newOdb(modelName)
  className = modelName.hcapitalize
  return eval("#{className}.new")
end

#tablesObject



197
198
199
200
# File 'lib/hdb/hdbgenerator.rb', line 197

def tables
  result = hdb(@connectionName).select(:tablename).from(:pg_tables).where(["schemaname != 'pg_catalog'", "schemaname != 'information_schema'"]).execute
  return result.table.column(:tablename)
end

#toType(type) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/hdb/hdbgenerator.rb', line 169

def toType(type)

  toPgTypes = {
    boolean: 'bool',
    integer: 'int4',
    float: 'real',
    text: 'text',
    html: 'text',
    date: 'date',
    datetime: 'timestamp',
    binary: 'bytea',
    oneToOne: 'int4',
    manyToOne: 'int4',
    oneToMany: 'int4',
    manyToMany: 'int4',
    serialized: 'text',
    virtual: nil
  }
  return toPgTypes.include?(type.to_sym) ? toPgTypes[type.to_sym] : type

end