1 // Copyright 2011 Leo Przybylski. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without modification, are
4 // permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice, this list of
7 // conditions and the following disclaimer.
8 //
9 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
10 // of conditions and the following disclaimer in the documentation and/or other materials
11 // provided with the distribution.
12 //
13 // THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
14 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
16 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
17 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
21 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 //
23 // The views and conclusions contained in the software and documentation are those of the
24 // authors and should not be interpreted as representing official policies, either expressed
25 // or implied, of Leo Przybylski.
26 package liquibase.sqlgenerator.ext;
27
28 import liquibase.database.Database;
29 import liquibase.database.core.MySQLDatabase;
30 import liquibase.exception.ValidationErrors;
31 import liquibase.sql.Sql;
32 import liquibase.sql.UnparsedSql;
33 import liquibase.sqlgenerator.SqlGenerator;
34 import liquibase.sqlgenerator.SqlGeneratorChain;
35 import liquibase.sqlgenerator.core.AbstractSqlGenerator;
36 import liquibase.statement.ext.DescribeSequenceStatement;
37
38 import java.util.List;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41
42 import static liquibase.ext.Constants.EXTENSION_PRIORITY;
43
44 /**
45 * Getting the current sequence value
46 *
47 * @author Leo Przybylski (leo [at] rsmart.com)
48 */
49 public class DescribeSequenceGeneratorMySql extends AbstractSqlGenerator<DescribeSequenceStatement> {
50
51 @Override
52 public int getPriority() {
53 return EXTENSION_PRIORITY;
54 }
55
56 @Override
57 public boolean supports(final DescribeSequenceStatement statement, final Database database) {
58 return database instanceof MySQLDatabase;
59 }
60
61 @Override
62 public ValidationErrors validate(DescribeSequenceStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
63 return new ValidationErrors();
64 }
65
66 @Override
67 public Sql[] generateSql(DescribeSequenceStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
68 List<Sql> list = new ArrayList<Sql>();
69 list.add(new UnparsedSql("select max(id) as \"MAX\" from " + statement.getSequenceName()));
70 list.addAll(Arrays.asList(sqlGeneratorChain.generateSql(statement, database)));
71
72 return list.toArray(new Sql[list.size()]);
73
74 }
75 }