doctrine DQL query Error: Expected end of string, got '*' - doctrine

I have DQL query that throws this exception:
[Doctrine\ORM\Query\QueryException] Error: Expected end of string, got '*'
Here is the query:
UPDATE Bundle:Table t SET t.column = :variable * 100
I don't understand the problem with this query, and those two are working:
UPDATE Bundle:Table t SET t.column = 100 * :variable
UPDATE Bundle:Table t SET t.column = 1 * :variable * 100
Any idea?

Is :variable definitely a number (int/float)? If it's a string then it may be that it can't produce a query that's syntactically correct:
UPDATE some table t SET t.column = '1' * 100;
Whereas maybe the versions with a number first "100 * :variable" and "1 * :variable * 100" can cast it correctly because there is an integer first.
Maybe you can post your setParameter() and check if you can call getSQL() to see what the constructed SQL query looks like.

The issue is related to https://github.com/doctrine/orm/issues/8011 and not fixed until today.
Most likely
UPDATE Bundle:Table t SET t.column = ((:variable * 100))
will do the trick and have the parser handle the expression correctly. This has been described in https://github.com/doctrine/orm/issues/8011#issuecomment-758492375.

Related

How to pass a list to an IN clause via a placeholder with Ruby Sequel

I am trying to pass a quoted list to a placeholder in an IN clause with Ruby Sequel.
Specifically:
query = "
SELECT *
FROM PRICE
WHERE REGION IN (?)"
Then I use Ruby Sequel: dataset = dbr[query, param_values]; dataset.sql.
If I pass param_values as a string like this: param_values = "'NSW1', 'VIC1'", the where clause becomes:
WHERE REGION IN ((N'''NSW1'', ''VIC1''')) which doesn't work - presumably too many quotes. If I try passing the parameter as an array of quoted strings like this: param_values = %w[NSW1 VIC1]
the where clause becomes: WHERE REGION IN ((N'NSW1' = N'VIC1')) which won't work.
Is there something that will work?
I would use Sequel's query builder language instead of writing SQL manually which would look like this if you used default naming conventions:
DB[:prices]
.select(:date, :price, :region_id)
.where(region_id: %w[NSW1 VIC1])
query = "
SELECT *
FROM PRICE
WHERE REGION IN ?"
regions = Sequel.lit("'NSW1', 'VIC1'")
sql = db.fetch(query, regions).sql
puts "sql: #{sql}"
Gives:
sql: SELECT *
FROM PRICE
WHERE REGION IN ('NSW1', 'VIC1')
which is what we want.

Calculation into a Field Using Conditional Logic in oracle form

How to calculate Field Using Conditional Logic in oracle form
like if i select one item then formula one calculate value else formula2
Sounds like an ordinary IF:
if :block.item = 'A' then
:block.calculated_field := 100 * :block.item_B / 3.14;
elsif :block.item = 'B' then
:block.calculated_field := 200 * :block.item_c * 1.66;
end if;
If that's not it, could you explain it somewhat better? Perhaps even attach screenshot which shows what you need?
use case when like :
case when :block.item = 'A' then
:block.calculated_field := 100 * :block.item_B / 3.14;
when :block.item = 'B' then
:block.calculated_field := 200 * :block.item_c * 1.66;
end

adding array as parameter to sql query in ruby

I have an array
ziparray = ["95626", "95645", "95837"]
I want to pass this to my sql query ,
sql = "SELECT * from table_name WHERE code in ($1);"
res1 = conn.exec(sql, [ziparray])
It does work for single values.
I am using pg gem and connecting to database using
conn = PG.connect()
I am using postgres and it doesn't take double quotes . I am assuming that to be the problem.
How to achieve this.
Update
I could convert to desired string using
str = "'"
str << ziparray.join("','")
str << "'"
#print str
But I guess the problem is passing of multiple parameters.
this works -
res1 = conn.exec(fipscodesql, ['95626'])
But not this
res1 = conn.exec(fipscodesql, ['95626', '95625'])
and this is exactly what I did when I converted the array to string. I guess this is not the right way to use parameters.
is there any other way.
As others said, you can't parametrise a whole array. Use this instead:
ziparray = ["95626", "95645", "95837"]
zip_placeholders = ziparray.map.with_index(1) { |_, i| "$#{i}" }.join(', ')
sql = "SELECT * from table_name WHERE code in (#{zip_placeholders});"
# => "SELECT * from table_name WHERE code in ($1, $2, $3)"
Then you can use the normal parameter binding.
[ziparray].map { |zip| "'#{zip}'" }.join(',')
SQL method "IN" doesn't use array.
So:
IN(234) - correct
IN(234,543) - correct
IN([234,543]) - wrong
You can try to convert array to string and pass variable $1 like '95626, 95645, 95837'
If x is an array and x = ['a','b','c']
quotes = (select * from quote in (?), x)
We cannot use this in a SQL parameter
x.join(',') returns "a,b,c". If it is int this is not a problem.
Instead, save this array in a variable and use it in a SQL parameter.
a = x and then use quotes = (select * from quote in (?),a)

Search sqlite3 table for an integer using C++

I have an sqliteCpp wrapper for my sqlite3 and was wondering if there is a query to search for integer in 'Symcod' columns as shown below:
sym = 100;
SQLite::Statement query(db, "SELECT Symcod FROM RawData WHERE EXISTS Symcod = sym");
but this gives me an Syntax error. Is there a way I can search the table for a integer using a variable name?
The sqlitecpp documentation suggests
// Compile a SQL query, containing one parameter (index 1)
SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?");
// Bind the integer value 6 to the first parameter of the SQL query
query.bind(1, 6);
// Loop to execute the query step by step, to get rows of result
while (query.executeStep())
{
//.... do stuff with e.g. query.getColumn()
}
So you would construct your query using a ? for the variable, then do query.bind(1,sym)

SEQUEL - Subquery count syntax

Can anyone help me with the sequel syntax for the following the ruby orm sequel:
SELECT *, (SELECT COUNT(*) FROM todos WHERE reference_email_id = "emails".id) todo_count
FROM "emails"
INNER JOIN "email_participants"
ON ("email_participants"."email_id" = "emails"."id")
WHERE ("user_id" = 1)
I cannot quite get the syntax, I have this so far:
scope = Email.inner_join(:email_participants, {email_id: :id})
.where(user_id: query.user_id)
.select_append {
Attachment.where(reference_email_id: Sequel.qualify(:emails, :id))
.count(:id)
.exists
.as(:attachment_count)
}
I get the following error:
missing FROM-clause entry for table "emails" LINE 1: ... FROM
"attachments" WHERE ("reference_email_id" = "emails"."...
My guess is you should remove the .exists line. It's hard to say conclusively since you didn't post the SQL produced.

Resources