Use table alias unlikely to conflict with a column name (#705)
This commit is contained in:
@@ -97,11 +97,11 @@ createReadStatement selectQuery countQuery isSingle countTotal asCsv =
|
|||||||
where
|
where
|
||||||
sql = [qc|
|
sql = [qc|
|
||||||
WITH {sourceCTEName} AS ({selectQuery}) SELECT {cols}
|
WITH {sourceCTEName} AS ({selectQuery}) SELECT {cols}
|
||||||
FROM ( SELECT * FROM {sourceCTEName}) t |]
|
FROM ( SELECT * FROM {sourceCTEName}) _postgrest_t |]
|
||||||
countResultF = if countTotal then "("<>countQuery<>")" else "null"
|
countResultF = if countTotal then "("<>countQuery<>")" else "null"
|
||||||
cols = intercalate ", " [
|
cols = intercalate ", " [
|
||||||
countResultF <> " AS total_result_set",
|
countResultF <> " AS total_result_set",
|
||||||
"pg_catalog.count(t) AS page_total",
|
"pg_catalog.count(_postgrest_t) AS page_total",
|
||||||
noLocationF <> " AS header",
|
noLocationF <> " AS header",
|
||||||
bodyF <> " AS body"
|
bodyF <> " AS body"
|
||||||
]
|
]
|
||||||
@@ -129,10 +129,10 @@ createWriteStatement qi _ mutateQuery isSingle HeadersOnly
|
|||||||
sql = [qc|
|
sql = [qc|
|
||||||
WITH {sourceCTEName} AS ({mutateQuery} RETURNING {fromQi qi}.*)
|
WITH {sourceCTEName} AS ({mutateQuery} RETURNING {fromQi qi}.*)
|
||||||
SELECT {cols}
|
SELECT {cols}
|
||||||
FROM (SELECT 1 FROM {sourceCTEName}) t |]
|
FROM (SELECT 1 FROM {sourceCTEName}) _postgrest_t |]
|
||||||
cols = intercalate ", " [
|
cols = intercalate ", " [
|
||||||
"'' AS total_result_set",
|
"'' AS total_result_set",
|
||||||
"pg_catalog.count(t) AS page_total",
|
"pg_catalog.count(_postgrest_t) AS page_total",
|
||||||
if isSingle then locationF pKeys else noLocationF,
|
if isSingle then locationF pKeys else noLocationF,
|
||||||
"''"
|
"''"
|
||||||
]
|
]
|
||||||
@@ -144,10 +144,10 @@ createWriteStatement qi selectQuery mutateQuery isSingle Full
|
|||||||
sql = [qc|
|
sql = [qc|
|
||||||
WITH {sourceCTEName} AS ({mutateQuery} RETURNING {fromQi qi}.*)
|
WITH {sourceCTEName} AS ({mutateQuery} RETURNING {fromQi qi}.*)
|
||||||
SELECT {cols}
|
SELECT {cols}
|
||||||
FROM ({selectQuery}) t |]
|
FROM ({selectQuery}) _postgrest_t |]
|
||||||
cols = intercalate ", " [
|
cols = intercalate ", " [
|
||||||
"'' AS total_result_set", -- when updateing it does not make sense
|
"'' AS total_result_set", -- when updateing it does not make sense
|
||||||
"pg_catalog.count(t) AS page_total",
|
"pg_catalog.count(_postgrest_t) AS page_total",
|
||||||
if isSingle then locationF pKeys else noLocationF <> " AS header",
|
if isSingle then locationF pKeys else noLocationF <> " AS header",
|
||||||
bodyF <> " AS body"
|
bodyF <> " AS body"
|
||||||
]
|
]
|
||||||
@@ -205,15 +205,15 @@ callProc qi params selectQuery countQuery _ countTotal isSingle =
|
|||||||
WITH {sourceCTEName} AS ({_callSql})
|
WITH {sourceCTEName} AS ({_callSql})
|
||||||
SELECT
|
SELECT
|
||||||
{countResultF} AS total_result_set,
|
{countResultF} AS total_result_set,
|
||||||
pg_catalog.count(t) AS page_total,
|
pg_catalog.count(_postgrest_t) AS page_total,
|
||||||
case
|
case
|
||||||
when pg_catalog.count(1) > 1 then
|
when pg_catalog.count(1) > 1 then
|
||||||
{bodyF}
|
{bodyF}
|
||||||
else
|
else
|
||||||
coalesce(((array_agg(row_to_json(t)))[1]->{_procName})::character varying, {bodyF})
|
coalesce(((array_agg(row_to_json(_postgrest_t)))[1]->{_procName})::character varying, {bodyF})
|
||||||
|
|
||||||
end as body
|
end as body
|
||||||
FROM ({selectQuery}) t;
|
FROM ({selectQuery}) _postgrest_t;
|
||||||
|]
|
|]
|
||||||
-- FROM (select * from {sourceCTEName} {limitF range}) t;
|
-- FROM (select * from {sourceCTEName} {limitF range}) t;
|
||||||
countResultF = if countTotal then "("<>countQuery<>")" else "null::bigint" :: Text
|
countResultF = if countTotal then "("<>countQuery<>")" else "null::bigint" :: Text
|
||||||
@@ -397,13 +397,13 @@ asCsvF = asCsvHeaderF <> " || '\n' || " <> asCsvBodyF
|
|||||||
" ) s" <>
|
" ) s" <>
|
||||||
" ) a" <>
|
" ) a" <>
|
||||||
")"
|
")"
|
||||||
asCsvBodyF = "coalesce(string_agg(substring(t::text, 2, length(t::text) - 2), '\n'), '')"
|
asCsvBodyF = "coalesce(string_agg(substring(_postgrest_t::text, 2, length(_postgrest_t::text) - 2), '\n'), '')"
|
||||||
|
|
||||||
asJsonF :: SqlFragment
|
asJsonF :: SqlFragment
|
||||||
asJsonF = "coalesce(array_to_json(array_agg(row_to_json(t))), '[]')::character varying"
|
asJsonF = "coalesce(array_to_json(array_agg(row_to_json(_postgrest_t))), '[]')::character varying"
|
||||||
|
|
||||||
asJsonSingleF :: SqlFragment --TODO! unsafe when the query actually returns multiple rows, used only on inserting and returning single element
|
asJsonSingleF :: SqlFragment --TODO! unsafe when the query actually returns multiple rows, used only on inserting and returning single element
|
||||||
asJsonSingleF = "coalesce(string_agg(row_to_json(t)::text, ','), '')::character varying "
|
asJsonSingleF = "coalesce(string_agg(row_to_json(_postgrest_t)::text, ','), '')::character varying "
|
||||||
|
|
||||||
locationF :: [Text] -> SqlFragment
|
locationF :: [Text] -> SqlFragment
|
||||||
locationF pKeys =
|
locationF pKeys =
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ spec = do
|
|||||||
it "should not confuse count column with pg_catalog.count aggregate" $
|
it "should not confuse count column with pg_catalog.count aggregate" $
|
||||||
get "/has_count_column" `shouldRespondWith` 200
|
get "/has_count_column" `shouldRespondWith` 200
|
||||||
|
|
||||||
|
describe "Querying a table with a column called t" $
|
||||||
|
it "should not conflict with internal postgrest table alias" $
|
||||||
|
get "/clashing_column?select=t" `shouldRespondWith` 200
|
||||||
|
|
||||||
describe "Querying a nonexistent table" $
|
describe "Querying a nonexistent table" $
|
||||||
it "causes a 404" $
|
it "causes a 404" $
|
||||||
get "/faketable" `shouldRespondWith` 404
|
get "/faketable" `shouldRespondWith` 404
|
||||||
|
|||||||
Vendored
+1
@@ -38,6 +38,7 @@ GRANT ALL ON TABLE
|
|||||||
, "Escap3e;"
|
, "Escap3e;"
|
||||||
, "ghostBusters"
|
, "ghostBusters"
|
||||||
, "withUnique"
|
, "withUnique"
|
||||||
|
, "clashing_column"
|
||||||
, "موارد"
|
, "موارد"
|
||||||
, addresses
|
, addresses
|
||||||
, orders
|
, orders
|
||||||
|
|||||||
Vendored
+4
@@ -703,6 +703,10 @@ CREATE TABLE "withUnique" (
|
|||||||
extra text
|
extra text
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE clashing_column (
|
||||||
|
t text
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Name: id; Type: DEFAULT; Schema: test; Owner: -
|
-- Name: id; Type: DEFAULT; Schema: test; Owner: -
|
||||||
|
|||||||
Reference in New Issue
Block a user