diff options
| author | 2022-03-19 01:50:28 -0300 | |
|---|---|---|
| committer | 2022-05-14 21:25:51 +0200 | |
| commit | 2a638374b3e0a4cc44aee1ba9136a55f4d8fa01c (patch) | |
| tree | e8b307c0a25d5789aa9c31e2fd568893465557af /sqlite.zig | |
| parent | add a test for a bind marker with an optional type (diff) | |
| download | zig-sqlite-2a638374b3e0a4cc44aee1ba9136a55f4d8fa01c.tar.gz zig-sqlite-2a638374b3e0a4cc44aee1ba9136a55f4d8fa01c.tar.xz zig-sqlite-2a638374b3e0a4cc44aee1ba9136a55f4d8fa01c.zip | |
add Db.runMulti
Diffstat (limited to '')
| -rw-r--r-- | sqlite.zig | 25 |
1 files changed, 24 insertions, 1 deletions
| @@ -876,6 +876,24 @@ pub const Db = struct { | |||
| 876 | return errors.errorFromResultCode(result); | 876 | return errors.errorFromResultCode(result); |
| 877 | } | 877 | } |
| 878 | } | 878 | } |
| 879 | |||
| 880 | /// This is a convenience function to run statements that do not need | ||
| 881 | /// bindings to values, but have multiple commands inside. | ||
| 882 | /// | ||
| 883 | /// Exmaple: 'create table a(); create table b();' | ||
| 884 | pub fn runMulti(self: *Self, comptime query: []const u8, options: QueryOptions) !void { | ||
| 885 | while (true) { | ||
| 886 | var sql_tail_ptr: ?[*:0]const u8 = null; | ||
| 887 | options.sql_tail_ptr = &sql_tail_ptr; | ||
| 888 | |||
| 889 | // continuously prepare and execute | ||
| 890 | var stmt = try self.prepareWithDiags(query, options); | ||
| 891 | defer stmt.deinit(); | ||
| 892 | if (sql_tail_ptr == null) break; | ||
| 893 | |||
| 894 | try stmt.exec(options, .{}); | ||
| 895 | } | ||
| 896 | } | ||
| 879 | }; | 897 | }; |
| 880 | 898 | ||
| 881 | /// Savepoint is a helper type for managing savepoints. | 899 | /// Savepoint is a helper type for managing savepoints. |
| @@ -996,6 +1014,11 @@ pub const Savepoint = struct { | |||
| 996 | pub const QueryOptions = struct { | 1014 | pub const QueryOptions = struct { |
| 997 | /// if provided, diags will be populated in case of failures. | 1015 | /// if provided, diags will be populated in case of failures. |
| 998 | diags: ?*Diagnostics = null, | 1016 | diags: ?*Diagnostics = null, |
| 1017 | |||
| 1018 | /// if provided, sql_tail_ptr will point to the last uncompiled statement | ||
| 1019 | /// in the prepare() call. this is useful for multiple-statements being | ||
| 1020 | /// processed. | ||
| 1021 | sql_tail_ptr: ?*?[*:0]const u8 = null, | ||
| 999 | }; | 1022 | }; |
| 1000 | 1023 | ||
| 1001 | /// Iterator allows iterating over a result set. | 1024 | /// Iterator allows iterating over a result set. |
| @@ -1532,7 +1555,7 @@ pub const DynamicStatement = struct { | |||
| 1532 | @intCast(c_int, query.len), | 1555 | @intCast(c_int, query.len), |
| 1533 | flags, | 1556 | flags, |
| 1534 | &tmp, | 1557 | &tmp, |
| 1535 | null, | 1558 | options.sql_tail_ptr, |
| 1536 | ); | 1559 | ); |
| 1537 | if (result != c.SQLITE_OK) { | 1560 | if (result != c.SQLITE_OK) { |
| 1538 | diags.err = getLastDetailedErrorFromDb(db.db); | 1561 | diags.err = getLastDetailedErrorFromDb(db.db); |