%w(sequel common/password).each { |lib| require lib }
  
DB = Sequel.sqlite('data/base.db')

USER = <<-END
  set_schema do
    primary_key :id
    varchar :name
    varchar :password
  end
  
  after_create do
    set(:password => Password.update(self.password))
  end
  
  def valid(pw)
    Password.check(pw, self.password)
  end
END

class Students < Sequel::Model
  eval USER
  
  def is_subscribed?(exam_id)
    Grades.filter(:student_id => self.id, :exam_id => exam_id).count != 0
  end
  
end

class Professors < Sequel::Model
  eval USER
end

class Exams < Sequel::Model
  set_schema do
    primary_key :id
    integer :prof_id
    varchar :description
    datetime :date
  end
end

class Grades < Sequel::Model
  set_schema do
    integer :student_id
    integer :exam_id
    integer :grade
    primary_key :student_id, :exam_id
  end
end

Students.create_table unless Students.table_exists?
Professors.create_table unless Professors.table_exists?
Exams.create_table unless Exams.table_exists?
Grades.create_table unless Grades.table_exists?