First of all, we recommend using MacPorts for installing PostgreSQL and doxygen.
sudo port install postgresql82 sudo port install doxygen
Setting up PostgreSQL is outside the scope of this (half-assed) tutorial, so check out the Installation and Getting Started chapters of the PostgreSQL online documentation.
Installing BaseTen
Once you have PostgreSQL running and a simple schema set up, we can get to the actual BaseTen stuff. The only requirement that BaseTen has about the database schema is that your tables have primary keys, so if your tables do, it's time to install BaseTen. Either copy the frameworks from the distribution disk image to /Library/Frameworks, or build BaseTen from the source repository.
Preparing the database
BaseTen uses triggers and stored procedures on the PostgreSQL server to do some of its magic; most notably, BaseTen's ability to detect database changes without polling requires special support on the database server. Again, the BaseTen additions should work with any database schema, even if you have triggers of your own. Starting with BaseTen Developer Preview 1, this step is performed with the BaseTen Assistant (you can also see how it's done by hand).
First, launch the Assistant and enter the address of the server and the database you want to use, along with any possible login credentials. The Assistant will offer to upload the BaseTen additions to the database. Next, you will see a screen with the schema and table information for your database. You can click on the "Enabled" checkboxes to enable a table to be used with BaseTen. If you didn't create any tables yet, you can import a Core Data model (a .xcdatamodel file) and create tables based on that.
Putting it all together
Here's a minimal example of a BaseTen application. It connects to a database called basetentest on the localhost, and lists all the rows of the test table, printing out the value of the value column for each.
#import <Foundation/Foundation.h>
#import <BaseTen/BaseTen.h>
int main(int argc, char** argv)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
BXDatabaseContext* ctx = [[BXDatabaseContext alloc] init];
// The context should connect to this database
[ctx setDatabaseURI: [NSURL URLWithString:@"pgsql://localhost/basetentest"]];
// Get an entity description for a table in our database
BXEntityDescription* entity = [ctx entityForTable:@"test" inSchema:@"public" error:nil];
// Perform the fetch
NSError* error = nil;
NSArray* result = [ctx executeFetchForEntity:entity withPredicate:nil error:&error];
if (nil != result)
{
NSEnumerator* e = [result objectEnumerator];
BXDatabaseObject* currentObject = nil;
while ((currentObject = [e nextObject]))
{
NSLog(@"Value: \t%@", [currentObject valueForKey:@"value"]);
}
}
else
{
NSLog(@"Error: %@", error);
}
[pool release];
return 0;
}
The BaseTen Reference Manual will tell you more. Please bear with us as we add some real-world examples to this wiki.
