001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jdbc.support.incrementer;
018
019import javax.sql.DataSource;
020
021/**
022 * {@link DataFieldMaxValueIncrementer} that increments the maximum value of a given HSQL table
023 * with the equivalent of an auto-increment column. Note: If you use this class, your HSQL
024 * key column should <i>NOT</i> be auto-increment, as the sequence table does the job.
025 *
026 * <p>The sequence is kept in a table. There should be one sequence table per
027 * table that needs an auto-generated key.
028 *
029 * <p>Example:
030 *
031 * <pre class="code">create table tab (id int not null primary key, text varchar(100));
032 * create table tab_sequence (value identity);
033 * insert into tab_sequence values(0);</pre>
034 *
035 * If "cacheSize" is set, the intermediate values are served without querying the
036 * database. If the server or your application is stopped or crashes or a transaction
037 * is rolled back, the unused values will never be served. The maximum hole size in
038 * numbering is consequently the value of cacheSize.
039 *
040 * <p><b>NOTE:</b> HSQL now supports sequences and you should consider using them instead:
041 * {@link HsqlSequenceMaxValueIncrementer}
042 *
043 * @author Jean-Pierre Pawlak
044 * @author Thomas Risberg
045 * @author Juergen Hoeller
046 * @see HsqlSequenceMaxValueIncrementer
047 */
048public class HsqlMaxValueIncrementer extends AbstractIdentityColumnMaxValueIncrementer {
049
050        /**
051         * Default constructor for bean property style usage.
052         * @see #setDataSource
053         * @see #setIncrementerName
054         * @see #setColumnName
055         */
056        public HsqlMaxValueIncrementer() {
057        }
058
059        /**
060         * Convenience constructor.
061         * @param dataSource the DataSource to use
062         * @param incrementerName the name of the sequence/table to use
063         * @param columnName the name of the column in the sequence table to use
064         */
065        public HsqlMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) {
066                super(dataSource, incrementerName, columnName);
067        }
068
069
070        @Override
071        protected String getIncrementStatement() {
072                return "insert into " + getIncrementerName() + " values(null)";
073        }
074
075        @Override
076        protected String getIdentityStatement() {
077                return "select max(identity()) from " + getIncrementerName();
078        }
079
080}