<commit_before>/*
 * 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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.bridje.orm.Column;
import org.bridje.orm.Condition;
import org.bridje.orm.OrderBy;
import org.bridje.orm.Query;
import org.bridje.orm.TableColumn;
import org.bridje.orm.impl.sql.DeleteBuilder;
import org.bridje.orm.impl.sql.InsertBuilder;
import org.bridje.orm.impl.sql.SelectBuilder;
import org.bridje.orm.impl.sql.UpdateBuilder;

class QueryImpl<T> extends AbstractQuery<T> implements Query<T>
{
    private final TableImpl<T> table;

    private final EntityContextImpl ctx;

    private Condition condition;

    private OrderBy[] orderBy;

    private int page;

    private int pageSize;

    private Map<TableColumn<?,?>, Object> sets;

    public QueryImpl(EntityContextImpl ctx, TableImpl<T> table)
    {
        this.table = table;
        this.ctx = ctx;
    }

    @Override
    public Query<T> paging(int page, int size)
    {
        this.page = page;
        this.pageSize = size;
        return this;
    }

    @Override
    public Query<T> where(Condition condition)
    {
        this.condition = condition;
        return this;
    }

    @Override
    public Query<T> orderBy(OrderBy... statements)
    {
        orderBy = statements;
        return this;
    }

    @Override
    protected SelectBuilder createQuery(String fields, List<Object> parameters)
    {
        SelectBuilder qb = new SelectBuilder(ctx.getDialect());
        qb.select(fields)
            .from(ctx.getDialect().identifier(table.getName()));
        if(condition != null)
        {
            qb.where(condition.writeSQL(parameters, ctx));
        }
        if(orderBy != null)
        {
            qb.orderBy(Arrays
                .stream(orderBy)
                .map((ob) -> table.buildOrderBy(ob, parameters, ctx) )
                .collect(Collectors.joining(", ")));
        }
        return qb;
    }

    @Override
    public Condition getCondition()
    {
        return condition;
    }

    @Override
    public OrderBy[] getOrderBy()
    {
        return orderBy;
    }

    @Override
    public int getPage()
    {
        return page;
    }

    @Override
    public int getPageSize()
    {
        return pageSize;
    }

    @Override
    protected TableImpl<T> getTable()
    {
        return table;
    }

    @Override
    protected EntityContextImpl getCtx()
    {
        return ctx;
    }

    @Override
    protected TableImpl<?> getBaseTable()
    {
        return table;
    }

    @Override
    public int delete() throws SQLException
    {
        List<Object> parameters = new ArrayList<>();
        DeleteBuilder qb = new DeleteBuilder();
        qb.delete(ctx.getDialect().identifier(table.getName()));
        if(condition != null)
        {
            qb.where(condition.writeSQL(parameters, ctx));
        }
        return ctx.doUpdate(qb.toString(), parameters.toArray());
    }

    @Override
    public T updateOne() throws SQLException
    {
        List<Object> parameters = new ArrayList<>();
        UpdateBuilder qb = new UpdateBuilder(ctx.getDialect());
        qb.update(ctx.getDialect().identifier(table.getName()));
        getSets().forEach((column, value) ->
        {
            if(value instanceof Column)
            {
                qb.set(column.writeSQL(parameters, ctx),((Column)value).writeSQL(parameters, ctx));
            }
            else
            {
                qb.set(column.writeSQL(parameters, ctx));
                parameters.add(value);
            }
        });
        if(condition != null) qb.where(condition.writeSQL(parameters, ctx));<commit_msg>Fix in the ORM updateOne method.<commit_after>/*
 * 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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.bridje.orm.Column;
import org.bridje.orm.Condition;
import org.bridje.orm.OrderBy;
import org.bridje.orm.Query;
import org.bridje.orm.TableColumn;
import org.bridje.orm.impl.sql.DeleteBuilder;
import org.bridje.orm.impl.sql.InsertBuilder;
import org.bridje.orm.impl.sql.SelectBuilder;
import org.bridje.orm.impl.sql.UpdateBuilder;

class QueryImpl<T> extends AbstractQuery<T> implements Query<T>
{
    private final TableImpl<T> table;

    private final EntityContextImpl ctx;

    private Condition condition;

    private OrderBy[] orderBy;

    private int page;

    private int pageSize;

    private Map<TableColumn<?,?>, Object> sets;

    public QueryImpl(EntityContextImpl ctx, TableImpl<T> table)
    {
        this.table = table;
        this.ctx = ctx;
    }

    @Override
    public Query<T> paging(int page, int size)
    {
        this.page = page;
        this.pageSize = size;
        return this;
    }

    @Override
    public Query<T> where(Condition condition)
    {
        this.condition = condition;
        return this;
    }

    @Override
    public Query<T> orderBy(OrderBy... statements)
    {
        orderBy = statements;
        return this;
    }

    @Override
    protected SelectBuilder createQuery(String fields, List<Object> parameters)
    {
        SelectBuilder qb = new SelectBuilder(ctx.getDialect());
        qb.select(fields)
            .from(ctx.getDialect().identifier(table.getName()));
        if(condition != null)
        {
            qb.where(condition.writeSQL(parameters, ctx));
        }
        if(orderBy != null)
        {
            qb.orderBy(Arrays
                .stream(orderBy)
                .map((ob) -> table.buildOrderBy(ob, parameters, ctx) )
                .collect(Collectors.joining(", ")));
        }
        return qb;
    }

    @Override
    public Condition getCondition()
    {
        return condition;
    }

    @Override
    public OrderBy[] getOrderBy()
    {
        return orderBy;
    }

    @Override
    public int getPage()
    {
        return page;
    }

    @Override
    public int getPageSize()
    {
        return pageSize;
    }

    @Override
    protected TableImpl<T> getTable()
    {
        return table;
    }

    @Override
    protected EntityContextImpl getCtx()
    {
        return ctx;
    }

    @Override
    protected TableImpl<?> getBaseTable()
    {
        return table;
    }

    @Override
    public int delete() throws SQLException
    {
        List<Object> parameters = new ArrayList<>();
        DeleteBuilder qb = new DeleteBuilder();
        qb.delete(ctx.getDialect().identifier(table.getName()));
        if(condition != null) qb.where(condition.writeSQL(parameters, ctx));
        return ctx.doUpdate(qb.toString(), parameters.toArray());
    }

    @Override
    public T updateOne() throws SQLException
    {
        List<Object> parameters = new ArrayList<>();
        UpdateBuilder qb = new UpdateBuilder(ctx.getDialect());
        qb.update(ctx.getDialect().identifier(table.getName()));
        getSets().forEach((column, value) ->
        {
            if(value instanceof Column)
            {
                qb.set(column.writeSQL(parameters, ctx),((Column)value).writeSQL(parameters, ctx));
            }
            else
            {
                qb.set(column.writeSQL(parameters, ctx));
                parameters.add(value);
            }
        });
        if(condition != null) qb.where(condition.writeSQL(parameters, ctx));
{'fraction_non_alphanumeric': 0.10722050577869321, 'fraction_numerical': 0.001830873097608422, 'mean_word_length': 2.3628318584070795, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6538784', 'n_tokens_mistral': 2508, 'n_tokens_neox': 2453, 'n_words': 624}