class HDBGenerator

Public Class Methods

new(dropTables = false, connectionName = "default") click to toggle source
# 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

Public Instance Methods

addField(modelName, fieldName, type, constraint = nil, default = nil, reference = nil, onDelete = nil, onUpdate = nil) click to toggle source
# 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) click to toggle source
# File lib/hdb/hdbgenerator.rb, line 117
def addSystemFields(modelName)
  self.addField(modelName, :id, 'SERIAL', 'PRIMARY KEY')
end
after(modelName) click to toggle source
# File lib/hdb/hdbgenerator.rb, line 124
def after(modelName)
end
before(modelName) click to toggle source
# File lib/hdb/hdbgenerator.rb, line 121
def before(modelName)
end
createScaffoldTable(modelName) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
dataLoader(modelName) click to toggle source
# 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
databases() click to toggle source
# 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
disableTrigger(modelName) click to toggle source
# 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) click to toggle source
# File lib/hdb/hdbgenerator.rb, line 156
def dropField(modelName, fieldName)
  hdb(@connectionName).execute("ALTER TABLE #{modelName} DROP COLUMN #{fieldName}") 
end
dropFields(modelName, fields) click to toggle source
# File lib/hdb/hdbgenerator.rb, line 148
def dropFields(modelName, fields)

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

end
dropTable(modelName) click to toggle source
# 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) click to toggle source
# 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") click to toggle source

column_name, data_type, column_default, ordinal_position

# 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") click to toggle source
# 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) click to toggle source
# File lib/hdb/hdbgenerator.rb, line 6
def newOdb(modelName)
  className = modelName.hcapitalize
  return eval("#{className}.new")
end
tables() click to toggle source
# 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) click to toggle source
# 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