Class: StudentsDB

Inherits:
Object
  • Object
show all
Defined in:
lib/model_gem_source/database/students_db.rb

Instance Method Summary collapse

Constructor Details

#initializeStudentsDB

Returns a new instance of StudentsDB.



6
7
8
9
10
11
12
13
# File 'lib/model_gem_source/database/students_db.rb', line 6

def initialize()
    self.db_connection = Mysql2::Client.new(:host => "localhost", :username => "root")
    self.db_connection.query('CREATE DATABASE IF NOT EXISTS my_db')
    self.db_connection.query('USE my_db')
    self.db_connection.query('DROP TABLE IF EXISTS student')
    self.db_connection.query(File.read('database/scripts/create_table.sql'))
    self.fill_data()
end

Instance Method Details

#add_student(student_json) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/model_gem_source/database/students_db.rb', line 44

def add_student(student_json)
    puts student_json[:lastname]
    db_connection.query("""
    INSERT INTO student (lastname, firstname, patronymic, git, phone, email, telegram) VALUES
    ROW(
        \"#{attr_or_null(student_json[:lastname])}\",
        \"#{attr_or_null(student_json[:firstname])}\",
        \"#{attr_or_null(student_json[:patronymic])}\", 
        \"#{attr_or_null(student_json[:git])}\", 
        \"#{attr_or_null(student_json[:phone])}\",
        \"#{attr_or_null(student_json[:email])}\",
        \"#{attr_or_null(student_json[:telegram])}\"
    )
    """)
end

#countObject



64
65
66
# File 'lib/model_gem_source/database/students_db.rb', line 64

def count()
    db_connection.query("SELECT * FROM student").count
end

#remove_by_id(id) ⇒ Object



23
24
25
# File 'lib/model_gem_source/database/students_db.rb', line 23

def remove_by_id(id)
    db_connection.query("DELETE FROM student WHERE id = #{id}")
end

#replace_by_id(id, student_json) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/model_gem_source/database/students_db.rb', line 27

def replace_by_id(id, student_json)
    db_connection.query("DELETE FROM student WHERE id = #{id}")
    db_connection.query("""
        INSERT INTO student (id, lastname, firstname, patronymic, git, phone, email, telegram) VALUES
        ROW(
            \"#{id}\",
            \"#{attr_or_null(student_json[:lastname])}\",
            \"#{attr_or_null(student_json[:firstname])}\",
            \"#{attr_or_null(student_json[:patronymic])}\", 
            \"#{attr_or_null(student_json[:git])}\", 
            \"#{attr_or_null(student_json[:phone])}\",
            \"#{attr_or_null(student_json[:email])}\",
            \"#{attr_or_null(student_json[:telegram])}\"
        )
        """)
end

#select_by_id(id) ⇒ Object



19
20
21
# File 'lib/model_gem_source/database/students_db.rb', line 19

def select_by_id(id)
    db_connection.query("SELECT * FROM student WHERE id = #{id}").map { |x| x }[0]
end

#select_students(from, to) ⇒ Object



60
61
62
# File 'lib/model_gem_source/database/students_db.rb', line 60

def select_students(from, to)
    db_connection.query("SELECT * FROM student LIMIT #{from}, #{to}")
end