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