001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 Echo Three, LLC 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// http://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// Generated File -- DO NOT EDIT BY HAND 017// -------------------------------------------------------------------------------- 018 019/** 020 * CachedSearchFactory.java 021 */ 022 023package com.echothree.model.data.search.server.factory; 024 025import com.echothree.model.data.index.common.pk.IndexPK; 026import com.echothree.model.data.search.common.pk.SearchDefaultOperatorPK; 027import com.echothree.model.data.search.common.pk.SearchSortOrderPK; 028import com.echothree.model.data.search.common.pk.SearchSortDirectionPK; 029 030import com.echothree.model.data.index.server.entity.Index; 031import com.echothree.model.data.search.server.entity.SearchDefaultOperator; 032import com.echothree.model.data.search.server.entity.SearchSortOrder; 033import com.echothree.model.data.search.server.entity.SearchSortDirection; 034 035import com.echothree.model.data.search.common.CachedSearchConstants; 036import com.echothree.model.data.search.common.pk.CachedSearchPK; 037import com.echothree.model.data.search.server.value.CachedSearchValue; 038import com.echothree.model.data.search.server.entity.CachedSearch; 039import com.echothree.util.common.exception.PersistenceDatabaseException; 040import com.echothree.util.common.exception.PersistenceDatabaseUpdateException; 041import com.echothree.util.common.exception.PersistenceNotNullException; 042import com.echothree.util.server.persistence.BaseFactory; 043import com.echothree.util.server.persistence.EntityIdGenerator; 044import com.echothree.util.server.persistence.EntityPermission; 045import com.echothree.util.server.persistence.PersistenceDebugFlags; 046import com.echothree.util.server.persistence.Session; 047import com.echothree.util.server.persistence.ThreadSession; 048import java.sql.Clob; 049import java.sql.PreparedStatement; 050import java.sql.ResultSet; 051import java.sql.SQLException; 052import java.sql.Types; 053import java.io.ByteArrayInputStream; 054import java.io.StringReader; 055import java.util.ArrayList; 056import java.util.Collection; 057import java.util.HashSet; 058import java.util.List; 059import java.util.Map; 060import java.util.Set; 061import org.apache.commons.logging.Log; 062import org.apache.commons.logging.LogFactory; 063 064public class CachedSearchFactory 065 implements BaseFactory<CachedSearchPK, CachedSearch> { 066 067 //final private static Log log = LogFactory.getLog(CachedSearchFactory.class); 068 069 final private static String SQL_SELECT_READ_ONLY = "SELECT csrch_cachedsearchid, csrch_idx_indexid, csrch_querysha1hash, csrch_query, csrch_parsedquerysha1hash, csrch_parsedquery, csrch_srchdefop_searchdefaultoperatorid, csrch_srchsrtord_searchsortorderid, csrch_srchsrtdir_searchsortdirectionid, csrch_fromtime, csrch_thrutime FROM cachedsearches WHERE csrch_cachedsearchid = ?"; 070 final private static String SQL_SELECT_READ_WRITE = "SELECT csrch_cachedsearchid, csrch_idx_indexid, csrch_querysha1hash, csrch_query, csrch_parsedquerysha1hash, csrch_parsedquery, csrch_srchdefop_searchdefaultoperatorid, csrch_srchsrtord_searchsortorderid, csrch_srchsrtdir_searchsortdirectionid, csrch_fromtime, csrch_thrutime FROM cachedsearches WHERE csrch_cachedsearchid = ? FOR UPDATE"; 071 final private static String SQL_INSERT = "INSERT INTO cachedsearches (csrch_cachedsearchid, csrch_idx_indexid, csrch_querysha1hash, csrch_query, csrch_parsedquerysha1hash, csrch_parsedquery, csrch_srchdefop_searchdefaultoperatorid, csrch_srchsrtord_searchsortorderid, csrch_srchsrtdir_searchsortdirectionid, csrch_fromtime, csrch_thrutime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 072 final private static String SQL_UPDATE = "UPDATE cachedsearches SET csrch_idx_indexid = ?, csrch_querysha1hash = ?, csrch_query = ?, csrch_parsedquerysha1hash = ?, csrch_parsedquery = ?, csrch_srchdefop_searchdefaultoperatorid = ?, csrch_srchsrtord_searchsortorderid = ?, csrch_srchsrtdir_searchsortdirectionid = ?, csrch_fromtime = ?, csrch_thrutime = ? WHERE csrch_cachedsearchid = ?"; 073 final private static String SQL_DELETE = "DELETE FROM cachedsearches WHERE csrch_cachedsearchid = ?"; 074 final private static String SQL_VALID = "SELECT COUNT(*) FROM cachedsearches WHERE csrch_cachedsearchid = ?"; 075 076 final private static String PK_COLUMN = "csrch_cachedsearchid"; 077 final private static String ALL_COLUMNS = "csrch_cachedsearchid, csrch_idx_indexid, csrch_querysha1hash, csrch_query, csrch_parsedquerysha1hash, csrch_parsedquery, csrch_srchdefop_searchdefaultoperatorid, csrch_srchsrtord_searchsortorderid, csrch_srchsrtdir_searchsortdirectionid, csrch_fromtime, csrch_thrutime"; 078 final public static String TABLE_NAME = "cachedsearches"; 079 080 final public static String CSRCH_CACHEDSEARCHID = "csrch_cachedsearchid"; 081 final public static String CSRCH_IDX_INDEXID = "csrch_idx_indexid"; 082 final public static String CSRCH_QUERYSHA1HASH = "csrch_querysha1hash"; 083 final public static String CSRCH_QUERY = "csrch_query"; 084 final public static String CSRCH_PARSEDQUERYSHA1HASH = "csrch_parsedquerysha1hash"; 085 final public static String CSRCH_PARSEDQUERY = "csrch_parsedquery"; 086 final public static String CSRCH_SRCHDEFOP_SEARCHDEFAULTOPERATORID = "csrch_srchdefop_searchdefaultoperatorid"; 087 final public static String CSRCH_SRCHSRTORD_SEARCHSORTORDERID = "csrch_srchsrtord_searchsortorderid"; 088 final public static String CSRCH_SRCHSRTDIR_SEARCHSORTDIRECTIONID = "csrch_srchsrtdir_searchsortdirectionid"; 089 final public static String CSRCH_FROMTIME = "csrch_fromtime"; 090 final public static String CSRCH_THRUTIME = "csrch_thrutime"; 091 092 final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(CachedSearchConstants.COMPONENT_VENDOR_NAME, CachedSearchConstants.ENTITY_TYPE_NAME); 093 094 /** Creates a new instance of CachedSearchFactory */ 095 private CachedSearchFactory() { 096 super(); 097 } 098 099 private static class CachedSearchFactoryHolder { 100 static CachedSearchFactory instance = new CachedSearchFactory(); 101 } 102 103 public static CachedSearchFactory getInstance() { 104 return CachedSearchFactoryHolder.instance; 105 } 106 107 @Override 108 public String getPKColumn() { 109 return PK_COLUMN; 110 } 111 112 @Override 113 public String getAllColumns() { 114 return ALL_COLUMNS; 115 } 116 117 @Override 118 public String getTableName() { 119 return TABLE_NAME; 120 } 121 122 @Override 123 public String getComponentVendorName() { 124 return CachedSearchConstants.COMPONENT_VENDOR_NAME; 125 } 126 127 @Override 128 public String getEntityTypeName() { 129 return CachedSearchConstants.ENTITY_TYPE_NAME; 130 } 131 132 public PreparedStatement prepareStatement(String query) { 133 return ThreadSession.currentSession().prepareStatement(CachedSearchFactory.class, query); 134 } 135 136 public CachedSearchPK getNextPK() { 137 return new CachedSearchPK(entityIdGenerator.getNextEntityId()); 138 } 139 140 public Set<CachedSearchPK> getPKsFromResultSetAsSet(ResultSet rs) 141 throws PersistenceDatabaseException { 142 Set<CachedSearchPK> _result = new HashSet<>(); 143 144 try { 145 while(rs.next()) { 146 _result.add(getPKFromResultSet(rs)); 147 } 148 } catch (SQLException se) { 149 throw new PersistenceDatabaseException(se); 150 } 151 152 return _result; 153 } 154 155 public java.util.List<CachedSearchPK> getPKsFromResultSetAsList(ResultSet rs) 156 throws PersistenceDatabaseException { 157 java.util.List<CachedSearchPK> _result = new ArrayList<>(); 158 159 try { 160 while(rs.next()) { 161 _result.add(getPKFromResultSet(rs)); 162 } 163 } catch (SQLException se) { 164 throw new PersistenceDatabaseException(se); 165 } 166 167 return _result; 168 } 169 170 public CachedSearchPK getPKFromResultSet(ResultSet rs) 171 throws PersistenceDatabaseException { 172 CachedSearchPK _result; 173 174 try { 175 long csrch_cachedsearchid = rs.getLong(CSRCH_CACHEDSEARCHID); 176 Long _entityId = rs.wasNull() ? null : csrch_cachedsearchid; 177 178 _result = new CachedSearchPK(_entityId); 179 } catch (SQLException se) { 180 throw new PersistenceDatabaseException(se); 181 } 182 183 return _result; 184 } 185 186 public java.util.List<CachedSearchValue> getValuesFromPKs(Session session, Collection<CachedSearchPK> pks) 187 throws PersistenceDatabaseException { 188 java.util.List<CachedSearchValue> _values = new ArrayList<>(pks.size()); 189 190 for(CachedSearchPK _pk: pks) { 191 _values.add(getValueFromPK(session, _pk)); 192 } 193 194 return _values; 195 } 196 197 public CachedSearchValue getValueFromPK(Session session, CachedSearchPK pk) 198 throws PersistenceDatabaseException { 199 CachedSearchValue _value; 200 201 // See if we already have the entity in the session cache 202 CachedSearch _entity = (CachedSearch)session.getEntity(pk); 203 if(_entity == null) 204 _value = getEntityFromPK(session, EntityPermission.READ_ONLY, pk).getCachedSearchValue(); 205 else 206 _value = _entity.getCachedSearchValue(); 207 208 return _value; 209 } 210 211 public java.util.List<CachedSearchValue> getValuesFromResultSet(Session session, ResultSet rs) 212 throws PersistenceDatabaseException { 213 java.util.List<CachedSearchValue> _result = new ArrayList<>(); 214 215 try { 216 while(rs.next()) { 217 _result.add(getValueFromResultSet(session, rs)); 218 } 219 } catch (SQLException se) { 220 throw new PersistenceDatabaseException(se); 221 } 222 223 return _result; 224 } 225 226 public CachedSearchValue getValueFromResultSet(Session session, ResultSet rs) 227 throws PersistenceDatabaseException { 228 CachedSearchValue _value; 229 230 try { 231 Long csrch_cachedsearchid = rs.getLong(CSRCH_CACHEDSEARCHID); 232 CachedSearchPK _pk = new CachedSearchPK(csrch_cachedsearchid); 233 234 // See if we already have the entity in the session cache 235 CachedSearch _entity = (CachedSearch)session.getEntity(_pk); 236 237 if(_entity == null) { 238 Long csrch_idx_indexid = rs.getLong(CSRCH_IDX_INDEXID); 239 if(rs.wasNull()) 240 csrch_idx_indexid = null; 241 242 String csrch_querysha1hash = rs.getString(CSRCH_QUERYSHA1HASH); 243 if(rs.wasNull()) 244 csrch_querysha1hash = null; 245 246 Clob csrch_query = rs.getClob(CSRCH_QUERY); 247 if(rs.wasNull()) 248 csrch_query = null; 249 250 String csrch_parsedquerysha1hash = rs.getString(CSRCH_PARSEDQUERYSHA1HASH); 251 if(rs.wasNull()) 252 csrch_parsedquerysha1hash = null; 253 254 Clob csrch_parsedquery = rs.getClob(CSRCH_PARSEDQUERY); 255 if(rs.wasNull()) 256 csrch_parsedquery = null; 257 258 Long csrch_srchdefop_searchdefaultoperatorid = rs.getLong(CSRCH_SRCHDEFOP_SEARCHDEFAULTOPERATORID); 259 if(rs.wasNull()) 260 csrch_srchdefop_searchdefaultoperatorid = null; 261 262 Long csrch_srchsrtord_searchsortorderid = rs.getLong(CSRCH_SRCHSRTORD_SEARCHSORTORDERID); 263 if(rs.wasNull()) 264 csrch_srchsrtord_searchsortorderid = null; 265 266 Long csrch_srchsrtdir_searchsortdirectionid = rs.getLong(CSRCH_SRCHSRTDIR_SEARCHSORTDIRECTIONID); 267 if(rs.wasNull()) 268 csrch_srchsrtdir_searchsortdirectionid = null; 269 270 Long csrch_fromtime = rs.getLong(CSRCH_FROMTIME); 271 if(rs.wasNull()) 272 csrch_fromtime = null; 273 274 Long csrch_thrutime = rs.getLong(CSRCH_THRUTIME); 275 if(rs.wasNull()) 276 csrch_thrutime = null; 277 278 _value = new CachedSearchValue(_pk, new IndexPK(csrch_idx_indexid), csrch_querysha1hash, csrch_query == null? null: csrch_query.getSubString(1L, (int)csrch_query.length()), csrch_parsedquerysha1hash, csrch_parsedquery == null? null: csrch_parsedquery.getSubString(1L, (int)csrch_parsedquery.length()), new SearchDefaultOperatorPK(csrch_srchdefop_searchdefaultoperatorid), new SearchSortOrderPK(csrch_srchsrtord_searchsortorderid), new SearchSortDirectionPK(csrch_srchsrtdir_searchsortdirectionid), csrch_fromtime, csrch_thrutime); 279 } else 280 _value = _entity.getCachedSearchValue(); 281 } catch (SQLException se) { 282 throw new PersistenceDatabaseException(se); 283 } 284 285 return _value; 286 } 287 288 public java.util.List<CachedSearch> getEntitiesFromPKs(EntityPermission entityPermission, Collection<CachedSearchPK> pks) 289 throws PersistenceDatabaseException { 290 return getEntitiesFromPKs(ThreadSession.currentSession(), entityPermission, pks); 291 } 292 293 public java.util.List<CachedSearch> getEntitiesFromPKs(Session session, EntityPermission entityPermission, Collection<CachedSearchPK> pks) 294 throws PersistenceDatabaseException { 295 java.util.List<CachedSearch> _entities = new ArrayList<>(pks.size()); 296 297 for(CachedSearchPK _pk: pks) { 298 _entities.add(getEntityFromPK(session, entityPermission, _pk)); 299 } 300 301 return _entities; 302 } 303 304 public CachedSearch getEntityFromValue(EntityPermission entityPermission, CachedSearchValue value) { 305 return getEntityFromPK(ThreadSession.currentSession(), entityPermission, value.getPrimaryKey()); 306 } 307 308 public CachedSearch getEntityFromValue(Session session, EntityPermission entityPermission, CachedSearchValue value) { 309 return getEntityFromPK(session, entityPermission, value.getPrimaryKey()); 310 } 311 312 public CachedSearch getEntityFromPK(EntityPermission entityPermission, CachedSearchPK pk) 313 throws PersistenceDatabaseException { 314 return getEntityFromPK(ThreadSession.currentSession(), entityPermission, pk); 315 } 316 317 public CachedSearch getEntityFromCache(Session session, CachedSearchPK pk) { 318 CachedSearchValue _value = (CachedSearchValue)session.getValueCache().get(pk); 319 320 return _value == null ? null : new CachedSearch(_value, EntityPermission.READ_ONLY); 321 } 322 323 public CachedSearch getEntityFromPK(Session session, EntityPermission entityPermission, CachedSearchPK pk) 324 throws PersistenceDatabaseException { 325 CachedSearch _entity; 326 327 // See if we already have the entity in the session cache 328 _entity = (CachedSearch)session.getEntity(pk); 329 if(_entity != null) { 330 // If the requested permission is READ_WRITE, and the cached permission is 331 // READ_ONLY, then pretend that the cached object wasn't found, and create 332 // a new entity that is READ_WRITE. 333 if(entityPermission.equals(EntityPermission.READ_WRITE)) { 334 if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY)) 335 _entity = null; 336 } 337 } 338 339 if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) { 340 _entity = getEntityFromCache(session, pk); 341 } 342 343 if(_entity == null) { 344 PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE); 345 long _entityId = pk.getEntityId(); 346 ResultSet _rs = null; 347 348 try { 349 _ps.setLong(1, _entityId); 350 _rs = _ps.executeQuery(); 351 if(_rs.next()) { 352 _entity = getEntityFromResultSet(session, entityPermission, _rs); 353 } 354 } catch (SQLException se) { 355 throw new PersistenceDatabaseException(se); 356 } finally { 357 if(_rs != null) { 358 try { 359 _rs.close(); 360 } catch (SQLException se) { 361 // do nothing 362 } 363 } 364 } 365 } 366 367 return _entity; 368 } 369 370 public Set<CachedSearchPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params) 371 throws PersistenceDatabaseException { 372 Set<CachedSearchPK> _pks; 373 ResultSet _rs = null; 374 375 try { 376 if(params.length != 0) { 377 Session.setQueryParams(ps, params); 378 } 379 380 _rs = ps.executeQuery(); 381 _pks = getPKsFromResultSetAsSet(_rs); 382 _rs.close(); 383 } catch (SQLException se) { 384 throw new PersistenceDatabaseException(se); 385 } finally { 386 if(_rs != null) { 387 try { 388 _rs.close(); 389 } catch (SQLException se) { 390 // do nothing 391 } 392 } 393 } 394 395 return _pks; 396 } 397 398 public java.util.List<CachedSearchPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params) 399 throws PersistenceDatabaseException { 400 java.util.List<CachedSearchPK> _pks; 401 ResultSet _rs = null; 402 403 try { 404 if(params.length != 0) { 405 Session.setQueryParams(ps, params); 406 } 407 408 _rs = ps.executeQuery(); 409 _pks = getPKsFromResultSetAsList(_rs); 410 _rs.close(); 411 } catch (SQLException se) { 412 throw new PersistenceDatabaseException(se); 413 } finally { 414 if(_rs != null) { 415 try { 416 _rs.close(); 417 } catch (SQLException se) { 418 // do nothing 419 } 420 } 421 } 422 423 return _pks; 424 } 425 426 public CachedSearchPK getPKFromQuery(PreparedStatement ps, final Object... params) 427 throws PersistenceDatabaseException { 428 CachedSearchPK _pk = null; 429 ResultSet _rs = null; 430 431 try { 432 if(params.length != 0) { 433 Session.setQueryParams(ps, params); 434 } 435 436 _rs = ps.executeQuery(); 437 if(_rs.next()) { 438 _pk = getPKFromResultSet(_rs); 439 } 440 _rs.close(); 441 } catch (SQLException se) { 442 throw new PersistenceDatabaseException(se); 443 } finally { 444 if(_rs != null) { 445 try { 446 _rs.close(); 447 } catch (SQLException se) { 448 // do nothing 449 } 450 } 451 } 452 453 return _pk; 454 } 455 456 public java.util.List<CachedSearch> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 457 throws PersistenceDatabaseException { 458 PreparedStatement ps = session.prepareStatement(CachedSearchFactory.class, queryMap.get(entityPermission)); 459 460 return getEntitiesFromQuery(session, entityPermission, ps, params); 461 } 462 463 public java.util.List<CachedSearch> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 464 throws PersistenceDatabaseException { 465 Session session = ThreadSession.currentSession(); 466 PreparedStatement ps = session.prepareStatement(CachedSearchFactory.class, queryMap.get(entityPermission)); 467 468 return getEntitiesFromQuery(session, entityPermission, ps, params); 469 } 470 471 public java.util.List<CachedSearch> getEntitiesFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 472 throws PersistenceDatabaseException { 473 PreparedStatement ps = session.prepareStatement(CachedSearchFactory.class, queryMap.get(entityPermission)); 474 475 return getEntitiesFromQuery(session, entityPermission, ps); 476 } 477 478 public java.util.List<CachedSearch> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 479 throws PersistenceDatabaseException { 480 Session session = ThreadSession.currentSession(); 481 PreparedStatement ps = session.prepareStatement(CachedSearchFactory.class, queryMap.get(entityPermission)); 482 483 return getEntitiesFromQuery(session, entityPermission, ps); 484 } 485 486 public java.util.List<CachedSearch> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps) 487 throws PersistenceDatabaseException { 488 return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps); 489 } 490 491 public java.util.List<CachedSearch> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params) 492 throws PersistenceDatabaseException { 493 return getEntitiesFromQuery(ThreadSession.currentSession(), entityPermission, ps, params); 494 } 495 496 public java.util.List<CachedSearch> getEntitiesFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params) 497 throws PersistenceDatabaseException { 498 java.util.List<CachedSearch> _entities; 499 ResultSet _rs = null; 500 501 try { 502 if(params.length != 0) { 503 Session.setQueryParams(ps, params); 504 } 505 506 _rs = ps.executeQuery(); 507 _entities = getEntitiesFromResultSet(session, entityPermission, _rs); 508 _rs.close(); 509 } catch (SQLException se) { 510 throw new PersistenceDatabaseException(se); 511 } finally { 512 if(_rs != null) { 513 try { 514 _rs.close(); 515 } catch (SQLException se) { 516 // do nothing 517 } 518 } 519 } 520 521 return _entities; 522 } 523 524 public CachedSearch getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 525 throws PersistenceDatabaseException { 526 PreparedStatement ps = session.prepareStatement(CachedSearchFactory.class, queryMap.get(entityPermission)); 527 528 return getEntityFromQuery(session, entityPermission, ps, params); 529 } 530 531 public CachedSearch getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 532 throws PersistenceDatabaseException { 533 Session session = ThreadSession.currentSession(); 534 PreparedStatement ps = session.prepareStatement(CachedSearchFactory.class, queryMap.get(entityPermission)); 535 536 return getEntityFromQuery(session, entityPermission, ps, params); 537 } 538 539 public CachedSearch getEntityFromQuery(Session session, EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 540 throws PersistenceDatabaseException { 541 PreparedStatement ps = session.prepareStatement(CachedSearchFactory.class, queryMap.get(entityPermission)); 542 543 return getEntityFromQuery(session, entityPermission, ps); 544 } 545 546 public CachedSearch getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 547 throws PersistenceDatabaseException { 548 Session session = ThreadSession.currentSession(); 549 PreparedStatement ps = session.prepareStatement(CachedSearchFactory.class, queryMap.get(entityPermission)); 550 551 return getEntityFromQuery(session, entityPermission, ps); 552 } 553 554 public CachedSearch getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps) 555 throws PersistenceDatabaseException { 556 return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps); 557 } 558 559 public CachedSearch getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params) 560 throws PersistenceDatabaseException { 561 return getEntityFromQuery(ThreadSession.currentSession(), entityPermission, ps, params); 562 } 563 564 public CachedSearch getEntityFromQuery(Session session, EntityPermission entityPermission, PreparedStatement ps, final Object... params) 565 throws PersistenceDatabaseException { 566 CachedSearch _entity = null; 567 ResultSet _rs = null; 568 569 try { 570 if(params.length != 0) { 571 Session.setQueryParams(ps, params); 572 } 573 574 _rs = ps.executeQuery(); 575 if(_rs.next()) { 576 _entity = getEntityFromResultSet(session, entityPermission, _rs); 577 } 578 _rs.close(); 579 } catch (SQLException se) { 580 throw new PersistenceDatabaseException(se); 581 } finally { 582 if(_rs != null) { 583 try { 584 _rs.close(); 585 } catch (SQLException se) { 586 // do nothing 587 } 588 } 589 } 590 591 return _entity; 592 } 593 594 public java.util.List<CachedSearch> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs) 595 throws PersistenceDatabaseException { 596 return getEntitiesFromResultSet(ThreadSession.currentSession(), entityPermission, rs); 597 } 598 599 public java.util.List<CachedSearch> getEntitiesFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs) 600 throws PersistenceDatabaseException { 601 java.util.List<CachedSearch> _result = new ArrayList<>(); 602 603 try { 604 while(rs.next()) { 605 _result.add(getEntityFromResultSet(session, entityPermission, rs)); 606 } 607 } catch (SQLException se) { 608 throw new PersistenceDatabaseException(se); 609 } 610 611 return _result; 612 } 613 614 public CachedSearch getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs) 615 throws PersistenceDatabaseException { 616 return getEntityFromResultSet(ThreadSession.currentSession(), entityPermission, rs); 617 } 618 619 public CachedSearch getEntityFromResultSet(Session session, EntityPermission entityPermission, ResultSet rs) 620 throws PersistenceDatabaseException { 621 CachedSearch _entity; 622 623 try { 624 Long csrch_cachedsearchid = rs.getLong(CSRCH_CACHEDSEARCHID); 625 CachedSearchPK _pk = new CachedSearchPK(csrch_cachedsearchid); 626 627 // See if we already have the entity in the session cache 628 _entity = (CachedSearch)session.getEntity(_pk); 629 if(_entity != null) { 630 // If the requested permission is READ_WRITE, and the cached permission is 631 // READ_ONLY, then pretend that the cached object wasn't found, and create 632 // a new entity that is READ_WRITE. 633 if(entityPermission.equals(EntityPermission.READ_WRITE)) { 634 if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY)) 635 _entity = null; 636 } 637 } 638 boolean foundInSessionCache = _entity != null; 639 640 if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) { 641 _entity = getEntityFromCache(session, _pk); 642 } 643 644 if(_entity == null) { 645 Long csrch_idx_indexid = rs.getLong(CSRCH_IDX_INDEXID); 646 if(rs.wasNull()) 647 csrch_idx_indexid = null; 648 649 String csrch_querysha1hash = rs.getString(CSRCH_QUERYSHA1HASH); 650 if(rs.wasNull()) 651 csrch_querysha1hash = null; 652 653 Clob csrch_query = rs.getClob(CSRCH_QUERY); 654 if(rs.wasNull()) 655 csrch_query = null; 656 657 String csrch_parsedquerysha1hash = rs.getString(CSRCH_PARSEDQUERYSHA1HASH); 658 if(rs.wasNull()) 659 csrch_parsedquerysha1hash = null; 660 661 Clob csrch_parsedquery = rs.getClob(CSRCH_PARSEDQUERY); 662 if(rs.wasNull()) 663 csrch_parsedquery = null; 664 665 Long csrch_srchdefop_searchdefaultoperatorid = rs.getLong(CSRCH_SRCHDEFOP_SEARCHDEFAULTOPERATORID); 666 if(rs.wasNull()) 667 csrch_srchdefop_searchdefaultoperatorid = null; 668 669 Long csrch_srchsrtord_searchsortorderid = rs.getLong(CSRCH_SRCHSRTORD_SEARCHSORTORDERID); 670 if(rs.wasNull()) 671 csrch_srchsrtord_searchsortorderid = null; 672 673 Long csrch_srchsrtdir_searchsortdirectionid = rs.getLong(CSRCH_SRCHSRTDIR_SEARCHSORTDIRECTIONID); 674 if(rs.wasNull()) 675 csrch_srchsrtdir_searchsortdirectionid = null; 676 677 Long csrch_fromtime = rs.getLong(CSRCH_FROMTIME); 678 if(rs.wasNull()) 679 csrch_fromtime = null; 680 681 Long csrch_thrutime = rs.getLong(CSRCH_THRUTIME); 682 if(rs.wasNull()) 683 csrch_thrutime = null; 684 685 CachedSearchValue _value = new CachedSearchValue(_pk, csrch_idx_indexid == null? null: new IndexPK(csrch_idx_indexid), csrch_querysha1hash, csrch_query == null? null: csrch_query.getSubString(1L, (int)csrch_query.length()), csrch_parsedquerysha1hash, csrch_parsedquery == null? null: csrch_parsedquery.getSubString(1L, (int)csrch_parsedquery.length()), csrch_srchdefop_searchdefaultoperatorid == null? null: new SearchDefaultOperatorPK(csrch_srchdefop_searchdefaultoperatorid), csrch_srchsrtord_searchsortorderid == null? null: new SearchSortOrderPK(csrch_srchsrtord_searchsortorderid), csrch_srchsrtdir_searchsortdirectionid == null? null: new SearchSortDirectionPK(csrch_srchsrtdir_searchsortdirectionid), csrch_fromtime, csrch_thrutime); 686 _entity = new CachedSearch(_value, entityPermission); 687 } 688 689 if(!foundInSessionCache) { 690 if(entityPermission.equals(EntityPermission.READ_ONLY)) { 691 session.putReadOnlyEntity(_pk, _entity); 692 session.getValueCache().put(_entity.getCachedSearchValue()); 693 } else { 694 session.putReadWriteEntity(_pk, _entity); 695 } 696 } 697 } catch (SQLException se) { 698 throw new PersistenceDatabaseException(se); 699 } 700 701 return _entity; 702 } 703 704 public CachedSearch create(Session session, Index index, String querySha1Hash, String query, String parsedQuerySha1Hash, String parsedQuery, SearchDefaultOperator searchDefaultOperator, SearchSortOrder searchSortOrder, SearchSortDirection searchSortDirection, Long fromTime, Long thruTime) 705 throws PersistenceDatabaseException, PersistenceNotNullException { 706 return create(session, index == null ? null : index.getPrimaryKey(), querySha1Hash, query, parsedQuerySha1Hash, parsedQuery, searchDefaultOperator == null ? null : searchDefaultOperator.getPrimaryKey(), searchSortOrder == null ? null : searchSortOrder.getPrimaryKey(), searchSortDirection == null ? null : searchSortDirection.getPrimaryKey(), fromTime, thruTime); 707 } 708 709 public CachedSearch create(Index index, String querySha1Hash, String query, String parsedQuerySha1Hash, String parsedQuery, SearchDefaultOperator searchDefaultOperator, SearchSortOrder searchSortOrder, SearchSortDirection searchSortDirection, Long fromTime, Long thruTime) 710 throws PersistenceDatabaseException, PersistenceNotNullException { 711 return create(ThreadSession.currentSession(), index == null ? null : index.getPrimaryKey(), querySha1Hash, query, parsedQuerySha1Hash, parsedQuery, searchDefaultOperator == null ? null : searchDefaultOperator.getPrimaryKey(), searchSortOrder == null ? null : searchSortOrder.getPrimaryKey(), searchSortDirection == null ? null : searchSortDirection.getPrimaryKey(), fromTime, thruTime); 712 } 713 714 private void bindForCreate(PreparedStatement _ps, CachedSearchValue _value) 715 throws SQLException { 716 _ps.setLong(1, _value.getEntityId()); 717 718 IndexPK csrch_idx_indexid = _value.getIndexPK(); 719 if(csrch_idx_indexid == null) 720 _ps.setNull(2, Types.BIGINT); 721 else 722 _ps.setLong(2, csrch_idx_indexid.getEntityId()); 723 724 String csrch_querysha1hash = _value.getQuerySha1Hash(); 725 if(csrch_querysha1hash == null) 726 _ps.setNull(3, Types.VARCHAR); 727 else 728 _ps.setString(3, csrch_querysha1hash); 729 730 String csrch_query = _value.getQuery(); 731 if(csrch_query == null) 732 _ps.setNull(4, Types.CLOB); 733 else 734 _ps.setCharacterStream(4, new StringReader(csrch_query), csrch_query.length()); 735 736 String csrch_parsedquerysha1hash = _value.getParsedQuerySha1Hash(); 737 if(csrch_parsedquerysha1hash == null) 738 _ps.setNull(5, Types.VARCHAR); 739 else 740 _ps.setString(5, csrch_parsedquerysha1hash); 741 742 String csrch_parsedquery = _value.getParsedQuery(); 743 if(csrch_parsedquery == null) 744 _ps.setNull(6, Types.CLOB); 745 else 746 _ps.setCharacterStream(6, new StringReader(csrch_parsedquery), csrch_parsedquery.length()); 747 748 SearchDefaultOperatorPK csrch_srchdefop_searchdefaultoperatorid = _value.getSearchDefaultOperatorPK(); 749 if(csrch_srchdefop_searchdefaultoperatorid == null) 750 _ps.setNull(7, Types.BIGINT); 751 else 752 _ps.setLong(7, csrch_srchdefop_searchdefaultoperatorid.getEntityId()); 753 754 SearchSortOrderPK csrch_srchsrtord_searchsortorderid = _value.getSearchSortOrderPK(); 755 if(csrch_srchsrtord_searchsortorderid == null) 756 _ps.setNull(8, Types.BIGINT); 757 else 758 _ps.setLong(8, csrch_srchsrtord_searchsortorderid.getEntityId()); 759 760 SearchSortDirectionPK csrch_srchsrtdir_searchsortdirectionid = _value.getSearchSortDirectionPK(); 761 if(csrch_srchsrtdir_searchsortdirectionid == null) 762 _ps.setNull(9, Types.BIGINT); 763 else 764 _ps.setLong(9, csrch_srchsrtdir_searchsortdirectionid.getEntityId()); 765 766 Long csrch_fromtime = _value.getFromTime(); 767 if(csrch_fromtime == null) 768 _ps.setNull(10, Types.BIGINT); 769 else 770 _ps.setLong(10, csrch_fromtime); 771 772 Long csrch_thrutime = _value.getThruTime(); 773 if(csrch_thrutime == null) 774 _ps.setNull(11, Types.BIGINT); 775 else 776 _ps.setLong(11, csrch_thrutime); 777 778 } 779 780 public CachedSearch create(Session session, IndexPK indexPK, String querySha1Hash, String query, String parsedQuerySha1Hash, String parsedQuery, SearchDefaultOperatorPK searchDefaultOperatorPK, SearchSortOrderPK searchSortOrderPK, SearchSortDirectionPK searchSortDirectionPK, Long fromTime, Long thruTime) 781 throws PersistenceDatabaseException, PersistenceNotNullException { 782 CachedSearchPK _pk = getNextPK(); 783 CachedSearchValue _value = new CachedSearchValue(_pk, indexPK, querySha1Hash, query, parsedQuerySha1Hash, parsedQuery, searchDefaultOperatorPK, searchSortOrderPK, searchSortDirectionPK, fromTime, thruTime); 784 785 PreparedStatement _ps = session.prepareStatement(SQL_INSERT); 786 787 try { 788 bindForCreate(_ps, _value); 789 790 if(PersistenceDebugFlags.CheckEntityInsertRowCount) { 791 int _count = _ps.executeUpdate(); 792 793 if(_count != 1) { 794 throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count); 795 } 796 } else { 797 _ps.executeUpdate(); 798 } 799 800 session.getValueCache().put(_value); 801 } catch (SQLException se) { 802 throw new PersistenceDatabaseException(se); 803 } 804 805 CachedSearch _entity = new CachedSearch(_value, EntityPermission.READ_ONLY); 806 session.putReadOnlyEntity(_pk, _entity); 807 808 return _entity; 809 } 810 811 public CachedSearch create(IndexPK indexPK, String querySha1Hash, String query, String parsedQuerySha1Hash, String parsedQuery, SearchDefaultOperatorPK searchDefaultOperatorPK, SearchSortOrderPK searchSortOrderPK, SearchSortDirectionPK searchSortDirectionPK, Long fromTime, Long thruTime) 812 throws PersistenceDatabaseException, PersistenceNotNullException { 813 return create(ThreadSession.currentSession(), indexPK, querySha1Hash, query, parsedQuerySha1Hash, parsedQuery, searchDefaultOperatorPK, searchSortOrderPK, searchSortDirectionPK, fromTime, thruTime); 814 } 815 816 public void create(Session session, Collection<CachedSearchValue> _values) 817 throws PersistenceDatabaseException, PersistenceNotNullException { 818 int _size = _values.size(); 819 820 if(_size > 0) { 821 PreparedStatement _ps = session.prepareStatement(SQL_INSERT); 822 List<CachedSearchValue> _cacheValues = new ArrayList<>(_size); 823 824 try { 825 for(CachedSearchValue _value : _values) { 826 _value.setEntityId(entityIdGenerator.getNextEntityId()); 827 bindForCreate(_ps, _value); 828 829 _ps.addBatch(); 830 831 _cacheValues.add(_value); 832 } 833 834 if(PersistenceDebugFlags.CheckEntityInsertRowCount) { 835 int[] _counts = _ps.executeBatch(); 836 837 for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) { 838 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 839 throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 840 } 841 } 842 } else { 843 _ps.executeBatch(); 844 } 845 846 _ps.clearBatch(); 847 } catch (SQLException se) { 848 throw new PersistenceDatabaseException(se); 849 } 850 851 _cacheValues.forEach((_cacheValue) -> { 852 CachedSearch _cacheEntity = new CachedSearch(_cacheValue, EntityPermission.READ_ONLY); 853 854 session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity); 855 }); 856 } 857 } 858 859 public void create(Collection<CachedSearchValue> _values) 860 throws PersistenceDatabaseException, PersistenceNotNullException { 861 create(ThreadSession.currentSession(), _values); 862 } 863 864 private boolean bindForStore(PreparedStatement _ps, CachedSearchValue _value) 865 throws SQLException { 866 boolean _hasBeenModified = _value.hasBeenModified(); 867 868 if(_hasBeenModified) { 869 IndexPK csrch_idx_indexid = _value.getIndexPK(); 870 if(csrch_idx_indexid == null) 871 _ps.setNull(1, Types.BIGINT); 872 else 873 _ps.setLong(1, csrch_idx_indexid.getEntityId()); 874 875 String csrch_querysha1hash = _value.getQuerySha1Hash(); 876 if(csrch_querysha1hash == null) 877 _ps.setNull(2, Types.VARCHAR); 878 else 879 _ps.setString(2, csrch_querysha1hash); 880 881 String csrch_query = _value.getQuery(); 882 if(csrch_query == null) 883 _ps.setNull(3, Types.CLOB); 884 else 885 _ps.setCharacterStream(3, new StringReader(csrch_query), csrch_query.length()); 886 887 String csrch_parsedquerysha1hash = _value.getParsedQuerySha1Hash(); 888 if(csrch_parsedquerysha1hash == null) 889 _ps.setNull(4, Types.VARCHAR); 890 else 891 _ps.setString(4, csrch_parsedquerysha1hash); 892 893 String csrch_parsedquery = _value.getParsedQuery(); 894 if(csrch_parsedquery == null) 895 _ps.setNull(5, Types.CLOB); 896 else 897 _ps.setCharacterStream(5, new StringReader(csrch_parsedquery), csrch_parsedquery.length()); 898 899 SearchDefaultOperatorPK csrch_srchdefop_searchdefaultoperatorid = _value.getSearchDefaultOperatorPK(); 900 if(csrch_srchdefop_searchdefaultoperatorid == null) 901 _ps.setNull(6, Types.BIGINT); 902 else 903 _ps.setLong(6, csrch_srchdefop_searchdefaultoperatorid.getEntityId()); 904 905 SearchSortOrderPK csrch_srchsrtord_searchsortorderid = _value.getSearchSortOrderPK(); 906 if(csrch_srchsrtord_searchsortorderid == null) 907 _ps.setNull(7, Types.BIGINT); 908 else 909 _ps.setLong(7, csrch_srchsrtord_searchsortorderid.getEntityId()); 910 911 SearchSortDirectionPK csrch_srchsrtdir_searchsortdirectionid = _value.getSearchSortDirectionPK(); 912 if(csrch_srchsrtdir_searchsortdirectionid == null) 913 _ps.setNull(8, Types.BIGINT); 914 else 915 _ps.setLong(8, csrch_srchsrtdir_searchsortdirectionid.getEntityId()); 916 917 Long csrch_fromtime = _value.getFromTime(); 918 if(csrch_fromtime == null) 919 _ps.setNull(9, Types.BIGINT); 920 else 921 _ps.setLong(9, csrch_fromtime); 922 923 Long csrch_thrutime = _value.getThruTime(); 924 if(csrch_thrutime == null) 925 _ps.setNull(10, Types.BIGINT); 926 else 927 _ps.setLong(10, csrch_thrutime); 928 929 _ps.setLong(11, _value.getPrimaryKey().getEntityId()); 930 931 _value.clearHasBeenModified(); 932 } 933 934 return _hasBeenModified; 935 } 936 937 @Override 938 public void store(Session session, CachedSearch entity) 939 throws PersistenceDatabaseException { 940 PreparedStatement _ps = session.prepareStatement(SQL_UPDATE); 941 942 try { 943 CachedSearchValue _value = entity.getCachedSearchValue(); 944 945 if(bindForStore(_ps, _value)) { 946 if(PersistenceDebugFlags.CheckEntityUpdateRowCount) { 947 int _count = _ps.executeUpdate(); 948 949 if(_count != 1) { 950 throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count); 951 } 952 } else { 953 _ps.executeUpdate(); 954 } 955 956 session.getValueCache().put(_value); 957 } 958 } catch (SQLException se) { 959 throw new PersistenceDatabaseException(se); 960 } 961 } 962 963 @Override 964 public void store(Session session, Collection<CachedSearch> entities) 965 throws PersistenceDatabaseException { 966 if(entities.size() > 0) { 967 PreparedStatement _ps = session.prepareStatement(SQL_UPDATE); 968 int _modifiedEntities = 0; 969 970 try { 971 for(CachedSearch entity : entities) { 972 if(bindForStore(_ps, entity.getCachedSearchValue())) { 973 _ps.addBatch(); 974 _modifiedEntities++; 975 } 976 } 977 978 if(_modifiedEntities != 0) { 979 if(PersistenceDebugFlags.CheckEntityUpdateRowCount) { 980 int[] _counts = _ps.executeBatch(); 981 982 for(int _countOffset = 0 ; _countOffset < _modifiedEntities ; _countOffset++) { 983 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 984 throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 985 } 986 } 987 } else { 988 _ps.executeBatch(); 989 } 990 991 _ps.clearBatch(); 992 993 entities.forEach((entity) -> { 994 session.getValueCache().put(entity.getCachedSearchValue()); 995 }); 996 } 997 } catch (SQLException se) { 998 throw new PersistenceDatabaseException(se); 999 } 1000 } 1001 } 1002 1003 @Override 1004 public void store(Collection<CachedSearch> entities) 1005 throws PersistenceDatabaseException { 1006 store(ThreadSession.currentSession(), entities); 1007 } 1008 1009 @Override 1010 public void remove(Session session, CachedSearch entity) 1011 throws PersistenceDatabaseException { 1012 remove(session, entity.getPrimaryKey()); 1013 } 1014 1015 @Override 1016 public void remove(Session session, CachedSearchPK pk) 1017 throws PersistenceDatabaseException { 1018 PreparedStatement _ps = session.prepareStatement(SQL_DELETE); 1019 long _entityId = pk.getEntityId(); 1020 1021 try { 1022 _ps.setLong(1, _entityId); 1023 1024 if(PersistenceDebugFlags.CheckEntityDeleteRowCount) { 1025 int _count = _ps.executeUpdate(); 1026 1027 if(_count != 1) { 1028 throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count); 1029 } 1030 } else { 1031 _ps.executeUpdate(); 1032 } 1033 1034 session.getValueCache().remove(pk); 1035 } catch (SQLException se) { 1036 throw new PersistenceDatabaseException(se); 1037 } 1038 1039 session.removed(pk, false); 1040 } 1041 1042 @Override 1043 public void remove(Session session, Collection<CachedSearchPK> pks) 1044 throws PersistenceDatabaseException { 1045 if(pks.size() > 0) { 1046 PreparedStatement _ps = session.prepareStatement(SQL_DELETE); 1047 int _modifiedEntities = 0; 1048 1049 try { 1050 for(CachedSearchPK pk : pks) { 1051 long _entityId = pk.getEntityId(); 1052 1053 _ps.setLong(1, _entityId); 1054 1055 _ps.addBatch(); 1056 _modifiedEntities++; 1057 } 1058 1059 if(_modifiedEntities != 0) { 1060 if(PersistenceDebugFlags.CheckEntityDeleteRowCount) { 1061 int[] _counts = _ps.executeBatch(); 1062 1063 for(int _countOffset = 0 ; _countOffset < _modifiedEntities ; _countOffset++) { 1064 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 1065 throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 1066 } 1067 } 1068 } else { 1069 _ps.executeBatch(); 1070 } 1071 1072 _ps.clearBatch(); 1073 1074 pks.forEach((pk) -> { 1075 session.getValueCache().remove(pk); 1076 }); 1077 } 1078 } catch (SQLException se) { 1079 throw new PersistenceDatabaseException(se); 1080 } 1081 1082 pks.forEach((pk) -> { 1083 session.removed(pk, true); 1084 }); 1085 } 1086 } 1087 1088 @Override 1089 public void remove(Collection<CachedSearchPK> pks) 1090 throws PersistenceDatabaseException { 1091 remove(ThreadSession.currentSession(), pks); 1092 } 1093 1094 @Override 1095 public boolean validPK(Session session, CachedSearchPK pk) 1096 throws PersistenceDatabaseException { 1097 boolean valid = false; 1098 PreparedStatement _ps = session.prepareStatement(SQL_VALID); 1099 ResultSet _rs = null; 1100 1101 try { 1102 _ps.setLong(1, pk.getEntityId()); 1103 1104 _rs = _ps.executeQuery(); 1105 if(_rs.next()) { 1106 long _count = _rs.getLong(1); 1107 if(_rs.wasNull()) 1108 _count = 0; 1109 1110 if(_count == 1) 1111 valid = true; 1112 } 1113 } catch (SQLException se) { 1114 throw new PersistenceDatabaseException(se); 1115 } finally { 1116 if(_rs != null) { 1117 try { 1118 _rs.close(); 1119 } catch (SQLException se) { 1120 // do nothing 1121 } 1122 } 1123 } 1124 1125 return valid; 1126 } 1127 1128}