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