Skip to content

Commit

Permalink
🐛 Fix sql strings formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Danieloni1 committed Aug 7, 2024
1 parent 48008a2 commit d8f4b1e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 114 deletions.
111 changes: 54 additions & 57 deletions roomserver/storage/postgres/event_json_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,63 +54,60 @@ const bulkSelectEventJSONSQL = "" +
" WHERE event_nid = ANY($1)" +
" ORDER BY event_nid ASC"

const selectRoomsUnderSpaceSQL = "" +
"WITH room_ids AS (" +
" SELECT DISTINCT" +
" (REGEXP_MATCHES(event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" +
" FROM roomserver_event_json" +
" WHERE event_json LIKE '%\"state_key\":\"$1\"%'" +
" AND event_json LIKE '%\"type\":\"m.space.parent\"%'" +
")," +
"dm_rooms AS (" +
" SELECT" +
" ARRAY_AGG(DISTINCT r.room_id) AS dm_array" +
" FROM roomserver_event_json e" +
" CROSS JOIN LATERAL (" +
" SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" +
" ) AS r" +
" WHERE e.event_json LIKE '%\"is_direct\":true%'" +
" AND r.room_id = ANY (" +
" SELECT room_id FROM room_ids" +
" )" +
")," +
"operation_rooms AS (" +
" SELECT" +
" ARRAY_AGG(DISTINCT r.room_id) AS operation_array" +
" FROM roomserver_event_json e" +
" CROSS JOIN LATERAL (" +
" SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" +
" ) AS r" +
" WHERE e.event_json LIKE '%\"type\":\"connect.operation\"%'" +
" AND r.room_id = ANY (" +
" SELECT room_id FROM room_ids" +
" )" +
")," +
"team_rooms AS (" +
" SELECT" +
" ARRAY_AGG(DISTINCT r.room_id) AS team_array" +
" FROM roomserver_event_json e" +
" CROSS JOIN LATERAL (" +
" SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" +
" ) AS r" +
" WHERE r.room_id = ANY (" +
" SELECT room_id FROM room_ids" +
" )" +
" AND r.room_id NOT IN (" +
" SELECT UNNEST(operation_rooms.operation_array) FROM operation_rooms" +
" )" +
" AND r.room_id NOT IN (" +
" SELECT UNNEST(dm_rooms.dm_array) FROM dm_rooms" +
" )" +
")" +
"SELECT" +
" dm_rooms.dm_array," +
" operation_rooms.operation_array," +
" team_rooms.team_array" +
"FROM" +
" dm_rooms," +
" operation_rooms," +
" team_rooms;"
const selectRoomsUnderSpaceSQL = `
WITH room_ids AS (
SELECT DISTINCT
(REGEXP_MATCHES(event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id
FROM roomserver_event_json
WHERE event_json LIKE '%\"state_key\":\"$1\"%'
AND event_json LIKE '%\"type\":\"m.space.parent\"%'
),
dm_rooms AS (
SELECT
ARRAY_AGG(DISTINCT r.room_id) AS dm_array
FROM roomserver_event_json e
CROSS JOIN LATERAL (
SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id
) AS r
WHERE e.event_json LIKE '%\"is_direct\":true%'
AND r.room_id = ANY (
SELECT room_id FROM room_ids
)
),
operation_rooms AS (
SELECT
ARRAY_AGG(DISTINCT r.room_id) AS operation_array
FROM roomserver_event_json e
CROSS JOIN LATERAL (
SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id
) AS r
WHERE e.event_json LIKE '%\"type\":\"connect.operation\"%'
AND r.room_id = ANY (
SELECT room_id FROM room_ids
)
),
team_rooms AS (
SELECT
ARRAY_AGG(DISTINCT r.room_id) AS team_array
FROM roomserver_event_json e
CROSS JOIN LATERAL (
SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id) AS r
WHERE r.room_id = ANY (
SELECT room_id FROM room_ids)
AND r.room_id NOT IN (
SELECT UNNEST(operation_rooms.operation_array) FROM operation_rooms)
AND r.room_id NOT IN (
SELECT UNNEST(dm_rooms.dm_array) FROM dm_rooms)
)
SELECT
dm_rooms.dm_array,
operation_rooms.operation_array,
team_rooms.team_array
FROM
dm_rooms,
operation_rooms,
team_rooms
`

