org.eclipse.jnosql.diana.column.query.InsertQueryParser Maven / Gradle / Ivy
/*
*
* Copyright (c) 2017 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*
*/
package org.eclipse.jnosql.diana.column.query;
import jakarta.nosql.Params;
import jakarta.nosql.QueryException;
import jakarta.nosql.ServiceLoaderProvider;
import jakarta.nosql.column.ColumnEntity;
import jakarta.nosql.column.ColumnFamilyManager;
import jakarta.nosql.column.ColumnFamilyManagerAsync;
import jakarta.nosql.column.ColumnObserverParser;
import jakarta.nosql.column.ColumnPreparedStatement;
import jakarta.nosql.column.ColumnPreparedStatementAsync;
import jakarta.nosql.query.Condition;
import jakarta.nosql.query.InsertQuery;
import jakarta.nosql.query.InsertQuery.InsertQueryProvider;
import jakarta.nosql.query.JSONQueryValue;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;
final class InsertQueryParser extends ConditionQueryParser {
private final InsertQueryProvider insertQueryProvider;
InsertQueryParser() {
this.insertQueryProvider = ServiceLoaderProvider.get(InsertQueryProvider.class);
}
Stream query(String query, ColumnFamilyManager manager, ColumnObserverParser observer) {
InsertQuery insertQuery = insertQueryProvider.apply(query);
String columnFamily = insertQuery.getEntity();
Params params = Params.newParams();
ColumnEntity entity = getEntity(insertQuery, columnFamily, params, observer);
Optional ttl = insertQuery.getTtl();
if (params.isNotEmpty()) {
throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
}
if (ttl.isPresent()) {
return Stream.of(manager.insert(entity, ttl.get()));
} else {
return Stream.of(manager.insert(entity));
}
}
void queryAsync(String query, ColumnFamilyManagerAsync manager,
Consumer> callBack,
ColumnObserverParser observer) {
InsertQuery insertQuery = insertQueryProvider.apply(query);
String columnFamily = observer.fireEntity(insertQuery.getEntity());
Params params = Params.newParams();
ColumnEntity entity = getEntity(insertQuery, columnFamily, params, observer);
Optional ttl = insertQuery.getTtl();
if (params.isNotEmpty()) {
throw new QueryException("To run a query with a parameter use a PrepareStatement instead.");
}
if (ttl.isPresent()) {
manager.insert(entity, ttl.get(), c -> callBack.accept(Stream.of(c)));
} else {
manager.insert(entity, c -> callBack.accept(Stream.of(c)));
}
}
ColumnPreparedStatement prepare(String query, ColumnFamilyManager manager,
ColumnObserverParser observer) {
InsertQuery insertQuery = insertQueryProvider.apply(query);
String columnFamily = observer.fireEntity(insertQuery.getEntity());
Params params = Params.newParams();
Optional ttl = insertQuery.getTtl();
ColumnEntity entity = getEntity(insertQuery, columnFamily, params, observer);
return DefaultColumnPreparedStatement.insert(entity, params, query, ttl.orElse(null), manager);
}
ColumnPreparedStatementAsync prepareAsync(String query, ColumnFamilyManagerAsync manager,
ColumnObserverParser observer) {
Params params = Params.newParams();
InsertQuery insertQuery = insertQueryProvider.apply(query);
String columnFamily = observer.fireEntity(insertQuery.getEntity());
Optional ttl = insertQuery.getTtl();
ColumnEntity entity = getEntity(insertQuery, columnFamily, params, observer);
return DefaultColumnPreparedStatementAsync.insert(entity, params, query, ttl.orElse(null), manager);
}
private ColumnEntity getEntity(InsertQuery insertQuery, String columnFamily, Params params,
ColumnObserverParser observer) {
return getEntity(new InsertQueryConditionSupplier(insertQuery), columnFamily, params, observer);
}
private static final class InsertQueryConditionSupplier implements ConditionQuerySupplier {
private final InsertQuery query;
private InsertQueryConditionSupplier(InsertQuery query) {
this.query = query;
}
@Override
public List getConditions() {
return query.getConditions();
}
@Override
public Optional getValue() {
return query.getValue();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy