CakePHP 3 - sql statement string to query object - cakephp-3.0

Is there a way to convert an sql string to a query object?
I have a complicated SQL statement as a string. Later I would join with a query object.
So i would need to convert my SQL string to a query object.

Related

Selecting rows in mySQL table by comparing the value to python dictionary

I have 2 databases one in mongoDB and one table in mySQL. I am taking data from mongoDB which is being stored in a dictionary and looks like this-
dictionary = {
"message_code": "12345"
}
I want to match this message_code value in the mySQL tables colums called final_code and retrieve table rows which match the message_code. How can I do that?
SELECT *
FROM mysql_table
WHERE final_code = dictionary.message_code
Something like this?
You pass a string to mysql, not python objects. So you must build that string interpolating the values you want in python, i.e.
'SELECT * FROM mysql_table WHERE final_code='+dictionary["message_code"]
and pass that string to mysql as query.

Flatten a json string to a nested table in BigQuery

I have a json string and I managed to transpose the json string to the following table:
SQL I used and the outcome
However, there is sub-tree in the json string called 'activequeue' - it is an array, and with this way of SQL query I'm not getting a nested table attached to the column named 'activequeue' with columns such as 'id' etc.
Any ways I can achieve this?

MySQL Query - Convert ResultSet to JSON

I need to convert select * from {table_name} into JSON output.Instead of specifying separate key-value pair in 'JSON_OBJECT'.
I have tried this query
select JSON_ARRAYAGG(json_object("col_name_1",alias.column_1,"col_name_2",alias.column_2)) from {table_name} alias;
This one is giving me results what I need, but my question is instead of giving as key-value pair in json_object I need to fetch all columns by giving something like '*'.
MySQL Database Version: 5.7.24

How to escape a whole sql string instead of escaping each argument?

I use https://github.com/mysqljs/mysql.git library.
I have a mysql db query architecture in which I can not modify the SQL query file one by one to escape each argument for there are too many files, but all the SQL queries will call the query method of a same base mysql instance, so I wonder if I can escape the eventual SQL string in the base mysql query method.
I want to escape the whole SQL string like
select * from tableA where name = 'foo'bar
to
select * from tableA where name = 'foo\'bar'
with some function like mysql_escape("select * from tableA where name = 'foo'bar'") instead of doing this using preparing queries or concating escaped strings.
There isn't a way to do this that wont result in a really inefficient function or some bad hack. Just use parameterized queries, Its basically what they are there for. If you cant use those you use concat strings.
Running mysql_escape on a whole query will require the function to know what characters are part of your query and what characters are part of the input values. You could write some kind of stupid regex to try pull the values from the query and then escape them but its just a bad idea.

mysql query parameter string array

I am trying to pass a parameter to a query, rather than write copious text I have narrowed it down to this simple explanation.
The frament I am trying to insert into is
where pkw_0.keyword in (:kwd)
I have used a String[] to construct a string of the form vals="'AVal','BVal'" which I pass to the query using setParameter("kwd",vals); The query returns zero results. However if I construct the query by hand and use the mysql console the query returns 1 result which is expected.
So I am assuming that either a single string is incorrect for the parameter or there is some conditioning of the values that I need to do prior to passing them via the setParameter call.
Each parameter can only represent a single literal value. You will need to create multiple placeholders in your prepared statement (one for each value) and then provide each value to MySQL as a separate parameter.