View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.impl.config.ds.hsql;
17  
18  import java.sql.PreparedStatement;
19  import java.sql.SQLException;
20  
21  import de.smartics.properties.spi.config.ds.DataSourceProxy;
22  
23  /**
24   * Data source proxy to create HSQL data sources.
25   */
26  public abstract class AbstractHSqlDataSourceProxy implements DataSourceProxy
27  {
28    // ********************************* Fields *********************************
29  
30    // --- constants ------------------------------------------------------------
31  
32    /**
33     * The class version identifier.
34     */
35    private static final long serialVersionUID = 1L;
36  
37    // --- members --------------------------------------------------------------
38  
39    // ****************************** Initializer *******************************
40  
41    // ****************************** Constructors ******************************
42  
43    /**
44     * Default constructor.
45     */
46    protected AbstractHSqlDataSourceProxy()
47    {
48    }
49  
50    // ****************************** Inner Classes *****************************
51  
52    // ********************************* Methods ********************************
53  
54    // --- init -----------------------------------------------------------------
55  
56    // --- factory --------------------------------------------------------------
57  
58    // --- get&set --------------------------------------------------------------
59  
60    // --- business -------------------------------------------------------------
61  
62    @Override
63    public final String getCreateTableSqlTemplate()
64    {
65      return "CREATE TABLE ${table} (${configColumn} VARCHAR(128) NOT NULL,"
66             + " ${nameColumn} VARCHAR(64) NOT NULL, ${valueColumn} VARCHAR(255),"
67             + " CONSTRAINT prime UNIQUE (${configColumn}, ${nameColumn}))";
68    }
69  
70    @Override
71    public final String getInsertOrUpdateSqlTemplate()
72    {
73      return "MERGE INTO ${table} USING (VALUES ?, ?, ?) N (c,n,v)"
74             + " ON ${table}.${configColumn}=N.c AND ${table}.${nameColumn}=N.n"
75             + " WHEN MATCHED THEN UPDATE SET ${valueColumn} = ?"
76             + " WHEN NOT MATCHED THEN INSERT (${configColumn}, ${nameColumn}, ${valueColumn}) VALUES (?, ?, ?)";
77    }
78  
79    @Override
80    public final void setInsertOrUpdate(final PreparedStatement statement,
81        final String config, final String name, final String value)
82      throws SQLException
83    {
84      statement.setString(1, config);
85      statement.setString(2, name);
86      statement.setString(3, value);
87      statement.setString(4, value);
88      statement.setString(5, config);
89      statement.setString(6, name);
90      statement.setString(7, value);
91    }
92  
93    // --- object basics --------------------------------------------------------
94  
95  }