001    /*
002     * The MIT License
003     *
004     * Copyright (c) 2012, Ninja Squad
005     *
006     * Permission is hereby granted, free of charge, to any person obtaining a copy
007     * of this software and associated documentation files (the "Software"), to deal
008     * in the Software without restriction, including without limitation the rights
009     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010     * copies of the Software, and to permit persons to whom the Software is
011     * furnished to do so, subject to the following conditions:
012     *
013     * The above copyright notice and this permission notice shall be included in
014     * all copies or substantial portions of the Software.
015     *
016     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
017     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
018     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
019     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
020     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
021     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
022     * THE SOFTWARE.
023     */
024    
025    package com.ninja_squad.dbsetup.operation;
026    
027    import com.ninja_squad.dbsetup.bind.BinderConfiguration;
028    import com.ninja_squad.dbsetup.util.Preconditions;
029    
030    import javax.annotation.Nonnull;
031    import javax.annotation.concurrent.Immutable;
032    import java.sql.Connection;
033    import java.sql.SQLException;
034    import java.sql.Statement;
035    import java.util.ArrayList;
036    import java.util.Arrays;
037    import java.util.List;
038    
039    /**
040     * An operation which deletes everything from a given database table.
041     * @author JB Nizet
042     */
043    @Immutable
044    public final class DeleteAll implements Operation {
045    
046        private final String table;
047    
048        private DeleteAll(String table) {
049            Preconditions.checkNotNull(table, "table may not be null");
050            this.table = table;
051        }
052    
053        @Override
054        public void execute(Connection connection, BinderConfiguration configuration) throws SQLException {
055            Statement stmt = connection.createStatement();
056            try {
057                stmt.executeUpdate("delete from " + table);
058            }
059            finally {
060                stmt.close();
061            }
062        }
063    
064        /**
065         * Returns an operation which deletes all the rows from the given table.
066         * @param table the table to delete everything from.
067         */
068        public static DeleteAll from(@Nonnull String table) {
069            return new DeleteAll(table);
070        }
071    
072        /**
073         * Returns a composite operation which deletes all the rows from the given tables, in the same order as the
074         * tables. If A has a foreign key to B, which has a foreign key to C, tables should be listed in the following
075         * order: A, B, C. Otherwise, referential constraint will break. If there is a cycle in the dependencies, you might
076         * want to use a sequence of {@link SqlOperation} to disable the foreign key constraints, then delete everything
077         * from the tables, then use another sequence of {@link SqlOperation} to re-enable the foreign key constraints.
078         * @param tables the tables to delete everything from.
079         */
080        public static Operation from(@Nonnull String... tables) {
081            return from(Arrays.asList(tables));
082        }
083    
084        /**
085         * Returns a composite operation which deletes all the rows from the given tables, in the same order as the
086         * tables. If A has a foreign key to B, which has a foreign key to C, tables should be listed in the following
087         * order: A, B, C. Otherwise, referential constraint will break. If there is a cycle in the dependencies, you might
088         * want to use a sequence of {@link SqlOperation} to disable the foreign key constraints, then delete everything
089         * from the tables, then use another sequence of {@link SqlOperation} to re-enable the foreign key constraints.
090         * @param tables the tables to delete everything from.
091         */
092        public static Operation from(@Nonnull List<String> tables) {
093            List<DeleteAll> operations = new ArrayList<DeleteAll>(tables.size());
094            for (String table : tables) {
095                operations.add(new DeleteAll(table));
096            }
097            return CompositeOperation.sequenceOf(operations);
098        }
099    
100        @Override
101        public String toString() {
102            return "delete from " + table;
103        }
104    
105        @Override
106        public int hashCode() {
107            return table.hashCode();
108        }
109    
110        @Override
111        public boolean equals(Object obj) {
112            if (this == obj) {
113                return true;
114            }
115            if (obj == null) {
116                return false;
117            }
118            if (getClass() != obj.getClass()) {
119                return false;
120            }
121            DeleteAll other = (DeleteAll) obj;
122            return this.table.equals(other.table);
123        }
124    }