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;
026
027 import java.util.List;
028
029 import javax.annotation.Nonnull;
030
031 import com.ninja_squad.dbsetup.operation.CompositeOperation;
032 import com.ninja_squad.dbsetup.operation.DeleteAll;
033 import com.ninja_squad.dbsetup.operation.Insert;
034 import com.ninja_squad.dbsetup.operation.Operation;
035 import com.ninja_squad.dbsetup.operation.SqlOperation;
036 import com.ninja_squad.dbsetup.operation.Truncate;
037
038 /**
039 * A static factory class for operations. Static import of this class can help make the code more readable.
040 * @author JB Nizet
041 */
042 public final class Operations {
043 private Operations() {
044 }
045
046 /**
047 * Creates a <code>delete from table</code> operation.
048 * @param table the table to delete all from
049 * @see DeleteAll
050 */
051 public static DeleteAll deleteAllFrom(@Nonnull String table) {
052 return DeleteAll.from(table);
053 }
054
055 /**
056 * Creates a sequence of <code>delete from table</code> operations.
057 * @param tables the tables to delete all from
058 * @see DeleteAll
059 */
060 public static Operation deleteAllFrom(@Nonnull String... tables) {
061 return DeleteAll.from(tables);
062 }
063
064 /**
065 * Creates a sequence of <code>delete from ...</code> operations.
066 * @param tables the tables to delete all from
067 * @see DeleteAll
068 */
069 public static Operation deleteAllFrom(@Nonnull List<String> tables) {
070 return DeleteAll.from(tables);
071 }
072
073 /**
074 * Creates a <code>truncate table ...</code> operation.
075 * @param table the table to truncate
076 * @see Truncate
077 */
078 public static Truncate truncate(@Nonnull String table) {
079 return Truncate.table(table);
080 }
081
082 /**
083 * Creates a sequence of <code>truncate table ...</code> operations.
084 * @param tables the tables to truncate
085 * @see Truncate
086 */
087 public static Operation truncate(@Nonnull String... tables) {
088 return Truncate.tables(tables);
089 }
090
091 /**
092 * Creates a sequence of <code>truncate table ...</code> operations.
093 * @param tables the tables to truncate
094 * @see Truncate
095 */
096 public static Operation truncate(@Nonnull List<String> tables) {
097 return Truncate.tables(tables);
098 }
099
100 /**
101 * Creates a SQL operation.
102 * @param sqlStatement the SQL statement to execute (using {@link java.sql.Statement#executeUpdate(String)})
103 * @see SqlOperation
104 */
105 public static SqlOperation sql(@Nonnull String sqlStatement) {
106 return SqlOperation.of(sqlStatement);
107 }
108
109 /**
110 * Creates a sequence of SQL operations.
111 * @param sqlStatements the SQL statements to execute (using {@link java.sql.Statement#executeUpdate(String)})
112 * @see SqlOperation
113 */
114 public static Operation sql(@Nonnull String... sqlStatements) {
115 return SqlOperation.of(sqlStatements);
116 }
117
118 /**
119 * Creates a sequence of SQL operations.
120 * @param sqlStatements the SQL statements to execute (using {@link java.sql.Statement#executeUpdate(String)})
121 * @see SqlOperation
122 */
123 public static Operation sql(@Nonnull List<String> sqlStatements) {
124 return SqlOperation.of(sqlStatements);
125 }
126
127 /**
128 * Creates a builder for a sequence of insert operations.
129 * @param table the table to insert into
130 * @see Insert
131 */
132 public static Insert.Builder insertInto(@Nonnull String table) {
133 return Insert.into(table);
134 }
135
136 /**
137 * Creates a sequence of operations.
138 * @param operations the operations to put in a sequence
139 * @see CompositeOperation
140 */
141 public static Operation sequenceOf(@Nonnull Operation... operations) {
142 return CompositeOperation.sequenceOf(operations);
143 }
144
145 /**
146 * Creates a sequence of operations.
147 * @param operations the operations to put in a sequence
148 * @see CompositeOperation
149 */
150 public static Operation sequenceOf(@Nonnull List<? extends Operation> operations) {
151 return CompositeOperation.sequenceOf(operations);
152 }
153 }