scala newbie having troubles with Option, what's the equivalent of the ternary operator - scala

I've already read that the if statement in scala always returns an expression
So I'm trying to do the following (pseudo code)
sql = "select * from xx" + iif(order.isDefined, "order by " order.get, "")
I'm trying with
val sql: String = "select * from xx" + if (order.isDefined) {" order by " + order.get} else {""}
But I get this error:
illegal start of simple expression
order is an Option[String]
I just want to have an optional parameter to a method, and if that parameter (in this case order) is not passed then just skip it
what would be the most idiomatic way to achieve what I'm trying to do?
-- edit --
I guess I hurried up too much to ask
I found this way,
val orderBy = order.map( " order by " + _ ).getOrElse("")
Is this the right way to do it?
I thought map was meant for other purposes...

First of all you are not using Option[T] idiomatically, try this:
"select * from xx" + order.map(" order by " + _).getOrElse("")
or with different syntax:
"select * from xx" + (order map {" order by " + _} getOrElse "")
Which is roughly equivalent to:
"select * from xx" + order match {
case Some(o) => " order by " + o
case None => ""
}
Have a look at scala.Option Cheat Sheet. But if you really want to go the ugly way of ifs (missing parentheses around if):
"select * from xx" + (if(order.isDefined) {" order by " + order.get} else {""})

...or, if you really want to impress your friends:
order.foldLeft ("") ((_,b)=>"order by " + b)
(I would still recommend Tomasz's answer, but I think this one is not included in the scala.Option cheat sheet, so i thought I'd mention it)

Related

aggregate function as tuple argument postgres

I want to pass aggregate function like min, max etc as query parameter using Tuple.
Below is my query:
"select $5(CAST (vol AS FLOAT)) AS agg_v, "
+ "time_bucket_gapfill" + "(($1::text || ' minutes')::interval, t) AS time_function_minute, "
+ "tag_id from rtdata "
+ "where tag_id = any($2) and t > $3 and t < $4 "
+ "GROUP BY (tag_id, time_function_minute) ORDER BY time_function_minute"
But I'm getting following exception:
io.vertx.pgclient.PgException: syntax error at or near
"("
at io.vertx.pgclient.impl.codec.ErrorResponse.toException(ErrorResponse.java:29)
at io.vertx.pgclient.impl.codec.PrepareStatementCommandCodec.handleErrorResponse(PrepareStatementCommandCodec.java:62)
at io.vertx.pgclient.impl.codec.PgDecoder.decodeError(PgDecoder.java:233)
at io.vertx.pgclient.impl.codec.PgDecoder.decodeMessage(PgDecoder.java:122)
at io.vertx.pgclient.impl.codec.PgDecoder.channelRead(PgDecoder.java:102)
at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:253)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1422)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:931)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:700)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:635)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:552)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:514)
at io.netty.util.concurrent.SingleThreadEventExecutor$6.run(SingleThreadEventExecutor.java:1044)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:745)
But If I replace $5 with hardcode aggregate function it works. How can I pass aggregate function dynamically in this scenario?
RxJava code Snippet:
return txBegin()
.flatMapObservable(tx ->
tx.rxPrepare(abovesql)
.flatMapObservable(pq -> {
return pq.createStream(50,
Tuple.of(
evalBucketInterval(req),
req.getTags().toArray(new Integer[0]),
parse(req.getStartDate()),
parse(req.getEndDate()),
parse(req.getAggFunc())))
.toObservable();
})
.doAfterTerminate(tx::commit))
.map(this::toFuncJson);
PostgreSQL allows to use parameters only as values and doesn't understand when you try to use parameters for function names, table names, etc. So you cannot pass aggregate name as a parameter.
I suggest to work around it in your application by concatenating the string value containing the aggregate function name. I guess it can be something like, but I am not sure about the exact syntax and what limitations of your environment are:
"select "+ my_agg_func_name +"(CAST (vol AS FLOAT)) AS agg_v, "
+ "time_bucket_gapfill" + "(($1::text || ' minutes')::interval, t) AS time_function_minute, "
+ "tag_id from rtdata "
+ "where tag_id = any($2) and t > $3 and t < $4 "
+ "GROUP BY (tag_id, time_function_minute) ORDER BY time_function_minute"

multiple use of expression via jpql alias keyword

