Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experimental] Add include_old and include_metadata options #60

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/core/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ pub fn extract_table_info(
json_extract(?1, '$.name') as name,
ifnull(json_extract(?1, '$.view_name'), json_extract(?1, '$.name')) as view_name,
json_extract(?1, '$.local_only') as local_only,
json_extract(?1, '$.insert_only') as insert_only",
json_extract(?1, '$.insert_only') as insert_only,
json_extract(?1, '$.include_old') as include_old,
json_extract(?1, '$.include_metadata') as include_metadata",
)?;
statement.bind_text(1, data, sqlite::Destructor::STATIC)?;

Expand Down
27 changes: 25 additions & 2 deletions crates/core/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn powersync_view_sql_impl(
let name = statement.column_text(0)?;
let view_name = statement.column_text(1)?;
let local_only = statement.column_int(2)? != 0;
let include_metadata = statement.column_int(5)? != 0;

let quoted_name = quote_identifier(view_name);
let internal_name = quote_internal_name(name, local_only);
Expand All @@ -48,6 +49,11 @@ fn powersync_view_sql_impl(
column_values.push(foo);
}

if include_metadata {
column_names_quoted.push(quote_identifier("_metadata"));
column_values.push(String::from("NULL"));
}

let view_statement = format!(
"CREATE VIEW {:}({:}) AS SELECT {:} FROM {:} -- powersync-auto-generated",
quoted_name,
Expand Down Expand Up @@ -206,6 +212,9 @@ fn powersync_trigger_update_sql_impl(
let view_name = statement.column_text(1)?;
let local_only = statement.column_int(2)? != 0;
let insert_only = statement.column_int(3)? != 0;
// TODO: allow accepting a column list
let include_old = statement.column_type(4)? == sqlite::ColumnType::Text;
let include_metadata = statement.column_int(5)? != 0;

let quoted_name = quote_identifier(view_name);
let internal_name = quote_internal_name(name, local_only);
Expand All @@ -218,6 +227,20 @@ fn powersync_trigger_update_sql_impl(
let json_fragment_new = json_object_fragment("NEW", &stmt2)?;
stmt2.reset()?;
let json_fragment_old = json_object_fragment("OLD", &stmt2)?;
let old_fragment: String;
let metadata_fragment: &str;

if include_old {
old_fragment = format!(", 'old', {:}", json_fragment_old);
} else {
old_fragment = String::from("");
}

if include_metadata {
metadata_fragment = ", 'metadata', NEW._metadata";
} else {
metadata_fragment = "";
}

return if !local_only && !insert_only {
let trigger = format!("\
Expand All @@ -232,10 +255,10 @@ BEGIN
UPDATE {:}
SET data = {:}
WHERE id = NEW.id;
INSERT INTO powersync_crud_(data) VALUES(json_object('op', 'PATCH', 'type', {:}, 'id', NEW.id, 'data', json(powersync_diff({:}, {:}))));
INSERT INTO powersync_crud_(data) VALUES(json_object('op', 'PATCH', 'type', {:}, 'id', NEW.id, 'data', json(powersync_diff({:}, {:})){:}{:}));
INSERT OR IGNORE INTO ps_updated_rows(row_type, row_id) VALUES({:}, NEW.id);
INSERT OR REPLACE INTO ps_buckets(name, last_op, target_op) VALUES('$local', 0, {:});
END", trigger_name, quoted_name, internal_name, json_fragment_new, type_string, json_fragment_old, json_fragment_new, type_string, MAX_OP_ID);
END", trigger_name, quoted_name, internal_name, json_fragment_new, type_string, json_fragment_old, json_fragment_new, old_fragment, metadata_fragment, type_string, MAX_OP_ID);
Ok(trigger)
} else if local_only {
let trigger = format!(
Expand Down