Skip to content

Commit

Permalink
Fix #738 - Add support for User Mode in Database Operations
Browse files Browse the repository at this point in the history
  • Loading branch information
dangmai committed Apr 23, 2023
1 parent 29e159b commit f1ff74b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

## Formatting Changes

- Add support for User Mode in Database Operations ([issue](https://github.com/dangmai/prettier-plugin-apex/issues/738)).

## Internal Changes

- `start-apex-server` takes optional `-c` flag, which will be passed on to `apex-ast-serializer` as a comma-delimited list of allowed origins that will be added to the CORS headers returned by the parsing server.
Expand Down
2 changes: 2 additions & 0 deletions src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ function handleStatement(
const parts: Doc[] = [];
parts.push(doc);
parts.push(" ");
pushIfExist(parts, path.call(print, "runAsMode", "value"), [" "], ["as "]);
parts.push(path.call(print, "expr"));
// upsert statement has an extra param that can be tacked on at the end
if (node.id) {
Expand All @@ -935,6 +936,7 @@ function handleDmlMergeStatement(path: AstPath, print: printFn): Doc {
const parts: Doc[] = [];
parts.push("merge");
parts.push(" ");
pushIfExist(parts, path.call(print, "runAsMode", "value"), [" "], ["as "]);
parts.push(path.call(print, "expr1"));
parts.push(line);
parts.push(path.call(print, "expr2"));
Expand Down
12 changes: 12 additions & 0 deletions tests/dml/DmlClass.cls
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class DMLClass {
new Account(Name = 'Inner Acme Three'),
new Account(Name = 'Inner Acme Four')
};
insert as user account;
insert as system account;
}
void updateTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand All @@ -27,6 +29,8 @@ class DMLClass {
new Account(Id = accountId, Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
update as user account;
update as system account;
}
void upsertWithoutOptFieldTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand All @@ -42,6 +46,8 @@ class DMLClass {
new Account(Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
upsert as user account;
upsert as system account;
}
void upsertWithOptFieldTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand Down Expand Up @@ -72,6 +78,8 @@ class DMLClass {
new Account(Id = accountId, Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
delete as user account;
delete as system account;
}
void undeleteTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1 ALL ROWS].Id;
Expand All @@ -90,6 +98,8 @@ class DMLClass {
new Account(Id = accountId, Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
undelete as user account;
undelete as system account;
}
void mergeTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand All @@ -104,6 +114,8 @@ class DMLClass {
merge account accounts;
merge account accountId;
merge account accountIds;
merge as user account accountId;
merge as system account accountIds;

merge new Account(Id = accountId, Name = 'Acme One') new Account(Id = accountId, Name = 'Acme Two');
merge new Account(Id = accountId, Name = 'Acme One') new Account[]{new Account(Id = accountId, Name = 'Acme Two')};
Expand Down
24 changes: 24 additions & 0 deletions tests/dml/__snapshots__/jsfmt.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class DMLClass {
new Account(Name = 'Inner Acme Three'),
new Account(Name = 'Inner Acme Four')
};
insert as user account;
insert as system account;
}
void updateTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand All @@ -30,6 +32,8 @@ class DMLClass {
new Account(Id = accountId, Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
update as user account;
update as system account;
}
void upsertWithoutOptFieldTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand All @@ -45,6 +49,8 @@ class DMLClass {
new Account(Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
upsert as user account;
upsert as system account;
}
void upsertWithOptFieldTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand Down Expand Up @@ -75,6 +81,8 @@ class DMLClass {
new Account(Id = accountId, Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
delete as user account;
delete as system account;
}
void undeleteTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1 ALL ROWS].Id;
Expand All @@ -93,6 +101,8 @@ class DMLClass {
new Account(Id = accountId, Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
undelete as user account;
undelete as system account;
}
void mergeTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand All @@ -107,6 +117,8 @@ class DMLClass {
merge account accounts;
merge account accountId;
merge account accountIds;
merge as user account accountId;
merge as system account accountIds;
merge new Account(Id = accountId, Name = 'Acme One') new Account(Id = accountId, Name = 'Acme Two');
merge new Account(Id = accountId, Name = 'Acme One') new Account[]{new Account(Id = accountId, Name = 'Acme Two')};
Expand All @@ -129,6 +141,8 @@ class DMLClass {
new Account(Name = 'Inner Acme Three'),
new Account(Name = 'Inner Acme Four')
};
insert as user account;
insert as system account;
}
void updateTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand All @@ -144,6 +158,8 @@ class DMLClass {
new Account(Id = accountId, Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
update as user account;
update as system account;
}
void upsertWithoutOptFieldTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand All @@ -159,6 +175,8 @@ class DMLClass {
new Account(Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
upsert as user account;
upsert as system account;
}
void upsertWithOptFieldTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand Down Expand Up @@ -195,6 +213,8 @@ class DMLClass {
new Account(Id = accountId, Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
delete as user account;
delete as system account;
}
void undeleteTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1 ALL ROWS].Id;
Expand All @@ -213,6 +233,8 @@ class DMLClass {
new Account(Id = accountId, Name = 'Inner Acme Three'),
new Account(Id = accountId, Name = 'Inner Acme Four')
};
undelete as user account;
undelete as system account;
}
void mergeTest() {
Id accountId = [SELECT Id FROM Account LIMIT 1].Id;
Expand All @@ -227,6 +249,8 @@ class DMLClass {
merge account accounts;
merge account accountId;
merge account accountIds;
merge as user account accountId;
merge as system account accountIds;
merge new Account(Id = accountId, Name = 'Acme One')
new Account(Id = accountId, Name = 'Acme Two');
Expand Down

0 comments on commit f1ff74b

Please sign in to comment.