{"id":20023075,"url":"https://github.com/2lchain/mysql","last_synced_at":"2025-05-05T01:31:42.504Z","repository":{"id":211162217,"uuid":"727712947","full_name":"2lchain/mysql","owner":"2lchain","description":"simple Mysql connection using mysql-c-api-8.0","archived":false,"fork":false,"pushed_at":"2024-03-03T16:19:37.000Z","size":81,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-08T15:05:10.082Z","etag":null,"topics":["mysql","zig"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/2lchain.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-12-05T12:27:30.000Z","updated_at":"2024-12-14T20:11:24.000Z","dependencies_parsed_at":"2023-12-17T20:45:36.637Z","dependency_job_id":"34b0af8e-dd04-46d2-b4bc-a1fd6a8c0c95","html_url":"https://github.com/2lchain/mysql","commit_stats":null,"previous_names":["nflvic/zcon","xmm15/zcon","wojciechmurimi/zcon","murymi/zcon","bxvyaw1p/mysql","2lchain/mysql"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2lchain%2Fmysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2lchain%2Fmysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2lchain%2Fmysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2lchain%2Fmysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/2lchain","download_url":"https://codeload.github.com/2lchain/mysql/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252423323,"owners_count":21745570,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mysql","zig"],"created_at":"2024-11-13T08:44:14.691Z","updated_at":"2025-05-05T01:31:42.237Z","avatar_url":"https://github.com/2lchain.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n#### Disclaimer: This is just a simple project to learn zig. Even though it works, Use it at your own risk.\nworks on  zig v0.12.0\n\n### Example usage \n\n```shell\n$ zig init\n```\n###### file-\u003e build.zig\n\n```zig\n\nconst std = @import(\"std\");\n\npub fn build(b: *std.Build) void {\n\n    const target = b.standardTargetOptions(.{});\n\n    const optimize = b.standardOptimizeOption(.{});\n\n    const pkg = b.dependency(\"zconn\", .{\n        .target = target,\n        .optimize = optimize\n    });\n\n    const example = pkg.builder.addExecutable(.{\n        .target = target,\n        .name = \"example\",\n        .root_source_file = .{ .path = \"src/main.zig\" },\n        .optimize = optimize,\n        .link_libc = true\n    });\n\n    example.root_module.addImport(\"zconn\", pkg.module(\"zconn\"));\n\n    const libs_to_link = [_][]const u8{\"mysqlclient\",\"zstd\",\"ssl\", \"crypto\" ,\"resolv\" ,\"m\"};\n\n    example.linkLibC();\n\t\n    for(libs_to_link) |l| {\n        example.linkSystemLibrary(l);\n    }\n\n    b.installArtifact(example);\n\n}\n\n```\n\n###### file-\u003e build.zig.zon\n\n```zig\n.{\n    .name = \"mysql_example\",\n\n    .version = \"0.0.0\",\n\n    .dependencies = .{\n\n        .zconn = .{\n            // clone zconn from github\n            // replace this with path to clone\n            .path = \"relative_path_to_clone\",\n        },\n    },\n\n \n    .paths = .{\n        \"\",\n    },\n}\n```\n\n# Examples\n#### single connection example\n```zig\n\nconst std = @import(\"std\");\nconst sql = @import(\"zconn\");\n\nvar gpa = @import(\"std\").heap.GeneralPurposeAllocator(.{}){};\n\npub fn main() !void {\n    const allocator = gpa.allocator();\n    const conn = try sql.Connection.newConnection(allocator, .{ \n                                                                .username = \"vic\",\n                                                                .databaseName = \"events\",\n                                                                .password = \"1234Victor\",\n                                                                .host = \"localhost\"\n                                                                });\n\n\n    const res = try conn.executeQuery(\"select 'hello world' as greeting;\", .{});\n\n    if(res.nextResultSet()) |t| {\n        if(t.nextRow()) |r| {\n            const row = try r.columns.?.toString();\n            defer allocator.free(row);\n\n            std.debug.print(\"{s}\\n\", .{row});\n        } else {\n            std.debug.print(\"Empty set\\n\", .{});\n        }\n    } else {\n        std.debug.print(\"Failed to query\\n\", .{});\n    }\n }   \n```\n\n\n#### pool example\n\n```zig\n\nconst std = @import(\"std\");\nconst sql = @import(\"zconn\");\n\nvar gpa = @import(\"std\").heap.GeneralPurposeAllocator(.{}){};\n\npub fn main() !void {\n\n    const allocator = gpa.allocator();\n\n    const pool = try sql.Pool.init(allocator,.{ \n        .databaseName = \"events\",\n         .host = \"localhost\",\n          .password = \"1234Victor\",\n           .username = \"vic\" \n           },\n           4);\n    defer pool.deInit();\n\n    const conn = pool.getConnection();\n    defer pool.dropConnection(conn);\n\n    const res = try conn.executeQuery(\"select ? as Greeting\", .{\"hello world\"});\n    defer res.deinit();\n\n }   \n```\n\n#### get single connection from pool example\n\n```zig\n\nconst std = @import(\"std\");\nconst sql = @import(\"zconn\");\n\nvar gpa = @import(\"std\").heap.GeneralPurposeAllocator(.{}){};\n\npub fn main() !void {\n\n    const allocator = gpa.allocator();\n\n    const pool = try sql.Pool.init(allocator,.{ \n        .databaseName = \"events\",\n         .host = \"localhost\",\n          .password = \"1234Victor\",\n           .username = \"vic\" \n           },\n           4);\n\n    defer pool.deInit();\n\n    // get it\n    const connection = pool.getConnection();\n\n    //drop it\n    defer pool.dropConnection(connection);\n\n    //query\n    _ = try connection.executeQuery(\"select ? as Greeting\", .{\"hello world\"});\n\n }   \n```\n\n#### single connection prepared statement example\n```zig\n\nconst std = @import(\"std\");\nconst sql = @import(\"zconn\");\n\nvar gpa = @import(\"std\").heap.GeneralPurposeAllocator(.{}){};\n\npub fn main() !void {\n\n    const allocator = gpa.allocator();\n\n    const connection = try sql.Connection.newConnection(allocator,.{ \n        .databaseName = \"events\",\n         .host = \"localhost\",\n          .password = \"1234Victor\",\n           .username = \"vic\" \n           });\n\n    const stmt = try connection.prepare(\"select ? as Greeting\");\n    defer stmt.close();\n\n    // execute statement\n    var res1 = try stmt.execute(.{\"hello world\"});\n\n    // free result 1\n    defer res1.deinit();\n\n    //std.debug.print(\"{s}\\n\", .{res.});\n\n\n    // execute statement using another param\n    var res2 = try stmt.execute(.{\"Good morning\"});\n\n    // free result 2\n    defer res2.deinit();\n\n    if(res2.nextResultSet()) |re| {\n        while(re.nextRow()) |ro| {\n            for(0..ro.colCount) |i| {\n                std.debug.print(\"{s}\\n\", .{ro.columns.?.get(i).?});\n            }\n        }\n\n    }\n\n    connection.close();\n }   \n```\n\n#### prepared statement from pool example\n\n```zig\n\nconst std = @import(\"std\");\nconst sql = @import(\"zconn\");\n\nvar gpa = @import(\"std\").heap.GeneralPurposeAllocator(.{}){};\n\npub fn main() !void {\n    const allocator = gpa.allocator();\n\n    const pool = try sql.Pool.init(allocator,.{ \n        .databaseName = \"events\",\n         .host = \"localhost\",\n          .password = \"1234Victor\",\n           .username = \"vic\" \n           },\n           4);\n\n    defer pool.deInit();\n\n    const connection = pool.getConnection();\n    defer pool.dropConnection(connection);\n\n    //create statement\n    const stmt = try connection.prepare(\"select ? as Greeting\");\n\n    //close statement\n    defer stmt.close();\n\n    var res = try stmt.execute(.{\"hello world\"});\n\n    // free result\n    defer res.deinit();\n\n    if(res.nextResultSet()) |re| {\n        while(re.nextRow()) |ro| {\n            const d = try ro.columns.?.toString();\n            defer allocator.free(d);\n            std.debug.print(\"{s}\\n\", .{d});\n        }\n\n    }\n\n }   \n```\n\n#### Known bugs\nyou need to free everything.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F2lchain%2Fmysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F2lchain%2Fmysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F2lchain%2Fmysql/lists"}