Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.bridje.orm.impl.AbstractQuery Maven / Gradle / Ivy
/*
* Copyright 2016 Bridje Framework.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bridje.orm.impl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.bridje.orm.Column;
import org.bridje.orm.Condition;
import org.bridje.orm.OrderBy;
import org.bridje.orm.Query;
import org.bridje.orm.Table;
import org.bridje.orm.TableColumn;
import org.bridje.orm.TableRelationColumn;
import org.bridje.orm.impl.sql.SelectBuilder;
abstract class AbstractQuery implements Query
{
@Override
public List fetchAll() throws SQLException
{
List params = new ArrayList<>();
TableImpl table = getTable();
EntityContextImpl ctx = getCtx();
String columns = table.allFieldsCommaSep();
SelectBuilder qb = createQuery(columns, params);
if(getPage() > 0)
{
int index = ((getPage() - 1) * getPageSize());
qb.limit(index, getPageSize());
}
return ctx.doQuery(qb.toString(),
(rs) -> table.parseAll(rs),
params.toArray());
}
@Override
public List fetchAll(Column column) throws SQLException
{
List params = new ArrayList<>();
TableImpl table = getTable();
AbstractColumn columnImpl = (AbstractColumn)column;
EntityContextImpl ctx = getCtx();
SelectBuilder qb = createQuery(column.writeSQL(params, ctx), params);
if(getPage() > 0)
{
int index = ((getPage() - 1) * getPageSize());
qb.limit(index, getPageSize());
}
return ctx.doQuery(qb.toString(),
(rs) -> (List)columnImpl.unserialize(table.parseAll(1, column, rs)),
params.toArray());
}
@Override
public T fetchOne() throws SQLException
{
List parameters = new ArrayList<>();
TableImpl table = getTable();
EntityContextImpl ctx = getCtx();
SelectBuilder qb = createQuery(
table.allFieldsCommaSep(),
parameters);
qb.limit(0, 1);
return ctx.doQuery(qb.toString(),
(rs) -> table.parse(rs),
parameters.toArray());
}
@Override
public C fetchOne(Column column) throws SQLException
{
List parameters = new ArrayList<>();
TableImpl table = getTable();
AbstractColumn columnImpl = (AbstractColumn)column;
EntityContextImpl ctx = getCtx();
SelectBuilder qb = createQuery(column.writeSQL(parameters, ctx), parameters);
qb.limit(0, 1);
return columnImpl.unserialize(ctx.doQuery(qb.toString(),
rs -> table.parse(1, column, rs),
parameters.toArray()));
}
@Override
public List fetchAll(Table table) throws SQLException
{
TableImpl tableImpl = (TableImpl)table;
List params = new ArrayList<>();
EntityContextImpl ctx = getCtx();
String columns = tableImpl.allFieldsCommaSep();
SelectBuilder qb = createQuery(columns, params);
if(getPage() > 0)
{
int index = ((getPage() - 1) * getPageSize());
qb.limit(index, getPageSize());
}
return ctx.doQuery(qb.toString(),
rs -> tableImpl.parseAll(rs),
params.toArray());
}
@Override
public R fetchOne(Table table) throws SQLException
{
TableImpl tableImpl = (TableImpl)table;
List parameters = new ArrayList<>();
EntityContextImpl ctx = getCtx();
SelectBuilder qb = createQuery(
tableImpl.allFieldsCommaSep(),
parameters);
qb.limit(0, 1);
return ctx.doQuery(qb.toString(),
rs -> tableImpl.parse(rs),
parameters.toArray());
}
@Override
public long count() throws SQLException
{
List parameters = new ArrayList<>();
TableImpl table = getTable();
EntityContextImpl ctx = getCtx();
SelectBuilder qb = createQuery("COUNT(*)", parameters);
return ctx.doQuery(qb.toString(),
rs -> table.parseCount(rs),
parameters.toArray());
}
@Override
public boolean exists() throws SQLException
{
return count() > 0;
}
@Override
public Query join(TableRelationColumn relation)
{
return new JoinQueryImpl<>(JoinType.INNER, this, (TableRelationColumnImpl)relation);
}
@Override
public Query leftJoin(TableRelationColumn relation)
{
return new JoinQueryImpl<>(JoinType.LEFT, this, (TableRelationColumnImpl)relation);
}
@Override
public Query rightJoin(TableRelationColumn relation)
{
return new JoinQueryImpl<>(JoinType.RIGHT, this, (TableRelationColumnImpl)relation);
}
@Override
public Query join(Table related, Condition on)
{
return new JoinQueryImpl<>(JoinType.INNER, this, (TableImpl)related, on);
}
@Override
public Query leftJoin(Table related, Condition on)
{
return new JoinQueryImpl<>(JoinType.LEFT, this, (TableImpl)related, on);
}
@Override
public Query rightJoin(Table related, Condition on)
{
return new JoinQueryImpl<>(JoinType.RIGHT, this, (TableImpl)related, on);
}
protected abstract Map, Object> getSets();
protected abstract int getPage();
protected abstract int getPageSize();
protected abstract TableImpl getTable();
protected abstract TableImpl> getBaseTable();
protected abstract EntityContextImpl getCtx();
protected abstract SelectBuilder createQuery(String fields, List parameters);
protected abstract Condition getCondition();
protected abstract OrderBy[] getOrderBy();
}