What's the proper syntax for SETTING a table value - sql

In the Dao Interface I need to use
#Query("UPDATE table SET user_name = value")
fun addValue(value: String)
Here value is not recognized as the input from the function addValue(value:String)
IDE reports value is an Unresolved symbol
How do make value from the function be recognised as input in the SQL statement
I'm assuming it might have something to do with Entities what Entities to I need to include in my Database class
Here
#Database(Entities = MyEntity::class], version = 1)
abstract class MyEntityDatabase: Room database()

The room provides colon operator to resolve the argument in the query. So you query #Query("UPDATE table SET user_name = value") will be changed to Query("UPDATE table SET user_name = :value") then room can resolve the argument.
So it should be like
#Query("UPDATE table SET user_name = :argName")
fun addValue(argName: String)

Related

Cast values in update query

I have this SQL native query which I want to use from Spring JPA repository:
update words
set low_range = :lowRange, high_range = :highRange
where keyword = :keyword
Error:
ERROR: column "low_range" is of type numeric but expression is of type bytea
I use this column definition:
#Column(name = "low_range")
private Float lowRange;
How I can convert or cast the value into this update query?

Calculated date in WHERE condition of CDS view

I'm trying to get a list of valid system status for the notification object, in order to not check all the notifications in the table, I want to execute the selection by checking only the last 2 years of data.
Maybe there is a better solution to my problem, but I'm still curious about this technical limitation. To my knowledge, the system status in SAP are kind of hardcoded and can't be determined per object via any table (SAP could add new system status any moment).
I tried to create the below CDS view, but the function dats_add_months can't be used in the where condition, is there a solution to that? Notice that 7.50 doesn't have session parameter for system date so I use an environment variable:
define view ZNOTIF_SYS_STATUS
with parameters sydat : abap.dats #<Environment.systemField: #SYSTEM_DATE
as select distinct from qmel as notif
inner join jest as notif_status on notif_status.objnr = notif.objnr
and notif_status.inact = ''
inner join tj02t as sys_status on sys_status.istat = notif_status.stat
and sys_status.spras = $session.system_language
{
key sys_status.txt04 as statusID,
sys_status.txt30 as description
} where notif.erdat > dats_add_months($parameters.sydat, -12, '') //THIS CAN'T BE USED!!
Putting built-in functions in RHS position of WHERE is supported only since 7.51 and you have 7.50 as you said. That is why it works for Haojie and not for you.
What can be done here? Possible option is CDS table function which consumes AMDP-class. Consider this sample:
Table function
#EndUserText.label: 'table_func months'
define table function ZTF_MONTHS
with parameters
#Environment.systemField : #SYSTEM_DATE
p_datum : syst_datum
returns {
mandt : abap.clnt;
num : qmnum;
type : qmart;
}
implemented by method zcl_cds_qmel=>get_last_two_years;
AMDP
CLASS zcl_cds_qmel DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_amdp_marker_hdb.
TYPES: tt_statuses TYPE STANDARD TABLE OF qmel.
CLASS-METHODS get_last_two_years FOR TABLE FUNCTION ztf_months.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_cds_qmel IMPLEMENTATION.
METHOD get_last_two_years BY DATABASE FUNCTION
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY.
twoyrs := add_months (CURRENT_DATE,-12)
RETURN SELECT mandt, qmnum AS num, qmart AS type FROM qmel WHERE erdat > twoyrs;
ENDMETHOD.
ENDCLASS.
It is very simplified compared to your original task but gives you the idea how to do this.

jdbi #sqlquery: could not determine data type of parameter $1

