Use IRIs instead of paths

FossilOrigin-Name: 91e2a5071199dc8d87735ad50ee74a6ce030c22f1bb0d70668bef172384dcde3
This commit is contained in:
Jonathan Schleifer 2024-08-11 11:48:19 +00:00
parent 5c91e6f832
commit bd3543537b
5 changed files with 53 additions and 55 deletions

View file

@ -38,45 +38,44 @@ OF_ASSUME_NONNULL_BEGIN
} }
/** /**
* @brief Creates a new connection to the database at the specified path. * @brief Creates a new connection to the database at the specified IRI.
* *
* @param path The path to the database * @param IRI The IRI to the database
* @return A new database connection * @return A new database connection
* @throw SL3OpenFailedException The database could not be opened * @throw SL3OpenFailedException The database could not be opened
*/ */
+ (instancetype)connectionWithPath: (OFString *)path; + (instancetype)connectionWithIRI: (OFIRI *)IRI;
/** /**
* @brief Creates a new connection to the database at the specified path. * @brief Creates a new connection to the database at the specified IRI.
* *
* @param path The path to the database * @param IRI The IRI to the database
* @param flags The flags to open the database with * @param flags The flags to open the database with
* @return A new database connection * @return A new database connection
* @throw SL3OpenFailedException The database could not be opened * @throw SL3OpenFailedException The database could not be opened
*/ */
+ (instancetype)connectionWithPath: (OFString *)path + (instancetype)connectionWithIRI: (OFIRI *)IRI flags: (int)flags;
flags: (int)flags;
/** /**
* @brief Initializes an already allocated connection to connect to the * @brief Initializes an already allocated connection to connect to the
* database at the specified path. * database at the specified IRI.
* *
* @param path The path to the database * @param IRI The IRI to the database
* @return An initialized connection to the specified database * @return An initialized connection to the specified database
* @throw SL3OpenFailedException The database could not be opened * @throw SL3OpenFailedException The database could not be opened
*/ */
- (instancetype)initWithPath: (OFString *)path; - (instancetype)initWithIRI: (OFIRI *)IRI;
/** /**
* @brief Initializes an already allocated connection to connect to the * @brief Initializes an already allocated connection to connect to the
* database at the specified path. * database at the specified IRI.
* *
* @param path The path to the database * @param IRI The IRI to the database
* @param flags The flags to open the database with * @param flags The flags to open the database with
* @return An initialized connection to the specified database * @return An initialized connection to the specified database
* @throw SL3OpenFailedException The database could not be opened * @throw SL3OpenFailedException The database could not be opened
*/ */
- (instancetype)initWithPath: (OFString *)path - (instancetype)initWithIRI: (OFIRI *)IRI
flags: (int)flags OF_DESIGNATED_INITIALIZER; flags: (int)flags OF_DESIGNATED_INITIALIZER;
/** /**

View file

@ -24,35 +24,33 @@
#import "SL3OpenFailedException.h" #import "SL3OpenFailedException.h"
@implementation SL3Connection @implementation SL3Connection
+ (instancetype)connectionWithPath: (OFString *)path + (instancetype)connectionWithIRI: (OFIRI *)IRI
{ {
return [[[self alloc] initWithPath: path] autorelease]; return [[[self alloc] initWithIRI: IRI] autorelease];
} }
+ (instancetype)connectionWithPath: (OFString *)path + (instancetype)connectionWithIRI: (OFIRI *)IRI flags: (int)flags
flags: (int)flags
{ {
return [[[self alloc] initWithPath: path return [[[self alloc] initWithIRI: IRI flags: flags] autorelease];
flags: flags] autorelease];
} }
- (instancetype)initWithPath: (OFString *)path - (instancetype)initWithIRI: (OFIRI *)IRI
{ {
return [self initWithPath: path return [self initWithIRI: IRI
flags: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE]; flags: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE];
} }
- (instancetype)initWithPath: (OFString *)path - (instancetype)initWithIRI: (OFIRI *)IRI flags: (int)flags
flags: (int)flags
{ {
self = [super init]; self = [super init];
@try { @try {
int code = sqlite3_open_v2(path.UTF8String, &_conn, flags, int code = sqlite3_open_v2(
IRI.fileSystemRepresentation.UTF8String, &_conn, flags,
NULL); NULL);
if (code != SQLITE_OK) if (code != SQLITE_OK)
@throw [SL3OpenFailedException exceptionWithPath: path @throw [SL3OpenFailedException exceptionWithIRI: IRI
flags: flags flags: flags
errorCode: code]; errorCode: code];
} @catch (id e) { } @catch (id e) {

View file

@ -28,14 +28,14 @@ OF_ASSUME_NONNULL_BEGIN
*/ */
@interface SL3OpenFailedException: SL3Exception @interface SL3OpenFailedException: SL3Exception
{ {
OFString *_path; OFIRI *_IRI;
int _flags; int _flags;
} }
/** /**
* @brief The path of the database that could not be opened. * @brief The IRI of the database that could not be opened.
*/ */
@property (readonly, nonatomic) OFString *path; @property (readonly, nonatomic) OFIRI *IRI;
/** /**
* @brief The flags with which the database could not be opened. * @brief The flags with which the database could not be opened.
@ -48,12 +48,12 @@ OF_ASSUME_NONNULL_BEGIN
/** /**
* @brief Creates a new open failed exception. * @brief Creates a new open failed exception.
* *
* @param path The path of the database that could not be opened * @param IRI The IRI of the database that could not be opened
* @param flags The flags with which the database could not be opened * @param flags The flags with which the database could not be opened
* @param errorCode The SQLite3 error code * @param errorCode The SQLite3 error code
* @return A new, autoreleased open failed exception * @return A new, autoreleased open failed exception
*/ */
+ (instancetype)exceptionWithPath: (OFString *)path + (instancetype)exceptionWithIRI: (OFIRI *)IFI
flags: (int)flags flags: (int)flags
errorCode: (int)errorCode; errorCode: (int)errorCode;
@ -63,12 +63,12 @@ OF_ASSUME_NONNULL_BEGIN
/** /**
* @brief Initializes an already allocated open failed exception. * @brief Initializes an already allocated open failed exception.
* *
* @param path The path of the database that could not be opened * @param IRI The IRI of the database that could not be opened
* @param flags The flags with which the database could not be opened * @param flags The flags with which the database could not be opened
* @param errorCode The SQLite3 error code * @param errorCode The SQLite3 error code
* @return An initialized open failed exception * @return An initialized open failed exception
*/ */
- (instancetype)initWithPath: (OFString *)path - (instancetype)initWithIRI: (OFIRI *)IRI
flags: (int)flags flags: (int)flags
errorCode: (int)errorCode OF_DESIGNATED_INITIALIZER; errorCode: (int)errorCode OF_DESIGNATED_INITIALIZER;
@end @end

