61 lines
1.6 KiB
Ruby
Executable file
61 lines
1.6 KiB
Ruby
Executable file
#!/usr/bin/env ruby
|
|
# frozen_string_literal: true
|
|
|
|
require 'connection_pool'
|
|
require 'pg'
|
|
require 'sinatra'
|
|
|
|
$DB_POOL = ConnectionPool.new size: 5, timeout: 5 do
|
|
PG.connect(
|
|
host: 'pg.causa-arcana.com',
|
|
dbname: 'leqsikoni',
|
|
user: 'leqsikoni',
|
|
password: 'ggeucene3ou7mqh2upehhm52tfp5bkcj',
|
|
).tap do |conn|
|
|
conn.type_map_for_results = PG::BasicTypeMapForResults.new conn
|
|
end
|
|
end
|
|
|
|
get '/examples' do
|
|
$DB_POOL.with do |db_conn|
|
|
ids =
|
|
case params[:ids]
|
|
when Array then params[:ids].map { |id| Integer id }
|
|
when String then params[:ids].split(',').map { |id| Integer id }
|
|
else raise 'Invalid param "ids"'
|
|
end
|
|
|
|
examples = db_conn.exec_params(
|
|
(
|
|
<<~SQL
|
|
SELECT
|
|
array_agg(foo.value ORDER BY foo.language_id DESC)::text[] AS values
|
|
FROM example_texts foo
|
|
INNER JOIN (
|
|
SELECT example_id, language_id, MIN(index) as min_index
|
|
FROM example_texts
|
|
WHERE
|
|
example_id = ANY($1)
|
|
AND
|
|
(language_id = 5 OR language_id = 2)
|
|
GROUP BY example_id, language_id
|
|
) bar
|
|
ON
|
|
foo.example_id = bar.example_id AND
|
|
foo.language_id = bar.language_id AND
|
|
foo.index = bar.min_index
|
|
GROUP BY foo.example_id
|
|
SQL
|
|
),
|
|
[PG::TextEncoder::Array.new.encode(ids)],
|
|
).map { |row| row['values'] }
|
|
|
|
<<~HTML
|
|
<ul>
|
|
#{examples.map do |example|
|
|
"<li>#{example[0]} — #{example[1]}</li>"
|
|
end.join}
|
|
</ul>
|
|
HTML
|
|
end
|
|
end
|