Iterate over string in mybatis - mybatis

my string parameter is like this: '(1,2,3,4)'
I want to iterate over this
SELECT * FROM table WHERE value IN #{myparameter}
How can I do this?

You can put the value into the query verbatim like this:
SELECT * FROM table WHERE value IN ${myparameter}
But note that this does not use prepared statements parameters. It has all the downsides of not using prepared statements, namely: it is not type save, vulnerable to SQL injection, requires escaping etc
You should better convert the string to a list and then generate the query in a type safe manner:
SELECT *
FROM table
WHERE value IN
<foreach item="item" index="index" collection="myparameter"
open="(" separator="," close=")">
#{item}
</foreach>
myparameter should be a Collection of whatever type the value column is.

Related

PostgreSQL - jsonb - How to get the datatype for value in query with jsonpath

In PostgreSQL using jsonb column, is there a way to select / convert an attribute with actual datatype the datatype instead of getting it as a string object when using jsonpath? I would like to try to avoid cast as well as -> and ->> type of construct since I have to select many attributes with very deep paths, I am trying to do it using jsonpath and * or ** in the path
Is it possible to do it this way or must I use the -> and ->> for each node in the path ? This will make the query look complicated as I have to select about 35+ attributes in the select with quite deep paths.
Also, how do we remove quotes from the selected value?
This is what I was trying, but doesn't work to remove quotes from Text value and gives an error on numeric
Select
PolicyNumber AS "POLICYNUMBER",
jsonb_path_query(payload, '$.**.ProdModelID')::text AS "PRODMODELID",
jsonb_path_query(payload, '$.**.CashOnHand')::float AS "CASHONHAND"
from policy_json_table
the PRODMODELID still shows the quotes around the value and when I add ::float to second column, it gives an error
SQL Error [22023]: ERROR: cannot cast jsonb string to type double precision
Thank you
When you try to directly cast the jsonb value to another datatype, postgres will attempt to first convert it to a json text and then parse that. See
How to convert Postgres json(b) to integer?
How to convert Postgres json(b) to float?
How to convert Postgres json(b) to text?
How to convert Postgres json(b) to boolean?
When you have strings in your JSON values, to avoid the quotes you'll need to extract them by using one of the json functions/operators returning text. In your case:
SELECT
PolicyNumber AS "POLICYNUMBER",
jsonb_path_query(payload, '$.**.ProdModelID') #>> '{}' AS "PRODMODELID",
(jsonb_path_query(payload, '$.**.CashOnHand') #>> '{}')::float AS "CASHONHAND"
FROM policy_json_table
jsonb_path_query function returns data with quotes (""), so you cannot cast this to integer or float. For casting value to integer, you need value without quotes.
You can use this SQL for getting without quotes:
Select
PolicyNumber AS "POLICYNUMBER",
(payload->>'ProdModelID')::text AS "PRODMODELID",
(payload->>'CashOnHand')::float AS "CASHONHAND"
from policy_json_table
If you need to use exactly jsonb_path_query then you can trim these quotes:
Select
PolicyNumber AS "POLICYNUMBER",
jsonb_path_query(payload, '$.**.ProdModelID')::text AS "PRODMODELID",
trim(jsonb_path_query(payload, '$.**.CashOnHand')::text, '"')::float AS "CASHONHAND"
from policy_json_table

Azure data factory: pass where clause as a string to dynamic query with quotes

I have a Lookup that retrieves a few records from a MS SQL table containing schema, table name and a whole where clause. These values are passed to a copy data (within a ForEach) In the copy data i use a Dynamic query statement like:
#concat('select a.*, current_date as crt_tms from ',item().shm_nam,'.',item().tab_nam,
item().where_clause )
This construction works fine without the where_clause or with a where clause with an integer. But it goes wrong with strings like:
'a where a.CODSYSBRN ='XXX' ;'
it's about the quote (')
How can i pass it through?
I know that the where clause as a fixed string in the dynamic query works when i use double quotes (to escape the single quote):
'a where a.CODSYSBRN =''XXX'' ;'
Point is i need the where clause to be completely dynamic because it differ per table
whatever i try i get this kind of error:
Syntax error or access violation;257 sql syntax error: incorrect syntax near "where a"
ps i also tested this, but with the same result:
select a.*, current_date as crt_tms from #{item().shm_nam}.#{item().tab_nam} a #{item().where_clause}
As you have mentioned you are getting whole where clause from the lookup table, the query must have included the column values in where clause for string and integer types separately.
Example lookup table:
In your copy activity, you can use Concat() function as you were already doing it, to combine static values & parameters.
#concat('select * from ',item().schma_name,'.',item().table_name,' ',item().where_clause)
For debugging purposes, I have added the expression in set variable activity, to see the value of the expression.
Iteration1:
Iteration2:

MyBatis dynamically including script

I am trying to dynamically include select query based on some input parameters.
The following is what I am trying to achieve.
<sql id="query1">
SELECT * from
table_1
WHERE a = #{param1}
</sql>
<sql id="query2">
SELECT * from
table_2
WHERE b = #{param2}
</sql>
<select id = "selectSomethingFromDB">
<include refid="#{readerIdName}" />
</select>
I am planning to pass the sql id name as parameter to the query and trying to dynamically choose the select query based on this param. (Sort of like a factory design implementation).
However #{readerIdName} is not getting replaced with the value I am passing as param.
Is something like this possible?
The xml attributes at this level are not evaluated in runtime by mybatis.
You can use a single <SQL id="..."> query and an <if test="..."> dynamic sql in it.

Does Mybatis support DDL?

My project is a Database center, like PLSQL but used in a web browser. Sometimes I need to create or alter a table, but I don't know if Mybatis supports DDL, and I haven't found any documents about this.
For the most part DDL works just like DML using mybatis. The one difference is that you will need to use ${} instead of #{} for parameters. Most databases do not support prepared statements with DDL. The $ notation is a string substitution rather than a parameter for a prepared statement.
<update id="exchangePartition" parameterType="java.util.Map">
alter table ${destinationTableName}
exchange partition ${destinationPartitionName}
with table ${sourceTableName}
including indexes
with validation
</update>
It is also helpful to know the call syntax with the statement type callable to invoke stored procedures.
<update id="gatherStatistics" statementType="CALLABLE" parameterType="Map">
{call
dbms_stats.gather_table_stats(
ownname => #{tableOwner},
tabname => #{tableName}
<if test="partitionName != null">
, partname => #{partitionName}
</if>
)
}
</update>
MyBatis supports any native SQL/PlSql commands.
So yes, it supports DDL statements.

How to pass table values parameter using dynamic query in SQL server 2008

I am calling one function using dynamic SQL query which accepts TVP. Is there any way to pass table values parameter in dynamic sql. One thing I can do to achieve this is to pass comma separated string and then convert this comma separated string to table. But the problem is then I have to convert string - table for each item in my table (The function is called for all the rows) which can be avoided if I can pass table in dynamic sql.
Thanks
Ashwani
Certainly. Where inputTable is your TVP type and #test is the variable:
exec sp_executesql
N'select
t.*
from #test as t
where t.ID = 2'
,N'#test inputTable readonly' -- note the readonly
,#test