Skip to content

Commit

Permalink
Add some documentation for Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
mjoerussell committed May 16, 2022
1 parent 6dddd36 commit 9bac9d8
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Connection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub const ConnectionOptions = struct {
environment: odbc.Environment,
connection: odbc.Connection,

/// Initialize a new Connection instance with the given ODBC version. The default version is 3.0.
/// This does not connect to a database.
pub fn init(config: ConnectionOptions) !Connection {
var connection: Connection = undefined;

Expand All @@ -58,34 +60,45 @@ pub fn init(config: ConnectionOptions) !Connection {
return connection;
}

/// Connect to a database using a server/host, username, and password.
pub fn connect(conn: *Connection, server_name: []const u8, username: []const u8, password: []const u8) !void {
try conn.connection.connect(server_name, username, password);
}

/// Connect to a database using a ConnectionConfig. The config will be converted to a connection string, and then
/// it will attempt to connect using connectExtended.
pub fn connectWithConfig(conn: *Connection, allocator: Allocator, connection_config: ConnectionConfig) !void {
var connection_string = try connection_config.getConnectionString(allocator);
defer allocator.free(connection_string);

try conn.connection.connectExtended(connection_string, .NoPrompt);
}

/// Connect using a pre-created connection string.
pub fn connectExtended(conn: *Connection, connection_string: []const u8) !void {
try conn.connection.connectExtended(connection_string, .NoPrompt);
}

/// Close the connection and environment handles. Does not disconnect from the data source.
pub fn deinit(self: *Connection) void {
self.connection.deinit() catch {};
self.environment.deinit() catch {};
}

/// Disconnect from the currently connected data source.
pub fn disconnect(self: *Connection) void {
self.connection.disconnect() catch {};
}

/// Sets the commit mode which will be used in all cursors created from this connection.
/// If the commit mode is `auto`, then each statement execution will be immediately committed. If the
/// commit mode is `manual` then statement executions won't be commited until the user calls `cursor.commit()`.
/// This can be used for transaction management.
pub fn setCommitMode(self: *Connection, mode: CommitMode) !void {
try self.connection.setAttribute(.{ .Autocommit = mode == .auto });
}

/// Get a new cursor, with which you can execute SQL statements.
pub fn getCursor(self: *Connection, allocator: Allocator) !Cursor {
return try Cursor.init(allocator, self.connection);
}
Expand Down

0 comments on commit 9bac9d8

Please sign in to comment.