Class: StudentListDBAdapter
- Inherits:
-
Object
- Object
- StudentListDBAdapter
- Defined in:
- lib/source/Students_list_DB.rb
Instance Method Summary collapse
- #add_student(student) ⇒ Object
- #count_student ⇒ Object
- #delete_student(id_student) ⇒ Object
- #get_k_n_student_short_list(k, n, data_list = nil) ⇒ Object
-
#initialize ⇒ StudentListDBAdapter
constructor
A new instance of StudentListDBAdapter.
-
#into_hash(arr) ⇒ Object
last_name, first_name, paternal_name, phone, telegram, email, git.
- #replace_student(id_student, student) ⇒ Object
- #student_by_id(id_student) ⇒ Object
Constructor Details
#initialize ⇒ StudentListDBAdapter
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_student ⇒ Object
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 |