All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fluentjdbc.DatabaseTableWithTimestamps Maven / Gradle / Ivy

There is a newer version: 0.5.3
Show newest version
package org.fluentjdbc;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;
import java.time.Instant;

/**
 * {@link DatabaseTable} which automatically adds created_at and updated_at
 * columns on {@link #insert()} and updated_at on {@link #update()}
 */
@ParametersAreNonnullByDefault
public class DatabaseTableWithTimestamps extends DatabaseTableImpl {

    public DatabaseTableWithTimestamps(String tableName, DatabaseStatementFactory factory) {
        super(tableName, factory);
    }

    public DatabaseTableWithTimestamps(String tableName) {
        super(tableName);
    }

    /**
     * Creates a {@link DatabaseInsertBuilder} object to fluently generate a INSERT ... statement,
     * adding created_at and updated_at to the list of fields
     */
    @Override
    public DatabaseInsertBuilder insert() {
        Instant now = Instant.now();
        return super.insert()
            .setField("updated_at", now)
            .setField("created_at", now);
    }

    /**
     * Creates a {@link DatabaseBulkInsertBuilder} object to fluently generate a INSERT ... statement
     * for a list of objects and automatically add created_at and updated_at values. Example:
     *
     * 
     *     public void saveAll(List<TagType> tagTypes, Connection connection) {
     *         tagTypesTable.bulkInsert(tagTypes)
     *             .setField("name", TagType::getName)
     *             .generatePrimaryKeys("id", TagType::setId)
     *             .execute(connection);
     *     }
     * 
*/ @Override public DatabaseBulkInsertBuilder bulkInsert(@Nonnull Iterable objects) { return super.bulkInsert(objects) .setField("updated_at", t -> Instant.now()) .setField("created_at", t -> Instant.now()); } /** * Creates a {@link DatabaseBulkUpdateBuilder} object to fluently generate a UPDATE ... statement * for a list of objects. Example: * *
     *     public void updateAll(List<TagType> tagTypes, Connection connection) {
     *         tagTypesTable.bulkUpdate(tagTypes)
     *              .where("id", TagType::getId)
     *              .setField("name", TagType::getName)
     *              .execute(connection);
     *     }
     * 
*/ @Override public DatabaseBulkUpdateBuilder bulkUpdate(@Nonnull Iterable objects) { return super.bulkUpdate(objects).setField("updated_at", t -> Instant.now()); } /** * Creates a {@link DatabaseUpdateBuilder} object to fluently generate a UPDATE ... statement, * adding updated_at to the list of fields */ @Override public DatabaseUpdateBuilder update() { return super.update().setField("updated_at", Instant.now()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy