Add documentation

FossilOrigin-Name: e4c8de38e0a1c749ea0f05f594091f16d659a4d2e329672fb7208a4ff36a77cb
This commit is contained in:
Jonathan Schleifer 2024-08-11 17:45:27 +00:00
parent 7a728f9bd3
commit 50b4eaa1ed
12 changed files with 185 additions and 21 deletions

17
Doxyfile Normal file
View file

@ -0,0 +1,17 @@
PROJECT_NAME = "ObjPgSQL"
OUTPUT_DIRECTORY = docs/
INPUT = src src/exceptions
FILE_PATTERNS = *.h *.m
HTML_OUTPUT = .
HAVE_DOT = NO
GENERATE_LATEX = NO
HIDE_UNDOC_CLASSES = YES
HIDE_UNDOC_MEMBERS = YES
TYPEDEF_HIDES_STRUCT = YES
PREDEFINED = OF_DESIGNATED_INITIALIZER= \
OF_GENERIC(...)= \
OF_SENTINEL
MACRO_EXPANSION = YES
EXPAND_ONLY_PREDEF = YES
IGNORE_PREFIX = PG
EXTRACT_STATIC = yes

View file

@ -24,21 +24,65 @@
OF_ASSUME_NONNULL_BEGIN OF_ASSUME_NONNULL_BEGIN
/** @file */
/**
* @brief A result row.
*/
typedef OFDictionary OF_GENERIC(OFString *, id) *PGRow; typedef OFDictionary OF_GENERIC(OFString *, id) *PGRow;
/**
* @class PGConnection PGConnection.h ObjPgSQL/ObjPgSQL.h
*
* @brief A connection to a database.
*/
@interface PGConnection: OFObject @interface PGConnection: OFObject
{ {
PGconn *_connection; PGconn *_connection;
OFDictionary OF_GENERIC(OFString *, OFString *) *_parameters; OFDictionary OF_GENERIC(OFString *, OFString *) *_parameters;
} }
/**
* @brief The parameters for the database connection. See `PQconnectdb` in the
* PostgreSQL documentation.
*/
@property (nonatomic, copy) @property (nonatomic, copy)
OFDictionary OF_GENERIC(OFString *, OFString *) *parameters; OFDictionary OF_GENERIC(OFString *, OFString *) *parameters;
/**
* @brief Connects to the database.
*
* @throw PGConnectionFailedException The connection failed
*/
- (void)connect; - (void)connect;
/**
* @brief Resets the connection to the database.
*/
- (void)reset; - (void)reset;
/**
* @brief Closes the connection to the database.
*/
- (void)close; - (void)close;
/**
* @brief Executes the specified command.
*
* @param command The command to execute
* @return The result of the command, if any
* @throw PGCommandFailedException Executing the command failed.
*/
- (nullable PGResult *)executeCommand: (OFConstantString *)command; - (nullable PGResult *)executeCommand: (OFConstantString *)command;
/**
* @brief Executes the specified command.
*
* @param command The command to execute
* @param firstParameter First parameter for the command
* @return The result of the command, if any
* @throw PGCommandFailedException Executing the command failed.
*/
- (nullable PGResult *)executeCommand: (OFConstantString *)command - (nullable PGResult *)executeCommand: (OFConstantString *)command
parameters: (id)firstParameter, ... OF_SENTINEL; parameters: (id)firstParameter, ... OF_SENTINEL;
@end @end

View file

@ -22,11 +22,25 @@
#import "PGResult+Private.h" #import "PGResult+Private.h"
#import "PGConnectionFailedException.h" #import "PGConnectionFailedException.h"
#import "PGCommandFailedException.h" #import "PGExecuteCommandFailedException.h"
@implementation PGConnection @implementation PGConnection
@synthesize pg_connection = _connection, parameters = _parameters; @synthesize pg_connection = _connection, parameters = _parameters;
- (instancetype)init
{
self = [super init];
@try {
_parameters = [[OFDictionary alloc] init];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc - (void)dealloc
{ {
[_parameters release]; [_parameters release];
@ -84,7 +98,7 @@
if (PQresultStatus(result) == PGRES_FATAL_ERROR) { if (PQresultStatus(result) == PGRES_FATAL_ERROR) {
PQclear(result); PQclear(result);
@throw [PGCommandFailedException @throw [PGExecuteCommandFailedException
exceptionWithConnection: self exceptionWithConnection: self
command: command]; command: command];
} }
@ -97,7 +111,7 @@
return nil; return nil;
default: default:
PQclear(result); PQclear(result);
@throw [PGCommandFailedException @throw [PGExecuteCommandFailedException
exceptionWithConnection: self exceptionWithConnection: self
command: command]; command: command];
} }
@ -159,7 +173,7 @@
return nil; return nil;
default: default:
PQclear(result); PQclear(result);
@throw [PGCommandFailedException @throw [PGExecuteCommandFailedException
exceptionWithConnection: self exceptionWithConnection: self
command: command]; command: command];
} }

