refactor: Migrar ArrayList a ArrayListUnmanaged (Zig 0.15)

- statement.zig: collectAll(), Row.toValues()
- vtable.zig: generateSchema(), EponymousModule.query()
- Documentación actualizada con sintaxis moderna

Cambio de API:
- std.ArrayList(T).init(alloc) → std.ArrayListUnmanaged(T) = .{}
- list.append(item) → list.append(alloc, item)
- list.deinit() → list.deinit(alloc)
- list.toOwnedSlice() → list.toOwnedSlice(alloc)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
R.Eugenio 2025-12-23 01:27:25 +01:00
parent 04c5d41956
commit 97de601dcd
2 changed files with 35 additions and 35 deletions

View file

@ -531,18 +531,18 @@ pub const Statement = struct {
/// Each row is represented as an array of column values. /// Each row is represented as an array of column values.
/// Caller owns the returned memory. /// Caller owns the returned memory.
pub fn collectAll(self: *Self, allocator: std.mem.Allocator) ![]Row.Values { pub fn collectAll(self: *Self, allocator: std.mem.Allocator) ![]Row.Values {
var rows = std.ArrayList(Row.Values).init(allocator); var rows: std.ArrayListUnmanaged(Row.Values) = .{};
errdefer { errdefer {
for (rows.items) |*r| r.deinit(allocator); for (rows.items) |*r| r.deinit(allocator);
rows.deinit(); rows.deinit(allocator);
} }
while (try self.step()) { while (try self.step()) {
const row = Row{ .stmt = self }; const row = Row{ .stmt = self };
try rows.append(try row.toValues(allocator)); try rows.append(allocator, try row.toValues(allocator));
} }
return rows.toOwnedSlice(); return rows.toOwnedSlice(allocator);
} }
}; };
@ -680,17 +680,17 @@ pub const Row = struct {
/// name: []const u8, // Will be allocated copy /// name: []const u8, // Will be allocated copy
/// }; /// };
/// ///
/// var users = std.ArrayList(User).init(allocator); /// var users: std.ArrayListUnmanaged(User) = .{};
/// defer { /// defer {
/// for (users.items) |user| Row.freeStruct(User, user, allocator); /// for (users.items) |user| Row.freeStruct(User, user, allocator);
/// users.deinit(); /// users.deinit(allocator);
/// } /// }
/// ///
/// var stmt = try db.prepare("SELECT id, name FROM users"); /// var stmt = try db.prepare("SELECT id, name FROM users");
/// var iter = stmt.iterator(); /// var iter = stmt.iterator();
/// while (try iter.next()) |row| { /// while (try iter.next()) |row| {
/// const user = try row.toAlloc(User, allocator); /// const user = try row.toAlloc(User, allocator);
/// try users.append(user); /// try users.append(allocator, user);
/// } /// }
/// ``` /// ```
pub fn toAlloc(self: Self, comptime T: type, allocator: std.mem.Allocator) !T { pub fn toAlloc(self: Self, comptime T: type, allocator: std.mem.Allocator) !T {
@ -862,10 +862,10 @@ pub const Row = struct {
const items = try allocator.alloc(Value, count); const items = try allocator.alloc(Value, count);
errdefer allocator.free(items); errdefer allocator.free(items);
var text_copies = std.ArrayList([]u8).init(allocator); var text_copies: std.ArrayListUnmanaged([]u8) = .{};
errdefer { errdefer {
for (text_copies.items) |copy| allocator.free(copy); for (text_copies.items) |copy| allocator.free(copy);
text_copies.deinit(); text_copies.deinit(allocator);
} }
for (0..count) |i| { for (0..count) |i| {
@ -877,7 +877,7 @@ pub const Row = struct {
.text => blk: { .text => blk: {
if (self.text(idx)) |t| { if (self.text(idx)) |t| {
const copy = try allocator.dupe(u8, t); const copy = try allocator.dupe(u8, t);
try text_copies.append(copy); try text_copies.append(allocator, copy);
break :blk .{ .text = copy }; break :blk .{ .text = copy };
} }
break :blk .{ .null_value = {} }; break :blk .{ .null_value = {} };
@ -885,7 +885,7 @@ pub const Row = struct {
.blob => blk: { .blob => blk: {
if (self.blob(idx)) |b| { if (self.blob(idx)) |b| {
const copy = try allocator.dupe(u8, b); const copy = try allocator.dupe(u8, b);
try text_copies.append(copy); try text_copies.append(allocator, copy);
break :blk .{ .blob = copy }; break :blk .{ .blob = copy };
} }
break :blk .{ .null_value = {} }; break :blk .{ .null_value = {} };

View file

@ -164,26 +164,26 @@ pub fn SimpleVTable(comptime Row: type) type {
/// Generates CREATE TABLE statement for a virtual table schema. /// Generates CREATE TABLE statement for a virtual table schema.
pub fn generateSchema(allocator: std.mem.Allocator, table_name: []const u8, columns: []const ColumnDef) ![]u8 { pub fn generateSchema(allocator: std.mem.Allocator, table_name: []const u8, columns: []const ColumnDef) ![]u8 {
var sql = std.ArrayList(u8).init(allocator); var sql: std.ArrayListUnmanaged(u8) = .{};
errdefer sql.deinit(); errdefer sql.deinit(allocator);
try sql.appendSlice("CREATE TABLE "); try sql.appendSlice(allocator, "CREATE TABLE ");
try sql.appendSlice(table_name); try sql.appendSlice(allocator, table_name);
try sql.appendSlice("("); try sql.appendSlice(allocator, "(");
for (columns, 0..) |col, i| { for (columns, 0..) |col, i| {
if (i > 0) try sql.appendSlice(", "); if (i > 0) try sql.appendSlice(allocator, ", ");
try sql.appendSlice(col.name); try sql.appendSlice(allocator, col.name);
try sql.append(' '); try sql.append(allocator, ' ');
try sql.appendSlice(col.col_type); try sql.appendSlice(allocator, col.col_type);
if (col.constraints) |cons| { if (col.constraints) |cons| {
try sql.append(' '); try sql.append(allocator, ' ');
try sql.appendSlice(cons); try sql.appendSlice(allocator, cons);
} }
} }
try sql.appendSlice(")"); try sql.appendSlice(allocator, ")");
return sql.toOwnedSlice(); return sql.toOwnedSlice(allocator);
} }
/// Helper to set a result value in a sqlite3_context. /// Helper to set a result value in a sqlite3_context.
@ -279,22 +279,22 @@ pub const EponymousModule = struct {
/// Generates SQL to query an eponymous module. /// Generates SQL to query an eponymous module.
pub fn query(self: *const EponymousModule, allocator: std.mem.Allocator, args: []const []const u8) ![]u8 { pub fn query(self: *const EponymousModule, allocator: std.mem.Allocator, args: []const []const u8) ![]u8 {
var sql = std.ArrayList(u8).init(allocator); var sql: std.ArrayListUnmanaged(u8) = .{};
errdefer sql.deinit(); errdefer sql.deinit(allocator);
try sql.appendSlice("SELECT * FROM "); try sql.appendSlice(allocator, "SELECT * FROM ");
try sql.appendSlice(self.name); try sql.appendSlice(allocator, self.name);
try sql.append('('); try sql.append(allocator, '(');
for (args, 0..) |arg, i| { for (args, 0..) |arg, i| {
if (i > 0) try sql.appendSlice(", "); if (i > 0) try sql.appendSlice(allocator, ", ");
try sql.append('\''); try sql.append(allocator, '\'');
try sql.appendSlice(arg); try sql.appendSlice(allocator, arg);
try sql.append('\''); try sql.append(allocator, '\'');
} }
try sql.append(')'); try sql.append(allocator, ')');
return sql.toOwnedSlice(); return sql.toOwnedSlice(allocator);
} }
}; };