-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5553 from unisonweb/cp/project-list-updates
Project List API updates for UCM Desktop
- Loading branch information
Showing
16 changed files
with
242 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
codebase2/codebase-sqlite/sql/015-add-project-branch-last-accessed.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- Add a new column to the project_branch table to store the last time that project branch was accessed. | ||
-- This column is stored as a unix epoch time. | ||
ALTER TABLE project_branch ADD COLUMN last_accessed INTEGER NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Unison.Sqlite.Utils (likeEscape) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as Text | ||
|
||
-- | Escape special characters for "LIKE" matches. | ||
-- | ||
-- Prepared statements prevent sql injection, but it's still possible some user | ||
-- may be able to craft a query using a fake "hash" that would let them see more than they | ||
-- ought to. | ||
-- | ||
-- You still need to provide the escape char in the sql query, E.g. | ||
-- | ||
-- @@ | ||
-- SELECT * FROM table | ||
-- WHERE txt LIKE ? ESCAPE '\' | ||
-- @@ | ||
-- | ||
-- >>> likeEscape '\\' "Nat.%" | ||
-- "Nat.\%" | ||
likeEscape :: Char -> Text -> Text | ||
likeEscape '%' _ = error "Can't use % or _ as escape characters" | ||
likeEscape '_' _ = error "Can't use % or _ as escape characters" | ||
likeEscape escapeChar pat = | ||
flip Text.concatMap pat \case | ||
'%' -> Text.pack [escapeChar, '%'] | ||
'_' -> Text.pack [escapeChar, '_'] | ||
c | ||
| c == escapeChar -> Text.pack [escapeChar, escapeChar] | ||
| otherwise -> Text.singleton c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,3 +121,4 @@ default-extensions: | |
- TypeOperators | ||
- ViewPatterns | ||
- ImportQualifiedPost | ||
- QuasiQuotes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
unison-share-api/src/Unison/Server/Local/Endpoints/Projects/Queries.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Unison.Server.Local.Endpoints.Projects.Queries | ||
( listProjects, | ||
listProjectBranches, | ||
) | ||
where | ||
|
||
import Data.Text (Text) | ||
import U.Codebase.Sqlite.DbId (ProjectId) | ||
import Unison.Server.Local.Endpoints.Projects.Types | ||
import Unison.Sqlite | ||
|
||
-- | Load all project listings, optionally requiring an infix match with a query. | ||
listProjects :: Maybe Text -> Transaction [ProjectListing] | ||
listProjects mayUnsafeQuery = do | ||
let mayQuery = fmap (likeEscape '\\') mayUnsafeQuery | ||
queryListRow | ||
[sql| | ||
SELECT project.name, branch.name | ||
FROM project | ||
LEFT JOIN most_recent_branch mrb | ||
ON project.id = mrb.project_id | ||
LEFT JOIN project_branch branch | ||
ON mrb.branch_id = branch.branch_id | ||
WHERE (:mayQuery IS NULL OR project.name LIKE '%' || :mayQuery || '%' ESCAPE '\') | ||
ORDER BY branch.last_accessed DESC NULLS LAST, project.name ASC | ||
|] | ||
|
||
-- | Load all project listings, optionally requiring an infix match with a query. | ||
listProjectBranches :: ProjectId -> Maybe Text -> Transaction [ProjectBranchListing] | ||
listProjectBranches projectId mayUnsafeQuery = do | ||
let mayQuery = fmap (likeEscape '\\') mayUnsafeQuery | ||
queryListRow | ||
[sql| | ||
SELECT project_branch.name | ||
FROM project_branch | ||
WHERE project_branch.project_id = :projectId | ||
AND (:mayQuery IS NULL OR project_branch.name LIKE '%' || :mayQuery || '%' ESCAPE '\') | ||
ORDER BY project_branch.last_accessed DESC NULLS LAST, project_branch.name ASC | ||
|] |
Oops, something went wrong.