Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/murymi/zcon
simple Mysql connection using mysql-c-api-8.0
https://github.com/murymi/zcon
mysql zig
Last synced: 7 days ago
JSON representation
simple Mysql connection using mysql-c-api-8.0
- Host: GitHub
- URL: https://github.com/murymi/zcon
- Owner: murymi
- Created: 2023-12-05T12:27:30.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-03-03T16:19:37.000Z (9 months ago)
- Last Synced: 2024-07-23T23:36:46.227Z (4 months ago)
- Topics: mysql, zig
- Language: Zig
- Homepage:
- Size: 79.1 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#### Disclaimer: This is just a simple project to learn zig. Even though it works, Use it at your own risk.
works on zig v0.12.0### Example usage
```shell
$ zig init
```
###### file-> build.zig```zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const pkg = b.dependency("zconn", .{
.target = target,
.optimize = optimize
});const example = pkg.builder.addExecutable(.{
.target = target,
.name = "example",
.root_source_file = .{ .path = "src/main.zig" },
.optimize = optimize,
.link_libc = true
});example.root_module.addImport("zconn", pkg.module("zconn"));
const libs_to_link = [_][]const u8{"mysqlclient","zstd","ssl", "crypto" ,"resolv" ,"m"};
example.linkLibC();
for(libs_to_link) |l| {
example.linkSystemLibrary(l);
}b.installArtifact(example);
}
```
###### file-> build.zig.zon
```zig
.{
.name = "mysql_example",.version = "0.0.0",
.dependencies = .{
.zconn = .{
// clone zconn from github
// replace this with path to clone
.path = "relative_path_to_clone",
},
},
.paths = .{
"",
},
}
```# Examples
#### single connection example
```zigconst std = @import("std");
const sql = @import("zconn");var gpa = @import("std").heap.GeneralPurposeAllocator(.{}){};
pub fn main() !void {
const allocator = gpa.allocator();
const conn = try sql.Connection.newConnection(allocator, .{
.username = "vic",
.databaseName = "events",
.password = "1234Victor",
.host = "localhost"
});const res = try conn.executeQuery("select 'hello world' as greeting;", .{});
if(res.nextResultSet()) |t| {
if(t.nextRow()) |r| {
const row = try r.columns.?.toString();
defer allocator.free(row);std.debug.print("{s}\n", .{row});
} else {
std.debug.print("Empty set\n", .{});
}
} else {
std.debug.print("Failed to query\n", .{});
}
}
```#### pool example
```zig
const std = @import("std");
const sql = @import("zconn");var gpa = @import("std").heap.GeneralPurposeAllocator(.{}){};
pub fn main() !void {
const allocator = gpa.allocator();
const pool = try sql.Pool.init(allocator,.{
.databaseName = "events",
.host = "localhost",
.password = "1234Victor",
.username = "vic"
},
4);
defer pool.deInit();const conn = pool.getConnection();
defer pool.dropConnection(conn);const res = try conn.executeQuery("select ? as Greeting", .{"hello world"});
defer res.deinit();}
```#### get single connection from pool example
```zig
const std = @import("std");
const sql = @import("zconn");var gpa = @import("std").heap.GeneralPurposeAllocator(.{}){};
pub fn main() !void {
const allocator = gpa.allocator();
const pool = try sql.Pool.init(allocator,.{
.databaseName = "events",
.host = "localhost",
.password = "1234Victor",
.username = "vic"
},
4);defer pool.deInit();
// get it
const connection = pool.getConnection();//drop it
defer pool.dropConnection(connection);//query
_ = try connection.executeQuery("select ? as Greeting", .{"hello world"});}
```#### single connection prepared statement example
```zigconst std = @import("std");
const sql = @import("zconn");var gpa = @import("std").heap.GeneralPurposeAllocator(.{}){};
pub fn main() !void {
const allocator = gpa.allocator();
const connection = try sql.Connection.newConnection(allocator,.{
.databaseName = "events",
.host = "localhost",
.password = "1234Victor",
.username = "vic"
});const stmt = try connection.prepare("select ? as Greeting");
defer stmt.close();// execute statement
var res1 = try stmt.execute(.{"hello world"});// free result 1
defer res1.deinit();//std.debug.print("{s}\n", .{res.});
// execute statement using another param
var res2 = try stmt.execute(.{"Good morning"});// free result 2
defer res2.deinit();if(res2.nextResultSet()) |re| {
while(re.nextRow()) |ro| {
for(0..ro.colCount) |i| {
std.debug.print("{s}\n", .{ro.columns.?.get(i).?});
}
}}
connection.close();
}
```#### prepared statement from pool example
```zig
const std = @import("std");
const sql = @import("zconn");var gpa = @import("std").heap.GeneralPurposeAllocator(.{}){};
pub fn main() !void {
const allocator = gpa.allocator();const pool = try sql.Pool.init(allocator,.{
.databaseName = "events",
.host = "localhost",
.password = "1234Victor",
.username = "vic"
},
4);defer pool.deInit();
const connection = pool.getConnection();
defer pool.dropConnection(connection);//create statement
const stmt = try connection.prepare("select ? as Greeting");//close statement
defer stmt.close();var res = try stmt.execute(.{"hello world"});
// free result
defer res.deinit();if(res.nextResultSet()) |re| {
while(re.nextRow()) |ro| {
const d = try ro.columns.?.toString();
defer allocator.free(d);
std.debug.print("{s}\n", .{d});
}}
}
```#### Known bugs
you need to free everything.