001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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 * CachedExecutedSearchFactory.java 021 */ 022 023package com.echothree.model.data.search.server.factory; 024 025import com.echothree.model.data.search.common.pk.CachedSearchPK; 026 027import com.echothree.model.data.search.server.entity.CachedSearch; 028 029import com.echothree.model.data.search.common.CachedExecutedSearchConstants; 030import com.echothree.model.data.search.common.pk.CachedExecutedSearchPK; 031import com.echothree.model.data.search.server.value.CachedExecutedSearchValue; 032import com.echothree.model.data.search.server.entity.CachedExecutedSearch; 033import com.echothree.util.common.exception.PersistenceDatabaseException; 034import com.echothree.util.common.exception.PersistenceDatabaseUpdateException; 035import com.echothree.util.common.exception.PersistenceNotNullException; 036import com.echothree.util.server.persistence.BaseFactory; 037import com.echothree.util.server.persistence.EntityIdGenerator; 038import com.echothree.util.server.persistence.EntityPermission; 039import com.echothree.util.server.persistence.PersistenceDebugFlags; 040import com.echothree.util.server.persistence.Session; 041import java.sql.PreparedStatement; 042import java.sql.ResultSet; 043import java.sql.SQLException; 044import java.sql.Types; 045import java.io.ByteArrayInputStream; 046import java.io.StringReader; 047import java.util.ArrayList; 048import java.util.Collection; 049import java.util.HashSet; 050import java.util.List; 051import java.util.Map; 052import java.util.Set; 053import javax.enterprise.context.ApplicationScoped; 054import javax.enterprise.inject.spi.CDI; 055import javax.inject.Inject; 056import org.jooq.ResultQuery; 057import org.slf4j.Logger; 058import org.slf4j.LoggerFactory; 059 060@ApplicationScoped 061public class CachedExecutedSearchFactory 062 implements BaseFactory<CachedExecutedSearchPK, CachedExecutedSearch> { 063 064 @Inject 065 Session session; 066 067 //private static final Logger log = LoggerFactory.getLogger(CachedExecutedSearchFactory.class); 068 069 final private static String SQL_SELECT_READ_ONLY = "SELECT cxsrch_cachedexecutedsearchid, cxsrch_csrch_cachedsearchid, cxsrch_fromtime, cxsrch_thrutime FROM cachedexecutedsearches WHERE cxsrch_cachedexecutedsearchid = ?"; 070 final private static String SQL_SELECT_READ_WRITE = "SELECT cxsrch_cachedexecutedsearchid, cxsrch_csrch_cachedsearchid, cxsrch_fromtime, cxsrch_thrutime FROM cachedexecutedsearches WHERE cxsrch_cachedexecutedsearchid = ? FOR UPDATE"; 071 final private static String SQL_INSERT = "INSERT INTO cachedexecutedsearches (cxsrch_cachedexecutedsearchid, cxsrch_csrch_cachedsearchid, cxsrch_fromtime, cxsrch_thrutime) VALUES (?, ?, ?, ?)"; 072 final private static String SQL_UPDATE = "UPDATE cachedexecutedsearches SET cxsrch_csrch_cachedsearchid = ?, cxsrch_fromtime = ?, cxsrch_thrutime = ? WHERE cxsrch_cachedexecutedsearchid = ?"; 073 final private static String SQL_DELETE = "DELETE FROM cachedexecutedsearches WHERE cxsrch_cachedexecutedsearchid = ?"; 074 final private static String SQL_VALID = "SELECT COUNT(*) FROM cachedexecutedsearches WHERE cxsrch_cachedexecutedsearchid = ?"; 075 076 final public static String CXSRCH_CACHEDEXECUTEDSEARCHID = "cxsrch_cachedexecutedsearchid"; 077 final public static String CXSRCH_CSRCH_CACHEDSEARCHID = "cxsrch_csrch_cachedsearchid"; 078 final public static String CXSRCH_FROMTIME = "cxsrch_fromtime"; 079 final public static String CXSRCH_THRUTIME = "cxsrch_thrutime"; 080 081 final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(CachedExecutedSearchConstants.COMPONENT_VENDOR_NAME, CachedExecutedSearchConstants.ENTITY_TYPE_NAME); 082 083 /** Creates a new instance of CachedExecutedSearchFactory */ 084 protected CachedExecutedSearchFactory() { 085 super(); 086 } 087 088 public static CachedExecutedSearchFactory getInstance() { 089 return CDI.current().select(CachedExecutedSearchFactory.class).get(); 090 } 091 092 @Override 093 public String getComponentVendorName() { 094 return CachedExecutedSearchConstants.COMPONENT_VENDOR_NAME; 095 } 096 097 @Override 098 public String getEntityTypeName() { 099 return CachedExecutedSearchConstants.ENTITY_TYPE_NAME; 100 } 101 102 public PreparedStatement prepareStatement(String query) { 103 return session.prepareStatement(query); 104 } 105 106 public CachedExecutedSearchPK getNextPK() { 107 return new CachedExecutedSearchPK(entityIdGenerator.getNextEntityId()); 108 } 109 110 public Set<CachedExecutedSearchPK> getPKsFromResultSetAsSet(ResultSet rs) 111 throws PersistenceDatabaseException { 112 Set<CachedExecutedSearchPK> _result = new HashSet<>(); 113 114 try { 115 while(rs.next()) { 116 _result.add(getPKFromResultSet(rs)); 117 } 118 } catch (SQLException se) { 119 throw new PersistenceDatabaseException(se); 120 } 121 122 return _result; 123 } 124 125 public java.util.List<CachedExecutedSearchPK> getPKsFromResultSetAsList(ResultSet rs) 126 throws PersistenceDatabaseException { 127 java.util.List<CachedExecutedSearchPK> _result = new ArrayList<>(); 128 129 try { 130 while(rs.next()) { 131 _result.add(getPKFromResultSet(rs)); 132 } 133 } catch (SQLException se) { 134 throw new PersistenceDatabaseException(se); 135 } 136 137 return _result; 138 } 139 140 public CachedExecutedSearchPK getPKFromResultSet(ResultSet rs) 141 throws PersistenceDatabaseException { 142 CachedExecutedSearchPK _result; 143 144 try { 145 long cxsrch_cachedexecutedsearchid = rs.getLong(CXSRCH_CACHEDEXECUTEDSEARCHID); 146 Long _entityId = rs.wasNull() ? null : cxsrch_cachedexecutedsearchid; 147 148 _result = new CachedExecutedSearchPK(_entityId); 149 } catch (SQLException se) { 150 throw new PersistenceDatabaseException(se); 151 } 152 153 return _result; 154 } 155 156 public java.util.List<CachedExecutedSearchValue> getValuesFromPKs(Collection<CachedExecutedSearchPK> pks) 157 throws PersistenceDatabaseException { 158 java.util.List<CachedExecutedSearchValue> _values = new ArrayList<>(pks.size()); 159 160 for(CachedExecutedSearchPK _pk: pks) { 161 _values.add(getValueFromPK(_pk)); 162 } 163 164 return _values; 165 } 166 167 public CachedExecutedSearchValue getValueFromPK(CachedExecutedSearchPK pk) 168 throws PersistenceDatabaseException { 169 CachedExecutedSearchValue _value; 170 171 // See if we already have the entity in the session cache 172 CachedExecutedSearch _entity = (CachedExecutedSearch)session.getEntity(pk); 173 if(_entity == null) 174 _value = getEntityFromPK(EntityPermission.READ_ONLY, pk).getCachedExecutedSearchValue(); 175 else 176 _value = _entity.getCachedExecutedSearchValue(); 177 178 return _value; 179 } 180 181 public java.util.List<CachedExecutedSearchValue> getValuesFromResultSet(ResultSet rs) 182 throws PersistenceDatabaseException { 183 java.util.List<CachedExecutedSearchValue> _result = new ArrayList<>(); 184 185 try { 186 while(rs.next()) { 187 _result.add(getValueFromResultSet(rs)); 188 } 189 } catch (SQLException se) { 190 throw new PersistenceDatabaseException(se); 191 } 192 193 return _result; 194 } 195 196 public CachedExecutedSearchValue getValueFromResultSet(ResultSet rs) 197 throws PersistenceDatabaseException { 198 CachedExecutedSearchValue _value; 199 200 try { 201 Long cxsrch_cachedexecutedsearchid = rs.getLong(CXSRCH_CACHEDEXECUTEDSEARCHID); 202 CachedExecutedSearchPK _pk = new CachedExecutedSearchPK(cxsrch_cachedexecutedsearchid); 203 204 // See if we already have the entity in the session cache 205 CachedExecutedSearch _entity = (CachedExecutedSearch)session.getEntity(_pk); 206 207 if(_entity == null) { 208 Long cxsrch_csrch_cachedsearchid = rs.getLong(CXSRCH_CSRCH_CACHEDSEARCHID); 209 if(rs.wasNull()) 210 cxsrch_csrch_cachedsearchid = null; 211 212 Long cxsrch_fromtime = rs.getLong(CXSRCH_FROMTIME); 213 if(rs.wasNull()) 214 cxsrch_fromtime = null; 215 216 Long cxsrch_thrutime = rs.getLong(CXSRCH_THRUTIME); 217 if(rs.wasNull()) 218 cxsrch_thrutime = null; 219 220 _value = new CachedExecutedSearchValue(_pk, new CachedSearchPK(cxsrch_csrch_cachedsearchid), cxsrch_fromtime, cxsrch_thrutime); 221 } else 222 _value = _entity.getCachedExecutedSearchValue(); 223 } catch (SQLException se) { 224 throw new PersistenceDatabaseException(se); 225 } 226 227 return _value; 228 } 229 230 public java.util.List<CachedExecutedSearch> getEntitiesFromPKs(EntityPermission entityPermission, Collection<CachedExecutedSearchPK> pks) 231 throws PersistenceDatabaseException { 232 java.util.List<CachedExecutedSearch> _entities = new ArrayList<>(pks.size()); 233 234 for(CachedExecutedSearchPK _pk: pks) { 235 _entities.add(getEntityFromPK(entityPermission, _pk)); 236 } 237 238 return _entities; 239 } 240 241 public CachedExecutedSearch getEntityFromValue(EntityPermission entityPermission, CachedExecutedSearchValue value) { 242 return getEntityFromPK(entityPermission, value.getPrimaryKey()); 243 } 244 245 public CachedExecutedSearch getEntityFromCache(CachedExecutedSearchPK pk) { 246 CachedExecutedSearchValue _value = (CachedExecutedSearchValue)session.getValueCache().get(pk); 247 248 return _value == null ? null : new CachedExecutedSearch(_value, EntityPermission.READ_ONLY); 249 } 250 251 public CachedExecutedSearch getEntityFromPK(EntityPermission entityPermission, CachedExecutedSearchPK pk) 252 throws PersistenceDatabaseException { 253 CachedExecutedSearch _entity; 254 255 // See if we already have the entity in the session cache 256 _entity = (CachedExecutedSearch)session.getEntity(pk); 257 if(_entity != null) { 258 // If the requested permission is READ_WRITE, and the cached permission is 259 // READ_ONLY, then pretend that the cached object wasn't found, and create 260 // a new entity that is READ_WRITE. 261 if(entityPermission.equals(EntityPermission.READ_WRITE)) { 262 if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY)) 263 _entity = null; 264 } 265 } 266 267 if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) { 268 _entity = getEntityFromCache(pk); 269 } 270 271 if(_entity == null) { 272 PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE); 273 long _entityId = pk.getEntityId(); 274 ResultSet _rs = null; 275 276 try { 277 _ps.setLong(1, _entityId); 278 _rs = _ps.executeQuery(); 279 if(_rs.next()) { 280 _entity = getEntityFromResultSet(entityPermission, _rs); 281 } 282 } catch (SQLException se) { 283 throw new PersistenceDatabaseException(se); 284 } finally { 285 if(_rs != null) { 286 try { 287 _rs.close(); 288 } catch (SQLException se) { 289 // do nothing 290 } 291 } 292 } 293 } 294 295 return _entity; 296 } 297 298 public Set<CachedExecutedSearchPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params) 299 throws PersistenceDatabaseException { 300 Set<CachedExecutedSearchPK> _pks; 301 ResultSet _rs = null; 302 303 try { 304 if(params.length != 0) { 305 Session.setQueryParams(ps, params); 306 } 307 308 _rs = ps.executeQuery(); 309 _pks = getPKsFromResultSetAsSet(_rs); 310 _rs.close(); 311 } catch (SQLException se) { 312 throw new PersistenceDatabaseException(se); 313 } finally { 314 if(_rs != null) { 315 try { 316 _rs.close(); 317 } catch (SQLException se) { 318 // do nothing 319 } 320 } 321 } 322 323 return _pks; 324 } 325 326 public java.util.List<CachedExecutedSearchPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params) 327 throws PersistenceDatabaseException { 328 java.util.List<CachedExecutedSearchPK> _pks; 329 ResultSet _rs = null; 330 331 try { 332 if(params.length != 0) { 333 Session.setQueryParams(ps, params); 334 } 335 336 _rs = ps.executeQuery(); 337 _pks = getPKsFromResultSetAsList(_rs); 338 _rs.close(); 339 } catch (SQLException se) { 340 throw new PersistenceDatabaseException(se); 341 } finally { 342 if(_rs != null) { 343 try { 344 _rs.close(); 345 } catch (SQLException se) { 346 // do nothing 347 } 348 } 349 } 350 351 return _pks; 352 } 353 354 public CachedExecutedSearchPK getPKFromQuery(PreparedStatement ps, final Object... params) 355 throws PersistenceDatabaseException { 356 CachedExecutedSearchPK _pk = null; 357 ResultSet _rs = null; 358 359 try { 360 if(params.length != 0) { 361 Session.setQueryParams(ps, params); 362 } 363 364 _rs = ps.executeQuery(); 365 if(_rs.next()) { 366 _pk = getPKFromResultSet(_rs); 367 } 368 _rs.close(); 369 } catch (SQLException se) { 370 throw new PersistenceDatabaseException(se); 371 } finally { 372 if(_rs != null) { 373 try { 374 _rs.close(); 375 } catch (SQLException se) { 376 // do nothing 377 } 378 } 379 } 380 381 return _pk; 382 } 383 384 public java.util.List<CachedExecutedSearch> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 385 throws PersistenceDatabaseException { 386 PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission)); 387 388 return getEntitiesFromQuery(entityPermission, ps, params); 389 } 390 391 public java.util.List<CachedExecutedSearch> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 392 throws PersistenceDatabaseException { 393 PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission)); 394 395 return getEntitiesFromQuery(entityPermission, ps); 396 } 397 398 public java.util.List<CachedExecutedSearch> getEntitiesFromQuery(EntityPermission entityPermission, ResultQuery<?> query) 399 throws PersistenceDatabaseException { 400 PreparedStatement ps = session.prepareStatement(query.getSQL()); 401 402 return getEntitiesFromQuery(entityPermission, ps, query.getBindValues().toArray()); 403 } 404 405 public java.util.List<CachedExecutedSearch> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params) 406 throws PersistenceDatabaseException { 407 java.util.List<CachedExecutedSearch> _entities; 408 ResultSet _rs = null; 409 410 try { 411 if(params.length != 0) { 412 Session.setQueryParams(ps, params); 413 } 414 415 _rs = ps.executeQuery(); 416 _entities = getEntitiesFromResultSet(entityPermission, _rs); 417 _rs.close(); 418 } catch (SQLException se) { 419 throw new PersistenceDatabaseException(se); 420 } finally { 421 if(_rs != null) { 422 try { 423 _rs.close(); 424 } catch (SQLException se) { 425 // do nothing 426 } 427 } 428 } 429 430 return _entities; 431 } 432 433 public CachedExecutedSearch getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params) 434 throws PersistenceDatabaseException { 435 PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission)); 436 437 return getEntityFromQuery(entityPermission, ps, params); 438 } 439 440 public CachedExecutedSearch getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap) 441 throws PersistenceDatabaseException { 442 PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission)); 443 444 return getEntityFromQuery(entityPermission, ps); 445 } 446 447 public CachedExecutedSearch getEntityFromQuery(EntityPermission entityPermission, ResultQuery<?> query) 448 throws PersistenceDatabaseException { 449 PreparedStatement ps = session.prepareStatement(query.getSQL()); 450 451 return getEntityFromQuery(entityPermission, ps, query.getBindValues().toArray()); 452 } 453 454 public CachedExecutedSearch getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params) 455 throws PersistenceDatabaseException { 456 CachedExecutedSearch _entity = null; 457 ResultSet _rs = null; 458 459 try { 460 if(params.length != 0) { 461 Session.setQueryParams(ps, params); 462 } 463 464 _rs = ps.executeQuery(); 465 if(_rs.next()) { 466 _entity = getEntityFromResultSet(entityPermission, _rs); 467 } 468 _rs.close(); 469 } catch (SQLException se) { 470 throw new PersistenceDatabaseException(se); 471 } finally { 472 if(_rs != null) { 473 try { 474 _rs.close(); 475 } catch (SQLException se) { 476 // do nothing 477 } 478 } 479 } 480 481 return _entity; 482 } 483 484 public java.util.List<CachedExecutedSearch> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs) 485 throws PersistenceDatabaseException { 486 java.util.List<CachedExecutedSearch> _result = new ArrayList<>(); 487 488 try { 489 while(rs.next()) { 490 _result.add(getEntityFromResultSet(entityPermission, rs)); 491 } 492 } catch (SQLException se) { 493 throw new PersistenceDatabaseException(se); 494 } 495 496 return _result; 497 } 498 499 public CachedExecutedSearch getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs) 500 throws PersistenceDatabaseException { 501 CachedExecutedSearch _entity; 502 503 try { 504 Long cxsrch_cachedexecutedsearchid = rs.getLong(CXSRCH_CACHEDEXECUTEDSEARCHID); 505 CachedExecutedSearchPK _pk = new CachedExecutedSearchPK(cxsrch_cachedexecutedsearchid); 506 507 // See if we already have the entity in the session cache 508 _entity = (CachedExecutedSearch)session.getEntity(_pk); 509 if(_entity != null) { 510 // If the requested permission is READ_WRITE, and the cached permission is 511 // READ_ONLY, then pretend that the cached object wasn't found, and create 512 // a new entity that is READ_WRITE. 513 if(entityPermission.equals(EntityPermission.READ_WRITE)) { 514 if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY)) 515 _entity = null; 516 } 517 } 518 boolean foundInSessionCache = _entity != null; 519 520 if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) { 521 _entity = getEntityFromCache(_pk); 522 } 523 524 if(_entity == null) { 525 Long cxsrch_csrch_cachedsearchid = rs.getLong(CXSRCH_CSRCH_CACHEDSEARCHID); 526 if(rs.wasNull()) 527 cxsrch_csrch_cachedsearchid = null; 528 529 Long cxsrch_fromtime = rs.getLong(CXSRCH_FROMTIME); 530 if(rs.wasNull()) 531 cxsrch_fromtime = null; 532 533 Long cxsrch_thrutime = rs.getLong(CXSRCH_THRUTIME); 534 if(rs.wasNull()) 535 cxsrch_thrutime = null; 536 537 CachedExecutedSearchValue _value = new CachedExecutedSearchValue(_pk, cxsrch_csrch_cachedsearchid == null? null: new CachedSearchPK(cxsrch_csrch_cachedsearchid), cxsrch_fromtime, cxsrch_thrutime); 538 _entity = new CachedExecutedSearch(_value, entityPermission); 539 } 540 541 if(!foundInSessionCache) { 542 if(entityPermission.equals(EntityPermission.READ_ONLY)) { 543 session.putReadOnlyEntity(_pk, _entity); 544 session.getValueCache().put(_entity.getCachedExecutedSearchValue()); 545 } else { 546 session.putReadWriteEntity(_pk, _entity); 547 } 548 } 549 } catch (SQLException se) { 550 throw new PersistenceDatabaseException(se); 551 } 552 553 return _entity; 554 } 555 556 public CachedExecutedSearch create(CachedSearch cachedSearch, Long fromTime, Long thruTime) 557 throws PersistenceDatabaseException, PersistenceNotNullException { 558 return create(cachedSearch == null ? null : cachedSearch.getPrimaryKey(), fromTime, thruTime); 559 } 560 561 private void bindForCreate(PreparedStatement _ps, CachedExecutedSearchValue _value) 562 throws SQLException { 563 _ps.setLong(1, _value.getEntityId()); 564 565 CachedSearchPK cxsrch_csrch_cachedsearchid = _value.getCachedSearchPK(); 566 if(cxsrch_csrch_cachedsearchid == null) 567 _ps.setNull(2, Types.BIGINT); 568 else 569 _ps.setLong(2, cxsrch_csrch_cachedsearchid.getEntityId()); 570 571 Long cxsrch_fromtime = _value.getFromTime(); 572 if(cxsrch_fromtime == null) 573 _ps.setNull(3, Types.BIGINT); 574 else 575 _ps.setLong(3, cxsrch_fromtime); 576 577 Long cxsrch_thrutime = _value.getThruTime(); 578 if(cxsrch_thrutime == null) 579 _ps.setNull(4, Types.BIGINT); 580 else 581 _ps.setLong(4, cxsrch_thrutime); 582 583 } 584 585 public CachedExecutedSearch create(CachedSearchPK cachedSearchPK, Long fromTime, Long thruTime) 586 throws PersistenceDatabaseException, PersistenceNotNullException { 587 CachedExecutedSearchPK _pk = getNextPK(); 588 CachedExecutedSearchValue _value = new CachedExecutedSearchValue(_pk, cachedSearchPK, fromTime, thruTime); 589 590 PreparedStatement _ps = session.prepareStatement(SQL_INSERT); 591 592 try { 593 bindForCreate(_ps, _value); 594 595 if(PersistenceDebugFlags.CheckEntityInsertRowCount) { 596 int _count = _ps.executeUpdate(); 597 598 if(_count != 1) { 599 throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count); 600 } 601 } else { 602 _ps.executeUpdate(); 603 } 604 605 session.getValueCache().put(_value); 606 } catch (SQLException se) { 607 throw new PersistenceDatabaseException(se); 608 } 609 610 CachedExecutedSearch _entity = new CachedExecutedSearch(_value, EntityPermission.READ_ONLY); 611 session.putReadOnlyEntity(_pk, _entity); 612 613 return _entity; 614 } 615 616 public void create(Collection<CachedExecutedSearchValue> _values) 617 throws PersistenceDatabaseException, PersistenceNotNullException { 618 int _size = _values.size(); 619 620 if(_size > 0) { 621 PreparedStatement _ps = session.prepareStatement(SQL_INSERT); 622 List<CachedExecutedSearchValue> _cacheValues = new ArrayList<>(_size); 623 624 try { 625 for(CachedExecutedSearchValue _value : _values) { 626 _value.setEntityId(entityIdGenerator.getNextEntityId()); 627 bindForCreate(_ps, _value); 628 629 _ps.addBatch(); 630 631 _cacheValues.add(_value); 632 } 633 634 if(PersistenceDebugFlags.CheckEntityInsertRowCount) { 635 int[] _counts = _ps.executeBatch(); 636 637 for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) { 638 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 639 throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 640 } 641 } 642 } else { 643 _ps.executeBatch(); 644 } 645 646 _ps.clearBatch(); 647 } catch (SQLException se) { 648 throw new PersistenceDatabaseException(se); 649 } 650 651 _cacheValues.forEach((_cacheValue) -> { 652 CachedExecutedSearch _cacheEntity = new CachedExecutedSearch(_cacheValue, EntityPermission.READ_ONLY); 653 654 session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity); 655 }); 656 } 657 } 658 659 private boolean bindForStore(PreparedStatement _ps, CachedExecutedSearchValue _value) 660 throws SQLException { 661 boolean _hasBeenModified = _value.hasBeenModified(); 662 663 if(_hasBeenModified) { 664 CachedSearchPK cxsrch_csrch_cachedsearchid = _value.getCachedSearchPK(); 665 if(cxsrch_csrch_cachedsearchid == null) 666 _ps.setNull(1, Types.BIGINT); 667 else 668 _ps.setLong(1, cxsrch_csrch_cachedsearchid.getEntityId()); 669 670 Long cxsrch_fromtime = _value.getFromTime(); 671 if(cxsrch_fromtime == null) 672 _ps.setNull(2, Types.BIGINT); 673 else 674 _ps.setLong(2, cxsrch_fromtime); 675 676 Long cxsrch_thrutime = _value.getThruTime(); 677 if(cxsrch_thrutime == null) 678 _ps.setNull(3, Types.BIGINT); 679 else 680 _ps.setLong(3, cxsrch_thrutime); 681 682 _ps.setLong(4, _value.getPrimaryKey().getEntityId()); 683 684 _value.clearHasBeenModified(); 685 } 686 687 return _hasBeenModified; 688 } 689 690 @Override 691 public void store(CachedExecutedSearch entity) 692 throws PersistenceDatabaseException { 693 PreparedStatement _ps = session.prepareStatement(SQL_UPDATE); 694 695 try { 696 CachedExecutedSearchValue _value = entity.getCachedExecutedSearchValue(); 697 698 if(bindForStore(_ps, _value)) { 699 if(PersistenceDebugFlags.CheckEntityUpdateRowCount) { 700 int _count = _ps.executeUpdate(); 701 702 if(_count != 1) { 703 throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count); 704 } 705 } else { 706 _ps.executeUpdate(); 707 } 708 709 session.getValueCache().put(_value); 710 } 711 } catch (SQLException se) { 712 throw new PersistenceDatabaseException(se); 713 } 714 } 715 716 @Override 717 public void store(Collection<CachedExecutedSearch> entities) 718 throws PersistenceDatabaseException { 719 if(entities.size() > 0) { 720 PreparedStatement _ps = session.prepareStatement(SQL_UPDATE); 721 int _modifiedEntities = 0; 722 723 try { 724 for(CachedExecutedSearch entity : entities) { 725 if(bindForStore(_ps, entity.getCachedExecutedSearchValue())) { 726 _ps.addBatch(); 727 _modifiedEntities++; 728 } 729 } 730 731 if(_modifiedEntities != 0) { 732 if(PersistenceDebugFlags.CheckEntityUpdateRowCount) { 733 int[] _counts = _ps.executeBatch(); 734 735 for(int _countOffset = 0 ; _countOffset < _modifiedEntities ; _countOffset++) { 736 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 737 throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 738 } 739 } 740 } else { 741 _ps.executeBatch(); 742 } 743 744 _ps.clearBatch(); 745 746 entities.forEach((entity) -> { 747 session.getValueCache().put(entity.getCachedExecutedSearchValue()); 748 }); 749 } 750 } catch (SQLException se) { 751 throw new PersistenceDatabaseException(se); 752 } 753 } 754 } 755 756 @Override 757 public void remove(CachedExecutedSearch entity) 758 throws PersistenceDatabaseException { 759 remove(entity.getPrimaryKey()); 760 } 761 762 @Override 763 public void remove(CachedExecutedSearchPK pk) 764 throws PersistenceDatabaseException { 765 PreparedStatement _ps = session.prepareStatement(SQL_DELETE); 766 long _entityId = pk.getEntityId(); 767 768 try { 769 _ps.setLong(1, _entityId); 770 771 if(PersistenceDebugFlags.CheckEntityDeleteRowCount) { 772 int _count = _ps.executeUpdate(); 773 774 if(_count != 1) { 775 throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count); 776 } 777 } else { 778 _ps.executeUpdate(); 779 } 780 781 session.getValueCache().remove(pk); 782 } catch (SQLException se) { 783 throw new PersistenceDatabaseException(se); 784 } 785 786 session.removed(pk, false); 787 } 788 789 @Override 790 public void remove(Collection<CachedExecutedSearchPK> pks) 791 throws PersistenceDatabaseException { 792 if(pks.size() > 0) { 793 PreparedStatement _ps = session.prepareStatement(SQL_DELETE); 794 int _modifiedEntities = 0; 795 796 try { 797 for(CachedExecutedSearchPK pk : pks) { 798 long _entityId = pk.getEntityId(); 799 800 _ps.setLong(1, _entityId); 801 802 _ps.addBatch(); 803 _modifiedEntities++; 804 } 805 806 if(_modifiedEntities != 0) { 807 if(PersistenceDebugFlags.CheckEntityDeleteRowCount) { 808 int[] _counts = _ps.executeBatch(); 809 810 for(int _countOffset = 0 ; _countOffset < _modifiedEntities ; _countOffset++) { 811 if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) { 812 throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]); 813 } 814 } 815 } else { 816 _ps.executeBatch(); 817 } 818 819 _ps.clearBatch(); 820 821 pks.forEach((pk) -> { 822 session.getValueCache().remove(pk); 823 }); 824 } 825 } catch (SQLException se) { 826 throw new PersistenceDatabaseException(se); 827 } 828 829 pks.forEach((pk) -> { 830 session.removed(pk, true); 831 }); 832 } 833 } 834 835 @Override 836 public boolean validPK(CachedExecutedSearchPK pk) 837 throws PersistenceDatabaseException { 838 boolean valid = false; 839 PreparedStatement _ps = session.prepareStatement(SQL_VALID); 840 ResultSet _rs = null; 841 842 try { 843 _ps.setLong(1, pk.getEntityId()); 844 845 _rs = _ps.executeQuery(); 846 if(_rs.next()) { 847 long _count = _rs.getLong(1); 848 if(_rs.wasNull()) 849 _count = 0; 850 851 if(_count == 1) 852 valid = true; 853 } 854 } catch (SQLException se) { 855 throw new PersistenceDatabaseException(se); 856 } finally { 857 if(_rs != null) { 858 try { 859 _rs.close(); 860 } catch (SQLException se) { 861 // do nothing 862 } 863 } 864 } 865 866 return valid; 867 } 868 869}