PGShim2 - Objective C framework for accessing PostgreSQL databases
  • Objective-C 100%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2020-07-29 15:45:49 +01:00
PGShim2 add persistenceSetAllDirtyForUpsert method for non-observers 2020-07-29 15:45:49 +01:00
PGShim2.xcodeproj add persistenceSetAllDirtyForUpsert method for non-observers 2020-07-29 15:45:49 +01:00
PGShim2Tests add persistenceSetAllDirtyForUpsert method for non-observers 2020-07-29 15:45:49 +01:00
README.md Add new file 2016-08-05 17:45:20 +01:00

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;