Skip to content

Commit

Permalink
Fix bug with wrong enclosing character in SQL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Forslund committed Mar 4, 2016
1 parent e15e446 commit e9db821
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 43 deletions.
33 changes: 19 additions & 14 deletions src/main/java/com/speedment/Manager.java
Expand Up @@ -23,7 +23,9 @@
import com.speedment.encoder.Encoder;
import com.speedment.exception.SpeedmentException;
import com.speedment.field.ComparableField;
import com.speedment.field.trait.ComparableFieldTrait;
import com.speedment.field.trait.FieldTrait;
import com.speedment.field.trait.ReferenceFieldTrait;
import com.speedment.internal.core.runtime.Lifecyclable;
import com.speedment.stream.StreamDecorator;
import com.speedment.util.tuple.Tuple;
Expand Down Expand Up @@ -355,6 +357,23 @@ default Stream<ENTITY> stream() {
* @see Stream
*/
Stream<ENTITY> nativeStream(StreamDecorator decorator);

/**
* Finds and returns an Optional entity where the given field matches the
* given value. If no entity matches, an Optional.empty() is returned. If
* several entities match, then an arbitrary matching entity will be
* returned.
*
* @param <D> the database type
* @param <V> value type
* @param <F> the field type
* @param field to use
* @param value to match with the field
* @return An Optional entity where the given field matches the given value
*/
<D, V extends Comparable<? super V>,
F extends FieldTrait & ReferenceFieldTrait<ENTITY, D, V> & ComparableFieldTrait<ENTITY, D, V>>
Optional<ENTITY> findAny(F field, V value);

// TBI: Shall we expose this method in the API?
// Persistence
Expand Down Expand Up @@ -420,20 +439,6 @@ default Stream<ENTITY> stream() {
*/
ENTITY remove(ENTITY entity) throws SpeedmentException;

/**
* Finds and returns an Optional entity where the given field matches the
* given value. If no entity matches, an Optional.empty() is returned. If
* several entities match, then an arbitrary matching entity will be
* returned.
*
* @param <D> the database type
* @param <V> value type
* @param field to use
* @param value to match with the field
* @return An Optional entity where the given field matches the given value
*/
<D, V extends Comparable<? super V>> Optional<ENTITY> findAny(ComparableField<ENTITY, D, V> field, V value);

ENTITY persist(ENTITY entity, Consumer<MetaResult<ENTITY>> consumer) throws SpeedmentException;

ENTITY update(ENTITY entity, Consumer<MetaResult<ENTITY>> consumer) throws SpeedmentException;
Expand Down
Expand Up @@ -19,7 +19,9 @@
import com.speedment.Manager;
import com.speedment.Speedment;
import com.speedment.encoder.JsonEncoder;
import com.speedment.field.ComparableField;
import com.speedment.field.trait.ComparableFieldTrait;
import com.speedment.field.trait.FieldTrait;
import com.speedment.field.trait.ReferenceFieldTrait;
import com.speedment.internal.core.runtime.Lifecyclable;
import com.speedment.stream.StreamDecorator;
import java.util.stream.Stream;
Expand Down Expand Up @@ -59,33 +61,15 @@ public Stream<ENTITY> stream(StreamDecorator decorator) {
}

@Override
public <D, V extends Comparable<? super V>> Optional<ENTITY> findAny(ComparableField<ENTITY, D, V> field, V value) {
public <D, V extends Comparable<? super V>,
F extends FieldTrait & ReferenceFieldTrait<ENTITY, D, V> & ComparableFieldTrait<ENTITY, D, V>>
Optional<ENTITY> findAny(F field, V value) {

requireNonNull(field);
return speedment.getStreamSupplierComponent()
.findAny(getEntityClass(), field, value);
}

// @Override
// @SuppressWarnings("unchecked")
// public Optional<Object> find(ENTITY entity, Column column) {
// requireNonNull(entity);
// requireNonNull(column);
// return getTable()
// .streamOf(ForeignKey.class)
// .flatMap(fk -> fk.stream().filter(fkc -> fkc.getColumn().equals(column)))
// .map(oFkc -> {
// Table fkTable = oFkc.getForeignTable();
// Column fkColumn = oFkc.getForeignColumn();
//
// @SuppressWarnings("rawtypes")
// final Manager fkManager = speedment.get(ManagerComponent.class).findByTable(fkTable);
//
// Object key = get(entity, column);
//
// // This is an O(n) operation. We must use our short curcuit Fields...
// return fkManager.stream().filter(e -> fkManager.get(e, fkColumn).equals(key)).findAny();
// }).filter(o -> o.isPresent()).map(i -> i.get()).findAny();
// }
@Override
public Manager<ENTITY> initialize() {
state = State.INIITIALIZED;
Expand Down
Expand Up @@ -63,7 +63,7 @@ public SqlPredicateFragment transform(SpeedmentPredicate<?, ?, ?> model) {
protected SqlPredicateFragment render(SpeedmentPredicate<?, ?, ?> model) {
final PredicateType pt = model.getEffectivePredicateType();

final String cn = namingConvention.quoteField(model.getField().getColumnName());
final String cn = namingConvention.encloseField(model.getField().getColumnName());
switch (pt) {
// Constants
case ALWAYS_TRUE:
Expand Down
Expand Up @@ -18,6 +18,9 @@

import com.speedment.Manager;
import com.speedment.field.ComparableField;
import com.speedment.field.trait.ComparableFieldTrait;
import com.speedment.field.trait.FieldTrait;
import com.speedment.field.trait.ReferenceFieldTrait;
import com.speedment.stream.StreamDecorator;
import java.util.Optional;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -106,7 +109,9 @@ public interface MockManager<ENTITY> extends Manager<ENTITY> {
* @param finder the new finder supplier
* @return this instance
*/
public MockManager<ENTITY> setFinder(BiFunction<ComparableField<ENTITY, ?, ? extends Comparable<?>>, Comparable<?>, Optional<ENTITY>> finder);
public <D, V extends Comparable<? super V>,
F extends FieldTrait & ReferenceFieldTrait<ENTITY, D, V> & ComparableFieldTrait<ENTITY, D, V>>
MockManager<ENTITY> setFinder(BiFunction<F, V, Optional<ENTITY>> finder);

static <ENTITY> MockManager<ENTITY> of(Manager<ENTITY> manager) {
return new MockManagerImpl<>(manager);
Expand Down
Expand Up @@ -23,7 +23,9 @@
import com.speedment.db.MetaResult;
import com.speedment.exception.SpeedmentException;
import com.speedment.field.ComparableField;
import com.speedment.field.trait.ComparableFieldTrait;
import com.speedment.field.trait.FieldTrait;
import com.speedment.field.trait.ReferenceFieldTrait;
import com.speedment.stream.StreamDecorator;
import com.speedment.util.tuple.Tuple;
import java.util.Optional;
Expand All @@ -47,7 +49,7 @@ public class MockManagerImpl<ENTITY> implements MockManager<ENTITY> {
private Function<ENTITY, ENTITY> persister;
private Function<ENTITY, ENTITY> updater;
private Function<ENTITY, ENTITY> remover;
private BiFunction<ComparableField<ENTITY, ?, ? extends Comparable<?>>, Comparable<?>, Optional<ENTITY>> finder;
private BiFunction<FieldTrait, Comparable<?>, Optional<ENTITY>> finder;

public MockManagerImpl(Manager<ENTITY> inner) {
this.inner = inner;
Expand Down Expand Up @@ -97,8 +99,12 @@ public MockManager<ENTITY> setRemover(Function<ENTITY, ENTITY> remover) {
}

@Override
public MockManager<ENTITY> setFinder(BiFunction<ComparableField<ENTITY, ?, ? extends Comparable<?>>, Comparable<?>, Optional<ENTITY>> finder) {
this.finder = finder;
public <D, V extends Comparable<? super V>,
F extends FieldTrait & ReferenceFieldTrait<ENTITY, D, V> & ComparableFieldTrait<ENTITY, D, V>>
MockManager<ENTITY> setFinder(BiFunction<F, V, Optional<ENTITY>> finder) {
this.finder = (BiFunction<FieldTrait, Comparable<?>, Optional<ENTITY>>)
(BiFunction<?, ?, ?>) finder;

return this;
}

Expand Down Expand Up @@ -174,7 +180,9 @@ public ENTITY remove(ENTITY entity) throws SpeedmentException {
}

@Override
public <D, V extends Comparable<? super V>> Optional<ENTITY> findAny(ComparableField<ENTITY, D, V> field, V value) {
public <D, V extends Comparable<? super V>,
F extends FieldTrait & ReferenceFieldTrait<ENTITY, D, V> & ComparableFieldTrait<ENTITY, D, V>>
Optional<ENTITY> findAny(F field, V value) {
return finder.apply(field, value);
}

Expand Down

0 comments on commit e9db821

Please sign in to comment.