You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@metamodel.apache.org by Dana Borger <Da...@sas.com> on 2017/07/29 12:30:24 UTC
Select count
When selecting a count from a query result is there a better way than this? For example, I’m seeing Postgres return a Long and Oracle return a BigDecimal, so always casting to Long is no good.
Query q = new Query().
q.from(“mytable”);
q.selectCount(); // SELECT COUNT(*) FROM “mytable”;
…
try (DataSet dataSet = dataContext.executeQuery(q)) {
if (dataSet.next()) {
Row r = dataSet.getRow();
for (SelectItem item : r.getSelectItems()) {
Object N = r.getValue(item);
if (N instanceof Number) {
long count = ((Number)N).longValue());
}
else {
...
}
}
}
}
Thanks!
db
Re: Select count
Posted by Kasper Sørensen <i....@gmail.com>.
I always just go ahead and cast it to Number without instanceof checking
TBH. All the cases that I know of are some subclass of Number.
2017-07-29 5:30 GMT-07:00 Dana Borger <Da...@sas.com>:
>
>
> When selecting a count from a query result is there a better way than this?
> For example, I’m seeing Postgres return a Long and Oracle return a
> BigDecimal, so always casting to Long is no good.
>
>
>
> Query q = new Query().
>
> q.from(“mytable”);
>
> q.selectCount(); // SELECT COUNT(*) FROM “mytable”;
>
>
>
> …
>
>
>
> try (DataSet dataSet = dataContext.executeQuery(q)) {
>
> if (dataSet.next()) {
>
> Row r = dataSet.getRow();
>
> for (SelectItem item : r.getSelectItems()) {
>
> Object N = r.getValue(item);
>
> if (N instanceof Number) {
>
> long count = ((Number)N).longValue());
>
> }
>
> else {
>
> ...
>
> }
>
> }
>
> }
>
> }
>
>
>
> Thanks!
>
> db
>
>
>