1 21 22 package org.opensubsystems.blog.persist.db.hsqldb; 23 24 import java.sql.Connection ; 25 import java.sql.SQLException ; 26 import java.sql.Statement ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 30 import org.opensubsystems.blog.data.Blog; 31 import org.opensubsystems.blog.data.Entry; 32 import org.opensubsystems.blog.persist.db.BlogDatabaseSchema; 33 import org.opensubsystems.core.error.OSSException; 34 import org.opensubsystems.core.persist.db.DatabaseImpl; 35 import org.opensubsystems.core.util.DatabaseUtils; 36 import org.opensubsystems.core.util.Log; 37 38 47 public class HsqlDBBlogDatabaseSchema extends BlogDatabaseSchema 48 { 49 89 90 92 95 public static final int BLOG_COMMENTS_MAXLENGTH_HSQLDB = 32768; 96 97 100 public static final int BLOGENTRY_COMMENTS_MAXLENGTH_HSQLDB = 32768; 101 102 104 107 private static Logger s_logger = Log.getInstance(HsqlDBBlogDatabaseSchema.class); 108 109 111 114 static 115 { 116 Blog.setCommentsMaxLength(BLOG_COMMENTS_MAXLENGTH_HSQLDB); 118 Entry.setCommentsMaxLength(BLOGENTRY_COMMENTS_MAXLENGTH_HSQLDB); 119 } 120 121 126 public HsqlDBBlogDatabaseSchema( 127 ) throws OSSException 128 { 129 super(); 130 } 131 132 134 137 public void create( 138 Connection cntDBConnection, 139 String strUserName 140 ) throws SQLException 141 { 142 Statement stmQuery = null; 143 try 144 { 145 stmQuery = cntDBConnection.createStatement(); 146 if (stmQuery.execute( 147 "create cached table BF_BLOG" + NL + 148 "(" + NL + 149 " ID INTEGER IDENTITY," + NL + 150 " DOMAIN_ID INTEGER NOT NULL," + NL + 151 " FOLDER VARCHAR(" + BLOG_FOLDER_MAXLENGTH + ") NOT NULL," + NL + 152 " CAPTION VARCHAR(" + BLOG_CAPTION_MAXLENGTH + ") NOT NULL," + NL + 153 " COMMENTS VARCHAR(" + BLOG_COMMENTS_MAXLENGTH_HSQLDB + ") NOT NULL," + NL + 154 " CREATION_DATE TIMESTAMP NOT NULL," + NL + 155 " MODIFICATION_DATE TIMESTAMP NOT NULL" + NL + 156 ")")) 164 { 165 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 167 } 168 s_logger.log(Level.FINEST, "Table BF_BLOG created."); 169 if (stmQuery.execute("create unique index BF_BLOG_FLDR_UQ on BF_BLOG(FOLDER)")) 170 { 171 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 173 } 174 s_logger.log(Level.FINEST, "Index BF_BLOG_FLDR_UQ created."); 175 if (stmQuery.execute("grant all on BF_BLOG to " + strUserName)) 176 { 177 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 179 } 180 s_logger.log(Level.FINEST, 181 "Access for table BF_BLOG set for user " + strUserName); 182 183 185 if (stmQuery.execute( 186 "create cached table BF_BLOG_ENTRY" + NL + 187 "(" + NL + 188 " ID INTEGER IDENTITY," + NL + 189 " DOMAIN_ID INTEGER NOT NULL," + NL + 190 " BLOG_ID INTEGER NOT NULL," + NL + 191 " CAPTION VARCHAR(" + BLOGENTRY_CAPTION_MAXLENGTH + ") NOT NULL," + NL + 192 " COMMENTS VARCHAR(" + BLOGENTRY_COMMENTS_MAXLENGTH_HSQLDB + ") NOT NULL," + NL + 193 " IMAGEURL VARCHAR(" + BLOGENTRY_IMAGEURL_MAXLENGTH + ") NOT NULL," + NL + 194 " TARGETURL VARCHAR(" + BLOGENTRY_TARGETURL_MAXLENGTH + ") NOT NULL," + NL + 195 " CREATION_DATE TIMESTAMP NOT NULL," + NL + 196 " MODIFICATION_DATE TIMESTAMP NOT NULL," + NL + 197 " CONSTRAINT BF_BLOGENTR_FK FOREIGN KEY (BLOG_ID) " 200 + "REFERENCES BF_BLOG (ID) ON DELETE CASCADE" + NL + 201 ")")) 202 { 203 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 205 } 206 s_logger.log(Level.FINEST, "Table BF_BLOG_ENTRY created."); 207 if (stmQuery.execute("grant all on BF_BLOG_ENTRY to " + strUserName)) 208 { 209 stmQuery.getMoreResults(Statement.CLOSE_ALL_RESULTS); 211 } 212 s_logger.log(Level.FINEST, 213 "Access for table BF_BLOG_ENTRY set for user " + strUserName); 214 } 215 catch (SQLException sqleExc) 216 { 217 s_logger.log(Level.WARNING, 218 "Failed to create schema " + BLOG_SCHEMA_NAME, sqleExc); 219 throw sqleExc; 220 } 221 finally 222 { 223 DatabaseUtils.closeStatement(stmQuery); 224 } 225 } 226 227 230 public String getInsertBlogAndFetchGeneratedValues( 231 ) throws OSSException 232 { 233 return getInsertBlog(); 234 } 235 236 239 public String getUpdateBlogAndFetchGeneratedValues( 240 ) throws OSSException 241 { 242 StringBuffer buffer = new StringBuffer (); 243 244 buffer.append("update BF_BLOG set FOLDER = ?, CAPTION = ?, COMMENTS = ?," + 245 " MODIFICATION_DATE = "); 246 buffer.append(DatabaseImpl.getInstance().getCurrentTimestampFunctionCall()); 247 buffer.append(" where ID = ? and DOMAIN_ID = ? and MODIFICATION_DATE = ?"); 248 249 return buffer.toString(); 250 } 251 252 255 public String getInsertBlog( 256 ) throws OSSException 257 { 258 StringBuffer buffer = new StringBuffer (); 259 260 buffer.append("INSERT INTO BF_BLOG("); 262 buffer.append(BLOG_COLUMNS); 263 buffer.append(") VALUES (null, ?, ?, ?, ?, "); 264 buffer.append(DatabaseImpl.getInstance().getCurrentTimestampFunctionCall()); 265 buffer.append(", "); 266 buffer.append(DatabaseImpl.getInstance().getCurrentTimestampFunctionCall()); 267 buffer.append(")"); 268 269 return buffer.toString(); 270 } 271 272 275 public String getInsertEntry( 276 ) throws OSSException 277 { 278 StringBuffer buffer = new StringBuffer (); 279 280 buffer.append("INSERT INTO BF_BLOG_ENTRY("); 282 buffer.append(ENTRY_COLUMNS); 283 buffer.append(") VALUES (null, ?, ?, ?, ?, ?, ?, "); 284 buffer.append(DatabaseImpl.getInstance().getCurrentTimestampFunctionCall()); 285 buffer.append(", "); 286 buffer.append(DatabaseImpl.getInstance().getCurrentTimestampFunctionCall()); 287 buffer.append(")"); 288 289 return buffer.toString(); 290 } 291 292 295 public String getInsertEntryAndFetchGeneratedValues( 296 ) throws OSSException 297 { 298 return getInsertEntry(); 299 } 300 301 304 public String getUpdateEntryAndFetchGeneratedValues( 305 ) throws OSSException 306 { 307 StringBuffer buffer = new StringBuffer (); 308 309 buffer.append("update BF_BLOG_ENTRY set CAPTION = ?, COMMENTS = ?," + 310 " IMAGEURL = ?, TARGETURL = ?, MODIFICATION_DATE = "); 311 buffer.append(DatabaseImpl.getInstance().getCurrentTimestampFunctionCall()); 312 buffer.append(" where ID = ? and DOMAIN_ID = ? and MODIFICATION_DATE = ?"); 313 314 return buffer.toString(); 315 } 316 } 317 | Popular Tags |