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