Class: StudentDB

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStudentDB

Returns a new instance of StudentDB.



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

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('F:/Other/RLabs/dataset/RubyLabsReserved/Gem_fuss/lib/source/create_table.sql'))
  self.insert_data
end

Instance Attribute Details

#db_connectionObject

Returns the value of attribute db_connection.



7
8
9
# File 'lib/source/students_db.rb', line 7

def db_connection
  @db_connection
end

Instance Method Details

#add_student(student_data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/source/students_db.rb', line 38

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



71
72
73
74
# File 'lib/source/students_db.rb', line 71

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

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



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/source/students_db.rb', line 59

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



18
19
20
# File 'lib/source/students_db.rb', line 18

def insert_data
  db_connection.query(File.read('F:/Other/RLabs/dataset/RubyLabsReserved/Gem_fuss/lib/source/insert_data.sql'))
end

#parseNil(attr) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/source/students_db.rb', line 30

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

#remove_by_id(id) ⇒ Object



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

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

#replace_by_id(id, student_data) ⇒ Object



54
55
56
57
# File 'lib/source/students_db.rb', line 54

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



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

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