 CREATE OR REPLACE FUNCTION bq_find(i_coll text, i_json_query json, i_skip integer DEFAULT 0, i_limit integer DEFAULT null, i_sort json DEFAULT null)
 RETURNS table(bq_jdoc json) AS $$
 DECLARE
-  q text = format('select bq_jdoc::json from %I where 1=1', i_coll);
+  q text = format('select bq_jdoc::json from %I ', i_coll);
+  mq text;
+  sq text[];
+  s text;
 BEGIN
-IF (SELECT bq_collection_exists(i_coll))
-THEN
+  IF (SELECT bq_collection_exists(i_coll))
+  THEN
     IF json_typeof(i_sort) != 'array'
     THEN
       RAISE EXCEPTION

      USING HINT = 'The i_sort parameter to bq_find should be a json array';
    END IF;
    -- query match
    q := q || format(' and bq_jdoc @> (%s)::jsonb ',
                     quote_literal(i_json_query));
    -- sort
    IF (i_sort IS NOT NULL)
    THEN

    -- final query
    q := q || format(' offset %s ', i_skip);
    RETURN QUERY EXECUTE q;
  END IF;
END
$$ LANGUAGE plpgsql;

