001/*
002 * Copyright 2002-2018 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 Sybase table
023 * with the equivalent of an auto-increment column. Note: If you use this class, your table key
024 * column should <i>NOT</i> be defined as an IDENTITY column, as the sequence table does the job.
025 *
026 * <p>This class is intended to be used with Sybase Anywhere.
027 *
028 * <p>The sequence is kept in a table. There should be one sequence table per
029 * table that needs an auto-generated key.
030 *
031 * <p>Example:
032 *
033 * <pre class="code">create table tab (id int not null primary key, text varchar(100))
034 * create table tab_sequence (id bigint identity)
035 * insert into tab_sequence values(DEFAULT)</pre>
036 *
037 * If "cacheSize" is set, the intermediate values are served without querying the
038 * database. If the server or your application is stopped or crashes or a transaction
039 * is rolled back, the unused values will never be served. The maximum hole size in
040 * numbering is consequently the value of cacheSize.
041 *
042 * <b>HINT:</b> Since Sybase Anywhere supports the JDBC 3.0 {@code getGeneratedKeys}
043 * method, it is recommended to use IDENTITY columns directly in the tables and then
044 * using a {@link org.springframework.jdbc.core.simple.SimpleJdbcInsert} or utilizing
045 * a {@link org.springframework.jdbc.support.KeyHolder} when calling the with the
046 * {@code update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder)}
047 * method of the {@link org.springframework.jdbc.core.JdbcTemplate}.
048 *
049 * <p>Thanks to Tarald Saxi Stormark for the suggestion!
050 *
051 * @author Thomas Risberg
052 * @since 3.0.5
053 */
054public class SybaseAnywhereMaxValueIncrementer extends SybaseMaxValueIncrementer {
055
056        /**
057         * Default constructor for bean property style usage.
058         * @see #setDataSource
059         * @see #setIncrementerName
060         * @see #setColumnName
061         */
062        public SybaseAnywhereMaxValueIncrementer() {
063        }
064
065        /**
066         * Convenience constructor.
067         * @param dataSource the DataSource to use
068         * @param incrementerName the name of the sequence/table to use
069         * @param columnName the name of the column in the sequence table to use
070         */
071        public SybaseAnywhereMaxValueIncrementer(DataSource dataSource, String incrementerName, String columnName) {
072                super(dataSource, incrementerName, columnName);
073        }
074
075
076        @Override
077        protected String getIncrementStatement() {
078                return "insert into " + getIncrementerName() + " values(DEFAULT)";
079        }
080
081}