tiny.sql.StatementCursor
Defined in tiny.sql.
API (20)
Actions
Public operations.
Fields and members
Public fields and members.
accessallocatorfieldspredicate_valuespredicatesreaderrelationrow_bufferstatetable_rowwindow
Source
Source: lib/sql/src/statement/cursor.zig:42
zig
pub const Cursor = struct { allocator: Allocator, relation: *plan.PreparedRelation, reader: ?plan.RelationRead, fields: []const execute_mod.ProjectedColumn, access: access_mod.SelectAccess, predicates: []const ast_mod.Predicate, predicate_values: []row.Value, state: CursorState, window: execute_mod.Window = .{}, row_buffer: std.ArrayList(u8) = .empty, table_row: ?[]u8 = null, pub fn deinit(self: *Cursor) void { self.releaseTableRow(); switch (self.state) { .empty => {}, .single => |single| if (single.bytes) |bytes| self.allocator.free(bytes), .rowid => |*scan| scan.deinit(), .index => |*index_cursor| index_cursor.scan.deinit(), .scan => |*scan| scan.deinit(), .sorted => |sorted| { result_mod.freeSelectedRows(self.allocator, sorted.rows); self.allocator.free(sorted.rows); }, } if (self.reader) |*reader| reader.deinit(); self.row_buffer.deinit(self.allocator); for (self.predicate_values) |value| result_mod.freeValue(self.allocator, value); self.allocator.free(self.predicate_values); self.allocator.free(self.predicates); self.* = undefined; } pub fn next(self: *Cursor) ast_mod.Error!?[]const u8 { const phase = trace.scope("statement.cursor.next"); defer phase.end(); self.releaseTableRow(); if (self.window.full()) return null; return switch (self.state) { .empty => null, .single => |*single| self.nextSingle(single), .rowid => |*scan| self.nextRowid(scan), .index => |*index_cursor| self.nextIndex(index_cursor), .scan => |*scan| self.nextScan(scan), .sorted => |*sorted| self.nextSorted(sorted), }; } pub fn nextSingle(self: *Cursor, single: *SingleCursor) ast_mod.Error!?[]const u8 { if (single.emitted) return null; single.emitted = true; if (!self.window.admit()) return null; return single.bytes; } pub fn nextSorted(self: *Cursor, sorted: *SortedCursor) ast_mod.Error!?[]const u8 { _ = self; if (sorted.index >= sorted.rows.len) return null; const bytes = sorted.rows[sorted.index]; sorted.index += 1; return bytes; } pub fn nextRowid(self: *Cursor, scan: *relation_mod.Scan) ast_mod.Error!?[]const u8 { const access = switch (self.access) { .rowid => |rowid_access| rowid_access, else => unreachable, }; const rowid = try predicate_mod.rowidFromValue(self.predicate_values[access.predicate_index]); while (try scan.next()) |entry| { if (!predicate_mod.rowidMatches(entry.rowid, access.operator, rowid)) continue; if (!try predicate_mod.rowBytesMatchPredicates(self.predicates, self.predicate_values, &self.relation.handle, entry.rowid, entry.bytes)) continue; if (!self.window.admit()) continue; return try self.tableEntry(entry.rowid, entry.bytes); } return null; } pub fn nextIndex(self: *Cursor, index_cursor: *IndexCursor) ast_mod.Error!?[]const u8 { const index_slot = switch (self.access) { .index => |index_access| index_access.index_slot, else => unreachable, }; while (try index_cursor.scan.next()) |entry| { if (index_cursor.covered) { var index_values: [catalog_mod.max_columns]row.Value = undefined; var index_scratch: [page.size]u8 = undefined; const values = try predicate_mod.coveredIndexValues( self.predicates, self.predicate_values, &self.relation.handle, index_slot, entry, &index_values, &index_scratch, ) orelse continue; if (!self.window.admit()) continue; return try result_mod.selectedIndexRowInto( self.allocator, &self.row_buffer, self.fields, &self.relation.handle, index_slot, values, entry.rowid, ); } const reader = if (self.reader) |*value| value else unreachable; const bytes = (try reader.get(self.allocator, entry.rowid)) orelse continue; self.table_row = bytes; if (!try predicate_mod.rowBytesMatchPredicates(self.predicates, self.predicate_values, &self.relation.handle, entry.rowid, bytes)) { self.releaseTableRow(); continue; } if (!self.window.admit()) { self.releaseTableRow(); continue; } if (self.fields.len == 0) return bytes; const projected = try result_mod.selectedRowInto(self.allocator, &self.row_buffer, self.fields, entry.rowid, bytes); self.releaseTableRow(); return projected; } return null; } pub fn nextScan(self: *Cursor, scan: *relation_mod.Scan) ast_mod.Error!?[]const u8 { while (try scan.next()) |entry| { if (!try predicate_mod.rowBytesMatchPredicates(self.predicates, self.predicate_values, &self.relation.handle, entry.rowid, entry.bytes)) continue; if (!self.window.admit()) continue; return try self.tableEntry(entry.rowid, entry.bytes); } return null; } pub fn tableEntry(self: *Cursor, rowid: i64, bytes: []const u8) ast_mod.Error![]const u8 { if (self.fields.len == 0) return bytes; return try result_mod.selectedRowInto(self.allocator, &self.row_buffer, self.fields, rowid, bytes); } pub fn releaseTableRow(self: *Cursor) void { if (self.table_row) |bytes| { self.allocator.free(bytes); self.table_row = null; } }};Source: lib/sql/src/root.zig:176
zig
pub const StatementCursor = statement.Cursor;Also reachable as
Audit
| Definitions | 10 |
|---|---|
| Public names | 20 |
| Members | 11 |
| Version | 26.7.0 |
| Revision | daab053ee433 |