OrientDB row count - java

How to get specific row count in Java from Object DB ?
I need to get result for query like :
SELECT COUNT(id) FROM Users WHERE banned=true

Try using * as field:
SELECT COUNT(*) as count FROM Users WHERE banned = false

OrientDB does support SQL like queries and also supports the count(<field>|*) function according to the documentation: http://code.google.com/p/orient/wiki/SQLWhere#Functions

Related

how to use pg_column_size in hibernate?

I am trying to get the size of a row in a postgresql table, I found that pg_column_size would do the trick, but its not working with hibernate :
#Query("SELECT pg_column_size(t.*) as filesize FROM TABLE as t where name=:name")
int getSize(#Param("name") String name);
intellij is giving this error :
< operator > or AS expected, got '('
I guess the problem is that hibernate doesnt support specific postgresql queries, it only supports the basic sql queries.
so is there a way around this ? if not is there a way to get/estimate the size of a postgresql row in java ?
In order to use a built-in postgres function , you have to declare you JPA query as nativeQuery
you should first change query to native (hibernate will directly execute the query instead of jpa -> sql generation )
#Query(value="SELECT pg_column_size(t.*) as filesize FROM TABLE as t where name=:name",nativeQuery=true)
int getSize(#Param("name") String name);
Also be sur of the TABLE name to be correct .
Add nativeQuery = true after the native query.
#Query("SELECT pg_column_size(t.*) as filesize FROM users as t where t.name=:name",nativeQuery = true)
use above Query, Hope This will work.

Get LIMIT value for a Select query in Cassandra java driver

How can I access the LIMIT clause on a SELECT statement in Cassandra? Suppose that I have the following SELECT statement
Select select = QueryBuilder.select().all().from("test","users");
select.limit(10);
Then somewhere else in the code I want to get the LIMIT clause(in this example 10). Is there an API through which I could access it?
You can use select.getQueryString to get the full query string and then filter the string to get what you need.

Convert complex DB2 SQL with ROW_NUMBER() to mongo query

I'm new to MongoDB, I'm trying to convert existing DB2 query to MongoDB. I'm Using Java to run this query.
Current DB2 Query:
SELECT * FROM
(SELECT MBI.*, ROW_NUMBER() OVER Order by PDATE DESC AS rownumber
FROM USER1.COLLECTION1 MBI, USER1.COLLECTION2 IC
WHERE MBI.VISIBILITY = 1 and MBI.MBOXID = '1234'
AND UPPER(MBI.MBOXITID) >= '1234555'
AND UPPER(MBI.CATEGORY) = 'S'
AND UPPER(MBI.Mimetype) = 'PDF'
AND UPPER(MBI.Psystemid) = 'TBA'
AND days(current date) - days(MBI.PDATE) < 20
AND MBI.MBOXITID = IC.ICONTENTID) AS FinalResult
WHERE rownumber BETWEEN 1 and 2 Order by PDATE DESC
Am really not sure how to get the ROW_NUMBER details in MongoDB.
Can you help for me solve problem?
#bharathiraja, hopefully, you've done the migration by now and figured out the steps. you'll have to convert your inner joins to mongodb lookup operator and then remove nulls (because lookup is left-outer-join-operator). From your query, looks like you're trying to paginate using row_number(). In Mongodb aggregation framework, after you've done the sort on the PDATE, you can add the limit and skip operator to paginate.
At Couchbase (where I work), we've built row_number() just like standard SQL. Checkout. https://blog.couchbase.com/on-par-with-window-functions-in-n1ql/

jOOQ problems with limit..offset - no values sets

I am trying to build a query using jOOQ, this is my test code:
DSLContext create = DSL.using(SQLDialect.DERBY);
String query = create.select().from(TABLE).limit(1).offset(0).getSQL()
I get as query:
select field1, field2...fieldN etc from TABLE offset ? rows fetch next ? rows only
the problem is ? in ? rows fetch next ? rows only it seems to ignore the values that i used in limit and offset to build the query, why?
I am trying to select the first row from the results and I am using jooq 3.4.1
Thanks for the help
Query.getSQL() returns your SQL string with ? as placeholders for your bind variables. The idea is that you can feed this statement to a PreparedStatement and then explicitly bind all variables, which are available through Query.getBindValues().
You can also have jOOQ inline all your bind variables, by calling Query.getSQL(ParamType) as such:
String sql = query.getSQL(ParamType.INLINED);

Building ordered and limited delete request in jooq

I recently encountered the following problem with buiding queries in jooq (version 3.1.0):
I want to build delete statement with order and limit constraints. So, my aim is to build something like this:
DELETE FROM table ORDER BY field DESC LIMIT 1 (this is MySql syntax)
But i haven't found nesessary methods in result delete query object:
DSLContext context = createContext();
DeleteWhereStep delete = context.delete(createTable(table));
DeleteConditionStep whereStep = delete.where(condition);
whereStep.orderBy(...)//and no such method here
There are all nesessary methods in select statements and none for delete.
Is it possible to set order and limit for delete request in jooq?
As of jOOQ 3.2, these sorts of extensions are currently not implemented yet. Chances are, that #203 could be implemented in jOOQ 3.3, though.
In the mean time, you have two options:
Resort to plain SQL
i.e. write something like:
context.execute("DELETE FROM {0} ORDER BY {1} DESC LIMIT 1",
createTable(table),
field);
Manually transform your SQL statement into something equivalent
I suspect that the ORDER BY .. LIMIT extension to the MySQL DELETE statement is just sugar for:
DELETE FROM table t
WHERE t.id IN (
SELECT id FROM table
ORDER BY field LIMIT 1
)
Or with jOOQ:
context.delete(TABLE)
.where(TABLE.ID.in(
select(TABLE.ID)
.from(TABLE)
.orderBy(TABLE.FIELD)
.limit(1)
))

Categories

Resources