prisma $queryRaw doesn't work with single quotes in the string - postgresql

i have a query that goes
select count(id), status, "createdAt"::date from "ProjectLog" where "projectId" = (select id from "Project" where slug = ${id}) and "createdAt" > current_date - interval '${interval} day' group by "createdAt"::date, status;
i've also tried user Prisma.sql to pass the value inside the quotes but it keeps throwing error that it expected 1 argument but fount 2.
i did not have this issue with prisma 2.20.1
this issue is only happening on version 3.3.0
Query: select count(id) as count, status, "createdAt"::date from "ProjectLog" where "projectId" = (select id from "Project" where slug = $1) and "createdAt" > current_date - interval '$2 day' and key notnull group by "createdAt"::date, status
Param: ["main","30"]
PrismaClientKnownRequestError:
Invalid `prisma.queryRaw()` invocation:
Your raw query had an incorrect number of parameters. Expected: `1`, actual: `2`.
code: 'P1016',
clientVersion: '3.3.0',
meta: { expected: 1, actual: 2 }
}
any suggestions ?
i'm not sure if this is a bug.

didn't find an elegant solution in the prism documentation, I couldn't even find one, but there is a way that works...you can try this ${'variable'}

Try to use:
const interval = num + ' days';
${interval}::TEXT::INTERVAL
instead of interval '${interval} day'
This is work for me!

Related

HQL / Postgres : EXTRACT(dow from CURRENT_TIMESTAMP) with a parameter

I am trying to implement following query to retrieve users that need to be alerted weekly and send alerts on Monday.
Following query works :
String sql = "SELECT user_id FROM table"
+ " WHERE alert_interval = 7 AND EXTRACT(dow from CURRENT_TIMESTAMP) = 1))";
But if I want to pass a parameter to EXTRACT(dow from CURRENT_TIMESTAMP) like this :
String sql = "SELECT user_id FROM table"
+ " WHERE alert_interval = 7 AND EXTRACT(dow from : currentTs) = 1))";
var args = new MapSqlParameterSource("currentTs", currentTs);
jdbcOperations.query(sql, args, (ResultSet rs) -> ...);
I am getting following error :
nested exception is org.postgresql.util.PSQLException: ERROR: function pg_catalog.date_part(unknown, unknown) is not unique
Hint: Could not choose a best candidate function. You might need to add explicit type casts.
Not sure how I can pass a parameter to EXTRACT(dow from ...) ?
Any pointers would be appreciated.
Thank you.

PostgreSQL Getting error in order to change explicit type cast

SELECT COUNT(*)
FROM WASADMIN.DAILYTXNSREPORT
WHERE UBACCOUNTID = '01ED10EOD0100'
AND UBTXNAMT = '109.63'
AND UBTYPE = 'I'
AND UBVALUEDTTM LIKE '11/7/2015 12:00:00 AM%'
AND UBTXNAMTCR = '109.63'
AND UBTXNAMTDR = '0.0'
AND UBTXNCODE = 'IAP'
AND UBTXNNARRATION = 'Fixed Narrative:Interest Application'
AND UBTXNSRCBRANCH = '70000001.0'
AND UBTXNBASEEQ = '109.63'
AND UBCHANNELID = 'UXP'
An error occurred when executing the SQL command:
SELECT COUNT(*) FROM WASADMIN.UBTB_DAILYTXNSREPORT WHERE UBACCOUNTID='01ED10EOD0100' AND UBTXNAMT='109.63' AND UBTYPE='I' AND UBVALUEDTTM LIKE '11/7/2...
ERROR: operator does not exist: timestamp without time zone ~~ unknown
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 139
Expected answer: to get the count
LIKE is for comparing string values, not for timestamps.
I am not sure what the LIKE condition is supposed to achieve, but it seem you want to find rows where `` is equal to midnight of 2015-11-07. The following would do that:
AND ubvaluedttm = timestamp '2015-11-07 00:00:00'
A range condition is probably closer to what the LIKE condition would do:
AND ubvaluedttm >= timestamp '2015-11-07 00:00:00'
AND ubvaluedttm <= timestamp '2015-11-07 00:00:00.999999'

Why does the gorm postgresql throws pq: syntax error at or near ")"?

