• 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: SQL?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: SQL?


  • Subject: Re: SQL?
  • From: "Jerry W. Walker" <email@hidden>
  • Date: Wed, 8 Feb 2006 23:15:55 -0500

Hi, Jim,

All of the advice you've been given is correct, but focus on the EOQualifier (which is used in the EOFetchSpecification). In particular, study the static EOQualifier method:

  qualifierWithQualifierFormat(String format, NSArray arguments);

You set up an EOQualifier based on the properties in your Entity rather than the columns of your table. This carries two advantages:

* it allows you to focus on the object model rather than bifurcating your view between object model and database tables

* it provides the ability to set up qualifiers based not only on column values, but on values extracted through relations.

Finally, the qualifier can be used both to filter the data returned from a fetch as well as to further filter data from an NSArray already holding the results of an earlier fetch.

In case you need to do such a fetch, here is the set of classes usually needed in a typical context in which they would be used. Having their names, as well as the names of the methods typically used should allow you to look them up conveniently in the API documentation:

The general hierarchy of classes needed for a fetch is:

     EOEditingContext ec = defaultEditingContext();

EOQualifier qual = EOQualifier.qualifierWithQualifierFormat ("someAttribute > %@ AND someOtherAttribute = %@", arrayOfTwoValues);

EOSortOrdering so1 = new EOSortOrdering("someAttribute", EOSortOrdering.CompareCaseInsensitiveAscending);

EOSortOrdering so2 = new EOSortOrdering("someOtherAttribute", EOSortOrdering.CompareDescending);

     NSArray sortOrdering = new NSArray(new Object[] {so1, so2});

EOFetchSpecification mySpec = new EOFetchSpecification ("EntityName", qual, sortOrdering);

     NSArray myResults = ec.objectsWithFetchSpecification(mySpec);

The above represents the general case. You can shortcut some of that for simpler cases using the EOUtilities.objectsForEntityNamed() method and others like it (e.g. objectsMatchingKeyAndValue(), objectsMatchingValues(), etc.).


If you have already fetched data into an array and wish to further filter the data in that array, you can still use the EOQualifier as follows:


NSArray myFilteredResults = EOQualifier.filteredArrayWithQualifier(myResults, myAdditionalQualifier);

The above statement would apply the qualifier to the array just as it would have applied it to a fetch, creating a new array with the filtered results which you were looking for. Finally, if your initial results are in an NSMutableArray and you would like to modify that array by further filtering, you would use the EOQualifier method filterArrayWithQualifier. Notice the subtle difference in the method name from that earlier.



If you found this helpful and would like more information on "the WebObjects Way of fetching data" please see the message I sent to someone on this topic last year:

http://www.wodeveloper.com/omniLists/webobjects-dev/2005/July/ msg00118.html

The level of discourse is probably a bit below your level, but you might still find it helpful.

Regards,
Jerry

On Feb 8, 2006, at 7:45 PM, Sacha Michel Mallais wrote:

On Feb 8, 2006, at 4:33 PM, Jim Wong wrote:

Can I use SQL statements in WO to extract certain rows in my entity? Basically I want to filter my entity object for certain rows of data. how to do this?

Yes you can, but you generally don't want to. Take a look at the documentation for EOFetchSpecification.
http://developer.apple.com/documentation/WebObjects/Reference/API/ com/webobjects/eocontrol/EOFetchSpecification.html


If you _must_ use SQL (usually only necessary for optimization), then you'll have to do something like this:
...

--
__ Jerry W. Walker,
WebObjects Developer/Instructor for High Performance Industrial Strength Internet Enabled Systems


    email@hidden
    203 278-4085        office



_______________________________________________
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: 
 >SQL? (From: Jim Wong <email@hidden>)
 >Re: SQL? (From: Sacha Michel Mallais <email@hidden>)

  • Prev by Date: Re: Accessing the session in WOLongResponsePage.performAction
  • Next by Date: Seeking Advice for Getting Started
  • Previous by thread: Re: SQL?
  • Next by thread: Re: SQL?
  • Index(es):
    • Date
    • Thread