001package gu.sql2java; 002import java.util.List; 003import gu.sql2java.exception.ObjectRetrievalException; 004import gu.sql2java.exception.RuntimeDaoException; 005 006import java.util.Collection; 007/** 008 * Interface to handle database calls (save, load, count, etc...) for table. 009 * @author guyadong 010 */ 011public interface TableManager<B extends BaseBean> extends SqlRunner{ 012 013 public interface Action<B>{ 014 015 /** 016 * do action for {@code bean} 017 * @param bean input bean 018 */ 019 void call(B bean); 020 } 021 022 //_____________________________________________________________________ 023 // 024 // COUNT 025 //_____________________________________________________________________ 026 //24 027 /** 028 * Retrieves the number of rows of the table. 029 * 030 * @return the number of rows returned 031 * @throws RuntimeDaoException 032 */ 033 public int countAll()throws RuntimeDaoException; 034 035 //27 036 /** 037 * count the number of elements of a specific bean 038 * 039 * @param bean the bean to look for ant count 040 * @return the number of rows returned 041 * @throws RuntimeDaoException 042 */ 043 public int countUsingTemplate( B bean)throws RuntimeDaoException; 044 045 //20 046 /** 047 * count the number of elements of a specific bean given the search type 048 * 049 * @param bean the template to look for 050 * @param searchType exact ? like ? starting like ? ending link ? <br> 051 * {@value Constant#SEARCH_EXACT} {@link Constant#SEARCH_EXACT} <br> 052 * {@value Constant#SEARCH_LIKE} {@link Constant#SEARCH_LIKE} <br> 053 * {@value Constant#SEARCH_STARTING_LIKE} {@link Constant#SEARCH_STARTING_LIKE} <br> 054 * {@value Constant#SEARCH_ENDING_LIKE} {@link Constant#SEARCH_ENDING_LIKE} <br> 055 * @return the number of rows returned 056 * @throws RuntimeDaoException 057 */ 058 public int countUsingTemplate(B bean, int searchType)throws RuntimeDaoException; 059 060 //25 061 /** 062 * Retrieves the number of rows of the table with a 'where' clause. 063 * It is up to you to pass the 'WHERE' in your where clauses. 064 * 065 * @param where the restriction clause 066 * @return the number of rows returned 067 * @throws RuntimeDaoException 068 */ 069 public int countWhere(String where)throws RuntimeDaoException; 070 071 //10 072 /** 073 * Deletes all rows from table. 074 * @return the number of deleted rows. 075 * @throws RuntimeDaoException 076 */ 077 public int deleteAll()throws RuntimeDaoException; 078 079 //11 080 /** 081 * Deletes rows from the table using a 'where' clause. 082 * It is up to you to pass the 'WHERE' in your where clauses. 083 * <br>Attention, if 'WHERE' is omitted it will delete all records. 084 * 085 * @param where the sql 'where' clause 086 * @return the number of deleted rows 087 * @throws RuntimeDaoException 088 */ 089 public int deleteByWhere(String where)throws RuntimeDaoException; 090 091 //21 092 /** 093 * Deletes rows using a template. 094 * 095 * @param bean the template object(s) to be deleted 096 * @return the number of deleted objects 097 * @throws RuntimeDaoException 098 */ 099 public int deleteUsingTemplate(B bean)throws RuntimeDaoException; 100 101 //2.1 102 /** 103 * Delete row according to its primary keys. 104 * 105 * @param keys primary keys value<br> 106 * for fd_face table<br> 107 * PK# 1 fd_face.id type Integer<br> 108 * for fd_feature table<br> 109 * PK# 1 fd_feature.md5 type String<br> 110 * for fd_image table<br> 111 * PK# 1 fd_image.md5 type String<br> 112 * for fd_store table<br> 113 * PK# 1 fd_store.md5 type String<br> 114 * @return the number of deleted rows 115 * @throws RuntimeDaoException 116 */ 117 public int deleteByPrimaryKey(Object ...keys)throws RuntimeDaoException; 118 119 //2.2 120 /** 121 * Delete row according to primary keys of bean.<br> 122 * 123 * @param bean will be deleted ,all keys must not be null 124 * @return the number of deleted rows,0 returned if bean is null 125 * @throws RuntimeDaoException 126 */ 127 public int delete(B bean)throws RuntimeDaoException; 128 129 //2.4 130 /** 131 * Delete beans.<br> 132 * 133 * @param beans B array will be deleted 134 * @return the number of deleted rows 135 * @throws RuntimeDaoException 136 */ 137 @SuppressWarnings("unchecked") 138 public int delete(B... beans)throws RuntimeDaoException; 139 140 //2.5 141 /** 142 * Delete beans.<br> 143 * 144 * @param beans B collection will be deleted 145 * @return the number of deleted rows 146 * @throws RuntimeDaoException 147 */ 148 public int delete(Collection<B> beans)throws RuntimeDaoException; 149 ////////////////////////////////////// 150 // LOAD ALL 151 ////////////////////////////////////// 152 153 //5 154 /** 155 * Loads all the rows from table. 156 * 157 * @return an array of B bean 158 * @throws RuntimeDaoException 159 */ 160 public B[] loadAll()throws RuntimeDaoException; 161 162 //5-1 163 /** 164 * Loads each row from table and dealt with action. 165 * @param action Action object for do something(not null) 166 * @return the count dealt by action 167 * @throws RuntimeDaoException 168 */ 169 public int loadAll(Action<B> action)throws RuntimeDaoException; 170 171 //6 172 /** 173 * Loads the given number of rows from table, given the start row. 174 * 175 * @param startRow the start row to be used (first row = 1, last row = -1) 176 * @param numRows the number of rows to be retrieved (all rows = a negative number) 177 * @return an array of B bean 178 * @throws RuntimeDaoException 179 */ 180 public B[] loadAll(int startRow, int numRows)throws RuntimeDaoException; 181 182 //6-1 183 /** 184 * Loads the given number of rows from table, given the start row and dealt with action. 185 * @param startRow the start row to be used (first row = 1, last row = -1) 186 * @param numRows the number of rows to be retrieved (all rows = a negative number) 187 * @param action Action object for do something(not null) 188 * @return the count dealt by action 189 * @throws RuntimeDaoException 190 */ 191 public int loadAll(int startRow, int numRows,Action<B> action)throws RuntimeDaoException; 192 193 //5-2 194 /** 195 * Loads all the rows from table. 196 * 197 * @return a list of B bean 198 * @throws RuntimeDaoException 199 */ 200 public List<B> loadAllAsList()throws RuntimeDaoException; 201 202 //6-2 203 /** 204 * Loads the given number of rows from table, given the start row. 205 * 206 * @param startRow the start row to be used (first row = 1, last row = -1) 207 * @param numRows the number of rows to be retrieved (all rows = a negative number) 208 * @return a list of B bean 209 * @throws RuntimeDaoException 210 */ 211 public List<B> loadAllAsList(int startRow, int numRows)throws RuntimeDaoException; 212 213 //1.2 214 /** 215 * Loads a B bean from the table using primary key fields of {@code bean}. 216 * @param bean the B bean with primary key fields 217 * @return a unique B or {@code null} if not found or bean is null 218 * @throws RuntimeDaoException 219 */ 220 public B loadByPrimaryKey(B bean)throws RuntimeDaoException; 221 222 //1.2.2 223 /** 224 * see also {@link #loadByPrimaryKey(BaseBean)} 225 * @param bean 226 * @return a unique B ,otherwise throw exception 227 * @throws ObjectRetrievalException not found 228 * @throws RuntimeDaoException 229 */ 230 public B loadByPrimaryKeyChecked(B bean)throws RuntimeDaoException,ObjectRetrievalException; 231 //1.3 232 /** 233 * Loads a B bean from the table using primary key fields. 234 * when you don't know which is primary key of table,you can use the method. 235 * @param keys primary keys value:<br> 236 * for fd_face table<br> 237 * PK# 1 fd_face.id type Integer<br> 238 * for fd_feature table<br> 239 * PK# 1 fd_feature.md5 type String<br> 240 * for fd_image table<br> 241 * PK# 1 fd_image.md5 type String<br> 242 * for fd_store table<br> 243 * PK# 1 fd_store.md5 type String<br> 244 * @return a unique B or {@code null} if not found 245 * @throws RuntimeDaoException 246 */ 247 public B loadByPrimaryKey(Object ...keys)throws RuntimeDaoException; 248 249 //1.3.2 250 /** 251 * see also {@link #loadByPrimaryKey(Object...)} 252 * @param keys 253 * @return a unique B,otherwise throw exception 254 * @throws ObjectRetrievalException not found 255 * @throws RuntimeDaoException 256 */ 257 public B loadByPrimaryKeyChecked(Object ...keys)throws RuntimeDaoException,ObjectRetrievalException; 258 259 //1.5 260 /** 261 * Returns true if this table contains row with primary key fields. 262 * @param keys primary keys value 263 * @see #loadByPrimaryKey(Object...) 264 * @return 265 * @throws RuntimeDaoException 266 */ 267 public boolean existsPrimaryKey(Object ...keys)throws RuntimeDaoException; 268 269 //1.6 270 /** 271 * Returns true if this table contains row specified by primary key fields of B.<br> 272 * when you don't know which is primary key of table,you can use the method. 273 * @param bean the B bean with primary key fields 274 * @return 275 * @see #loadByPrimaryKey(BaseBean) 276 * @throws RuntimeDaoException 277 */ 278 public boolean existsByPrimaryKey(B bean)throws RuntimeDaoException; 279 //1.7 280 /** 281 * Check duplicated row by primary keys,if row exists throw exception 282 * @param bean the B bean with primary key fields 283 * @return always bean 284 * @see #existsByPrimaryKey(BaseBean) 285 * @throws ObjectRetrievalException has duplicated record 286 * @throws RuntimeDaoException 287 */ 288 public B checkDuplicate(B bean)throws RuntimeDaoException,ObjectRetrievalException; 289 290 ////////////////////////////////////// 291 // SQL 'WHERE' METHOD 292 ////////////////////////////////////// 293 //7 294 /** 295 * Retrieves an array of B given a sql 'where' clause. 296 * 297 * @param where the sql 'where' clause 298 * @return 299 * @throws RuntimeDaoException 300 */ 301 public B[] loadByWhere(String where)throws RuntimeDaoException; 302 303 //7-1 304 /** 305 * Retrieves each row of B bean given a sql 'where' clause and dealt with action. 306 * @param where the sql 'where' clause 307 * @param action Action object for do something(not null) 308 * @return the count dealt by action 309 * @throws RuntimeDaoException 310 */ 311 public int loadByWhere(String where,Action<B> action)throws RuntimeDaoException; 312 313 //8 314 /** 315 * Retrieves an array of B bean given a sql where clause, and a list of fields. 316 * It is up to you to pass the 'WHERE' in your where clauses. 317 * 318 * @param where the sql 'WHERE' clause 319 * @param fieldList array of field's ID 320 * @return 321 * @throws RuntimeDaoException 322 */ 323 public B[] loadByWhere(String where, int[] fieldList)throws RuntimeDaoException; 324 325 //8-1 326 /** 327 * Retrieves each row of B bean given a sql where clause, and a list of fields, 328 * and dealt with action. 329 * It is up to you to pass the 'WHERE' in your where clauses. 330 * @param where the sql 'WHERE' clause 331 * @param fieldList array of field's ID 332 * @param action Action object for do something(not null) 333 * @return the count dealt by action 334 * @throws RuntimeDaoException 335 */ 336 public int loadByWhere(String where, int[] fieldList,Action<B> action)throws RuntimeDaoException; 337 338 //9 339 /** 340 * Retrieves an array of B bean given a sql where clause and a list of fields, and startRow and numRows. 341 * It is up to you to pass the 'WHERE' in your where clauses. 342 * 343 * @param where the sql 'where' clause 344 * @param fieldList table of the field's associated constants 345 * @param startRow the start row to be used (first row = 1, last row = -1) 346 * @param numRows the number of rows to be retrieved (all rows = a negative number) 347 * @return 348 * @throws RuntimeDaoException 349 */ 350 public B[] loadByWhere(String where, int[] fieldList, int startRow, int numRows)throws RuntimeDaoException; 351 352 //9-1 353 /** 354 * Retrieves each row of B bean given a sql where clause and a list of fields, and startRow and numRows, 355 * and dealt with action. 356 * It is up to you to pass the 'WHERE' in your where clauses. 357 * 358 * @param where the sql 'where' clause 359 * @param fieldList table of the field's associated constants 360 * @param startRow the start row to be used (first row = 1, last row = -1) 361 * @param numRows the number of rows to be retrieved (all rows = a negative number) 362 * @param action Action object for do something(not null) 363 * @return the count dealt by action 364 * @throws RuntimeDaoException 365 */ 366 public int loadByWhere(String where, int[] fieldList, int startRow, int numRows,Action<B> action)throws RuntimeDaoException; 367 //7 368 /** 369 * Retrieves a list of B bean given a sql 'where' clause. 370 * 371 * @param where the sql 'where' clause 372 * @return 373 * @throws RuntimeDaoException 374 */ 375 public List<B> loadByWhereAsList(String where)throws RuntimeDaoException; 376 377 //8 378 /** 379 * Retrieves a list of B bean given a sql where clause, and a list of fields. 380 * It is up to you to pass the 'WHERE' in your where clauses. 381 * 382 * @param where the sql 'WHERE' clause 383 * @param fieldList array of field's ID 384 * @return 385 * @throws RuntimeDaoException 386 */ 387 public List<B> loadByWhereAsList(String where, int[] fieldList)throws RuntimeDaoException; 388 389 //9-2 390 /** 391 * Retrieves a list of B bean given a sql where clause and a list of fields, and startRow and numRows. 392 * It is up to you to pass the 'WHERE' in your where clauses. 393 * 394 * @param where the sql 'where' clause 395 * @param fieldList table of the field's associated constants 396 * @param startRow the start row to be used (first row = 1, last row = -1) 397 * @param numRows the number of rows to be retrieved (all rows = a negative number) 398 * @return 399 * @throws RuntimeDaoException 400 */ 401 public List<B> loadByWhereAsList(String where, int[] fieldList, int startRow, int numRows)throws RuntimeDaoException; 402 403 //9-3 404 /** 405 * Retrieves each row of B bean given a sql where clause and a list of fields, and startRow and numRows, 406 * and dealt wity action 407 * It is up to you to pass the 'WHERE' in your where clauses. 408 * 409 * @param where the sql 'where' clause 410 * @param fieldList table of the field's associated constants 411 * @param startRow the start row to be used (first row = 1, last row = -1) 412 * @param numRows the number of rows to be retrieved (all rows = a negative number) 413 * @param action Action object for do something(not null) 414 * @return the count dealt by action 415 * @throws RuntimeDaoException 416 */ 417 public int loadByWhereForAction(String where, int[] fieldList, int startRow, int numRows,Action<B> action)throws RuntimeDaoException; 418 419 //_____________________________________________________________________ 420 // 421 // USING TEMPLATE 422 //_____________________________________________________________________ 423 //18 424 /** 425 * Loads a unique B bean from a template one giving a c 426 * 427 * @param bean the B bean to look for 428 * @return the bean matching the template,or {@code null} if not found or null input argument 429 * @throws ObjectRetrievalException more than one row 430 * @throws RuntimeDaoException 431 */ 432 public B loadUniqueUsingTemplate(B bean)throws RuntimeDaoException; 433 434 //18-1 435 /** 436 * Loads a unique B bean from a template one giving a c 437 * 438 * @param bean the B bean to look for 439 * @return the bean matching the template 440 * @throws ObjectRetrievalException not found or more than one row 441 * @throws RuntimeDaoException 442 */ 443 public B loadUniqueUsingTemplateChecked(B bean)throws RuntimeDaoException,ObjectRetrievalException; 444 445 //19 446 /** 447 * Loads an array of B from a template one. 448 * 449 * @param bean the B bean template to look for 450 * @return all the B beans matching the template 451 * @throws RuntimeDaoException 452 */ 453 public B[] loadUsingTemplate(B bean)throws RuntimeDaoException; 454 455 //19-1 456 /** 457 * Loads each row from a template one and dealt with action. 458 * 459 * @param bean the B bean template to look for 460 * @param action Action object for do something(not null) 461 * @return the count dealt by action 462 * @throws RuntimeDaoException 463 */ 464 public int loadUsingTemplate(B bean,Action<B> action)throws RuntimeDaoException; 465 466 //20 467 /** 468 * Loads an array of B bean from a template one, given the start row and number of rows. 469 * 470 * @param bean the B bean template to look for 471 * @param startRow the start row to be used (first row = 1, last row=-1) 472 * @param numRows the number of rows to be retrieved (all rows = a negative number) 473 * @return all the B matching the template 474 * @throws RuntimeDaoException 475 */ 476 public B[] loadUsingTemplate(B bean, int startRow, int numRows)throws RuntimeDaoException; 477 478 //20-1 479 /** 480 * Loads each row from a template one, given the start row and number of rows and dealt with action. 481 * 482 * @param bean the B bean template to look for 483 * @param startRow the start row to be used (first row = 1, last row=-1) 484 * @param numRows the number of rows to be retrieved (all rows = a negative number) 485 * @param action Action object for do something(not null) 486 * @return the count dealt by action 487 * @throws RuntimeDaoException 488 */ 489 public int loadUsingTemplate(B bean, int startRow, int numRows,Action<B> action)throws RuntimeDaoException; 490 491 //20-5 492 /** 493 * Loads each row from a template one, given the start row and number of rows and dealt with action. 494 * 495 * @param bean the B template to look for 496 * @param fieldList table of the field's associated constants 497 * @param startRow the start row to be used (first row = 1, last row=-1) 498 * @param numRows the number of rows to be retrieved (all rows = a negative number) 499 * @param searchType exact ? like ? starting like ? ending link ? <br> 500 * {@value Constant#SEARCH_EXACT} {@link Constant#SEARCH_EXACT} <br> 501 * {@value Constant#SEARCH_LIKE} {@link Constant#SEARCH_LIKE} <br> 502 * {@value Constant#SEARCH_STARTING_LIKE} {@link Constant#SEARCH_STARTING_LIKE} <br> 503 * {@value Constant#SEARCH_ENDING_LIKE} {@link Constant#SEARCH_ENDING_LIKE} <br> 504 * @param action Action object for do something(not null) 505 * @return the count dealt by action 506 * @throws RuntimeDaoException 507 */ 508 public int loadUsingTemplate(B bean, int[] fieldList, int startRow, int numRows,int searchType, Action<B> action)throws RuntimeDaoException; 509 //20-4 510 /** 511 * Loads a list of B bean from a template one, given the start row and number of rows. 512 * 513 * @param bean the B bean template to look for 514 * @param startRow the start row to be used (first row = 1, last row=-1) 515 * @param numRows the number of rows to be retrieved (all rows = a negative number) 516 * @param searchType exact ? like ? starting like ? ending link ? <br> 517 * {@value Constant#SEARCH_EXACT} {@link Constant#SEARCH_EXACT} <br> 518 * {@value Constant#SEARCH_LIKE} {@link Constant#SEARCH_LIKE} <br> 519 * {@value Constant#SEARCH_STARTING_LIKE} {@link Constant#SEARCH_STARTING_LIKE} <br> 520 * {@value Constant#SEARCH_ENDING_LIKE} {@link Constant#SEARCH_ENDING_LIKE} <br> 521 * @return all the B bean matching the template 522 * @throws RuntimeDaoException 523 */ 524 public B[] loadUsingTemplate(B bean, int startRow, int numRows, int searchType)throws RuntimeDaoException; 525 526 //19-2 527 /** 528 * Loads a list of B bean from a template one. 529 * 530 * @param bean the B bean template to look for 531 * @return all the B beans matching the template 532 * @throws RuntimeDaoException 533 */ 534 public List<B> loadUsingTemplateAsList(B bean)throws RuntimeDaoException; 535 536 //20-2 537 /** 538 * Loads a list of B bean from a template one, given the start row and number of rows. 539 * 540 * @param bean the B bean template to look for 541 * @param startRow the start row to be used (first row = 1, last row=-1) 542 * @param numRows the number of rows to be retrieved (all rows = a negative number) 543 * @return all the B bean matching the template 544 * @throws RuntimeDaoException 545 */ 546 public List<B> loadUsingTemplateAsList(B bean, int startRow, int numRows)throws RuntimeDaoException; 547 548 //20-3 549 /** 550 * Loads an array of B bean from a template one, given the start row and number of rows. 551 * 552 * @param bean the B bean template to look for 553 * @param startRow the start row to be used (first row = 1, last row=-1) 554 * @param numRows the number of rows to be retrieved (all rows = a negative number) 555 * @param searchType exact ? like ? starting like ? ending link? <br> 556 * {@value Constant#SEARCH_EXACT} {@link Constant#SEARCH_EXACT} <br> 557 * {@value Constant#SEARCH_LIKE} {@link Constant#SEARCH_LIKE} <br> 558 * {@value Constant#SEARCH_STARTING_LIKE} {@link Constant#SEARCH_STARTING_LIKE} <br> 559 * {@value Constant#SEARCH_ENDING_LIKE} {@link Constant#SEARCH_ENDING_LIKE} <br> 560 * @return all the B beans matching the template 561 * @throws RuntimeDaoException 562 */ 563 public List<B> loadUsingTemplateAsList(B bean, int startRow, int numRows, int searchType)throws RuntimeDaoException; 564 565 //_____________________________________________________________________ 566 // 567 // LISTENER 568 //_____________________________________________________________________ 569 570 //35 571 /** 572 * Registers a unique {@link TableListener} listener.<br> 573 * do nothing if {@code TableListener} instance exists 574 * @param listener 575 */ 576 public void registerListener(TableListener<B> listener); 577 578 //36 579 /** 580 * remove listener. 581 * @param listener 582 */ 583 public void unregisterListener(TableListener<B> listener); 584 585 //_____________________________________________________________________ 586 // 587 // SAVE 588 //_____________________________________________________________________ 589 //12 590 /** 591 * Saves the B bean into the database. 592 * 593 * @param bean the B bean to be saved 594 * @return the inserted or updated bean,or null if bean is null 595 * @throws RuntimeDaoException 596 */ 597 public B save(B bean)throws RuntimeDaoException; 598 599 //15 600 /** 601 * Saves an array of B bean into the database. 602 * 603 * @param beans the array of B bean to be saved 604 * @return always beans saved 605 * @throws RuntimeDaoException 606 */ 607 public B[] save(B[] beans)throws RuntimeDaoException; 608 609 //15-2 610 /** 611 * Saves a collection of B bean into the database. 612 * 613 * @param beans the B bean table to be saved 614 * @return alwarys beans saved 615 * @throws RuntimeDaoException 616 */ 617 public <C extends Collection<B>> C saveAsTransaction(C beans)throws RuntimeDaoException; 618 619 //15-3 620 /** 621 * Saves an array of B bean into the database as transaction. 622 * 623 * @param beans the B bean table to be saved 624 * @return alwarys beans saved 625 * @see #save(BaseBean[]) 626 * @throws RuntimeDaoException 627 */ 628 public B[] saveAsTransaction(B[] beans)throws RuntimeDaoException; 629 630 //15-4 631 /** 632 * Saves a collection of B bean into the database as transaction. 633 * 634 * @param beans the B bean table to be saved 635 * @return alwarys beans saved 636 * @throws RuntimeDaoException 637 */ 638 public <C extends Collection<B>> C save(C beans)throws RuntimeDaoException; 639 640 /** 641 * Load column from table. 642 * @param column column name or java file name of B 643 * @param distinct select distinct values 644 * @param where the sql 'where' clause 645 * @param startRow the start row to be used (first row = 1, last row = -1) 646 * @param numRows the number of rows to be retrieved (all rows = a negative number) 647 * @return an list of column 648 * @throws RuntimeDaoException 649 */ 650 public <T>List<T> loadColumnAsList(String column,boolean distinct,String where,int startRow,int numRows)throws RuntimeDaoException; 651 652}