Class: ShnaiderCode::StudentsDB

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

Instance Method Summary collapse

Constructor Details

#initializeStudentsDB

Returns a new instance of StudentsDB.



8
9
10
11
12
13
14
15
# File 'lib/source/database/students_db.rb', line 8

def initialize()
    self.db_connection = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "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(File.dirname(__FILE__) + '/scripts/create_table.sql'))
    self.fill_data()
end

Instance Method Details

#add_student(student_json) ⇒ Object



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

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



66
67
68
# File 'lib/source/database/students_db.rb', line 66

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

#remove_by_id(id) ⇒ Object



25
26
27
# File 'lib/source/database/students_db.rb', line 25

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

#replace_by_id(id, student_json) ⇒ Object



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

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



21
22
23
# File 'lib/source/database/students_db.rb', line 21

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

#select_students(from, to) ⇒ Object



62
63
64
# File 'lib/source/database/students_db.rb', line 62

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