Class: DataSet

Inherits:
Object
  • Object
show all
Includes:
DB, DBConfig
Defined in:
lib/sqlloader.rb

Overview

A collection of data that can be inserted together into a database

Defined Under Namespace

Classes: DataPiece

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DBConfig

#get_config_path, #load_config

Methods included from DB

#get_db_connection

Constructor Details

#initialize(directory, user_config = {}) ⇒ DataSet

Creates a dataset using the SQL and the configuration contained in directory. The supplied user configuration overrides the configuration contained in the directory



27
28
29
30
31
32
33
34
35
36
# File 'lib/sqlloader.rb', line 27

def initialize(directory, user_config = {})
  @ups = []
  @downs = []
  @directory = directory
  populate() 
  @config = load_config(directory).merge(user_config)
  if insufficient(config)
    raise ArgumentError, 'No database name'
  end
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



21
22
23
# File 'lib/sqlloader.rb', line 21

def config
  @config
end

#directoryObject

Returns the value of attribute directory.



21
22
23
# File 'lib/sqlloader.rb', line 21

def directory
  @directory
end

#downsObject

Returns the value of attribute downs.



21
22
23
# File 'lib/sqlloader.rb', line 21

def downs
  @downs
end

#upsObject

Returns the value of attribute ups.



21
22
23
# File 'lib/sqlloader.rb', line 21

def ups
  @ups
end

Instance Method Details

#deleteObject

Executes the SQL scripts marked as reset scripts



62
63
64
65
66
67
68
69
# File 'lib/sqlloader.rb', line 62

def delete
  get_db_connection(@config) do |db_connection|
  @downs.each do |s|
      result = db_connection.exec(s.statement)
      puts "#{s.filename}: #{result.cmd_status}"
    end
  end
end

#loadObject

Executes the SQL scripts associated with the dataset.



51
52
53
54
55
56
57
58
# File 'lib/sqlloader.rb', line 51

def load
  get_db_connection(@config) do |db_connection|
    @ups.each do |s|
      result = db_connection.exec(s.statement)
      puts "#{s.filename}: #{result.cmd_status}"
    end
  end
end

#resetObject

If there are reset scripts available, executes them before executing once again the regular scripts



41
42
43
44
45
46
47
# File 'lib/sqlloader.rb', line 41

def reset
 if @downs.empty?
    raise ArgumentError, 'There are no reset scripts to run'
  end
 delete
 load
end

#to_sObject



71
72
73
# File 'lib/sqlloader.rb', line 71

def to_s
  File.basename(@directory)
end