-----------------------------------------------------------------
-- PL/SQL: DBMS_SQL - Query Example continued  (Section 4.9.2)  -
--         This package contains procedures that drive          -
--         procedures in p20.sql                                -
-- Chapter 4; Oracle Programming -- A Primer                    -
--            by R. Sunderraman                                 -
-----------------------------------------------------------------

CREATE OR REPLACE PACKAGE dsql_driver AS
procedure drive_get_columns(startch IN char);
procedure drive_get_query_results(query_string IN varchar2,
                                  ncols IN number,
                                  column_types IN dsql.string9_table);
END dsql_driver;
/

CREATE OR REPLACE PACKAGE BODY dsql_driver AS
procedure drive_get_columns(startch IN char) AS
  dd_table dsql.dd_table_type;
  i binary_integer;
  n number;
  prev varchar2(30);
  newentry boolean;
BEGIN
--  i := 0;
--  n := 0;
  dsql.get_columns(startch,dd_table,n);
  dbms_output.put_line('N =  ' || n);
  
  dbms_output.put_line('Tables Starting with ' || startch);
  dbms_output.put_line('------------------------');
  prev := '';
  newentry := false;
  for i in 1 .. dd_table.count loop
    if (i = 1) then
      dbms_output.put(dd_table(i).table_name || '(');
      prev := dd_table(i).table_name;
    elsif (prev != dd_table(i).table_name) then
      dbms_output.put_line(');');
      dbms_output.put(dd_table(i).table_name || '(');
      prev := dd_table(i).table_name;
      newentry := false;
    end if;
    if (newentry) then
      dbms_output.put(', ');
    end if;
    newentry := true;
    dbms_output.put(dd_table(i).column_name || ':');
    dbms_output.put(dd_table(i).data_type);
  end loop;  
  dbms_output.put_line(');');
END;

procedure drive_get_query_results(query_string IN varchar2,
                                  ncols IN number,
                                  column_types IN dsql.string9_table) AS
result dsql.string1024_table;
nrows number;
i binary_integer;

BEGIN
  dsql.get_query_results(query_string,ncols,column_types,
                         result,nrows);
  for i in 1 .. nrows loop
    dbms_output.put_line(result(i));
  end loop;
END;

END dsql_driver;
/
show errors