I am trying to add optional/nullable parameters to my sql query.
#SqlQuery("select * from temp where param1 = :param1")
List<Temp> findValues(#Bind("param1") String param1)
Problem: param can be null. So I tried to add null check in the sql query.
SqlQuery("select * from temp where ( (:param1 is not null) and (param1 = :param1"))
This throws the following error:
org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $1
In my problem I have to add multiple params all of which are nullable. (optional filters)
NOTE coalesce(:param1, param1) = param1 : doesn't work quite right. If param1 is sent, it gives back results where value is either param1 value, or null.
How can I make this null check work? Or is there a way that I can generate the sql query and pass it as a string to #SqlQuery?
I have looked everywhere but can't seem to find a solution. Any help would be appreciated.
If you want to use it as optional filters, you could use #Define. Using #Define, you define attributes for the template engine to use(jdbi v2 uses ST v3). Then, your example would look like something like this:
#SqlQuery("SELECT * FROM temp WHERE 1 = 1 <if(PARAM1)> AND param1 = :param1 <endif>)
List<Temp> findValues(#Define("PARAM1") #Bind("param1") String param1)
So, in this way, if value for param1 is null, query will look like:
SELECT * FROM temp WHERE 1 = 1
otherwise:
SELECT * FROM temp WHERE 1 = 1 AND param1 = :param1

QueryDSL like operation SimplePath

Similarly to this question I would like to perform an SQL "like" operation using my own user defined type called "AccountNumber".
The QueryDSL Entity class the field which defines the column looks like this:
public final SimplePath<com.myorg.types.AccountNumber> accountNumber;
I have tried the following code to achieve a "like" operation in SQL but get an error when the types are compared before the query is run:
final Path path=QBusinessEvent.businessEvent.accountNumber;
final Expression<AccountNumber> constant = Expressions.constant(AccountNumber.valueOfWithWildcard(pRegion.toString()));
final BooleanExpression booleanOperation = Expressions.booleanOperation(Ops.STARTS_WITH, path, constant);
expressionBuilder.and(booleanOperation);
The error is:
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [7!%%] did not match expected type [com.myorg.types.AccountNumber (n/a)]
Has anyone ever been able to achieve this using QueryDSL/JPA combination?
Did you try using a String constant instead?
Path<?> path = QBusinessEvent.businessEvent.accountNumber;
Expression<String> constant = Expressions.constant(pRegion.toString());
Predicate predicate = Expressions.predicate(Ops.STARTS_WITH, path, constant);
In the end, I was given a tip by my colleague to do the following:
if (pRegion != null) {
expressionBuilder.and(Expressions.booleanTemplate("{0} like concat({1}, '%')", qBusinessEvent.accountNumber, pRegion));
}
This seems to do the trick!
It seems like there is bug/ambiguity. In my case, I need to search by couple fields with different types (String, Number), e.g. SQL looks like:
SELECT * FROM table AS t WHERE t.name = "%some%" OR t.id = "%some%";
My code looks like:
BooleanBuilder where = _getDefaultPredicateBuilder();
BooleanBuilder whereLike = new BooleanBuilder();
for(String likeField: _likeFields){
whereLike = whereLike.or(_pathBuilder.getString(likeField).contains(likeValue));
}
where.and(whereLike);
If first _likeFields is type of String - request works fine, otherwise it throws Exception.

Spring Hibernate SQL Query

I have a VO class which has the getter and setter of another VO class too. For example:
Class DocumentVO{
PrintJobVO job;
PrintRunVO run;
String id;
getters and setters..
}
Now I have a requirement to use the Native SQL Query using spring hibernate. When I want to map the ids I have a problem. My query is,
select {r.*},{d.*}
from runs {r}, documents {d}
where {r}.RUN_ID as {r.id} = d.RUN_ID as {d.run.id}
Here run is of type PrintRunVO which has its id and other values. How can I map them in my SQL? I am getting an error like invalid user.table.column, table.column, or column specification.
What's the way to overcome this?
Use the Result Transformer concept in your plain SQL query.
String query = "myquery";
SQLQuery q = session.createSQLQuery(query);
q.addScalar("param1", Hibernate.STRING);
q.addScalar("param2", Hibernate.STRING);
q.setResultTransformer(Transformers.aliasToBean(MyVO.class));
q.setParameter("queryParam1", "some value");
return q.list();