I'm using spring data with a postgresql server and i want to perform some GPS-data range queries. This means, given a coordinate i compute on the fly the distance from the entry to the given point and check for a certain range.
Since i also want to order my data regarding the distance and additionally i want to retrieve the actual distance too, in sql i would use the AS keyword to compute the expression only once and then use this auxiliary expression in the where and the order by part.
However, so far I haven't yet figured out how to do this in jqpl. So my query should do something like this:
SELECT NEW Result(p, <distance-expression>) FROM MyModel p where <distance-expression> <= :rangeParam order by <distance-expression>
however, i'm afraid that the will be evaluated more than once for each entry and so this will have a negative impact on the runtime/response time of the query.
Is there any way in jqpl to use the AS keyword to avoid the multiple evaluation of
<distance-expression>?
Best regards
A native query with an inner view should get the job done. Assuming class Location(id, latitude, longitude) and the Haversine formula for finding distances between points on great circles, the following repository method declaration with a custom native query should be sufficient:
#Query(nativeQuery = true
, value = "SELECT "
+ " r.id "
+ " , r.latitude "
+ " , r.longitude "
+ "FROM "
+ " (SELECT "
+ " l.id AS id "
+ " , l.latitude AS latitude "
+ " , l.longitude AS longitude "
+ " , 2 * 6371 * ASIN(SQRT(POWER(SIN(RADIANS((l.latitude - ?1) / 2)), 2) + COS(RADIANS(l.latitude))*COS(RADIANS(?1))*POWER(SIN(RADIANS((l.longitude - ?2) / 2)), 2))) AS distance "
+ " FROM "
+ " location l) AS r "
+ "WHERE "
+ " r.distance < ?3")
List<Location> findAllByProximity(BigDecimal latitude
, BigDecimal longitude
, BigDecimal distance);
Sample available on Github as an example (metric units assumed).
Note: The reason behind using a native query in the example as opposed to JPQL is the lack of support for trigonometric functions in JPQL. In cases where the expression is simpler and can be coded using native JPQL functions, the native query can be replaced with a JPA query.

query not executing when some input is given to it via 2 combo boxes and a text field because it work without the "where" thing

String query="select book_code, book_name, student_name, class, roll_no, issue_date, return_date from lib where" + s1 + "" + s2 + "" + s3 + ";" ;
ResultSet rs=stmt.executeQuery(query);
This is the code where s1,s2 are combo boxes from which the search field and operator is being selected and s3 is the search criteria.
Where am I wrong?
I think your mistake might be that you are missing a space when concatenating your query like this.
where" + s1 + "" + s2 + "" + s3 + ";"
(there is no space after the where keyword)
If you are using string you have to include that in single quotes.
String query="select book_code, book_name, student_name, class, roll_no, issue_date, return_date from lib where " + s1 + "" + s2 + "'" + s3 + "'";
So use the query in if condition according to the type that you choose.

JDBC select query with join and like fails