SELECT_QUERY = `SELECT * FROM events WHERE c_id = ? AND start_time > ? and
end_time < ?`
query := sr.db.Raw(SELECT_QUERY, request.GetCId(), startTime, endTime)
var v = request.GetVIds()
if len(v) > 0 {
query = query.Where(` v_id IN (?) `, v)
} //Only this block introduces first ) after end_time
var c = request.GetStatus().String()
if len(c) > 0 {
query = query.Where( " status = ? ", c) // this introduces the other opening brace //after AND
}
Following is the query generated and found in logs
SELECT * FROM events WHERE c_id = 1 AND start_time > '2020-04-16 18:42:00' and
end_time < '2020-04-16 18:45:50' ) AND ( v_id IN (1,2)) AND ( status = 'STATUS_MIDDLE_CLASS' ORDER BY start_time DESC LIMIT 5 OFFSET 1
The other solution in stackoverflow and internet article doesn't help.
PS: Is it because I mix db.Raw( ) and query.Where() ?
Changing ? to $1 doesn't fix the issue.
Basically a few things fixed the issue.
1) Mixing Raw and query.Where was one issue.
After making the Raw query to sr.db.Where
2)
SELECT_QUERY = `SELECT * FROM events WHERE c_id = ? AND start_time > ? and
end_time < ?`
already has select * from. And then using query := sr.db.Raw(SELECT_QUERY, request.GetCId(), startTime, endTime) introduces nested select *.
So, changed SELECT_QUERY as follows
SELECT_QUERY = `events WHERE c_id = ? AND start_time > ? and
end_time < ?`
solved the issue.
I found a workaround to the error which I received when I tried to add a timestamp filed in Go/Gorm solution with PostgreSQL equivalent to default: now() which is default: time.Now().Format(time.RFC3339)
I received the error because I use AutoMigrate() to create the tables in PostgreSQL. The one problem I found is when trying to use the default value of a function instead of a string (which one can use for a fixed timestamp).
So I had to go into DataGrid (since I use JetBrains, but you can use any PostgreSQL admin tool like pgAdmin) and manually add the timestamp field with default of now() or merely update the existing field to add the default of now(). Then the error goes away when doing your next build in Go.

ERROR: operator does not exist : time without time zone >= bytea

I get error like this
ERROR: operator does not exist : time without time zone >= bytea
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
when i try the following sql in java hibernate
select sto.name AS store,
sum(odi.subtotal_price) AS sales, sum(odi.qty) AS qty_sold,
((sum(odi.subtotal_price))/(sum(odi.qty))) AS average,
min(CAST(ord.date_out AS date)) AS start_date,
max(CAST(ord.date_out AS date)) AS end_date,
concat(min(CAST(ord.date_out AS time)), ' - ', max(CAST(ord.date_out AS time))) as time,
sum(odi.cost_of_good_sold*odi.qty) AS COGS, ((sum(odi.cost_of_good_sold*odi.qty))/(sum(odi.qty))) AS average_cogs,
date_trunc('day', ord.date_out) AS trx_day
FROM trx_order_detail_item odi
LEFT JOIN trx_order AS ord on ord.id = odi.order_id
LEFT JOIN mst_store AS sto on sto.id = ord.store_id
WHERE sto.id = :store and ord.date_out between :date1 and :date2
and CAST(ord.date_out AS TIME) BETWEEN :hour1 AND :hour2 and ord.order_status_id IN :orderstatus and ord.void_status = :voidStatus
GROUP BY sto.name, date_trunc('day', ord.date_out)
ORDER BY date_trunc('day', ord.date_out)
the error exist in CAST(ord.date_out AS TIME) BETWEEN :hour1 AND :hour2
before i added that it runs perfectly
any suggestion for this?

Coldfusion return query based on calculated age

I've got one site in ColdFusion where the client now wants to get a list of certain users who are over a particular age. Here's my code so far:
<cfset minRefAge = 21 >
<cfquery name="rsReferees" datasource="nbsa">
SELECT ID, userFirstName + ' ' + userLastName AS refName, userTown, userDOB, userAccess
FROM UsersSSO
WHERE (dateDiff("yyyy", userDOB, now() ) => #minRefAge#) AND userAccess = 4
</cfquery>
userDOB is my date of birth field. When I run it, I get the following error:
The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.
I can't spot the error. Could someone help?
Looks like your greater than or equal to sign is backwards. Other than that, your query looks fine to me.
SELECT ID, userFirstName + ' ' + userLastName AS refName, userTown, userDOB, userAccess
FROM UsersSSO
WHERE (dateDiff("yyyy", userDOB, now() ) >= #minRefAge#)
AND userAccess = 4
You may also need to put the ref age in quotes: '#minRefAge#'