View file

@ -24,6 +24,14 @@ OF_ASSUME_NONNULL_BEGIN
@class PGResultRow; @class PGResultRow;
/**
* @class PGResult PGResult.h ObjPgSQL/ObjPgSQL.h
*
* @brief A PostgreSQL result.
*
* This is a regular OFArray, where each entry in the array represents a result
* row.
*/
@interface PGResult: OFArray OF_GENERIC(PGResultRow *) @interface PGResult: OFArray OF_GENERIC(PGResultRow *)
{ {
PGresult *_result; PGresult *_result;

View file

@ -24,6 +24,14 @@
OF_ASSUME_NONNULL_BEGIN OF_ASSUME_NONNULL_BEGIN
/**
* @class PGResult PGResult.h ObjPgSQL/ObjPgSQL.h
*
* @brief A PostgreSQL result row.
*
* This is a regular OFDictionary, where each entry in the dictionary
* represents a column of the result row.
*/
@interface PGResultRow: OFDictionary OF_GENERIC(OFString *, id) @interface PGResultRow: OFDictionary OF_GENERIC(OFString *, id)
{ {
PGResult *_result; PGResult *_result;

View file

@ -20,6 +20,12 @@
OF_ASSUME_NONNULL_BEGIN OF_ASSUME_NONNULL_BEGIN
/**
* @class PGConnectionFailedException PGConnectionFailedException.h
* PgSQL/PgSQL.h
*
* @brief An exception indicating that connecting to the database failed.
*/
@interface PGConnectionFailedException: PGException @interface PGConnectionFailedException: PGException
@end @end

View file

@ -23,6 +23,6 @@
{ {
return [OFString stringWithFormat: return [OFString stringWithFormat:
@"Establishing a PostgreSQL connection failed:\n%@\n" @"Establishing a PostgreSQL connection failed:\n%@\n"
"Parameters: %@", _error, [_connection parameters]]; "Parameters: %@", _errorMessage, [_connection parameters]];
} }
@end @end

View file

@ -22,15 +22,45 @@
OF_ASSUME_NONNULL_BEGIN OF_ASSUME_NONNULL_BEGIN
/**
* @class PGException PGException.h ObjPgSQL/ObjPgSQL.h
*
* @brief A PostgreSQL exception.
*/
@interface PGException: OFException @interface PGException: OFException
{ {
PGConnection *_connection; PGConnection *_connection;
OFString *_error; OFString *_errorMessage;
} }
/**
* @brief The connection for which the exception occurred.
*/
@property (readonly, nonatomic) PGConnection *connection; @property (readonly, nonatomic) PGConnection *connection;
/**
* @brief An error message for the exception.
*/
@property (readonly, nonatomic) OFString *errorMessage;
+ (instancetype)exception OF_UNAVAILABLE;
/**
* @brief Creates a new PostgreSQL exception.
*
* @param connection The connection for which the exception occurred
* @return A new, autoreleased PostgreSQL exception
*/
+ (instancetype)exceptionWithConnection: (PGConnection *)connection; + (instancetype)exceptionWithConnection: (PGConnection *)connection;
- (instancetype)init OF_UNAVAILABLE;
/**
* @brief Initializes an already allocated PostgreSQL exception.
*
* @param connection The connection for which the exception occurred
* @return An initialized PostgreSQL exception
*/
- (instancetype)initWithConnection: (PGConnection *)connection - (instancetype)initWithConnection: (PGConnection *)connection
OF_DESIGNATED_INITIALIZER; OF_DESIGNATED_INITIALIZER;
@end @end

View file

@ -20,20 +20,30 @@
#import "PGConnection+Private.h" #import "PGConnection+Private.h"
@implementation PGException @implementation PGException
@synthesize connection = _connection; @synthesize connection = _connection, errorMessage = _errorMessage;
+ (instancetype)exception
{
OF_UNRECOGNIZED_SELECTOR
}
+ (instancetype)exceptionWithConnection: (PGConnection *)connection + (instancetype)exceptionWithConnection: (PGConnection *)connection
{ {
return [[[self alloc] initWithConnection: connection] autorelease]; return [[[self alloc] initWithConnection: connection] autorelease];
} }
- (instancetype)init
{
OF_INVALID_INIT_METHOD
}
- (instancetype)initWithConnection: (PGConnection *)connection - (instancetype)initWithConnection: (PGConnection *)connection
{ {
self = [super init]; self = [super init];
@try { @try {
_connection = [connection retain]; _connection = [connection retain];
_error = [[OFString alloc] _errorMessage = [[OFString alloc]
initWithCString: PQerrorMessage([_connection pg_connection]) initWithCString: PQerrorMessage([_connection pg_connection])
encoding: [OFLocale encoding]]; encoding: [OFLocale encoding]];
} @catch (id e) { } @catch (id e) {
@ -47,7 +57,7 @@
- (void)dealloc - (void)dealloc
{ {
[_connection release]; [_connection release];
[_error release]; [_errorMessage release];
[super dealloc]; [super dealloc];
} }
@ -55,6 +65,6 @@
- (OFString *)description - (OFString *)description
{ {
return [OFString stringWithFormat: @"A PostgreSQL operation failed: %@", return [OFString stringWithFormat: @"A PostgreSQL operation failed: %@",
_error]; _errorMessage];
} }
@end @end

View file

@ -20,20 +20,46 @@
OF_ASSUME_NONNULL_BEGIN OF_ASSUME_NONNULL_BEGIN
@interface PGCommandFailedException: PGException /**
* @class PGExecuteCommandFailedException PGExecuteCommandFailedException.h
* ObjPgSQL/ObjPgSQL.h
*
* @brief An exception indicating that executing a command failed.
*/
@interface PGExecuteCommandFailedException: PGException
{ {
OFString *_command; OFConstantString *_command;
} }
@property (readonly, nonatomic) OFString *command; /**
* @brief The command that could not be executed.
*/
@property (readonly, nonatomic) OFConstantString *command;
+ (instancetype)exceptionWithConnection: (PGConnection *)connection + (instancetype)exceptionWithConnection: (PGConnection *)connection
OF_UNAVAILABLE; OF_UNAVAILABLE;
/**
* @brief Creates a new execte command failed exception.
*
* @param connection The connection for which the command could not be executed
* @param command The command which could not be executed
* @return A new, autoreleased execute command failed exception
*/
+ (instancetype)exceptionWithConnection: (PGConnection *)connection + (instancetype)exceptionWithConnection: (PGConnection *)connection
command: (OFString *)command; command: (OFConstantString *)command;
- (instancetype)initWithConnection: (PGConnection *)connection OF_UNAVAILABLE; - (instancetype)initWithConnection: (PGConnection *)connection OF_UNAVAILABLE;
/**
* @brief Initializes an already allocated execte command failed exception.
*
* @param connection The connection for which the command could not be executed
* @param command The command which could not be executed
* @return An initialized execute command failed exception
*/
- (instancetype)initWithConnection: (PGConnection *)connection - (instancetype)initWithConnection: (PGConnection *)connection
command: (OFString *)command command: (OFConstantString *)command
OF_DESIGNATED_INITIALIZER; OF_DESIGNATED_INITIALIZER;
@end @end

View file

@ -16,9 +16,9 @@
* PERFORMANCE OF THIS SOFTWARE. * PERFORMANCE OF THIS SOFTWARE.
*/ */
#import "PGCommandFailedException.h" #import "PGExecuteCommandFailedException.h"
@implementation PGCommandFailedException @implementation PGExecuteCommandFailedException
@synthesize command = _command; @synthesize command = _command;
+ (instancetype)exceptionWithConnection: (PGConnection *)connection + (instancetype)exceptionWithConnection: (PGConnection *)connection
@ -27,7 +27,7 @@
} }
+ (instancetype)exceptionWithConnection: (PGConnection *)connection + (instancetype)exceptionWithConnection: (PGConnection *)connection
command: (OFString *)command command: (OFConstantString *)command
{ {
return [[[self alloc] initWithConnection: connection return [[[self alloc] initWithConnection: connection
command: command] autorelease]; command: command] autorelease];
@ -39,7 +39,7 @@
} }
- (instancetype)initWithConnection: (PGConnection *)connection - (instancetype)initWithConnection: (PGConnection *)connection
command: (OFString *)command command: (OFConstantString *)command
{ {
self = [super initWithConnection: connection]; self = [super initWithConnection: connection];
@ -63,6 +63,7 @@
- (OFString *)description - (OFString *)description
{ {
return [OFString stringWithFormat: @"A PostgreSQL command failed: %@\n" return [OFString stringWithFormat: @"A PostgreSQL command failed: %@\n"
@"Command: %@", _error, _command]; @"Command: %@",
_errorMessage, _command];
} }
@end @end

View file

@ -1,5 +1,5 @@
exceptions_sources = files( exceptions_sources = files(
'PGCommandFailedException.m',
'PGConnectionFailedException.m', 'PGConnectionFailedException.m',
'PGException.m', 'PGException.m',
'PGExecuteCommandFailedException.m',
) )