org.fluentjdbc.DatabaseTableImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fluent-jdbc Show documentation
Show all versions of fluent-jdbc Show documentation
A Java library used to execute JDBC statements and build SQL
package org.fluentjdbc;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Provides a starting point for for fluent-jdbc with explicit Connection management.
*
* Example
*
* DatabaseTable table = new DatabaseTableImpl("database_table_test_table");
* Object id = table.insert()
* .setPrimaryKey("id", null)
* .setField("code", 1002)
* .setField("name", "insertTest")
* .execute(connection);
*
* List<Long> result = table.where("name", "insertTest").orderBy("code").listLongs(connection, "code");
* table.where("id", id).setField("name", updatedName).execute(connection);
* table.where("id", id).delete(connection);
*
*/
@ParametersAreNonnullByDefault
public class DatabaseTableImpl implements DatabaseTable {
private final String tableName;
private final DatabaseStatementFactory factory;
public DatabaseTableImpl(@Nonnull String tableName) {
this(tableName, new DatabaseStatementFactory(DatabaseReporter.LOGGING_REPORTER));
}
public DatabaseTableImpl(@Nonnull String tableName, @Nonnull DatabaseStatementFactory factory) {
this.tableName = tableName;
this.factory = factory;
}
/**
* If you haven't called {@link #orderBy}, the results of {@link DatabaseListableQueryBuilder#list}
* will be unpredictable. Call unordered()
if you are okay with this.
*/
@Override
@CheckReturnValue
public DatabaseTableQueryBuilder unordered() {
return new DatabaseTableQueryBuilder(this);
}
/**
* Adds an order by
clause to the query. Needed in order to list results
* in a predictable order.
*/
@Override
@CheckReturnValue
public DatabaseTableQueryBuilder orderBy(String orderByClause) {
return query().orderBy(orderByClause);
}
@Override
@CheckReturnValue
public DatabaseTableAlias alias(String alias) {
return new DatabaseTableAlias(this, alias);
}
@Override
@CheckReturnValue
public String getTableName() {
return tableName;
}
/**
* Creates a {@link DatabaseSaveBuilder} which creates a INSERT
or UPDATE
* statement, depending on whether the row already exists in the database. If idValue is null,
* {@link DatabaseSaveBuilder} will attempt to use the table's autogeneration of primary keys
* if there is no row with matching unique keys
*/
@Override
@CheckReturnValue
public DatabaseSaveBuilder newSaveBuilder(String idField, @Nullable Long id) {
return new DatabaseSaveBuilderWithLong(this, idField, id);
}
/**
* Creates a {@link DatabaseSaveBuilder} which creates a INSERT
or UPDATE
* statement, depending on whether the row already exists in the database. Throws exception if idValue is null
*/
@Override
@CheckReturnValue
public DatabaseSaveBuilder newSaveBuilderWithString(String idField, String id) {
return new DatabaseSaveBuilderWithoutGeneratedKeys<>(this, idField, id);
}
/**
* Use instead of {@link #newSaveBuilder} if the database driver does not
* support RETURN_GENERATED_KEYS
*/
@Override
@CheckReturnValue
public DatabaseSaveBuilder newSaveBuilderNoGeneratedKeys(String idField, @Nullable Long id) {
return new DatabaseSaveBuilderWithoutGeneratedKeys<>(this, idField, id);
}
/**
* Creates a {@link DatabaseSaveBuilder} which creates a INSERT
or UPDATE
* statement, depending on whether the row already exists in the database.
* Generates UUID.randomUUID if idValue is null and row with matching unique keys does not already exist
*/
@Override
@CheckReturnValue
public DatabaseSaveBuilderWithUUID newSaveBuilderWithUUID(String idField, @Nullable UUID id) {
return new DatabaseSaveBuilderWithUUID(this, idField, id);
}
/**
* Creates a query object to be used to add {@link #where(String, Object)} statements and operations
*/
@Override
@CheckReturnValue
public DatabaseTableQueryBuilder query() {
return new DatabaseTableQueryBuilder(this);
}
/**
* Creates a {@link DatabaseInsertBuilder} object to fluently generate a INSERT ...
statement
*/
@Override
@CheckReturnValue
public DatabaseInsertBuilder insert() {
return new DatabaseInsertBuilder(this);
}
/**
* Creates a {@link DatabaseBulkInsertBuilder} object to fluently generate a INSERT ...
statement
* for a list of objects. Example:
*
*
* public void saveAll(List<TagType> tagTypes, Connection connection) {
* tagTypesTable.bulkInsert(tagTypes)
* .setField("name", TagType::getName)
* .generatePrimaryKeys("id", TagType::setId)
* .execute(connection);
* }
*
*/
@Override
@CheckReturnValue
public DatabaseBulkInsertBuilder bulkInsert(Iterable objects) {
return new DatabaseBulkInsertBuilder<>(this, objects);
}
/**
* Creates a {@link DatabaseBulkInsertBuilder} object to fluently generate a INSERT ...
statement
* for a list of objects. Example:
*
*
* public void saveAll(Stream<TagType> tagTypes, Connection connection) {
* tagTypesTable.bulkInsert(tagTypes)
* .setField("name", TagType::getName)
* .generatePrimaryKeys("id", TagType::setId)
* .execute(connection);
* }
*
*/
@Override
@CheckReturnValue
public DatabaseBulkInsertBuilder bulkInsert(Stream objects) {
return bulkInsert(objects.collect(Collectors.toList()));
}
/**
* Creates a {@link DatabaseBulkDeleteBuilder} object to fluently generate a DELETE ...
statement
* for a list of objects. Example:
*
* Example:
*
*
* public void deleteAll(List<TagType> tagTypes, Connection connection) {
* tagTypesTable.bulkDelete(tagTypes)
* .where("id", TagType::getId)
* .execute(connection);
* }
*
*/
@Override
@CheckReturnValue
public DatabaseBulkDeleteBuilder bulkDelete(Iterable objects) {
return new DatabaseBulkDeleteBuilder<>(this, objects);
}
@Override
@CheckReturnValue
public DatabaseBulkUpdateBuilder bulkUpdate(Iterable objects) {
return new DatabaseBulkUpdateBuilder<>(this, objects);
}
/**
* Creates a {@link DatabaseUpdateBuilder} object to fluently generate a UPDATE ...
statement
*/
@Override
@CheckReturnValue
public DatabaseUpdateBuilder update() {
return new DatabaseUpdateBuilder(this);
}
/**
* Executes DELETE FROM tableName WHERE ....
*/
@Override
@CheckReturnValue
public DatabaseDeleteBuilder delete() {
return new DatabaseDeleteBuilder(this);
}
@Override
public DatabaseStatement newStatement(String operation, String sql, List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy