Class: DBSourceAdapter
- Inherits:
-
Object
- Object
- DBSourceAdapter
- Defined in:
- lib/repositories/adapters/db_source_adapter.rb
Instance Method Summary collapse
- #add_student(student) ⇒ Object
-
#initialize ⇒ DBSourceAdapter
constructor
A new instance of DBSourceAdapter.
- #paginated_short_students(page, count, existing_data_list = nil) ⇒ Object
- #remove_student(student_id) ⇒ Object
- #replace_student(student_id, student) ⇒ Object
- #student_by_id(student_id) ⇒ Object
- #student_count ⇒ Object
Constructor Details
#initialize ⇒ DBSourceAdapter
Returns a new instance of DBSourceAdapter.
9 10 11 |
# File 'lib/repositories/adapters/db_source_adapter.rb', line 9 def initialize @db = DBDataSource.instance end |
Instance Method Details
#add_student(student) ⇒ Object
30 31 32 33 34 |
# File 'lib/repositories/adapters/db_source_adapter.rb', line 30 def add_student(student) template = 'INSERT INTO student(last_name, first_name, father_name, phone, telegram, email, git) VALUES (?, ?, ?, ?, ?, ?, ?)' @db.prepare_exec(template, *student_fields(student)) @db.query('SELECT LAST_INSERT_ID()').first.first[1] end |
#paginated_short_students(page, count, existing_data_list = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/repositories/adapters/db_source_adapter.rb', line 20 def paginated_short_students(page, count, existing_data_list = nil) offset = (page - 1) * count students = @db.prepare_exec('SELECT * FROM student LIMIT ?, ?', offset, count) slice = students.map { |h| StudentShort.from_student(Student.from_hash(h)) } return DataListStudentShort.new(slice) if existing_data_list.nil? existing_data_list.replace_objects(slice) existing_data_list end |
#remove_student(student_id) ⇒ Object
41 42 43 |
# File 'lib/repositories/adapters/db_source_adapter.rb', line 41 def remove_student(student_id) @db.prepare_exec('DELETE FROM student WHERE id = ?', student_id) end |
#replace_student(student_id, student) ⇒ Object
36 37 38 39 |
# File 'lib/repositories/adapters/db_source_adapter.rb', line 36 def replace_student(student_id, student) template = 'UPDATE student SET last_name=?, first_name=?, father_name=?, phone=?, telegram=?, email=?, git=? WHERE id=?' @db.prepare_exec(template, *student_fields(student), student_id) end |
#student_by_id(student_id) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/repositories/adapters/db_source_adapter.rb', line 13 def student_by_id(student_id) hash = @db.prepare_exec('SELECT * FROM student WHERE id = ?', student_id).first return nil if hash.nil? Student.from_hash(hash) end |
#student_count ⇒ Object
45 46 47 |
# File 'lib/repositories/adapters/db_source_adapter.rb', line 45 def student_count @db.query('SELECT COUNT(id) FROM student').first.first[1] end |