Re: Custom SQL query - HowTo

From: Mike Kienenberger (mkienen..mail.com)
Date: Thu Aug 03 2006 - 08:20:23 EDT

  • Next message: Mike Kienenberger: "Re: objectFromDataRow(String, DataRow) exist not in cayenne-1.2"

    Here's an example. Note that the only reason you need to supply one
    of the following (DataObject class, DataMap, ObjEntity, DbEntity, or
    ObjEntity name) to SQLTemplate is so that it can find an appropriate
    DataNode, which tells it where it get the JDBC connection information.
       The actual value supplied doesn't matter when you're fetching data
    rows just so long as the supplied object is in the same data node as
    where you want the query to run.

    [Side note: And yes, I know the default sql on here is not
    thread-safe, but it's not an issue in my dev environment, and getting
    a duplicate value isn't going to cause any technical problems.]

    private Integer fetchDocumentNumberAutoIncrement() {
            String defaultSql = "SELECT #result('max(DOCUMENT_NUMBER)+1'
    'long' 'ID') FROM ENG_WORK_MGMT.AUTHORIZATION_DOCUMENT";
                String oracleSql = "SELECT
    #result('SEQ_AuthDocDocumentNumber.NextVal' 'long' 'ID') FROM DUAL";
                
            SQLTemplate rawSelect = new SQLTemplate(getClass(), defaultSql);
            rawSelect.setTemplate(OracleAdapter.class.getName(), oracleSql);
            rawSelect.setFetchingDataRows(true);

            List list = getDataContext().performQuery(rawSelect);
            Map row = (Map)list.get(0);

            Number autoincrementID = (Number)row.get("ID");

            return new Integer(autoincrementID.intValue());
            }

    On 8/3/06, Ayhan Kondoz <Ayhan.Kondo..reenet-ag.de> wrote:
    > Hello,
    >
    > i need to make a custom SQL query where the resultSet doesn't match any
    > of the dataMap's. My SQL query only returns a list of id's. Something
    > like this
    >
    > SELECT customer.id FROM customer c WHERE .....
    >
    > And I want to iterate through this list of id's and do something with
    > them.
    >
    > Could someone please tell me how to do this with cayenne? I tried to use
    > SQLTemplate but it needs a DataMap or something similar to work but I
    > don't have an appropriate DataMap!!!
    > Then I tried to use a dataContext.performGenericQuery() but again I
    > don't know how to create a proper Query object.
    >
    > Thx
    > Ayhan Kondoz
    >



    This archive was generated by hypermail 2.0.0 : Thu Aug 03 2006 - 08:20:47 EDT