I am trying to search the data in two tables by getting the last four numbers of a SSN as input from the user.
I have a JDBC query that is as follows:
`String sql = "SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATE"
+"FROM" +" G.M_NAME_A N, G.M_ID_A M"
+"WHERE" + "N.NUMBER = M.NUMBER"
+"AND" + "M.SSN like 'l4ssn' ";'
It fails with either "FROM Keyword not found where expected" or "Invalid Column index".
Please help me format the query.
yes, it should fail. Check your SQL, you're missing a space in front of "FROM", and ditto most anywhere you're concatenating strings.
1、Change the SQL as follows. Space is added around FROM, WHERE and AND,If you follow what you write sql, sql stitching is so out:
String sql = "SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATEFROM G.M_NAME_A N, G.M_ID_A MWHEREN.NUMBER = M.NUMBERAND M.SSN like 'l4ssn' ";
String sql = "SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATE"
+" FROM G.M_NAME_A N, G.M_ID_A M"
+" WHERE N.NUMBER = M.NUMBER"
-- modify +" AND" + "M.SSN like 'l4ssn' ";'
+" AND M.SSN like '%l4ssn%' ";
Change the SQL as follows. Space is added around FROM, WHERE and AND
String sql = "SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATE"
+" FROM " +" G.M_NAME_A N, G.M_ID_A M"
+" WHERE " + "N.NUMBER = M.NUMBER"
+" AND " + "M.SSN like 'l4ssn' ";
Your query will look for M.SSN which is 14ssn it's just like saying M.SSN = '14ssn'. If you want to use query something similar to 14ssn use M.SSN like '%14ssn%' instead
Just on side note, use prepared statement instead of using the query parameter in your sql string.
http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
Try this query
String sql=" SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATE "+
" FROM G.M_NAME_A N,G.M_ID_A M "+
" WHERE N.NUMBER = M.NUMBER "+
" AND M.SSN like 'l4ssn' ";
The question is resolved using the following query and PreparedStatement:
String l4ssn=("%"+l4ssn);
String sql = "SELECT N.NUMBER,N.LAST_NAME,N.FIRST_NAME,M.SSN,M.SEX,M.BIRTH_DATE"
+" FROM " +" G.M_NAME_A N, G.M_ID_A M"
+" WHERE " + "N.NUMBER = M.NUMBER"
+" AND " + "M.SSN like ?";
PreparedStatement pstmt = new PreparedStatement(sql);
pstmt.setString(1,l4ssn);

JPQL "DISTINCT" returns only one result

I am confused by DISTINCT in JPQL. I have two JPQL queries identical except for "DISTINCT" in one of them:
String getObjectsForFlow =
"SELECT " +
" se.componentID " +
"FROM " +
" StatisticsEvent se " +
"WHERE " +
" se.serverID IS NOT NULL " +
" AND se.flowID = :uuid " +
" AND se.componentID IS NOT NULL " +
"ORDER BY " +
" se.timeStamp desc ";
String getObjectsForFlowDistinct =
"SELECT DISTINCT " +
" se.componentID " +
"FROM " +
" StatisticsEvent se " +
"WHERE " +
" se.serverID IS NOT NULL " +
" AND se.flowID = :uuid " +
" AND se.componentID IS NOT NULL " +
"ORDER BY " +
" se.timeStamp desc ";
I run a little code to get the results from each query and dump them to stdout, and I get many rows with some duplicates for non-distinct, but for distinct I get only one row which is part of the non-distinct list.
NOT DISTINCT
::: 01e2e915-35c1-6cf0-9d0e-14109fdb7235
::: 01e2e915-35c1-6cf0-9d0e-14109fdb7235
::: 01e2e915-35d9-afe0-9d0e-14109fdb7235
::: 01e2e915-35d9-afe0-9d0e-14109fdb7235
::: 01e2e915-35bd-c370-9d0e-14109fdb7235
::: 01e2e915-35bd-c370-9d0e-14109fdb7235
::: 01e2e915-35aa-1460-9d0e-14109fdb7235
::: 01e2e915-35d1-2460-9d0e-14109fdb7235
::: 01e2e915-35e1-7810-9d0e-14109fdb7235
::: 01e2e915-35e1-7810-9d0e-14109fdb7235
::: 01e2e915-35d0-12f0-9d0e-14109fdb7235
::: 01e2e915-35b0-cb20-9d0e-14109fdb7235
::: 01e2e915-35a8-66b0-9d0e-14109fdb7235
::: 01e2e915-35a8-66b0-9d0e-14109fdb7235
::: 01e2e915-35e2-6270-9d0e-14109fdb7235
::: 01e2e915-357f-33d0-9d0e-14109fdb7235
DISTINCT
::: 01e2e915-35e2-6270-9d0e-14109fdb7235
Where are the other entries? I would expect a DISTINCT list containing eleven (I think) entries.
Double check equals() method on your StatisticsEvent entity class. Maybe those semantically different values returns same when equals() is called hence producing this behavior
The problem was the "ORDER BY se.timeStamp" clause. To fulfill the request, JPQL added the ORDER BY field to the SELECT DISTINCT clause.
This is like a border case in the interplay between JPQL and SQL. The JPQL syntax clearly applies the DISTINCT modifier only to se.componentID, but when translated into SQL the ORDER BY field gets inserted.
I am surprised that the ORDER BY field had to be selected at all. Some databases can return a data set ORDERed by a field not in the SELECTion. Oracle can do so. My underlying database is Derby -- could this be a limitation in Derby?
Oracle does not support SELECT DISTINCT with an order by unless the order by columns are in the SELECT. Not sure if any databases do. It will work in Oracle if the DISTINCT is not required (does not run because rows are unique), but if it needs to run you will get an error.
You will get, "ORA-01791: not a SELECTed expression"
If you are using EclipseLink this functionality is controlled by the DatabasPlatform method,
shouldSelectDistinctIncludeOrderBy()
You can extend your platform to return false if your database does not require this.
Still, I don't see how adding the TIMESTAMP will change the query results?
Both queries are incorrect JPQL queries, because ORDER BY clause refers to the item that is not on select list. JPA 2.0 specification contains example that matches to this case:
The following two queries are not legal because the orderby_item is
not reflected in the SELECT clause of the query.
SELECT p.product_name
FROM Order o JOIN o.lineItems l JOIN l.product p JOIN o.customer c
WHERE c.lastname = ‘Smith’ AND c.firstname = ‘John’
ORDER BY p.price
SELECT p.product_name
FROM Order o, IN(o.lineItems) l JOIN o.customer c
WHERE c.lastname = ‘Smith’ AND c.firstname = ‘John’
ORDER BY
o.quantity
Of course it would be nicer if if implementation could give clear error message instead of trying to guess what is expected result of incorrect query.