Class: StudentListDBAdapter
- Inherits:
-
Object
- Object
- StudentListDBAdapter
- Defined in:
- lib/source/database/student_list_db.rb
Instance Method Summary collapse
-
#add_student(student) ⇒ Object
def add_student(student) st = client.prepare_exec('insert into students (first_name, middle_name, surname, phone_number, telegram, mail, git) VALUES (?, ?, ?, ?, ?, ?, ?)') st.execute(*student_attr(student)) self.client.query('SELECT seq from sqlite_sequence where name = "students"').first.first end.
- #count_student ⇒ Object
- #delete_student(id_student) ⇒ Object
- #get_k_n_student_short_list(k, n, data_list) ⇒ Object
-
#initialize ⇒ StudentListDBAdapter
constructor
A new instance of StudentListDBAdapter.
- #into_hash(arr) ⇒ Object
- #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/database/student_list_db.rb', line 6 def initialize self.client = DBUniversity.instance end |
Instance Method Details
#add_student(student) ⇒ Object
def add_student(student) st = client.prepare_exec('insert into students (first_name, middle_name, surname, phone_number, telegram, mail, git) VALUES (?, ?, ?, ?, ?, ?, ?)') st.execute(*student_attr(student)) self.client.query('SELECT seq from sqlite_sequence where name = "students"').first.first end
36 37 38 |
# File 'lib/source/database/student_list_db.rb', line 36 def add_student(student) client.prepare_exec('insert into students (first_name, middle_name, surname, phone_number, telegram, mail, git) VALUES (?, ?, ?, ?, ?, ?, ?)', *student_attr(student)).first end |
#count_student ⇒ Object
48 49 50 |
# File 'lib/source/database/student_list_db.rb', line 48 def count_student client.query('SELECT COUNT(id) FROM students').next[0] end |
#delete_student(id_student) ⇒ Object
39 40 41 |
# File 'lib/source/database/student_list_db.rb', line 39 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) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/source/database/student_list_db.rb', line 52 def get_k_n_student_short_list(k,n,data_list) offset = (k - 1) * n students = client.prepare_exec('SELECT * FROM students LIMIT ?, ?', offset, n) puts students.to_s slice = students.map { |h| h = h.transform_keys(&:to_sym) StudentShort.new(Student.new(**h)) } puts slice DataListStudentShort.new(slice) data_list.replace_objects(slice) data_list end |
#into_hash(arr) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/source/database/student_list_db.rb', line 10 def into_hash(arr) attrs = {} i=0 %i[id first_name middle_name surname phone_number telegram mail git].each do |attr| attrs[attr] = arr[i] unless arr[i].nil? i=i+1 end attrs end |
#replace_student(id_student, student) ⇒ Object
43 44 45 46 |
# File 'lib/source/database/student_list_db.rb', line 43 def replace_student(id_student, student) st ='UPDATE students SET first_name=?, middle_name=?, surname=?, phone_number=?, telegram=?, mail=?, 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 27 28 |
# File 'lib/source/database/student_list_db.rb', line 20 def student_by_id(id_student) id_student=id_student.first.to_i 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 |