Class: StudentDB

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStudentDB

Returns a new instance of StudentDB.



12
13
14
15
16
17
18
19
# File 'lib/database/students_db.rb', line 12

def initialize
  self.db_connection = Mysql2::Client.new(:host => "localhost", :username => "dimas", :password => "qwe123")
  self.db_connection.query('CREATE DATABASE IF NOT EXISTS stud_db')
  self.db_connection.query('USE stud_db')
  self.db_connection.query('DROP TABLE IF EXISTS student')
  self.db_connection.query(File.read('C:/Users/Dmitry/RubymineProjects/RubyLabs/mvcStudentXD/lib/database/scripts/create_table.sql'))
  self.insert_data
end

Instance Attribute Details

#db_connectionObject

Returns the value of attribute db_connection.



10
11
12
# File 'lib/database/students_db.rb', line 10

def db_connection
  @db_connection
end

Instance Method Details

#add_student(student_data) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/database/students_db.rb', line 41

def add_student(student_data)

  db_connection.query("""
      INSERT INTO student (last_name, first_name, parental_name, git, phone, email, telegram) VALUES
      ROW(
          \"#{parseNil(student_data[:last_name])}\",
          \"#{parseNil(student_data[:first_name])}\",
          \"#{parseNil(student_data[:parental_name])}\",
          \"#{parseNil(student_data[:git])}\",
          \"#{parseNil(student_data[:phone])}\",
          \"#{parseNil(student_data[:email])}\",
          \"#{parseNil(student_data[:telegram])}\"
      )
      """)
end

#countObject



74
75
76
77
# File 'lib/database/students_db.rb', line 74

def count
  result = db_connection.query("SELECT count(*) FROM student")
  result.first.values.first
end

#get_students_pag(k, n, existing_data = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/database/students_db.rb', line 62

def get_students_pag(k, n, existing_data = nil)
  rows = db_connection.query("SELECT * FROM student ORDER BY id LIMIT #{n} OFFSET #{(k-1)*n}")
  data_list = DataListStudentShort.new
  rows.each do |row|
    row_sym = row.transform_keys{|key| key.to_sym}
    data_list.append(StudentShort.from_student_class(Student.from_hash(row_sym)))
  end
  return data_list if existing_data.nil?
  existing_data.replace_objects(data_list.objects)
  existing_data
end

#insert_dataObject



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

def insert_data
  db_connection.query(File.read('C:/Users/Dmitry/RubymineProjects/RubyLabs/mvcStudentXD/lib/database/scripts/insert_data.sql'))
end

#parseNil(attr) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/database/students_db.rb', line 33

def parseNil(attr)
  if attr == nil
    "NULL"
  else
    attr
  end
end

#remove_by_id(id) ⇒ Object



29
30
31
# File 'lib/database/students_db.rb', line 29

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

#replace_by_id(id, student_data) ⇒ Object



57
58
59
60
# File 'lib/database/students_db.rb', line 57

def replace_by_id(id, student_data)
  self.remove_by_id(id)
  self.add_student(student_data.to_hash)
end

#select_by_id(id) ⇒ Object



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

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