View Javadoc

1   package de.mxro.async.map.sql;
2   
3   /**
4    * <P>
5    * Configuration to connect to interact with SQL database.
6    * 
7    * @author <a href="http://www.mxro.de">Max Rohde</a>
8    *
9    */
10  public abstract class SqlConnectionConfiguration {
11  
12  	public abstract String getConnectionString();
13  
14  	/**
15  	 * The driver to be used. For instance org.apache.derby.jdbc.EmbeddedDriver
16  	 * 
17  	 * @return
18  	 */
19  	public abstract String getDriverClassName();
20  
21  	public abstract String getTableName();
22  
23  	public String getGetTemplate() {
24  		return "SELECT Id, Value FROM " + getTableName() + " WHERE Id = ?";
25  	}
26  
27  	public String getDeleteTemplate() {
28  		return "DELETE FROM " + getTableName() + " WHERE Id = ?";
29  	}
30  
31  	/**
32  	 * <p>
33  	 * Set to true if the byte data to be inserted must be provided twice.
34  	 * </p>
35  	 * <p>
36  	 * <p>
37  	 * Required for instance when insert includes
38  	 * <code>ON DUPLICATE KEY UPDATE `Value` = ?</code> as for mysql.
39  	 * </p>
40  	 * 
41  	 * @return
42  	 */
43  	public abstract boolean supportsInsertOrUpdate();
44  
45  	public String getInsertOrUpdateTemplate() {
46  		return "INSERT INTO `"
47  				+ getTableName()
48  				+ "`(`Id`, `Value`) VALUES (?,?) ON DUPLICATE KEY UPDATE `Value` = ?";
49  	}
50  
51  	/**
52  	 * <p>
53  	 * Set to true if merge statement is supported.
54  	 * </p>
55  	 * <p>
56  	 * This statement also supports to check for existence and/or update in one
57  	 * call.
58  	 * </p>
59  	 * 
60  	 * @see http://www.h2database.com/html/grammar.html#merge
61  	 * 
62  	 * @return
63  	 */
64  	public abstract boolean supportsMerge();
65  
66  	public String getMergeTemplate() {
67  		return "MERGE INTO " + getTableName()
68  				+ " (Id, Value) KEY (Id) VALUES (?, ?)";
69  	}
70  
71  	public String getInsertTemplate() {
72  		return "INSERT INTO " + getTableName() + "(Id, Value) VALUES (?,?)";
73  	}
74  
75  	public String getUpdateTemplate() {
76  		return "UPDATE " + getTableName() + " SET Value = ? WHERE Id = ?";
77  	}
78  
79  	public String getDeleteTableTemplate(final String tableName) {
80  		return "DROP TABLE " + tableName + "";
81  	}
82  
83  	public String getCreateTableTemplate(final String tableName) {
84  		return "CREATE TABLE "
85  				+ tableName
86  				+ " (Id varchar( 512 ) NOT NULL primary key, Value blob NOT NULL)";
87  	}
88  
89  }