Class: Kaede::Database

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/kaede/database.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Database

Returns a new instance of Database.



11
12
13
# File 'lib/kaede/database.rb', line 11

def initialize(path)
  @db = Sequel.connect(path)
end

Instance Method Details

#add_channel(channel) ⇒ Object



100
101
102
# File 'lib/kaede/database.rb', line 100

def add_channel(channel)
  @db.from(:channels).insert(name: channel.name, for_recorder: channel.for_recorder, for_syoboi: channel.for_syoboi)
end

#add_tracking_title(tid) ⇒ Object



125
126
127
# File 'lib/kaede/database.rb', line 125

def add_tracking_title(tid)
  @db.from(:tracking_titles).insert(tid: tid, created_at: Time.now)
end

#delete_job(pid) ⇒ Object



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

def delete_job(pid)
  @db.from(:jobs).where(pid: pid).delete
end

#get_channelsObject



94
95
96
97
98
# File 'lib/kaede/database.rb', line 94

def get_channels
  @db.from(:channels).map do |row|
    Channel.new(row[:id], row[:name], row[:for_recorder], row[:for_syoboi])
  end
end

#get_jobsObject



46
47
48
# File 'lib/kaede/database.rb', line 46

def get_jobs
  @db.from(:jobs).select(:pid, :enqueued_at).where(finished_at: nil).where(Sequel.qualify(:jobs, :enqueued_at) >= Time.now).order(:enqueued_at).to_a
end

#get_program(pid) ⇒ Object



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

def get_program(pid)
  get_programs([pid])[pid]
end

#get_programs(pids) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kaede/database.rb', line 68

def get_programs(pids)
  programs = {}
  @db.from(:programs).inner_join(:channels, [[channel_id: :id]]).where(pid: pids).each do |row|
    program = Program.new(
      row[:pid],
      row[:tid],
      row[:start_time],
      row[:end_time],
      row[:name],
      row[:for_syoboi],
      row[:for_recorder],
      row[:count],
      row[:start_offset],
      row[:subtitle],
      row[:title],
      row[:comment],
    )
    programs[program.pid] = program
  end
  programs
end

#get_tracking_titlesObject



129
130
131
132
133
# File 'lib/kaede/database.rb', line 129

def get_tracking_titles
  @db.from(:tracking_titles).select(:tid).map do |row|
    row[:tid]
  end
end

#mark_finished(pid) ⇒ Object



90
91
92
# File 'lib/kaede/database.rb', line 90

def mark_finished(pid)
  @db.from(:jobs).where(pid: pid).update(finished_at: Time.now)
end

#prepare_tablesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kaede/database.rb', line 15

def prepare_tables
  @db.create_table?(:channels) do
    primary_key :id
    String :name, size: 255, null: false, unique: true
    Integer :for_recorder, null: false, unique: true
    Integer :for_syoboi, null: false, unique: true
  end
  @db.create_table?(:programs) do
    primary_key :pid
    Integer :tid, null: false
    DateTime :start_time, null: false
    DateTime :end_time, null: false
    foreign_key :channel_id, :channels
    String :count, size: 16, null: false
    Integer :start_offset, null: false
    String :subtitle, size: 255
    String :title, size: 255
    String :comment, size: 255
  end
  @db.create_table?(:jobs) do
    foreign_key :pid, :programs, primary_key: true
    DateTime :enqueued_at, null: false
    DateTime :finished_at
    DateTime :created_at, null: false
  end
  @db.create_table?(:tracking_titles) do
    Integer :tid, primary_key: true
    DateTime :created_at, null: false
  end
end

#update_job(pid, enqueued_at) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/kaede/database.rb', line 50

def update_job(pid, enqueued_at)
  @db.transaction do
    if @db.from(:jobs).where(pid: pid).select(1).first
      @db.from(:jobs).where(pid: pid).update(enqueued_at: enqueued_at)
    else
      @db.from(:jobs).insert(pid: pid, enqueued_at: enqueued_at, created_at: Time.now)
    end
  end
end

#update_program(program, channel) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/kaede/database.rb', line 104

def update_program(program, channel)
  attributes = {
    tid: program.tid,
    start_time: program.start_time,
    end_time: program.end_time,
    channel_id: channel.id,
    count: program.count,
    start_offset: program.start_offset,
    subtitle: program.subtitle,
    title: program.title,
    comment: program.comment,
  }
  @db.transaction do
    if @db.from(:programs).where(pid: program.pid).select(1).first
      @db.from(:programs).where(pid: program.pid).update(attributes)
    else
      @db.from(:programs).insert(attributes.merge(pid: program.pid))
    end
  end
end