type eventJSONStatements struct {
insertEventJSONStmt *sql.Stmt
Expand Down
111 changes: 54 additions & 57 deletions roomserver/storage/sqlite3/event_json_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,63 +46,60 @@ const bulkSelectEventJSONSQL = `
ORDER BY event_nid ASC
`

const selectRoomsUnderSpaceSQL = "" +
"WITH room_ids AS (" +
" SELECT DISTINCT" +
" (REGEXP_MATCHES(event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" +
" FROM roomserver_event_json" +
" WHERE event_json LIKE '%\"state_key\":\"$1\"%'" +
" AND event_json LIKE '%\"type\":\"m.space.parent\"%'" +
")," +
"dm_rooms AS (" +
" SELECT" +
" ARRAY_AGG(DISTINCT r.room_id) AS dm_array" +
" FROM roomserver_event_json e" +
" CROSS JOIN LATERAL (" +
" SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" +
" ) AS r" +
" WHERE e.event_json LIKE '%\"is_direct\":true%'" +
" AND r.room_id = ANY (" +
" SELECT room_id FROM room_ids" +
" )" +
")," +
"operation_rooms AS (" +
" SELECT" +
" ARRAY_AGG(DISTINCT r.room_id) AS operation_array" +
" FROM roomserver_event_json e" +
" CROSS JOIN LATERAL (" +
" SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" +
" ) AS r" +
" WHERE e.event_json LIKE '%\"type\":\"connect.operation\"%'" +
" AND r.room_id = ANY (" +
" SELECT room_id FROM room_ids" +
" )" +
")," +
"team_rooms AS (" +
" SELECT" +
" ARRAY_AGG(DISTINCT r.room_id) AS team_array" +
" FROM roomserver_event_json e" +
" CROSS JOIN LATERAL (" +
" SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id" +
" ) AS r" +
" WHERE r.room_id = ANY (" +
" SELECT room_id FROM room_ids" +
" )" +
" AND r.room_id NOT IN (" +
" SELECT UNNEST(operation_rooms.operation_array) FROM operation_rooms" +
" )" +
" AND r.room_id NOT IN (" +
" SELECT UNNEST(dm_rooms.dm_array) FROM dm_rooms" +
" )" +
")" +
"SELECT" +
" dm_rooms.dm_array," +
" operation_rooms.operation_array," +
" team_rooms.team_array" +
"FROM" +
" dm_rooms," +
" operation_rooms," +
" team_rooms;"
const selectRoomsUnderSpaceSQL = `
WITH room_ids AS (
SELECT DISTINCT
(REGEXP_MATCHES(event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id
FROM roomserver_event_json
WHERE event_json LIKE '%\"state_key\":\"$1\"%'
AND event_json LIKE '%\"type\":\"m.space.parent\"%'
),
dm_rooms AS (
SELECT
ARRAY_AGG(DISTINCT r.room_id) AS dm_array
FROM roomserver_event_json e
CROSS JOIN LATERAL (
SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id
) AS r
WHERE e.event_json LIKE '%\"is_direct\":true%'
AND r.room_id = ANY (
SELECT room_id FROM room_ids
)
),
operation_rooms AS (
SELECT
ARRAY_AGG(DISTINCT r.room_id) AS operation_array
FROM roomserver_event_json e
CROSS JOIN LATERAL (
SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id
) AS r
WHERE e.event_json LIKE '%\"type\":\"connect.operation\"%'
AND r.room_id = ANY (
SELECT room_id FROM room_ids
)
),
team_rooms AS (
SELECT
ARRAY_AGG(DISTINCT r.room_id) AS team_array
FROM roomserver_event_json e
CROSS JOIN LATERAL (
SELECT (REGEXP_MATCHES(e.event_json, '\"room_id\":\"([^\"]+)\"'))[1]::text AS room_id) AS r
WHERE r.room_id = ANY (
SELECT room_id FROM room_ids)
AND r.room_id NOT IN (
SELECT UNNEST(operation_rooms.operation_array) FROM operation_rooms)
AND r.room_id NOT IN (
SELECT UNNEST(dm_rooms.dm_array) FROM dm_rooms)
)
SELECT
dm_rooms.dm_array,
operation_rooms.operation_array,
team_rooms.team_array
FROM
dm_rooms,
operation_rooms,
team_rooms
`

type eventJSONStatements struct {
db *sql.DB
Expand Down

0 comments on commit d8f4b1e

Please sign in to comment.