Class: DBMapping::StudentMapper

Inherits:
PersonMapper show all
Defined in:
lib/data/sqlite/student_mapper.rb

Overview

Class to apply ER-mapping for Person::Person and Person::Student objects to a sqlite database

Instance Attribute Summary

Attributes inherited from Base

#db_base

Instance Method Summary collapse

Methods inherited from PersonMapper

#query_max_person_id

Methods inherited from Base

#check_max_id

Constructor Details

#initialize(database) ⇒ StudentMapper

initialization

Parameters:

  • database (String)

    the path to the database



9
10
11
# File 'lib/data/sqlite/student_mapper.rb', line 9

def initialize(database)
  @db_base = SqliteDatabase::DBStudent.new(database)
end

Instance Method Details

#generate_personsArray

public method to transform database persons to entity Person

Returns:

  • (Array)

    all transformed Person entities



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/data/sqlite/student_mapper.rb', line 15

def generate_persons
  results = @db_base.query_persons
  persons = Array.new()
  results.each { |result|
    id = Integer(result["Id"])
    mat_nr = @db_base.query_matnr_for_student(id).next
    if (mat_nr != nil)
      persons << Person::Student.new(result["Name"], id, mat_nr["Mat_Nr"])
    else
      persons << Person::Person.new(result["Name"], id)
    end
  }
  return persons
end

#persist_persons(persons) ⇒ Object

method to persist a list of Persons to the database

Parameters:

  • persons (Array)

    the Persons that should be be persisted



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/data/sqlite/student_mapper.rb', line 32

def persist_persons(persons)
  persons.each { |person|
    case (person)
    when Person::Student
      @db_base.insert_student(person.id, person.name, person.mat_nr)
    when Person::Person
      @db_base.insert_person(person.id, person.name)
    else
      raise ArgumentError, "Cannot persist object. Class not found."
    end
  }
  nil
end

#query_person(id) ⇒ Person::Person | nil

method to search for a person by its id

Returns:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/data/sqlite/student_mapper.rb', line 48

def query_person(id)
  result = @db_base.query_student_by_id(id).next
  if (result != nil)
    return Person::Student.new(result["Name"], Integer(result["Id"]), Integer(result["Mat_Nr"]))
  end
  result = @db_base.query_person_by_id(id).next
  if (result != nil)
    return Person::Person.new(result["Name"], Integer(result["Id"]))
  end
  nil
end