What is going to happen when one of your parameters is a string with a ? In it? 
 Oops....

If you are not already, you also want to get a EOSQLExpression subclass from 
the database plugin and use

public String<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html> 
formatStringValue(String<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html>
 string)

Formats string for use as a string constant in a SQL statement. 
EOSQLExpression's implementation encloses the string in single quotes, escaping 
any single quotes already present in string.
So as to avoid the Little Bobby Tables problem.

Chuck

From: 
<[email protected]<mailto:[email protected]>>
 on behalf of Markus Ruggiero 
<[email protected]<mailto:[email protected]>>
Date: Friday, September 25, 2015 at 2:26 AM
To: "[email protected]<mailto:[email protected]>" 
<[email protected]<mailto:[email protected]>>
Subject: Re: Preferred way for fetching with given SQL select statement?

Thanks for all your help. Here is the code I am using now (might need some more 
error checking)

Wish you all a WOnder-full day
---markus---

public NSArray<Product> findProducts(EOEditingContext ec, String query, 
NSArray<String> queryParameters) {

String resolvedQuery = query;
for (String aParameter : queryParameters) {
resolvedQuery = query.replaceFirst( "\\?<smb://?>", aParameter );
}
EOEntity entity = ERXEOAccessUtilities.entityNamed(ec, Product.ENTITY_NAME);
String modelName = entity.model().name();
NSArray<NSDictionary> rows = EOUtilities.rawRowsForSQL( ec, modelName, 
resolvedQuery, null );

NSMutableArray<String> productCodes = new NSMutableArray<String>();
for (NSDictionary dict : rows) {
NSArray<String> values = dict.allValues();  // there is only one value in the 
dict, but we get an Array
String productCode = values.objectAtIndex( 0 );
if (productCode != null) {
productCodes.addObject( productCode );
}
}
EOQualifier qualifier = Product.ATTRIBUTE_PRODUCT_CODE_ERXKEY.in( productCodes 
);
NSArray<Product> results = Product.fetchProducts( ec, qualifier, null );
return results;
}

// Example query:
SELECT prod.PRODUCT_CODE FROM PRODUCT prod, SITE site,  INDUSTRY_PRODUCT ipr, 
INDUSTRY ind WHERE prod.SITE_ID = site.ID AND site.SITE_NAME = '?' AND 
ipr.PRODUCT_ID = prod.ID AND ipr.INDUSTRY_ID = ind.ID AND ind.INDUSTRY_NAME 
='ANH' AND prod.PUB_TRIGGER = 'Y' AND ((prod.EXPIRATION_DATE > SYSDATE) OR 
(prod.EXPIRATION_DATE IS NULL));

Some are a lot more complex with inner and outer joins and embedded SELECT FROM 
... WHERE .... IN ....



On 24.09.2015, at 16:22, Markus Ruggiero 
<[email protected]<mailto:[email protected]>> wrote:

Folks,

What is the preferred way to fetch EOs when you have a db vendor specific 
"select id from ... where ....." statement?

I need to interface with a legacy system where I have to incorporate the 
functionality of an external java tool into a Wonder app. This external tool 
executes raw SQL select statements which it reads from a file. All the 
statements are in the form given above and return a list of primary key values 
(single column fortunately). The tool then iterates through that list and does 
whatever it has to do (issuing tons of more raw sql). My problem is that these 
select statements are rather complex and there are many of them. It is just not 
feasible to replace those with proper EOQualifiers. In addition these 
statements are parameterized with ? so they can be precompiled and then used 
with different query values. However everything can be done with standard EOs 
as soon as I have those corresponding to the returned list of ids.

Thanks a lot
---markus---
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      
([email protected]<mailto:[email protected]>)
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/webobjects-dev/mailinglists%40kataputt.com

This email sent to [email protected]<mailto:[email protected]>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to