• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Preferred way for fetching with given SQL select statement?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Preferred way for fetching with given SQL select statement?


  • Subject: Re: Preferred way for fetching with given SQL select statement?
  • From: Chuck Hill <email@hidden>
  • Date: Fri, 25 Sep 2015 20:49:05 +0000
  • Thread-topic: Preferred way for fetching with given SQL select statement?

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 formatStringValue(String 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: <webobjects-dev-bounces+chill=email@hidden> on behalf of Markus Ruggiero <email@hidden>
Date: Friday, September 25, 2015 at 2:26 AM
To: "email@hidden" <email@hidden>
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( "\\?", 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@hidden> 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@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Preferred way for fetching with given SQL select statement? (From: Markus Ruggiero <email@hidden>)
 >Re: Preferred way for fetching with given SQL select statement? (From: Markus Ruggiero <email@hidden>)

  • Prev by Date: Mark your calendar: WOWODC 2016
  • Next by Date: Re: WO Maven getting started
  • Previous by thread: Re: Preferred way for fetching with given SQL select statement?
  • Next by thread: maven.wocommunity.org : server not responding ?
  • Index(es):
    • Date
    • Thread