diff options
author | Jochen Topf <jochen@topf.org> | 2011-10-13 15:33:28 +0200 |
---|---|---|
committer | Jochen Topf <jochen@topf.org> | 2011-10-13 15:33:28 +0200 |
commit | 37ca149ae63128c6adabfc428a8d9b713bab7d69 (patch) | |
tree | b6b0cf55640c622d7f9ca23c37e960b4343e92c7 /web/lib | |
parent | a4c827ca515ae9cda7d1f3e007e99f7c268adcfa (diff) | |
download | taginfo-37ca149ae63128c6adabfc428a8d9b713bab7d69.tar taginfo-37ca149ae63128c6adabfc428a8d9b713bab7d69.tar.gz |
Make combinations table searchable.
Fixes https://github.com/joto/taginfo/issues/4
Diffstat (limited to 'web/lib')
-rw-r--r-- | web/lib/api/db.rb | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/web/lib/api/db.rb b/web/lib/api/db.rb index 82cf48c..3a3de71 100644 --- a/web/lib/api/db.rb +++ b/web/lib/api/db.rb @@ -193,7 +193,7 @@ class Taginfo < Sinatra::Base end api(2, 'db/keys/distribution', { - :description => 'Get map with distribution of this key in the database.', + :description => 'Get map with distribution of this key in the database (nodes only).', :parameters => { :key => 'Tag key (required).' }, :result => 'PNG image.', :example => { :key => 'amenity' }, @@ -311,7 +311,10 @@ class Taginfo < Sinatra::Base api(2, 'db/keys/keys', { :description => 'Find keys that are used together with a given key.', - :parameters => { :key => 'Tag key (required).' }, + :parameters => { + :key => 'Tag key (required).', + :query => 'Only show results where the other_key matches this query (substring match, optional).' + }, :paging => :optional, :filter => { :all => { :doc => 'No filter.' }, @@ -338,8 +341,8 @@ class Taginfo < Sinatra::Base params[:sortname] = ['from_fraction', 'together_count', 'other_key'] end - total = @db.count('db.keypairs'). - condition('key1 = ? OR key2 = ?', key, key). + cq = @db.count('db.keypairs') + total = (params[:query] != '' ? cq.condition("(key1 = ? AND key2 LIKE '%' || ? || '%') OR (key2 = ? AND key1 LIKE '%' || ? || '%')", key, params[:query], key, params[:query]) : cq.condition('key1 = ? OR key2 = ?', key, key)). condition("count_#{filter_type} > 0"). get_first_value().to_i @@ -347,8 +350,11 @@ class Taginfo < Sinatra::Base condition('key = ?', key). get_first_value() - res = @db.select("SELECT p.key1 AS other_key, p.count_#{filter_type} AS together_count, k.count_#{filter_type} AS other_count, CAST(p.count_#{filter_type} AS REAL) / k.count_#{filter_type} AS from_fraction FROM db.keypairs p, db.keys k WHERE p.key1=k.key AND p.key2=? AND p.count_#{filter_type} > 0 - UNION SELECT p.key2 AS other_key, p.count_#{filter_type} AS together_count, k.count_#{filter_type} AS other_count, CAST(p.count_#{filter_type} AS REAL) / k.count_#{filter_type} AS from_fraction FROM db.keypairs p, db.keys k WHERE p.key2=k.key AND p.key1=? AND p.count_#{filter_type} > 0", key, key). + res = (params[:query] != '' ? + @db.select("SELECT p.key1 AS other_key, p.count_#{filter_type} AS together_count, k.count_#{filter_type} AS other_count, CAST(p.count_#{filter_type} AS REAL) / k.count_#{filter_type} AS from_fraction FROM db.keypairs p, db.keys k WHERE p.key1=k.key AND p.key2=? AND (p.key1 LIKE '%' || ? || '%') AND p.count_#{filter_type} > 0 + UNION SELECT p.key2 AS other_key, p.count_#{filter_type} AS together_count, k.count_#{filter_type} AS other_count, CAST(p.count_#{filter_type} AS REAL) / k.count_#{filter_type} AS from_fraction FROM db.keypairs p, db.keys k WHERE p.key2=k.key AND p.key1=? AND (p.key2 LIKE '%' || ? || '%') AND p.count_#{filter_type} > 0", key, params[:query], key, params[:query]) : + @db.select("SELECT p.key1 AS other_key, p.count_#{filter_type} AS together_count, k.count_#{filter_type} AS other_count, CAST(p.count_#{filter_type} AS REAL) / k.count_#{filter_type} AS from_fraction FROM db.keypairs p, db.keys k WHERE p.key1=k.key AND p.key2=? AND p.count_#{filter_type} > 0 + UNION SELECT p.key2 AS other_key, p.count_#{filter_type} AS together_count, k.count_#{filter_type} AS other_count, CAST(p.count_#{filter_type} AS REAL) / k.count_#{filter_type} AS from_fraction FROM db.keypairs p, db.keys k WHERE p.key2=k.key AND p.key1=? AND p.count_#{filter_type} > 0", key, key)). order_by(params[:sortname], params[:sortorder]){ |o| o.together_count o.other_key |