View file

@ -19,7 +19,7 @@
#import "SL3OpenFailedException.h" #import "SL3OpenFailedException.h"
@implementation SL3OpenFailedException @implementation SL3OpenFailedException
@synthesize path = _path, flags = _flags; @synthesize IRI = _IRI, flags = _flags;
+ (instancetype)exceptionWithConnection: (SL3Connection *)connection + (instancetype)exceptionWithConnection: (SL3Connection *)connection
errorCode: (int)errorCode errorCode: (int)errorCode
@ -27,11 +27,11 @@
OF_UNRECOGNIZED_SELECTOR OF_UNRECOGNIZED_SELECTOR
} }
+ (instancetype)exceptionWithPath: (OFString *)path + (instancetype)exceptionWithIRI: (OFIRI *)IRI
flags: (int)flags flags: (int)flags
errorCode: (int)errorCode errorCode: (int)errorCode
{ {
return [[[self alloc] initWithPath: path return [[[self alloc] initWithIRI: IRI
flags: flags flags: flags
errorCode: errorCode] autorelease]; errorCode: errorCode] autorelease];
} }
@ -42,7 +42,7 @@
OF_INVALID_INIT_METHOD OF_INVALID_INIT_METHOD
} }
- (instancetype)initWithPath: (OFString *)path - (instancetype)initWithIRI: (OFIRI *)IRI
flags: (int)flags flags: (int)flags
errorCode: (int)errorCode errorCode: (int)errorCode
{ {
@ -50,7 +50,7 @@
errorCode: errorCode]; errorCode: errorCode];
@try { @try {
_path = [path copy]; _IRI = [IRI copy];
_flags = flags; _flags = flags;
} @catch (id e) { } @catch (id e) {
[self release]; [self release];
@ -62,7 +62,7 @@
- (void)dealloc - (void)dealloc
{ {
[_path release]; [_IRI release];
[super dealloc]; [super dealloc];
} }

View file

@ -28,14 +28,15 @@ OF_APPLICATION_DELEGATE(Tests)
@implementation Tests @implementation Tests
- (void)applicationDidFinishLaunching: (OFNotification *)notification - (void)applicationDidFinishLaunching: (OFNotification *)notification
{ {
OFIRI *IRI = [OFIRI fileIRIWithPath: @"tests.db"];
OFFileManager *fileManager = [OFFileManager defaultManager]; OFFileManager *fileManager = [OFFileManager defaultManager];
SL3Connection *conn; SL3Connection *conn;
SL3PreparedStatement *stmt; SL3PreparedStatement *stmt;
if ([fileManager fileExistsAtPath: @"tests.db"]) if ([fileManager fileExistsAtIRI: IRI])
[fileManager removeItemAtPath: @"tests.db"]; [fileManager removeItemAtIRI: IRI];
conn = [SL3Connection connectionWithPath: @"tests.db"]; conn = [SL3Connection connectionWithIRI: IRI];
[conn executeStatement: @"CREATE TABLE test (a INT, b TEXT, c BLOB)"]; [conn executeStatement: @"CREATE TABLE test (a INT, b TEXT, c BLOB)"];