Class: StudentListDBAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/source/Students_list_DB.rb

Instance Method Summary collapse

Constructor Details

#initializeStudentListDBAdapter

Returns a new instance of StudentListDBAdapter.



6
7
8
# File 'lib/source/Students_list_DB.rb', line 6

def initialize
  self.client = DBUniversity.instance
end

Instance Method Details

#add_student(student) ⇒ Object



41
42
43
44
# File 'lib/source/Students_list_DB.rb', line 41

def add_student(student)
  st = client.prepare_exec('insert into students (last_name, first_name, paternal_name, phone,
                        telegram, email, git) VALUES (?, ?, ?, ?, ?, ?, ?)',*student_attr(student))
end

#count_studentObject



55
56
57
# File 'lib/source/Students_list_DB.rb', line 55

def count_student
  client.query('SELECT COUNT(id) FROM students').next[0]
end

#delete_student(id_student) ⇒ Object



51
52
53
# File 'lib/source/Students_list_DB.rb', line 51

def delete_student(id_student)
  client.prepare_exec('DELETE FROM students WHERE id = ?', id_student)
end

#get_k_n_student_short_list(k, n, data_list = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/source/Students_list_DB.rb', line 28

def get_k_n_student_short_list(k,n,data_list=nil )
  offset = (k - 1) * n
  students = client.prepare_exec('SELECT * FROM students LIMIT ?, ?', offset, n)
  slice = students.map { |h|
    h = h.transform_keys(&:to_sym)
    StudentShort.new(Student.into_hash(h))
  }
  return DataListStudentShort.new(slice) if data_list.nil?

  data_list.replace_objects(slice)
  data_list
end

#into_hash(arr) ⇒ Object

last_name, first_name, paternal_name, phone, telegram, email, git



11
12
13
14
15
16
17
18
19
# File 'lib/source/Students_list_DB.rb', line 11

def into_hash(arr)
  attrs = {}
  i=0
  %i[id last_name first_name paternal_name phone telegram email git].each do |attr|
    attrs[attr] = arr[i] unless arr[i].nil?
    i=i+1
  end
  attrs
end

#replace_student(id_student, student) ⇒ Object



46
47
48
49
# File 'lib/source/Students_list_DB.rb', line 46

def replace_student(id_student, student)
  st ='UPDATE students SET last_name=?, first_name=?, paternal_name=?, phone=?, telegram=?, email=?, git=? WHERE id=?'
  client.prepare_exec(st,*student_attr(student), id_student)
end

#student_by_id(id_student) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/source/Students_list_DB.rb', line 20

def student_by_id(id_student)
  hash = client.prepare_exec('SELECT * FROM students WHERE id = ?', id_student).first
  hash=into_hash(hash)
  return nil if hash.nil?

  Student.new(**hash)
end