68
|
1 /*
|
|
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com>
|
|
3
|
|
4 Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 of this software and associated documentation files (the "Software"), to deal
|
|
6 in the Software without restriction, including without limitation the rights
|
|
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 copies of the Software, and to permit persons to whom the Software is
|
|
9 furnished to do so, subject to the following conditions:
|
|
10
|
|
11 The above copyright notice and this permission notice shall be included in
|
|
12 all copies or substantial portions of the Software.
|
|
13
|
|
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 THE SOFTWARE.
|
|
21 */
|
|
22
|
|
23 package fschmidt.db.base;
|
|
24
|
|
25 import java.sql.Connection;
|
|
26 import java.sql.SQLException;
|
|
27 import java.sql.DriverManager;
|
|
28 import java.sql.PreparedStatement;
|
|
29 import java.text.DateFormat;
|
|
30 import java.text.SimpleDateFormat;
|
|
31 import java.util.Properties;
|
|
32 import fschmidt.db.DbObject;
|
|
33 import fschmidt.db.DbKey;
|
|
34 import fschmidt.db.DbKeySetter;
|
|
35 import fschmidt.db.DbObjectFactory;
|
|
36 import fschmidt.db.SQLRuntimeException;
|
|
37 import fschmidt.db.LongKey;
|
|
38 import fschmidt.db.StringKey;
|
|
39 import fschmidt.db.DbStorable;
|
|
40 import fschmidt.db.extend.DbRecordExt;
|
|
41 import fschmidt.db.extend.DbDatabaseExt;
|
|
42 import fschmidt.db.extend.DbTableExt;
|
|
43 import fschmidt.db.extend.DbTransaction;
|
|
44
|
|
45
|
|
46 public abstract class DbDatabaseImpl extends DbDatabaseExt {
|
|
47 private final String url;
|
|
48 private final Properties props = new Properties();
|
|
49
|
|
50 public DbDatabaseImpl(String url,String user,String password) {
|
|
51 this.url = url;
|
|
52 props.setProperty("user",user);
|
|
53 props.setProperty("password",password);
|
|
54 }
|
|
55
|
|
56 public void setProperty(String key,String value) {
|
|
57 props.setProperty(key,value);
|
|
58 }
|
|
59
|
|
60 public Properties getProperties() {
|
|
61 return props;
|
|
62 }
|
|
63
|
|
64 public Connection getConnection()
|
|
65 throws SQLException
|
|
66 {
|
|
67 return DriverManager.getConnection(url,props);
|
|
68 }
|
|
69
|
|
70 public <K extends DbKey,V extends DbObject<K,V>> DbTableExt<K,V> newTable(DbDatabaseExt database,String tableName,DbKeySetter<K> keySetter,DbObjectFactory<K,V> objectFactory) {
|
|
71 return new DbTableImpl<K,V>(database,tableName,keySetter,objectFactory);
|
|
72 }
|
|
73
|
|
74 public <K extends DbKey,V extends DbObject<K,V>> DbRecordExt<K,V> newRecord(DbTableExt<K,V> table,V obj,K key) {
|
|
75 return new DbRecordImpl<K,V>(table,obj,key);
|
|
76 }
|
|
77
|
|
78 public <K extends DbKey,V extends DbObject<K,V>> DbRecordExt<K,V> newRecord(DbTableExt<K,V> table,V obj) {
|
|
79 return new DbRecordImpl<K,V>(table,obj);
|
|
80 }
|
|
81
|
|
82 public boolean isInTransaction() {
|
|
83 throw new UnsupportedOperationException();
|
|
84 }
|
|
85
|
|
86 public void beginTransaction()
|
|
87 throws SQLRuntimeException
|
|
88 {
|
|
89 throw new UnsupportedOperationException();
|
|
90 }
|
|
91
|
|
92 public void commitTransaction()
|
|
93 throws SQLRuntimeException
|
|
94 {
|
|
95 throw new UnsupportedOperationException();
|
|
96 }
|
|
97
|
|
98 public void endTransaction()
|
|
99 throws SQLRuntimeException
|
|
100 {
|
|
101 throw new UnsupportedOperationException();
|
|
102 }
|
|
103
|
|
104 public void runBeforeCommit(Runnable r) {
|
|
105 r.run();
|
|
106 }
|
|
107
|
|
108 public void runAfterCommit(Runnable r) {
|
|
109 r.run();
|
|
110 }
|
|
111
|
|
112 public DbTransaction getTransaction() {
|
|
113 return null;
|
|
114 }
|
|
115
|
|
116 public void ignoreRunnablesInThisTransaction() {
|
|
117 throw new UnsupportedOperationException();
|
|
118 }
|
|
119
|
|
120
|
|
121 public DbKeySetter<LongKey> newIdentityLongKeySetter(String fieldName) {
|
|
122 return new LongKeySetter(fieldName);
|
|
123 }
|
|
124
|
|
125 public DbKeySetter<LongKey> newAssignedLongKeySetter(String fieldName) {
|
|
126 return new LongForeignKeySetter(fieldName);
|
|
127 }
|
|
128
|
|
129 public DbKeySetter<StringKey> newAssignedStringKeySetter(String fieldName) {
|
|
130 return new StringKeySetter(fieldName);
|
|
131 }
|
|
132
|
|
133 public void setValue(PreparedStatement stmt,int idx,Object value)
|
|
134 throws SQLException
|
|
135 {
|
|
136 if( value==null ) {
|
|
137 throw new SQLException("null value");
|
|
138 } else if( value instanceof DbStorable ) {
|
|
139 ((DbStorable)value).setField(stmt,idx);
|
|
140 } else if( value instanceof Character ) {
|
|
141 stmt.setString(idx,value.toString());
|
|
142 } else if( value instanceof java.util.Date ) {
|
|
143 stmt.setTimestamp(idx,new java.sql.Timestamp(((java.util.Date)value).getTime()));
|
|
144 } else if( (new byte[0]).getClass().isInstance(value) ) {
|
|
145 stmt.setBytes(idx,(byte[])value);
|
|
146 } else if( value instanceof Boolean ) {
|
|
147 stmt.setBoolean( idx, ((Boolean)value).booleanValue() );
|
|
148 } else if( value instanceof Float ) {
|
|
149 stmt.setFloat( idx, ((Float)value).floatValue() );
|
|
150 } else {
|
|
151 stmt.setObject(idx,value);
|
|
152 }
|
|
153 }
|
|
154
|
|
155
|
|
156
|
|
157
|
|
158 public String quote(String s) {
|
|
159 return "'" + stringEncode(s) + "'";
|
|
160 }
|
|
161
|
|
162 public String stringEncode(String s) {
|
|
163 StringBuilder buf = new StringBuilder();
|
|
164 int n = s.length();
|
|
165 for( int i=0; i<n; i++ ) {
|
|
166 char c = s.charAt(i);
|
|
167 switch(c) {
|
|
168 case '\n':
|
|
169 buf.append("\\n");
|
|
170 break;
|
|
171 case '\t':
|
|
172 buf.append("\\t");
|
|
173 break;
|
|
174 case '\r':
|
|
175 buf.append("\\r");
|
|
176 break;
|
|
177 case '\'':
|
|
178 buf.append("\\'");
|
|
179 break;
|
|
180 default:
|
|
181 buf.append(c);
|
|
182 }
|
|
183 }
|
|
184 return buf.toString();
|
|
185 }
|
|
186
|
|
187 private static final DateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
188
|
|
189 public String quote(java.util.Date date) {
|
|
190 return "'" + stringEncode(date) + "'";
|
|
191 }
|
|
192
|
|
193 public String stringEncode(java.util.Date date) {
|
|
194 return dateFmt.format(date);
|
|
195 }
|
|
196
|
|
197 }
|