Class: Cellect::Server::Adapters::Postgres
- Inherits:
-
Default
- Object
- Default
- Cellect::Server::Adapters::Postgres
show all
- Defined in:
- lib/cellect/server/adapters/postgres.rb
Instance Method Summary
collapse
Methods inherited from Default
#load_workflow, #load_workflows, #workflow_for
Constructor Details
Returns a new instance of Postgres.
8
9
10
11
12
|
# File 'lib/cellect/server/adapters/postgres.rb', line 8
def initialize
@pg ||= ConnectionPool.new(size: ENV.fetch('PG_POOL_SIZE', 25).to_i) do
PG.connect connection_options
end
end
|
Instance Method Details
#connection_options ⇒ Object
64
65
66
67
68
69
70
71
72
|
# File 'lib/cellect/server/adapters/postgres.rb', line 64
def connection_options
{
host: ENV.fetch('PG_HOST', '127.0.0.1'),
port: ENV.fetch('PG_PORT', '5432'),
dbname: ENV.fetch('PG_DB', 'cellect'),
user: ENV.fetch('PG_USER', 'cellect'),
password: ENV.fetch('PG_PASS', '')
}
end
|
#load_data_for(workflow_name) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/cellect/server/adapters/postgres.rb', line 28
def load_data_for(workflow_name)
with_pg do |pg|
statement = " SELECT sms.id as id, sms.priority as priority, sms.subject_set_id as group_id \n FROM workflows w\n JOIN subject_sets_workflows ssw ON (ssw.workflow_id = w.id) \n JOIN subject_sets ss ON (ss.id = ssw.subject_set_id) \n JOIN set_member_subjects sms ON (s.id = sms.subject_set_id)\n WHERE w.id = \#{ workflow_name } \n SQL\n pg.exec(statement).collect do |row|\n {\n 'id' => row['id'].to_i,\n 'priority' => row['priority'].to_f,\n 'group_id' => row['group_id'].to_i\n }\n end\n end\nend\n"
|
#load_user(workflow_name, id) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/cellect/server/adapters/postgres.rb', line 48
def load_user(workflow_name, id)
with_pg do |pg|
statement = " SELECT subject_ids FROM user_seen_subjects\n WHERE user_id = \#{ id } AND workflow_id = \#{ workflow_name }\n SQL\n pg.exec(statement).collect do |row|\n row['subject_ids'].map(&:to_i)\n end\n end\nend\n"
|
#with_pg ⇒ Object
60
61
62
|
# File 'lib/cellect/server/adapters/postgres.rb', line 60
def with_pg
@pg.with{ |pg| yield pg }
end
|
#workflow_list ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/cellect/server/adapters/postgres.rb', line 14
def workflow_list
with_pg do |pg|
pg.exec('SELECT * FROM workflows').collect do |row|
{
'id' => row['id'].to_i,
'name' => row['id'],
'prioritized' => row['prioritized'] == 't',
'pairwise' => row['pairwise'] == 't',
'grouped' => row['grouped'] == 't'
}
end
end
end
|