@native-sdk/cli 0.8.4 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/app.zig +1178 -74
- package/build.zig +715 -17
- package/build.zig.zon +1 -0
- package/native-sdk.d.ts +10 -0
- package/package.json +12 -11
- package/packages/core/compile-surface/core.ts +435 -3
- package/packages/core/package-lock.json +11 -11
- package/packages/core/package.json +2 -2
- package/packages/core/scripts/compiler_typecheck.mjs +116 -15
- package/packages/core/scripts/gen_service_surface.mjs +268 -0
- package/packages/core/scripts/merge_static_archives.mjs +78 -0
- package/packages/core/scripts/npm_static_spike.mjs +64 -0
- package/packages/core/scripts/run_external_core_compiler.mjs +122 -6
- package/packages/core/scripts/run_external_service_compiler.mjs +280 -0
- package/packages/core/scripts/stage_external_core.mjs +9 -1
- package/packages/core/scripts/stage_external_services.mjs +172 -0
- package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
- package/packages/core/scripts/vendor_service_packages.mjs +189 -0
- package/packages/core/sdk/core.d.ts +224 -0
- package/packages/core/sdk/core.ts +694 -22
- package/packages/core/sdk/events.d.ts +89 -0
- package/packages/core/sdk/events.ts +128 -0
- package/packages/core/src/checker.ts +936 -15
- package/packages/core/src/cli.ts +156 -3
- package/packages/core/src/contract.ts +28 -5
- package/packages/core/src/devhost.ts +1876 -16
- package/packages/core/src/devhost_journal.mjs +183 -0
- package/packages/core/src/devhost_resolver.mjs +17 -1
- package/packages/core/src/devhost_service_worker.mjs +90 -0
- package/packages/core/src/diagnostics.ts +179 -22
- package/packages/core/src/frontend.ts +210 -34
- package/packages/core/src/infer.ts +24 -0
- package/packages/core/src/modules.ts +129 -80
- package/packages/core/src/service_contract.ts +597 -0
- package/packages/core/src/sqlite_cli.ts +93 -0
- package/packages/core/src/sqlite_codegen.ts +806 -0
- package/packages/core/src/sqlite_runtime_policy.ts +364 -0
- package/packages/core/src/typed_ast.ts +36 -8
- package/skill-data/automation/SKILL.md +1 -1
- package/skill-data/core/references/project-anatomy.md +1 -1
- package/skill-data/native-ui/SKILL.md +58 -5
- package/skill-data/ts-core/SKILL.md +123 -41
- package/skill-data/ts-services/SKILL.md +126 -0
- package/skill-data/ts-services/references/service-surface.md +604 -0
- package/skill-data/zig/SKILL.md +1 -1
- package/skills/native-sdk/SKILL.md +1 -1
- package/src/app_runner/no_migrations.zig +3 -0
- package/src/app_runner/no_services.zig +20 -0
- package/src/app_runner/root.zig +133 -4
- package/src/app_runner/ts_core_main.zig +254 -8
- package/src/app_runner/ts_core_mobile.zig +241 -0
- package/src/automation/protocol.zig +5 -1
- package/src/automation/snapshot.zig +31 -11
- package/src/embed/app_exports.zig +12 -1
- package/src/embed/c_api.zig +48 -2
- package/src/embed/conversions.zig +4 -5
- package/src/embed/host.zig +109 -1
- package/src/embed/root.zig +8 -0
- package/src/embed/tests.zig +241 -0
- package/src/embed/types.zig +51 -0
- package/src/embed/ui_host.zig +185 -5
- package/src/platform/android/NativeSdkActivity.java +131 -0
- package/src/platform/android/android_host.c +182 -1
- package/src/platform/android/native_sdk_app.h +17 -0
- package/src/platform/ios/native_sdk_app.h +17 -0
- package/src/platform/ios/uikit_host.m +115 -0
- package/src/platform/linux/cef_host.cpp +8 -2
- package/src/platform/linux/gtk_host.c +289 -22
- package/src/platform/linux/gtk_host.h +5 -1
- package/src/platform/linux/root.zig +109 -22
- package/src/platform/macos/appkit_host.h +24 -7
- package/src/platform/macos/appkit_host.m +686 -64
- package/src/platform/macos/cef_host.mm +400 -66
- package/src/platform/macos/root.zig +304 -47
- package/src/platform/null_platform.zig +435 -48
- package/src/platform/null_platform_tests.zig +113 -0
- package/src/platform/root.zig +19 -0
- package/src/platform/types.zig +281 -28
- package/src/platform/windows/root.zig +207 -27
- package/src/platform/windows/webview2_host.cpp +334 -51
- package/src/primitives/app_manifest/comptime_scene.zig +2 -0
- package/src/primitives/app_manifest/root.zig +4 -0
- package/src/primitives/app_manifest/tests.zig +3 -0
- package/src/primitives/app_manifest/types.zig +32 -0
- package/src/primitives/app_manifest/validation.zig +19 -1
- package/src/root.zig +53 -0
- package/src/runtime/api.zig +19 -0
- package/src/runtime/automation_commands.zig +35 -2
- package/src/runtime/automation_snapshot.zig +32 -20
- package/src/runtime/bridge_permission_tests.zig +58 -3
- package/src/runtime/bridge_responses.zig +4 -2
- package/src/runtime/builtin_bridge.zig +11 -1
- package/src/runtime/canvas_frame.zig +18 -11
- package/src/runtime/canvas_frame_animation_tests.zig +4 -0
- package/src/runtime/clock.zig +13 -1
- package/src/runtime/core.zig +145 -7
- package/src/runtime/credentials_store.zig +115 -0
- package/src/runtime/effects.zig +3701 -231
- package/src/runtime/effects_credentials_tests.zig +332 -0
- package/src/runtime/effects_file_tests.zig +587 -0
- package/src/runtime/effects_host_tests.zig +208 -1
- package/src/runtime/effects_notification_tests.zig +11 -0
- package/src/runtime/effects_relational_tests.zig +314 -0
- package/src/runtime/effects_store_tests.zig +211 -0
- package/src/runtime/file_access.zig +400 -0
- package/src/runtime/file_effect_crash_helper.zig +28 -0
- package/src/runtime/file_effect_crash_tests.zig +37 -0
- package/src/runtime/flow.zig +35 -10
- package/src/runtime/gpu_surface_events.zig +6 -1
- package/src/runtime/native_command_event_tests.zig +86 -2
- package/src/runtime/persist_store.zig +666 -0
- package/src/runtime/pty.zig +5 -2
- package/src/runtime/record_store.zig +788 -0
- package/src/runtime/relational_store.zig +854 -0
- package/src/runtime/root.zig +54 -0
- package/src/runtime/runtime_event_tests.zig +70 -0
- package/src/runtime/service_host.zig +711 -0
- package/src/runtime/service_pool.zig +1314 -0
- package/src/runtime/session_blobs.zig +4 -1
- package/src/runtime/session_journal.zig +158 -5
- package/src/runtime/session_record.zig +215 -0
- package/src/runtime/session_replay.zig +206 -11
- package/src/runtime/session_tests.zig +81 -0
- package/src/runtime/shell_layout.zig +1 -0
- package/src/runtime/shell_layout_tests.zig +120 -0
- package/src/runtime/sqlite_engine.zig +474 -0
- package/src/runtime/state.zig +16 -0
- package/src/runtime/system_services.zig +170 -38
- package/src/runtime/tests.zig +4 -0
- package/src/runtime/ts_core_host.zig +1067 -95
- package/src/runtime/ts_core_host_tests.zig +325 -11
- package/src/runtime/ts_ui_app.zig +953 -2
- package/src/runtime/ui_app.zig +434 -96
- package/src/runtime/ui_app_tests.zig +287 -11
- package/src/runtime/ui_app_window_tests.zig +61 -7
- package/src/runtime/validation.zig +42 -0
- package/src/runtime/view.zig +9 -0
- package/src/runtime/window_storage.zig +1 -0
- package/src/runtime/window_view_runtime.zig +2 -3
- package/src/runtime/window_views.zig +19 -0
- package/src/tooling/android.zig +21 -16
- package/src/tooling/db.zig +160 -0
- package/src/tooling/ios.zig +7 -1
- package/src/tooling/manifest.zig +214 -2
- package/src/tooling/package.zig +116 -0
- package/src/tooling/raw_manifest.zig +30 -0
- package/src/tooling/root.zig +1 -0
- package/src/tooling/templates.zig +189 -4
- package/src/tooling/ts_core.zig +366 -17
- package/src/tooling/verbs.zig +7 -0
- package/third_party/sqlite/README.md +13 -0
- package/third_party/sqlite/sqlite3.c +269649 -0
- package/third_party/sqlite/sqlite3.h +14349 -0
- package/tools/corewire/core_abi.zig +14 -2
- package/tools/corewire/emit.zig +98 -29
- package/tools/corewire/emit_facade.zig +442 -27
- package/tools/corewire/emit_profile.zig +6 -2
- package/tools/corewire/emit_service.zig +924 -0
- package/tools/corewire/main.zig +122 -0
- package/tools/corewire/service_contract.zig +250 -0
- package/tools/corewire/sidecar.zig +26 -15
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
//! Small internal SQLite seam shared by the record store and the relational
|
|
2
|
+
//! storage tier. SQLite remains an implementation detail: apps receive inert
|
|
3
|
+
//! effects, never a synchronous database handle.
|
|
4
|
+
|
|
5
|
+
const std = @import("std");
|
|
6
|
+
|
|
7
|
+
/// The tiny stable SQLite C ABI this engine uses. Keeping the declarations
|
|
8
|
+
/// here instead of translating sqlite3.h is load-bearing for capability
|
|
9
|
+
/// shedding: every app may import the SDK's effect types, while only apps
|
|
10
|
+
/// declaring `store` or `sqlite` compile and link the amalgamation itself.
|
|
11
|
+
/// SQLite's public ABI and these numeric constants are explicitly stable.
|
|
12
|
+
const c = struct {
|
|
13
|
+
const sqlite3 = opaque {};
|
|
14
|
+
const sqlite3_stmt = opaque {};
|
|
15
|
+
|
|
16
|
+
const SQLITE_OK: c_int = 0;
|
|
17
|
+
const SQLITE_ERROR: c_int = 1;
|
|
18
|
+
const SQLITE_BUSY: c_int = 5;
|
|
19
|
+
const SQLITE_LOCKED: c_int = 6;
|
|
20
|
+
const SQLITE_IOERR: c_int = 10;
|
|
21
|
+
const SQLITE_CORRUPT: c_int = 11;
|
|
22
|
+
const SQLITE_CONSTRAINT: c_int = 19;
|
|
23
|
+
const SQLITE_MISUSE: c_int = 21;
|
|
24
|
+
const SQLITE_AUTH: c_int = 23;
|
|
25
|
+
const SQLITE_NOTADB: c_int = 26;
|
|
26
|
+
const SQLITE_ROW: c_int = 100;
|
|
27
|
+
const SQLITE_DONE: c_int = 101;
|
|
28
|
+
|
|
29
|
+
const SQLITE_INTEGER: c_int = 1;
|
|
30
|
+
const SQLITE_FLOAT: c_int = 2;
|
|
31
|
+
const SQLITE_TEXT: c_int = 3;
|
|
32
|
+
const SQLITE_BLOB: c_int = 4;
|
|
33
|
+
const SQLITE_NULL: c_int = 5;
|
|
34
|
+
|
|
35
|
+
const SQLITE_OK_AUTHORIZER: c_int = 0;
|
|
36
|
+
const SQLITE_DENY_AUTHORIZER: c_int = 1;
|
|
37
|
+
const SQLITE_CREATE_INDEX: c_int = 1;
|
|
38
|
+
const SQLITE_CREATE_TEMP_INDEX: c_int = 3;
|
|
39
|
+
const SQLITE_CREATE_TEMP_TABLE: c_int = 4;
|
|
40
|
+
const SQLITE_CREATE_TEMP_TRIGGER: c_int = 5;
|
|
41
|
+
const SQLITE_CREATE_TEMP_VIEW: c_int = 6;
|
|
42
|
+
const SQLITE_CREATE_VIEW: c_int = 8;
|
|
43
|
+
const SQLITE_DROP_INDEX: c_int = 10;
|
|
44
|
+
const SQLITE_DROP_TEMP_INDEX: c_int = 12;
|
|
45
|
+
const SQLITE_DROP_TEMP_TABLE: c_int = 13;
|
|
46
|
+
const SQLITE_DROP_TEMP_TRIGGER: c_int = 14;
|
|
47
|
+
const SQLITE_DROP_TEMP_VIEW: c_int = 15;
|
|
48
|
+
const SQLITE_DROP_VIEW: c_int = 17;
|
|
49
|
+
const SQLITE_PRAGMA: c_int = 19;
|
|
50
|
+
const SQLITE_TRANSACTION: c_int = 22;
|
|
51
|
+
const SQLITE_ATTACH: c_int = 24;
|
|
52
|
+
const SQLITE_DETACH: c_int = 25;
|
|
53
|
+
const SQLITE_ALTER_TABLE: c_int = 26;
|
|
54
|
+
const SQLITE_CREATE_VTABLE: c_int = 29;
|
|
55
|
+
const SQLITE_DROP_VTABLE: c_int = 30;
|
|
56
|
+
const SQLITE_SAVEPOINT: c_int = 32;
|
|
57
|
+
|
|
58
|
+
const SQLITE_INSERT: c_int = 18;
|
|
59
|
+
const SQLITE_DELETE: c_int = 9;
|
|
60
|
+
const SQLITE_UPDATE: c_int = 23;
|
|
61
|
+
|
|
62
|
+
const SQLITE_OPEN_READWRITE: c_int = 0x00000002;
|
|
63
|
+
const SQLITE_OPEN_CREATE: c_int = 0x00000004;
|
|
64
|
+
const SQLITE_OPEN_FULLMUTEX: c_int = 0x00010000;
|
|
65
|
+
|
|
66
|
+
// Keep the references strong: a store-capable build that forgets to link
|
|
67
|
+
// the engine must fail at link time. The ordinary TestHarness is a
|
|
68
|
+
// separate type from RecordStoreTestHarness, so no-store app tests never
|
|
69
|
+
// analyze these calls merely through SDK reflection.
|
|
70
|
+
const linkage: std.builtin.GlobalLinkage = .strong;
|
|
71
|
+
const sqlite3_open_v2 = @extern(*allowzero const fn ([*:0]const u8, *?*sqlite3, c_int, ?[*:0]const u8) callconv(.c) c_int, .{ .name = "sqlite3_open_v2", .linkage = linkage });
|
|
72
|
+
const sqlite3_close_v2 = @extern(*allowzero const fn (*sqlite3) callconv(.c) c_int, .{ .name = "sqlite3_close_v2", .linkage = linkage });
|
|
73
|
+
const sqlite3_exec = @extern(*allowzero const fn (*sqlite3, [*:0]const u8, ?*const anyopaque, ?*anyopaque, ?*?[*:0]u8) callconv(.c) c_int, .{ .name = "sqlite3_exec", .linkage = linkage });
|
|
74
|
+
const sqlite3_set_authorizer = @extern(*allowzero const fn (*sqlite3, ?*const fn (?*anyopaque, c_int, ?[*:0]const u8, ?[*:0]const u8, ?[*:0]const u8, ?[*:0]const u8) callconv(.c) c_int, ?*anyopaque) callconv(.c) c_int, .{ .name = "sqlite3_set_authorizer", .linkage = linkage });
|
|
75
|
+
const sqlite3_update_hook = @extern(*allowzero const fn (*sqlite3, ?*const fn (?*anyopaque, c_int, ?[*:0]const u8, ?[*:0]const u8, i64) callconv(.c) void, ?*anyopaque) callconv(.c) ?*anyopaque, .{ .name = "sqlite3_update_hook", .linkage = linkage });
|
|
76
|
+
const sqlite3_prepare_v2 = @extern(*allowzero const fn (*sqlite3, [*:0]const u8, c_int, *?*sqlite3_stmt, ?*?[*:0]const u8) callconv(.c) c_int, .{ .name = "sqlite3_prepare_v2", .linkage = linkage });
|
|
77
|
+
const sqlite3_finalize = @extern(*allowzero const fn (*sqlite3_stmt) callconv(.c) c_int, .{ .name = "sqlite3_finalize", .linkage = linkage });
|
|
78
|
+
const sqlite3_reset = @extern(*allowzero const fn (*sqlite3_stmt) callconv(.c) c_int, .{ .name = "sqlite3_reset", .linkage = linkage });
|
|
79
|
+
const sqlite3_clear_bindings = @extern(*allowzero const fn (*sqlite3_stmt) callconv(.c) c_int, .{ .name = "sqlite3_clear_bindings", .linkage = linkage });
|
|
80
|
+
const sqlite3_bind_null = @extern(*allowzero const fn (*sqlite3_stmt, c_int) callconv(.c) c_int, .{ .name = "sqlite3_bind_null", .linkage = linkage });
|
|
81
|
+
const sqlite3_bind_int64 = @extern(*allowzero const fn (*sqlite3_stmt, c_int, i64) callconv(.c) c_int, .{ .name = "sqlite3_bind_int64", .linkage = linkage });
|
|
82
|
+
const sqlite3_bind_double = @extern(*allowzero const fn (*sqlite3_stmt, c_int, f64) callconv(.c) c_int, .{ .name = "sqlite3_bind_double", .linkage = linkage });
|
|
83
|
+
const sqlite3_bind_text = @extern(*allowzero const fn (*sqlite3_stmt, c_int, [*]const u8, c_int, ?*const anyopaque) callconv(.c) c_int, .{ .name = "sqlite3_bind_text", .linkage = linkage });
|
|
84
|
+
const sqlite3_bind_zeroblob64 = @extern(*allowzero const fn (*sqlite3_stmt, c_int, u64) callconv(.c) c_int, .{ .name = "sqlite3_bind_zeroblob64", .linkage = linkage });
|
|
85
|
+
const sqlite3_bind_blob64 = @extern(*allowzero const fn (*sqlite3_stmt, c_int, ?*const anyopaque, u64, ?*const anyopaque) callconv(.c) c_int, .{ .name = "sqlite3_bind_blob64", .linkage = linkage });
|
|
86
|
+
const sqlite3_bind_parameter_count = @extern(*allowzero const fn (*sqlite3_stmt) callconv(.c) c_int, .{ .name = "sqlite3_bind_parameter_count", .linkage = linkage });
|
|
87
|
+
const sqlite3_stmt_readonly = @extern(*allowzero const fn (*sqlite3_stmt) callconv(.c) c_int, .{ .name = "sqlite3_stmt_readonly", .linkage = linkage });
|
|
88
|
+
const sqlite3_step = @extern(*allowzero const fn (*sqlite3_stmt) callconv(.c) c_int, .{ .name = "sqlite3_step", .linkage = linkage });
|
|
89
|
+
const sqlite3_column_count = @extern(*allowzero const fn (*sqlite3_stmt) callconv(.c) c_int, .{ .name = "sqlite3_column_count", .linkage = linkage });
|
|
90
|
+
const sqlite3_column_name = @extern(*allowzero const fn (*sqlite3_stmt, c_int) callconv(.c) ?[*:0]const u8, .{ .name = "sqlite3_column_name", .linkage = linkage });
|
|
91
|
+
const sqlite3_column_type = @extern(*allowzero const fn (*sqlite3_stmt, c_int) callconv(.c) c_int, .{ .name = "sqlite3_column_type", .linkage = linkage });
|
|
92
|
+
const sqlite3_column_bytes = @extern(*allowzero const fn (*sqlite3_stmt, c_int) callconv(.c) c_int, .{ .name = "sqlite3_column_bytes", .linkage = linkage });
|
|
93
|
+
const sqlite3_column_blob = @extern(*allowzero const fn (*sqlite3_stmt, c_int) callconv(.c) ?*const anyopaque, .{ .name = "sqlite3_column_blob", .linkage = linkage });
|
|
94
|
+
const sqlite3_column_text = @extern(*allowzero const fn (*sqlite3_stmt, c_int) callconv(.c) ?[*]const u8, .{ .name = "sqlite3_column_text", .linkage = linkage });
|
|
95
|
+
const sqlite3_column_int64 = @extern(*allowzero const fn (*sqlite3_stmt, c_int) callconv(.c) i64, .{ .name = "sqlite3_column_int64", .linkage = linkage });
|
|
96
|
+
const sqlite3_column_double = @extern(*allowzero const fn (*sqlite3_stmt, c_int) callconv(.c) f64, .{ .name = "sqlite3_column_double", .linkage = linkage });
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
pub const Error = error{
|
|
100
|
+
Busy,
|
|
101
|
+
OpenFailed,
|
|
102
|
+
PrepareFailed,
|
|
103
|
+
BindFailed,
|
|
104
|
+
StepFailed,
|
|
105
|
+
ExecFailed,
|
|
106
|
+
Constraint,
|
|
107
|
+
Corrupt,
|
|
108
|
+
IoFailed,
|
|
109
|
+
Misuse,
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
pub const Step = enum { row, done };
|
|
113
|
+
|
|
114
|
+
pub const RelationalWriteObserver = struct {
|
|
115
|
+
context: *anyopaque,
|
|
116
|
+
write_fn: *const fn (context: *anyopaque, table: []const u8) void,
|
|
117
|
+
all_fn: *const fn (context: *anyopaque) void,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
pub const Connection = struct {
|
|
121
|
+
handle: *c.sqlite3,
|
|
122
|
+
|
|
123
|
+
pub fn open(path: [:0]const u8) Error!Connection {
|
|
124
|
+
var handle: ?*c.sqlite3 = null;
|
|
125
|
+
const flags = c.SQLITE_OPEN_READWRITE | c.SQLITE_OPEN_CREATE | c.SQLITE_OPEN_FULLMUTEX;
|
|
126
|
+
if (c.sqlite3_open_v2(path.ptr, &handle, flags, null) != c.SQLITE_OK) {
|
|
127
|
+
if (handle) |opened| _ = c.sqlite3_close_v2(opened);
|
|
128
|
+
return error.OpenFailed;
|
|
129
|
+
}
|
|
130
|
+
return .{ .handle = handle orelse return error.OpenFailed };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
pub fn close(self: *Connection) void {
|
|
134
|
+
_ = c.sqlite3_close_v2(self.handle);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
pub fn exec(self: *Connection, sql: [:0]const u8) Error!void {
|
|
138
|
+
const result = c.sqlite3_exec(self.handle, sql.ptr, null, null, null);
|
|
139
|
+
if (result != c.SQLITE_OK) return classify(result, error.ExecFailed);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/// Install the relational tier's SQL sandbox. The database is app-owned,
|
|
143
|
+
/// but SQL may not escape its engine-owned file with ATTACH/DETACH (VACUUM
|
|
144
|
+
/// INTO is authorized as ATTACH), or mutate lifecycle PRAGMAs the runtime
|
|
145
|
+
/// owns. Record-store connections do not install this policy.
|
|
146
|
+
pub fn installRelationalAuthorizer(self: *Connection) Error!void {
|
|
147
|
+
const result = c.sqlite3_set_authorizer(self.handle, relationalAuthorizer, null);
|
|
148
|
+
if (result != c.SQLITE_OK) return classify(result, error.Misuse);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/// Temporarily remove/reinstall the app-SQL authorizer around one
|
|
152
|
+
/// engine-owned lifecycle operation (migrations and `user_version`).
|
|
153
|
+
/// Callers must hold their connection's writer lock.
|
|
154
|
+
pub fn setRelationalAuthorizer(self: *Connection, enabled: bool) Error!void {
|
|
155
|
+
const callback: ?*const @TypeOf(relationalAuthorizer) = if (enabled) relationalAuthorizer else null;
|
|
156
|
+
const result = c.sqlite3_set_authorizer(self.handle, callback, null);
|
|
157
|
+
if (result != c.SQLITE_OK) return classify(result, error.Misuse);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/// Install the app-exec policy while a statement array is being prepared
|
|
161
|
+
/// and stepped. Authorizer write events cover cases sqlite3_update_hook
|
|
162
|
+
/// deliberately omits, including WITHOUT ROWID tables and truncate
|
|
163
|
+
/// optimization. Transaction control remains engine-owned so a batch
|
|
164
|
+
/// member cannot commit or roll back the surrounding transaction.
|
|
165
|
+
pub fn installRelationalExecAuthorizer(self: *Connection, observer: *RelationalWriteObserver) Error!void {
|
|
166
|
+
const result = c.sqlite3_set_authorizer(self.handle, relationalExecAuthorizer, observer);
|
|
167
|
+
if (result != c.SQLITE_OK) return classify(result, error.Misuse);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/// Migrations may change schema, but they may not escape the relational
|
|
171
|
+
/// sandbox or terminate the runtime's all-pending-migrations transaction.
|
|
172
|
+
pub fn installRelationalMigrationAuthorizer(self: *Connection) Error!void {
|
|
173
|
+
const result = c.sqlite3_set_authorizer(self.handle, relationalMigrationAuthorizer, null);
|
|
174
|
+
if (result != c.SQLITE_OK) return classify(result, error.Misuse);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/// Install SQLite's transaction-local table-write hook. SQLite invokes
|
|
178
|
+
/// it synchronously on the thread stepping the mutating statement.
|
|
179
|
+
pub fn installUpdateHook(
|
|
180
|
+
self: *Connection,
|
|
181
|
+
context: ?*anyopaque,
|
|
182
|
+
callback: ?*const fn (?*anyopaque, c_int, ?[*:0]const u8, ?[*:0]const u8, i64) callconv(.c) void,
|
|
183
|
+
) void {
|
|
184
|
+
_ = c.sqlite3_update_hook(self.handle, callback, context);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
pub fn prepare(self: *Connection, sql: [:0]const u8) Error!Statement {
|
|
188
|
+
var statement: ?*c.sqlite3_stmt = null;
|
|
189
|
+
const result = c.sqlite3_prepare_v2(self.handle, sql.ptr, @intCast(sql.len), &statement, null);
|
|
190
|
+
if (result != c.SQLITE_OK) return classify(result, error.PrepareFailed);
|
|
191
|
+
return .{ .handle = statement orelse return error.PrepareFailed };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/// Prepare exactly one statement. Runtime relational effects deliberately
|
|
195
|
+
/// refuse stacked SQL so one query command cannot smuggle writes after a
|
|
196
|
+
/// SELECT and every exec array element remains one auditable statement.
|
|
197
|
+
pub fn prepareOne(self: *Connection, sql: [:0]const u8) Error!Statement {
|
|
198
|
+
var statement: ?*c.sqlite3_stmt = null;
|
|
199
|
+
var tail: ?[*:0]const u8 = null;
|
|
200
|
+
const result = c.sqlite3_prepare_v2(self.handle, sql.ptr, @intCast(sql.len), &statement, &tail);
|
|
201
|
+
if (result != c.SQLITE_OK) return classify(result, error.PrepareFailed);
|
|
202
|
+
errdefer {
|
|
203
|
+
if (statement) |prepared| _ = c.sqlite3_finalize(prepared);
|
|
204
|
+
}
|
|
205
|
+
const remaining = if (tail) |pointer| std.mem.span(pointer) else "";
|
|
206
|
+
if (remaining.len > 0) {
|
|
207
|
+
// Ask SQLite itself whether the tail contains another statement.
|
|
208
|
+
// This accepts whitespace/comments and correctly treats trigger
|
|
209
|
+
// bodies as part of the first statement without growing a second
|
|
210
|
+
// SQL parser in the runtime.
|
|
211
|
+
var extra: ?*c.sqlite3_stmt = null;
|
|
212
|
+
const extra_result = c.sqlite3_prepare_v2(self.handle, remaining.ptr, @intCast(remaining.len), &extra, null);
|
|
213
|
+
if (extra_result != c.SQLITE_OK) return classify(extra_result, error.PrepareFailed);
|
|
214
|
+
if (extra) |prepared| {
|
|
215
|
+
_ = c.sqlite3_finalize(prepared);
|
|
216
|
+
return error.Misuse;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return .{ .handle = statement orelse return error.PrepareFailed };
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
pub const UpdateOperation = enum { insert, update, delete };
|
|
224
|
+
|
|
225
|
+
pub fn updateOperation(action: c_int) ?UpdateOperation {
|
|
226
|
+
return switch (action) {
|
|
227
|
+
c.SQLITE_INSERT => .insert,
|
|
228
|
+
c.SQLITE_UPDATE => .update,
|
|
229
|
+
c.SQLITE_DELETE => .delete,
|
|
230
|
+
else => null,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
pub const ColumnType = enum { integer, float, text, blob, null };
|
|
235
|
+
|
|
236
|
+
pub const Statement = struct {
|
|
237
|
+
handle: *c.sqlite3_stmt,
|
|
238
|
+
|
|
239
|
+
pub fn finalize(self: *Statement) void {
|
|
240
|
+
_ = c.sqlite3_finalize(self.handle);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
pub fn reset(self: *Statement) Error!void {
|
|
244
|
+
const reset_result = c.sqlite3_reset(self.handle);
|
|
245
|
+
if (reset_result != c.SQLITE_OK) return classify(reset_result, error.StepFailed);
|
|
246
|
+
const clear_result = c.sqlite3_clear_bindings(self.handle);
|
|
247
|
+
if (clear_result != c.SQLITE_OK) return classify(clear_result, error.BindFailed);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
pub fn bindInt(self: *Statement, index: c_int, value: i64) Error!void {
|
|
251
|
+
const result = c.sqlite3_bind_int64(self.handle, index, value);
|
|
252
|
+
if (result != c.SQLITE_OK) return classify(result, error.BindFailed);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
pub fn bindNull(self: *Statement, index: c_int) Error!void {
|
|
256
|
+
const result = c.sqlite3_bind_null(self.handle, index);
|
|
257
|
+
if (result != c.SQLITE_OK) return classify(result, error.BindFailed);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
pub fn bindFloat(self: *Statement, index: c_int, value: f64) Error!void {
|
|
261
|
+
const result = c.sqlite3_bind_double(self.handle, index, value);
|
|
262
|
+
if (result != c.SQLITE_OK) return classify(result, error.BindFailed);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
pub fn bindText(self: *Statement, index: c_int, bytes: []const u8) Error!void {
|
|
266
|
+
if (bytes.len > std.math.maxInt(c_int)) return error.BindFailed;
|
|
267
|
+
const result = c.sqlite3_bind_text(self.handle, index, bytes.ptr, @intCast(bytes.len), null);
|
|
268
|
+
if (result != c.SQLITE_OK) return classify(result, error.BindFailed);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
pub fn bindBlob(self: *Statement, index: c_int, bytes: []const u8) Error!void {
|
|
272
|
+
if (bytes.len == 0) {
|
|
273
|
+
const result = c.sqlite3_bind_zeroblob64(self.handle, index, 0);
|
|
274
|
+
if (result != c.SQLITE_OK) return classify(result, error.BindFailed);
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
const pointer: ?*const anyopaque = if (bytes.len == 0) null else bytes.ptr;
|
|
278
|
+
// Callers keep every bound slice alive through step/reset/finalize, so
|
|
279
|
+
// SQLITE_STATIC (a null destructor) is both correct and avoids the C
|
|
280
|
+
// macro's deliberately invalid -1 function pointer, which Zig 0.16
|
|
281
|
+
// refuses to materialize at comptime.
|
|
282
|
+
const result = c.sqlite3_bind_blob64(self.handle, index, pointer, bytes.len, null);
|
|
283
|
+
if (result != c.SQLITE_OK) return classify(result, error.BindFailed);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
pub fn step(self: *Statement) Error!Step {
|
|
287
|
+
const result = c.sqlite3_step(self.handle);
|
|
288
|
+
return switch (result) {
|
|
289
|
+
c.SQLITE_ROW => .row,
|
|
290
|
+
c.SQLITE_DONE => .done,
|
|
291
|
+
else => classify(result, error.StepFailed),
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
pub fn parameterCount(self: *const Statement) usize {
|
|
296
|
+
return @intCast(c.sqlite3_bind_parameter_count(self.handle));
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
pub fn readOnly(self: *const Statement) bool {
|
|
300
|
+
return c.sqlite3_stmt_readonly(self.handle) != 0;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
pub fn columnCount(self: *const Statement) usize {
|
|
304
|
+
return @intCast(c.sqlite3_column_count(self.handle));
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
pub fn columnName(self: *const Statement, index: c_int) []const u8 {
|
|
308
|
+
const name = c.sqlite3_column_name(self.handle, index) orelse return "";
|
|
309
|
+
return std.mem.span(name);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
pub fn columnType(self: *const Statement, index: c_int) ColumnType {
|
|
313
|
+
return switch (c.sqlite3_column_type(self.handle, index)) {
|
|
314
|
+
c.SQLITE_INTEGER => .integer,
|
|
315
|
+
c.SQLITE_FLOAT => .float,
|
|
316
|
+
c.SQLITE_TEXT => .text,
|
|
317
|
+
c.SQLITE_BLOB => .blob,
|
|
318
|
+
c.SQLITE_NULL => .null,
|
|
319
|
+
else => .null,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
pub fn columnBlob(self: *const Statement, index: c_int) []const u8 {
|
|
324
|
+
const len: usize = @intCast(c.sqlite3_column_bytes(self.handle, index));
|
|
325
|
+
if (len == 0) return "";
|
|
326
|
+
const raw = c.sqlite3_column_blob(self.handle, index) orelse return "";
|
|
327
|
+
const bytes: [*]const u8 = @ptrCast(raw);
|
|
328
|
+
return bytes[0..len];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
pub fn columnInt(self: *const Statement, index: c_int) i64 {
|
|
332
|
+
return c.sqlite3_column_int64(self.handle, index);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
pub fn columnFloat(self: *const Statement, index: c_int) f64 {
|
|
336
|
+
return c.sqlite3_column_double(self.handle, index);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
pub fn columnText(self: *const Statement, index: c_int) []const u8 {
|
|
340
|
+
const len: usize = @intCast(c.sqlite3_column_bytes(self.handle, index));
|
|
341
|
+
if (len == 0) return "";
|
|
342
|
+
const bytes = c.sqlite3_column_text(self.handle, index) orelse return "";
|
|
343
|
+
return bytes[0..len];
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
fn classify(result: c_int, fallback: Error) Error {
|
|
348
|
+
// Extended result codes retain the primary code in the low byte.
|
|
349
|
+
return switch (result & 0xff) {
|
|
350
|
+
c.SQLITE_BUSY, c.SQLITE_LOCKED => error.Busy,
|
|
351
|
+
c.SQLITE_CONSTRAINT => error.Constraint,
|
|
352
|
+
c.SQLITE_CORRUPT, c.SQLITE_NOTADB => error.Corrupt,
|
|
353
|
+
c.SQLITE_IOERR => error.IoFailed,
|
|
354
|
+
c.SQLITE_ERROR, c.SQLITE_MISUSE, c.SQLITE_AUTH => error.Misuse,
|
|
355
|
+
else => fallback,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
fn relationalAuthorizer(
|
|
360
|
+
_: ?*anyopaque,
|
|
361
|
+
action: c_int,
|
|
362
|
+
first: ?[*:0]const u8,
|
|
363
|
+
second: ?[*:0]const u8,
|
|
364
|
+
_: ?[*:0]const u8,
|
|
365
|
+
_: ?[*:0]const u8,
|
|
366
|
+
) callconv(.c) c_int {
|
|
367
|
+
return relationalPolicy(action, first, second);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
fn relationalExecAuthorizer(
|
|
371
|
+
context: ?*anyopaque,
|
|
372
|
+
action: c_int,
|
|
373
|
+
first: ?[*:0]const u8,
|
|
374
|
+
second: ?[*:0]const u8,
|
|
375
|
+
_: ?[*:0]const u8,
|
|
376
|
+
_: ?[*:0]const u8,
|
|
377
|
+
) callconv(.c) c_int {
|
|
378
|
+
if (action == c.SQLITE_TRANSACTION or action == c.SQLITE_SAVEPOINT) return c.SQLITE_DENY_AUTHORIZER;
|
|
379
|
+
if (schemaOperation(action)) {
|
|
380
|
+
const observer: *RelationalWriteObserver = @ptrCast(@alignCast(context orelse return c.SQLITE_DENY_AUTHORIZER));
|
|
381
|
+
observer.all_fn(observer.context);
|
|
382
|
+
} else if (updateOperation(action) != null) {
|
|
383
|
+
const observer: *RelationalWriteObserver = @ptrCast(@alignCast(context orelse return c.SQLITE_DENY_AUTHORIZER));
|
|
384
|
+
const table = if (first) |value| std.mem.span(value) else "";
|
|
385
|
+
if (table.len > 0) observer.write_fn(observer.context, table);
|
|
386
|
+
}
|
|
387
|
+
return relationalPolicy(action, first, second);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
fn relationalMigrationAuthorizer(
|
|
391
|
+
_: ?*anyopaque,
|
|
392
|
+
action: c_int,
|
|
393
|
+
first: ?[*:0]const u8,
|
|
394
|
+
second: ?[*:0]const u8,
|
|
395
|
+
_: ?[*:0]const u8,
|
|
396
|
+
_: ?[*:0]const u8,
|
|
397
|
+
) callconv(.c) c_int {
|
|
398
|
+
if (action == c.SQLITE_TRANSACTION or action == c.SQLITE_SAVEPOINT) return c.SQLITE_DENY_AUTHORIZER;
|
|
399
|
+
return relationalPolicy(action, first, second);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
fn relationalPolicy(action: c_int, first: ?[*:0]const u8, second: ?[*:0]const u8) c_int {
|
|
403
|
+
if (action == c.SQLITE_ATTACH or action == c.SQLITE_DETACH) return c.SQLITE_DENY_AUTHORIZER;
|
|
404
|
+
if (temporarySchemaOperation(action)) return c.SQLITE_DENY_AUTHORIZER;
|
|
405
|
+
if (action == c.SQLITE_PRAGMA and second != null) {
|
|
406
|
+
const name = if (first) |value| std.mem.span(value) else "";
|
|
407
|
+
for (relational_owned_pragmas) |owned| {
|
|
408
|
+
if (std.ascii.eqlIgnoreCase(name, owned)) return c.SQLITE_DENY_AUTHORIZER;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return c.SQLITE_OK_AUTHORIZER;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
fn temporarySchemaOperation(action: c_int) bool {
|
|
415
|
+
return (action >= c.SQLITE_CREATE_TEMP_INDEX and action <= c.SQLITE_CREATE_TEMP_VIEW) or
|
|
416
|
+
(action >= c.SQLITE_DROP_TEMP_INDEX and action <= c.SQLITE_DROP_TEMP_VIEW);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/// SQLite's schema authorizer action codes are stable and contiguous across
|
|
420
|
+
/// the CREATE and DROP families, including their TEMP variants. A successful
|
|
421
|
+
/// runtime DDL statement can change any prepared live-query shape, so callers
|
|
422
|
+
/// conservatively invalidate every subscription instead of trusting the DML
|
|
423
|
+
/// hooks, which report only sqlite_master for ALTER TABLE.
|
|
424
|
+
fn schemaOperation(action: c_int) bool {
|
|
425
|
+
return (action >= c.SQLITE_CREATE_INDEX and action <= c.SQLITE_CREATE_VIEW) or
|
|
426
|
+
(action >= c.SQLITE_DROP_INDEX and action <= c.SQLITE_DROP_VIEW) or
|
|
427
|
+
action == c.SQLITE_ALTER_TABLE or action == c.SQLITE_CREATE_VTABLE or action == c.SQLITE_DROP_VTABLE;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const relational_owned_pragmas = [_][]const u8{
|
|
431
|
+
"user_version",
|
|
432
|
+
"schema_version",
|
|
433
|
+
"writable_schema",
|
|
434
|
+
"query_only",
|
|
435
|
+
"journal_mode",
|
|
436
|
+
"synchronous",
|
|
437
|
+
"locking_mode",
|
|
438
|
+
"foreign_keys",
|
|
439
|
+
"defer_foreign_keys",
|
|
440
|
+
"busy_timeout",
|
|
441
|
+
"wal_autocheckpoint",
|
|
442
|
+
"temp_store",
|
|
443
|
+
"temp_store_directory",
|
|
444
|
+
"data_store_directory",
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
test "sqlite engine opens memory databases and executes statements" {
|
|
448
|
+
var db = try Connection.open(":memory:");
|
|
449
|
+
defer db.close();
|
|
450
|
+
try db.exec("CREATE TABLE t (k BLOB PRIMARY KEY, v BLOB NOT NULL);");
|
|
451
|
+
var insert = try db.prepare("INSERT INTO t(k,v) VALUES(?1,?2);");
|
|
452
|
+
defer insert.finalize();
|
|
453
|
+
try insert.bindBlob(1, "a");
|
|
454
|
+
try insert.bindBlob(2, "b");
|
|
455
|
+
try std.testing.expectEqual(Step.done, try insert.step());
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
test "prepareOne delegates statement boundaries to SQLite" {
|
|
459
|
+
var db = try Connection.open(":memory:");
|
|
460
|
+
defer db.close();
|
|
461
|
+
|
|
462
|
+
var commented = try db.prepareOne("SELECT 1; -- one statement\n");
|
|
463
|
+
defer commented.finalize();
|
|
464
|
+
try std.testing.expectEqual(Step.row, try commented.step());
|
|
465
|
+
|
|
466
|
+
try std.testing.expectError(error.Misuse, db.prepareOne("SELECT 1; ; SELECT 2;"));
|
|
467
|
+
|
|
468
|
+
try db.exec("CREATE TABLE source(value INTEGER); CREATE TABLE audit(value INTEGER);");
|
|
469
|
+
var trigger = try db.prepareOne(
|
|
470
|
+
"CREATE TRIGGER source_audit AFTER INSERT ON source BEGIN INSERT INTO audit(value) VALUES(new.value); END;",
|
|
471
|
+
);
|
|
472
|
+
defer trigger.finalize();
|
|
473
|
+
try std.testing.expectEqual(Step.done, try trigger.step());
|
|
474
|
+
}
|
package/src/runtime/state.zig
CHANGED
|
@@ -78,8 +78,24 @@ pub const RuntimeTrayItem = struct {
|
|
|
78
78
|
label: []const u8 = "",
|
|
79
79
|
separator: bool = false,
|
|
80
80
|
enabled: bool = true,
|
|
81
|
+
detail: []const u8 = "",
|
|
82
|
+
role: platform.TrayItemRole = .command,
|
|
83
|
+
key: []const u8 = "",
|
|
84
|
+
modifiers: platform.ShortcutModifiers = .{},
|
|
81
85
|
command_storage: [platform.max_tray_item_command_bytes]u8 = undefined,
|
|
82
86
|
label_storage: [platform.max_tray_item_label_bytes]u8 = undefined,
|
|
87
|
+
detail_storage: [platform.max_tray_item_detail_bytes]u8 = undefined,
|
|
88
|
+
key_storage: [platform.max_menu_key_bytes]u8 = undefined,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
pub const RuntimeStatusItem = struct {
|
|
92
|
+
id: platform.StatusItemId = 0,
|
|
93
|
+
active: bool = false,
|
|
94
|
+
visible: bool = true,
|
|
95
|
+
title: []const u8 = "",
|
|
96
|
+
title_storage: [platform.max_tray_title_bytes]u8 = undefined,
|
|
97
|
+
items: [platform.max_tray_items]RuntimeTrayItem = undefined,
|
|
98
|
+
item_count: usize = 0,
|
|
83
99
|
};
|
|
84
100
|
|
|
85
101
|
pub const ShellApplyMode = enum {
|