home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / template.z / Query.java < prev    next >
Text File  |  1996-12-09  |  14KB  |  427 lines

  1. /*
  2.  * Query.java
  3.  *
  4.  *
  5.  * Copyright 1996 Sybase, Inc. All rights reserved.
  6.  */
  7.  
  8. /**
  9.  * <P>The Query class provides an interface to a NetImpact Dynamo query.
  10.  *
  11.  * <P>The DBConnection class supports a CreateQuery() method, which is currently
  12.  * the only way to create a Query.
  13.  * (SQL tags in the document template are not yet available,
  14.  * until some design changes are made to Dynamo)
  15.  
  16.  * <P>The current document corresponds to the template that originated this request
  17.  *
  18.  * <P>Lifetime: can be created from DBConnection.createQuery()
  19.  * Becomes garbage when the servlet exits.
  20.  *
  21.  * <P>See SQL Anywhere documentation on NetImpact Dynamo for a description
  22.  * of the methods, but be aware that, in Java, the first character
  23.  * of a method name is always in lower case.
  24.  */
  25.  
  26. public class Query extends DynamoConnection 
  27. //*****************************************
  28. {
  29.     //-------------------------------------------------------------
  30.     // Constructor
  31.     //-------------------------------------------------------------
  32.  
  33.     /**
  34.      * Constructs a new Query object with the specified handle and name.
  35.      * Used from DynamoConnection.getQuery() to access query on the current template.
  36.      * @param handle handle to Dynamo client
  37.      * @param name   name of the query object
  38.      * @exception DynamoException If an error has occured 
  39.      */
  40.      
  41.     public Query(int handle, String name) throws DynamoException 
  42.     //**********************************************************
  43.     {
  44.         super(handle);
  45.         _name = name;
  46.         _qid = 0;
  47.         validate(true);        // link up, throw exception if no query exists
  48.     }
  49.     
  50.     /**
  51.      * Constructs a new Query object with the specified handle.
  52.      * Used from DBConnection.createQuery() to create an unnamed query object
  53.      * that will be associated with no particular query name
  54.      * @param handle handle to Dynamo client
  55.      * @param name   name of the query object
  56.      * @param counter number of query objects
  57.      * @exception DynamoException If an error has occured 
  58.      */
  59.      
  60.     public Query(int handle, String name, int counter) throws DynamoException
  61.     //***********************************************************************
  62.     {
  63.         super(handle);
  64.         _name = name + counter;
  65.         _qid = 0;
  66.         // no validation, that is done by DBConnection.createQuery
  67.     }
  68.  
  69.     //-------------------------------------------------------------
  70.     // Properties
  71.     //-------------------------------------------------------------
  72.  
  73.     /** 
  74.      * Return a boolean indicating if the query object is valid
  75.      * @return true if the query object is valid; false otherwise
  76.      * @exception DynamoException If an error has occured 
  77.      */
  78.      
  79.     public boolean getValid() throws DynamoException
  80.     //***********************************************
  81.     {
  82.         return validate(false);
  83.     }
  84.     
  85.     /** 
  86.      * Returns the name of this query object
  87.      */
  88.      
  89.     public String getName() 
  90.     //*********************
  91.     {
  92.         return _name;
  93.     }
  94.    
  95.     /**
  96.      * Returns the string name of the given column of the result set.
  97.      * Columns start at column 1 up to and including getColumnCount()
  98.      * @param colnum index of the column whose name is to be returned
  99.      * @exception DynamoException If an error has occured 
  100.      */
  101.      
  102.     public native String getColumnLabel(int colnum) throws DynamoException;
  103.     //********************************************************************
  104.     
  105.     /**
  106.      * Returns the number of columns in the result set
  107.      * @exception DynamoException If an error has occured 
  108.      */
  109.      
  110.     public int getColumnCount() throws DynamoException
  111.     //************************************************
  112.     {
  113.         return getIntQueryProperty("GetColumnCount");
  114.     }
  115.     
  116.     /**
  117.      * Returns a boolean indicating if result set is empty     
  118.      * @return true if the result is empty; false otherwise
  119.      * @exception DynamoException If an error has occured 
  120.      */
  121.      
  122.     public boolean getEmpty() throws DynamoException
  123.     //**********************************************
  124.     {
  125.         if (getIntQueryProperty("GetEmpty") == 0)
  126.             return true;
  127.         return false;
  128.     }
  129.     
  130.     /**
  131.      * Returns the number of rows in the result set     
  132.      * @exception DynamoException If an error has occured 
  133.      */
  134.      
  135.     public int getRowCount() throws DynamoException 
  136.     //*********************************************
  137.     {
  138.         return getIntQueryProperty("GetRowCount");
  139.     }
  140.  
  141.     /**
  142.      * Returns current ODBC error code of this query object
  143.      * @exception DynamoException If an error has occured 
  144.      */
  145.      
  146.     public int getErrorCode() throws DynamoException 
  147.     //**********************************************
  148.     {
  149.         return getIntQueryProperty("GetErrorCode");
  150.     }
  151.     
  152.     /**
  153.      * Returns ODBC error information for this query object
  154.      * @exception DynamoException If an error has occured 
  155.      */
  156.      
  157.     public String getErrorInfo() throws DynamoException 
  158.     //*************************************************
  159.     {
  160.         return getStringQueryProperty("GetErrorInfo");
  161.     }
  162.     
  163.     /**
  164.      * Returns ODBC state of query. See your ODBC driver manuals     
  165.      * @exception DynamoException If an error has occured 
  166.      */
  167.      
  168.     public int getState() throws DynamoException 
  169.     //******************************************
  170.     {
  171.         return getIntQueryProperty("GetState");
  172.     }
  173.     
  174.     
  175.     // The names and return values mirror JDBC, with the following exceptions:
  176.     //      - column retrieval based on column number only, not by column name
  177.     //      - no Date or Timestamp or Time support (lost by DynamoScript!)
  178.     //      - no iostream for LONGVARCHAR (support in our ODBC?)
  179.     //      - no byte[] return value (to be done)
  180.     // getObject()/getValue() return a Java object as follows:
  181.     //  null    -- NULL column
  182.     //  Boolean -- SQL Bit result
  183.     //  Int     -- Any of the SQL integer values
  184.     //  Double  -- SQL Float or Double
  185.     //  String  -- other SQL fields converted to String
  186.     
  187.     /**
  188.      * Returns the value in the current row of the specified 
  189.      * column as an Object
  190.      * @param column index of the column
  191.      * @exception DynamoException If an error has occured 
  192.      */
  193.      
  194.     public Object getObject(int column) throws DynamoException 
  195.     //********************************************************
  196.     {
  197.         return getQueryValue(column);
  198.     }
  199.     
  200.     /**
  201.      * Returns the value in the current row of the specified 
  202.      * column as an Object
  203.      * @param column index of the column
  204.      * @exception DynamoException If an error has occured 
  205.      */
  206.  
  207.     public Object getValue(int column) throws DynamoException 
  208.     //*******************************************************
  209.     {
  210.         return getQueryValue(column);
  211.     }
  212.     
  213.     
  214.     /**
  215.      * Returns the value in the current row of the specified 
  216.      * column as a String
  217.      * @param index of the column
  218.      * @exception DynamoException If an error has occured 
  219.      */
  220.      
  221.     public String getString(int column) throws DynamoException 
  222.     //********************************************************
  223.     {
  224.         Object o = getQueryValue(column);
  225.         if (o == null)
  226.             return "";
  227.         else if (o instanceof String)
  228.             return (String) o;
  229.         else
  230.             throw new DynamoException("Not a string");
  231.     }
  232.     
  233.     
  234.     /**
  235.      * Returns the value in the current row of the specified 
  236.      * column as a boolean
  237.      * @param index of the column
  238.      * @exception DynamoException If an error has occured 
  239.      */
  240.      
  241.     public boolean getBoolean(int column) throws DynamoException 
  242.     //**********************************************************
  243.     {
  244.         Object o = getQueryValue(column);
  245.         if (o == null)
  246.             return false;
  247.         else if (o instanceof Boolean)
  248.             return ((Boolean) o).booleanValue();
  249.         else
  250.             throw new DynamoException("Not a boolean");
  251.     }
  252.     
  253.     /**
  254.      * Returns the value in the current row of the specified 
  255.      * column as a int
  256.      * @param index of the column
  257.      * @exception DynamoException If an error has occured 
  258.      */
  259.      
  260.     public int getInt(int column) throws DynamoException 
  261.     //**************************************************
  262.     {
  263.         Object o = getQueryValue(column);
  264.         if (o == null)
  265.             return 0;
  266.         else if (o instanceof Integer)
  267.             return ((Integer) o).intValue();
  268.         else
  269.             throw new DynamoException("Not an integer");
  270.     }
  271.  
  272.     /**
  273.      * Returns the value in the current row of the specified 
  274.      * column as a double
  275.      * @param index of the column
  276.      * @exception DynamoException If an error has occured 
  277.      */
  278.      
  279.     public double getDouble(int column) throws DynamoException 
  280.     //********************************************************
  281.     {
  282.         Object o = getQueryValue(column);
  283.         if (o == null)
  284.             return 0.0;
  285.         else if (o instanceof Double)
  286.             return ((Double) o).doubleValue();
  287.         else
  288.             throw new DynamoException("Not a string");
  289.     }
  290.  
  291.  
  292.     //-------------------------------------------------------------
  293.     // Public Methods
  294.     //-------------------------------------------------------------
  295.     
  296.     /** 
  297.      * Re-execute the SQL query (possibly changed with setSQL)
  298.      * Should be followed by a moveFirst() to reposition the new result set.
  299.      * @return true for success; false otherwise
  300.      * @exception DynamoException If an error has occured 
  301.      */
  302.      
  303.     public native boolean refresh() throws DynamoException;
  304.     //****************************************************
  305.  
  306.  
  307.     /**
  308.      * Updates the select statement for a query.
  309.      * @param sqlStatement new SQL statement
  310.      * @return true for success; false otherwise
  311.      * @exception DynamoException If an error has occured 
  312.      */
  313.      
  314.     public native boolean setSQL(String sqlStatement) throws DynamoException;
  315.     //**********************************************************************
  316.  
  317.     /** 
  318.      * Executes the current SQL query
  319.      * @return true for success; false otherwise
  320.      * @exception DynamoException If an error has occured 
  321.      */
  322.      
  323.     public boolean execute() throws DynamoException 
  324.     //*********************************************
  325.     {
  326.         if (getIntQueryProperty("Execute") == 0)
  327.             return false;
  328.         return true;
  329.     }
  330.     
  331.  
  332.     /**
  333.      * Moves the current row to the specified row in the result set    
  334.      * @param row index of row in the result set to move to
  335.      * @return true if the new row is valid; false otherwise
  336.      * @exception DynamoException If an error has occured 
  337.      */
  338.      
  339.     public boolean move(int row) throws DynamoException 
  340.     //*************************************************
  341.     {
  342.         return queryMove(true, row);
  343.     }
  344.     
  345.     /**
  346.      * Moves the current row to the first row in the result set    
  347.      * @return true if the new row is valid; false otherwise
  348.      * @exception DynamoException If an error has occured 
  349.      */
  350.      
  351.     public boolean moveFirst() throws DynamoException 
  352.     //***********************************************
  353.     {
  354.         return move(1);
  355.     }
  356.     
  357.     /**
  358.      * Moves the current row to the last row in the result set
  359.      * @return true if the new row is valid; false otherwise
  360.      * @exception DynamoException If an error has occured 
  361.      */
  362.      
  363.     public boolean moveLast() throws DynamoException 
  364.     //**********************************************
  365.     {
  366.         return move(-1);
  367.     }
  368.     
  369.     /**
  370.      * Moves the current row to the next row in the result set     
  371.      * @return true if the new row is valid; false otherwise
  372.      * @exception DynamoException If an error has occured 
  373.      */
  374.      
  375.     public boolean moveNext() throws DynamoException 
  376.     //**********************************************
  377.     {
  378.         return moveRelative(1);
  379.     }
  380.     
  381.     /**
  382.      * Moves the current row to the previous row in the result set 
  383.      * @return true if the new row is valid; false otherwise
  384.      * @exception DynamoException If an error has occured
  385.      */
  386.       
  387.     public boolean movePrevious() throws DynamoException 
  388.     //**************************************************
  389.     {
  390.         return moveRelative(-1);
  391.     }
  392.     
  393.     /**
  394.      * Moves to a row relative to the current row
  395.      * @param amount number of rows to move
  396.      * @return true if the new row is valid; false otherwise
  397.      * @exception DynamoException If an error has occured 
  398.      */
  399.      
  400.     public boolean moveRelative(int amount) throws DynamoException 
  401.     //************************************************************
  402.     {
  403.         return queryMove(false, amount);
  404.     }
  405.  
  406.  
  407.     //-------------------------------------------------------------
  408.     // Private Methods
  409.     //-------------------------------------------------------------
  410.  
  411.     private native boolean validate(boolean checkValid) throws DynamoException;
  412.     private native boolean queryMove(boolean absolute, int amount);
  413.     private native int getIntQueryProperty(String name);
  414.     private native String getStringQueryProperty(String name);
  415.     private native String getQueryValue(int column);
  416.  
  417.  
  418.     //-------------------------------------------------------------
  419.     // Data
  420.     //-------------------------------------------------------------
  421.  
  422.     private protected String _name;
  423.     private protected int _qid;
  424.  
  425. };
  426.  
  427.