PGShim2 - Objective C framework for accessing PostgreSQL databases
- Objective-C 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| PGShim2 | ||
| PGShim2.xcodeproj | ||
| PGShim2Tests | ||
| README.md | ||
PGShim2 is an Objective-C framework for accessing PostgreSQL databases.
There are three basic classes provided by the framework
PGConnectionPool
This is the master class which provides the management of connections
and allocates connections from a pool or creates new ones on demand.
PGConnection
This class provides access to an individual connection for execution
of SQL which produces output in the form of ...
PGResult
This gives details of the success/failure of a specific operation
and access to any returned rows.
Consider the following code snippet
// A PGConnection string as NSString
NSString *connstr = @"host=localhost dbname=pgshim2 user=shimuser connect_timeout=10";
// We have an array of arrays that are rows to insert
NSArray *newdata = @[ @[ @1, @1, @"Hello, world!" ],
@[ @2, @2, @"Second row" ],
@[ @3, @3, @"Third Row" ]
];
// The insert statement for the above rows
NSString *insSQL = @"insert into mytable values($1,$2,$3)";
// Create the connection pool object
PGConnectionPool *pool = [[PGConnectionPoole alloc] initWithConnectionString:connstr];
// allocate a connection on which to perform the transaction
PGConnection *transaction_handle = [pool allocateConnection];
PGResult *result = [transaction_handle begin];
for (id object in newdata)
{
result = [transaction_handle executeSQL:insSQL withParams:object];
}
result = [transaction_handle commit];
[transaction_handle returnToPool];
// the transaction handle is not no longer valid so ...
transaction_handle = nil;
We can also perform singleton operations on the pool itself. So to illustrate, a SELECT operation.
NSDictionary *row;
PGResult *res = [pool executeSQL:@"select * from mytable where mytext = 'third row'"];
if ( [res isTuplesOK])
&& (row = [res getNextRow]) )
)
{
NSLog( @"row with mytext=%@ has myint=%@",
[row objectForKey:@"mytext"],
[row objectForKey:@"myint"] );
}
// discard the rows and any allocated data
row = nil;
res=nil;