Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 3.02 KB | None | 0 0
  1. DECLARE
  2.  
  3.   l_query VARCHAR2(4000);
  4.   l_lat double precision;
  5.   l_lng double precision;
  6.   l_orientation CHAR(1) := v('P5_ORIENTATION');
  7.   l_distance_sort CHAR(3) := v('P5_DISTANCE_SORT');
  8.  
  9.  
  10. BEGIN
  11.  
  12.   --Sync index before retrieving rows to ensure up-to-date records every time
  13.   ctx_ddl.sync_index('properties_addr1_ctx_idx', '2M');
  14.  
  15.   --Used subquery here to get my distance sorting feature working correctly
  16.   l_query:=
  17.    'select *
  18.   from (
  19.   select
  20.        rowid,
  21.        p_id,
  22.        p_i_id,
  23.        a_id,
  24.        address_line_1,
  25.        address_line_2,
  26.        city,
  27.        postcode,
  28.        geom_location,
  29.        case when v(''P5_POSTCODE_SEARCH'') is null then ''N/A''
  30.        else ''value''
  31.        end as distance,    
  32.        notes,
  33.        image_filename,
  34.        property_image,
  35.        dbms_lob.getlength("PROPERTY_IMAGE_THUMBNAIL") "PROPERTY_IMAGE_THUMBNAIL",
  36.        agency_name,
  37.        contact_email,
  38.        contact_phone
  39.    from   vw_listings_overview ) l';
  40.  
  41.   IF v('P5_ADDRESS_SEARCH') IS NOT NULL THEN
  42.       l_query := l_query||' '||'
  43.      where (CONTAINS(address_line_1, ''$'|| v('P5_ADDRESS_SEARCH') ||''') > 0)';
  44.   ELSE
  45.       l_query := l_query||' '||'
  46.      where
  47.        (instr(upper(''address_line_1''),upper(nvl(:P5_ADDRESS_SEARCH, ''address_line_1''))) > 0)';
  48.   END IF;
  49.          
  50.   IF v('P5_POSTCODE_SEARCH') IS NOT NULL THEN
  51.  
  52.       brian.GEOCODE_GM_XML (:P5_POSTCODE_SEARCH, l_lat, l_lng);
  53.       DBMS_OUTPUT.PUT_LINE('Latitude ='||l_lat);
  54.       DBMS_OUTPUT.PUT_LINE('Longitude ='||l_lng);    
  55.                          
  56.       l_query := REPLACE(l_query, '''value''', 'cast(round(sdo_geom.sdo_distance(
  57.                         sdo_geometry(2001, 8307,
  58.                         sdo_point_type('||l_lng||','||l_lat||', null), null, null),
  59.                         geom_location, 0.005, ''unit=mile''), ''2'') as varchar2(10))');
  60.      
  61.   END IF;
  62.  
  63.   --Radio group item logic
  64.   IF l_orientation <> 'A' THEN
  65.       IF l_orientation = 'L' THEN
  66.           l_query :=
  67.               l_query ||' and l.property_image.getWidth() > l.property_image.getHeight() ';
  68.      
  69.       ELSIF l_orientation = 'P' THEN
  70.           l_query :=
  71.               l_query ||' and l.property_image.getWidth() < l.property_image.getHeight() ';
  72.       END IF;
  73.   END IF;
  74.  
  75.   IF l_distance_sort <> 'A' THEN
  76.       IF v('P5_POSTCODE_SEARCH') IS NOT NULL THEN
  77.           l_query :=
  78.               l_query ||' and distance < ' || l_distance_sort || ' ';
  79.       END IF;
  80.   END IF;
  81.  
  82.   --Append 'order by' clause onto query if user has searched via a postcode ->
  83.   IF v('P5_POSTCODE_SEARCH') IS NOT NULL THEN
  84.        l_query := l_query||' '||'order by distance asc nulls last;';
  85.   END IF;
  86.  
  87.   -- test and debug only
  88.     htp.p( l_query );
  89.     htp.p( '<br>' );
  90.     htp.p( v('P5_ADDRESS_SEARCH') );
  91.     htp.p( '<br>' );
  92.     htp.p('Orientation: '||v('P5_ORIENTATION') );
  93.     htp.p( '<br>' );
  94.     htp.p('Distance Sort: '||v('P5_DISTANCE_SORT') );
  95.     htp.p( '<br><br>' );
  96.  
  97.   RETURN l_query;
  98.  
  99. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement