| | | |
Offset 11, 15 lines modified | Offset 11, 14 lines modified |
11 | import·java.util.function.Function; | 11 | import·java.util.function.Function; |
12 | import·com.android.providers.media.DatabaseHelper; | 12 | import·com.android.providers.media.DatabaseHelper; |
13 | import·java.util.Arrays; | 13 | import·java.util.Arrays; |
14 | import·java.util.Set; | 14 | import·java.util.Set; |
15 | import·android.database.Cursor; | 15 | import·android.database.Cursor; |
16 | import·android.os.CancellationSignal; | 16 | import·android.os.CancellationSignal; |
17 | import·android.database.sqlite.SQLiteDatabase; | 17 | import·android.database.sqlite.SQLiteDatabase; |
18 | import·com.google.common.base.Strings; | |
19 | import·android.util.ArrayMap; | 18 | import·android.util.ArrayMap; |
20 | import·java.util.Objects; | 19 | import·java.util.Objects; |
21 | import·android.content.ContentValues; | 20 | import·android.content.ContentValues; |
22 | import·java.util.Iterator; | 21 | import·java.util.Iterator; |
23 | import·java.util.regex.Matcher; | 22 | import·java.util.regex.Matcher; |
24 | import·android.util.Log; | 23 | import·android.util.Log; |
25 | import·java.util.Locale; | 24 | import·java.util.Locale; |
Offset 28, 15 lines modified | Offset 27, 14 lines modified |
28 | import·java.util.Collection; | 27 | import·java.util.Collection; |
29 | import·java.util.regex.Pattern; | 28 | import·java.util.regex.Pattern; |
| |
30 | public·class·SQLiteQueryBuilder | 29 | public·class·SQLiteQueryBuilder |
31 | { | 30 | { |
32 | ····private·static·final·String·GET_ID_FOR_INSERT_CLAUSE·=·"_GET_ID(?)"; | 31 | ····private·static·final·String·GET_ID_FOR_INSERT_CLAUSE·=·"_GET_ID(?)"; |
33 | ····private·static·final·String·GET_ID_FOR_UPDATE_CLAUSE·=·"ifnull(_GET_ID(?),·_id)"; | 32 | ····private·static·final·String·GET_ID_FOR_UPDATE_CLAUSE·=·"ifnull(_GET_ID(?),·_id)"; |
34 | ····public·static·final·String·ROWID_COLUMN·=·"rowid"; | |
35 | ····private·static·final·int·STRICT_COLUMNS·=·2; | 33 | ····private·static·final·int·STRICT_COLUMNS·=·2; |
36 | ····private·static·final·int·STRICT_GRAMMAR·=·4; | 34 | ····private·static·final·int·STRICT_GRAMMAR·=·4; |
37 | ····private·static·final·int·STRICT_PARENTHESES·=·1; | 35 | ····private·static·final·int·STRICT_PARENTHESES·=·1; |
38 | ····private·static·final·String·TAG·=·"SQLiteQueryBuilder"; | 36 | ····private·static·final·String·TAG·=·"SQLiteQueryBuilder"; |
39 | ····private·static·final·Pattern·sAggregationPattern; | 37 | ····private·static·final·Pattern·sAggregationPattern; |
40 | ····private·static·final·Pattern·sCustomCollatorPattern; | 38 | ····private·static·final·Pattern·sCustomCollatorPattern; |
41 | ····private·static·final·Pattern·sPattern154193772; | 39 | ····private·static·final·Pattern·sPattern154193772; |
Offset 310, 18 lines modified | Offset 308, 14 lines modified |
310 | ········final·StringBuilder·sb·=·new·StringBuilder(); | 308 | ········final·StringBuilder·sb·=·new·StringBuilder(); |
311 | ········sb.append("Invalid·token·"); | 309 | ········sb.append("Invalid·token·"); |
312 | ········sb.append(string); | 310 | ········sb.append(string); |
313 | ········string·=·sb.toString(); | 311 | ········string·=·sb.toString(); |
314 | ········throw·new·IllegalArgumentException(string); | 312 | ········throw·new·IllegalArgumentException(string); |
315 | ····} | 313 | ····} |
316 | ···· | 314 | ···· |
317 | ····private·static·boolean·hasUserWhere(final·String·s)·{ | |
318 | ········return·Strings.isNullOrEmpty(s)·^·true; | |
319 | ····} | |
320 | ···· | |
321 | ····private·boolean·isCustomCollator(final·String·s)·{ | 315 | ····private·boolean·isCustomCollator(final·String·s)·{ |
322 | ········return·SQLiteQueryBuilder.sCustomCollatorPattern.matcher(s).matches(); | 316 | ········return·SQLiteQueryBuilder.sCustomCollatorPattern.matcher(s).matches(); |
323 | ····} | 317 | ····} |
324 | ···· | 318 | ···· |
325 | ····private·boolean·isTableOrColumn(final·String·s)·{ | 319 | ····private·boolean·isTableOrColumn(final·String·s)·{ |
326 | ········final·boolean·equals·=·this.mTables.equals(s); | 320 | ········final·boolean·equals·=·this.mTables.equals(s); |
327 | ········boolean·b·=·true; | 321 | ········boolean·b·=·true; |
Offset 342, 34 lines modified | Offset 336, 29 lines modified |
342 | ············sb.append(s2); | 336 | ············sb.append(s2); |
343 | ············sb.append(")"); | 337 | ············sb.append(")"); |
344 | ············return·sb.toString(); | 338 | ············return·sb.toString(); |
345 | ········} | 339 | ········} |
346 | ········return·s2; | 340 | ········return·s2; |
347 | ····} | 341 | ····} |
348 | ···· | 342 | ···· |
| 343 | ····private·static·boolean·shouldAppendRowId(final·ContentValues·contentValues)·{ |
| 344 | ········return·!contentValues.containsKey("_id")·&&·contentValues.containsKey("_data"); |
| 345 | ····} |
| 346 | ···· |
349 | ····private·String·wrap(final·String·s)·{ | 347 | ····private·String·wrap(final·String·s)·{ |
350 | ········if·(TextUtils.isEmpty((CharSequence)s))·{ | 348 | ········if·(TextUtils.isEmpty((CharSequence)s))·{ |
351 | ············return·s; | 349 | ············return·s; |
352 | ········} | 350 | ········} |
353 | ········final·StringBuilder·sb·=·new·StringBuilder(); | 351 | ········final·StringBuilder·sb·=·new·StringBuilder(); |
354 | ········sb.append("("); | 352 | ········sb.append("("); |
355 | ········sb.append(s); | 353 | ········sb.append(s); |
356 | ········sb.append(")"); | 354 | ········sb.append(")"); |
357 | ········return·sb.toString(); | 355 | ········return·sb.toString(); |
358 | ····} | 356 | ····} |
359 | ···· | 357 | ···· |
360 | ····public·void·allowRowidColumn()·{ | |
361 | ········if·(this.mProjectionMap·==·null)·{ | |
362 | ············this.mProjectionMap·=·(Map)new·ArrayMap(); | |
363 | ········} | |
364 | ········final·Map·mProjectionMap·=·this.mProjectionMap; | |
365 | ········final·String·s·=·"rowid"; | |
366 | ········mProjectionMap.put(s,·s); | |
367 | ····} | |
368 | ···· | |
369 | ····public·void·appendWhere(final·CharSequence·charSequence)·{ | 358 | ····public·void·appendWhere(final·CharSequence·charSequence)·{ |
370 | ········if·(this.mWhereClause·==·null)·{ | 359 | ········if·(this.mWhereClause·==·null)·{ |
371 | ············this.mWhereClause·=·new·StringBuilder(charSequence.length()·+·16); | 360 | ············this.mWhereClause·=·new·StringBuilder(charSequence.length()·+·16); |
372 | ········} | 361 | ········} |
373 | ········this.mWhereClause.append(charSequence); | 362 | ········this.mWhereClause.append(charSequence); |
374 | ····} | 363 | ····} |
375 | ···· | 364 | ···· |
Offset 431, 15 lines modified | Offset 420, 15 lines modified |
431 | ············} | 420 | ············} |
432 | ············if·(equals)·{ | 421 | ············if·(equals)·{ |
433 | ················sb.append(c); | 422 | ················sb.append(c); |
434 | ················sb.append(s2); | 423 | ················sb.append(s2); |
435 | ················sb.append(c); | 424 | ················sb.append(c); |
436 | ················sb.append(s); | 425 | ················sb.append(s); |
437 | ············} | 426 | ············} |
438 | ············if·(this.shouldAppendRowId(contentValues))·{ | 427 | ············if·(shouldAppendRowId(contentValues))·{ |
439 | ················sb.append(c); | 428 | ················sb.append(c); |
440 | ················sb.append("_id"); | 429 | ················sb.append("_id"); |
441 | ············} | 430 | ············} |
442 | ············sb.append(")·VALUES·("); | 431 | ············sb.append(")·VALUES·("); |
443 | ············for·(int·i·=·0;·i·<·values.size();·++i)·{ | 432 | ············for·(int·i·=·0;·i·<·values.size();·++i)·{ |
444 | ················if·(i·>·0)·{ | 433 | ················if·(i·>·0)·{ |
445 | ····················sb.append(c); | 434 | ····················sb.append(c); |
Offset 455, 15 lines modified | Offset 444, 15 lines modified |
455 | ················final·char·c3·=·')'; | 444 | ················final·char·c3·=·')'; |
456 | ················sb.append(c3); | 445 | ················sb.append(c3); |
457 | ················sb.append(c); | 446 | ················sb.append(c); |
458 | ················sb.append(c2); | 447 | ················sb.append(c2); |
459 | ················sb.append(s3); | 448 | ················sb.append(s3); |
460 | ················sb.append(c3); | 449 | ················sb.append(c3); |
461 | ············} | 450 | ············} |
462 | ············if·(this.shouldAppendRowId(contentValues))·{ | 451 | ············if·(shouldAppendRowId(contentValues))·{ |
463 | ················sb.append(c); | 452 | ················sb.append(c); |
464 | ················sb.append(DatabaseUtils.bindSelection("_GET_ID(?)",·new·Object[]·{·contentValues.getAsString("_data")·})); | 453 | ················sb.append(DatabaseUtils.bindSelection("_GET_ID(?)",·new·Object[]·{·contentValues.getAsString("_data")·})); |
465 | ············} | 454 | ············} |
466 | ············sb.append(")"); | 455 | ············sb.append(")"); |
467 | ············return·sb.toString(); | 456 | ············return·sb.toString(); |
468 | ········} | 457 | ········} |
469 | ········throw·new·IllegalArgumentException("Empty·values"); | 458 | ········throw·new·IllegalArgumentException("Empty·values"); |
Offset 506, 15 lines modified | Offset 495, 15 lines modified |
506 | ················sb.append(c); | 495 | ················sb.append(c); |
507 | ················sb.append(s2); | 496 | ················sb.append(s2); |
508 | ················sb.append(c2); | 497 | ················sb.append(c2); |
509 | ················sb.append('('); | 498 | ················sb.append('('); |
510 | ················sb.append("SELECT·generation·FROM·local_metadata"); | 499 | ················sb.append("SELECT·generation·FROM·local_metadata"); |
511 | ················sb.append(')'); | 500 | ················sb.append(')'); |
512 | ············} | 501 | ············} |
513 | ············if·(this.shouldAppendRowId(contentValues))·{ | 502 | ············if·(shouldAppendRowId(contentValues))·{ |
514 | ················sb.append(c); | 503 | ················sb.append(c); |
515 | ················sb.append("_id"); | 504 | ················sb.append("_id"); |
516 | ················sb.append(c2); | 505 | ················sb.append(c2); |
517 | ················sb.append(DatabaseUtils.bindSelection("ifnull(_GET_ID(?),·_id)",·new·Object[]·{·contentValues.getAsString("_data")·})); | 506 | ················sb.append(DatabaseUtils.bindSelection("ifnull(_GET_ID(?),·_id)",·new·Object[]·{·contentValues.getAsString("_data")·})); |
518 | ············} | 507 | ············} |
519 | ············appendClause(sb,·"·WHERE·",·this.computeWhere(s)); | 508 | ············appendClause(sb,·"·WHERE·",·this.computeWhere(s)); |
520 | ············return·sb.toString(); | 509 | ············return·sb.toString(); |
Offset 577, 15 lines modified | Offset 566, 15 lines modified |
577 | ····public·int·delete(final·SQLiteDatabase·sqLiteDatabase,·String·wrap,·final·String[]·array)·{ | 566 | ····public·int·delete(final·SQLiteDatabase·sqLiteDatabase,·String·wrap,·final·String[]·array)·{ |
578 | ········Objects.requireNonNull(this.mTables,·"No·tables·defined"); | 567 | ········Objects.requireNonNull(this.mTables,·"No·tables·defined"); |
579 | ········Objects.requireNonNull(sqLiteDatabase,·"No·database·defined"); | 568 | ········Objects.requireNonNull(sqLiteDatabase,·"No·database·defined"); |
580 | ········String·s·=·this.buildDelete(wrap); | 569 | ········String·s·=·this.buildDelete(wrap); |
581 | ········if·(this.isStrictGrammar())·{ | 570 | ········if·(this.isStrictGrammar())·{ |
582 | ············this.enforceStrictGrammar(wrap,·null,·null,·null,·null); | 571 | ············this.enforceStrictGrammar(wrap,·null,·null,·null,·null); |
583 | ········} | 572 | ········} |
584 | ········if·(this.isStrict()·&&·hasUserWhere(wrap))·{ | 573 | ········if·(this.isStrict())·{ |
585 | ············sqLiteDatabase.validateSql(s,·(CancellationSignal)null); | 574 | ············sqLiteDatabase.validateSql(s,·(CancellationSignal)null); |
586 | ············wrap·=·this.wrap(wrap); | 575 | ············wrap·=·this.wrap(wrap); |
587 | ············s·=·this.buildDelete(wrap); | 576 | ············s·=·this.buildDelete(wrap); |
588 | ········} | 577 | ········} |
589 | ········wrap·=·"SQLiteQueryBuilder"; | 578 | ········wrap·=·"SQLiteQueryBuilder"; |
590 | ········if·(Log.isLoggable(wrap,·3))·{ | 579 | ········if·(Log.isLoggable(wrap,·3))·{ |
591 | ············if·(Logging.IS_DEBUGGABLE)·{ | 580 | ············if·(Logging.IS_DEBUGGABLE)·{ |
Offset 688, 15 lines modified | Offset 677, 15 lines modified |
688 | ········String·s6·=·this.buildQuery(array,·s,·s2,·s3,·s4,·s5); | 677 | ········String·s6·=·this.buildQuery(array,·s,·s2,·s3,·s4,·s5); |
689 | ········if·(this.isStrictColumns())·{ | 678 | ········if·(this.isStrictColumns())·{ |
690 | ············this.enforceStrictColumns(array); | 679 | ············this.enforceStrictColumns(array); |
691 | ········} | 680 | ········} |
692 | ········if·(this.isStrictGrammar())·{ | 681 | ········if·(this.isStrictGrammar())·{ |
693 | ············this.enforceStrictGrammar(s,·s2,·s3,·s4,·s5); | 682 | ············this.enforceStrictGrammar(s,·s2,·s3,·s4,·s5); |
694 | ········} | 683 | ········} |
695 | ········if·(this.isStrict()·&&·hasUserWhere(s))·{ | 684 | ········if·(this.isStrict())·{ |
696 | ············sqLiteDatabase.validateSql(s6,·cancellationSignal); | 685 | ············sqLiteDatabase.validateSql(s6,·cancellationSignal); |
697 | ············s6·=·this.buildQuery(array,·this.wrap(s),·s2,·this.wrap(s3),·s4,·s5); | 686 | ············s6·=·this.buildQuery(array,·this.wrap(s),·s2,·this.wrap(s3),·s4,·s5); |
698 | ········} | 687 | ········} |
699 | ········final·String·s7·=·"SQLiteQueryBuilder"; | 688 | ········final·String·s7·=·"SQLiteQueryBuilder"; |
700 | ········if·(Log.isLoggable(s7,·3))·{ | 689 | ········if·(Log.isLoggable(s7,·3))·{ |
701 | ············if·(Logging.IS_DEBUGGABLE)·{ | 690 | ············if·(Logging.IS_DEBUGGABLE)·{ |
702 | ················final·StringBuilder·sb·=·new·StringBuilder(); | 691 | ················final·StringBuilder·sb·=·new·StringBuilder(); |
Offset 771, 37 lines modified | Offset 760, 26 lines modified |
771 | ········this.mTables·=·mTables; | 760 | ········this.mTables·=·mTables; |
772 | ····} | 761 | ····} |
773 | ···· | 762 | ···· |
774 | ····public·void·setTargetSdkVersion(final·int·mTargetSdkVersion)·{ | 763 | ····public·void·setTargetSdkVersion(final·int·mTargetSdkVersion)·{ |
775 | ········this.mTargetSdkVersion·=·mTargetSdkVersion; | 764 | ········this.mTargetSdkVersion·=·mTargetSdkVersion; |
776 | ····} | 765 | ····} |
777 | ···· | 766 | ···· |
778 | ····boolean·shouldAppendRowId(final·ContentValues·contentValues)·{ | |
779 | ········final·Map·mProjectionMap·=·this.mProjectionMap; | |
780 | ········boolean·b·=·true; | |
781 | ········final·String·s·=·"_id"; | |
782 | ········final·boolean·b2·=·mProjectionMap·!=·null·&&·mProjectionMap.containsKey(s)·&&·TextUtils.equals((CharSequence)this.mProjectionMap.get(s),·(CharSequence)s)·&&·b; | |
783 | ········if·(contentValues.containsKey(s)·||·!contentValues.containsKey("_data")·||·!b2)·{ | |
784 | ············b·=·false; | |
785 | ········} | |
786 | ········return·b; | |
787 | ····} | |
788 | ···· | |
789 | ····public·int·update(final·SQLiteDatabase·sqLiteDatabase,·final·ContentValues·contentValues,·String·s,·String[]·array)·{ | 767 | ····public·int·update(final·SQLiteDatabase·sqLiteDatabase,·final·ContentValues·contentValues,·String·s,·String[]·array)·{ |
790 | ········Objects.requireNonNull(this.mTables,·"No·tables·defined"); | 768 | ········Objects.requireNonNull(this.mTables,·"No·tables·defined"); |
791 | ········Objects.requireNonNull(sqLiteDatabase,·"No·database·defined"); | 769 | ········Objects.requireNonNull(sqLiteDatabase,·"No·database·defined"); |
792 | ········Objects.requireNonNull(contentValues,·"No·values·defined"); | 770 | ········Objects.requireNonNull(contentValues,·"No·values·defined"); |
793 | ········String·s2·=·this.buildUpdate(contentValues,·s); | 771 | ········String·s2·=·this.buildUpdate(contentValues,·s); |
794 | ········if·(this.isStrictColumns())·{ | 772 | ········if·(this.isStrictColumns())·{ |
795 | ············this.enforceStrictColumns(contentValues); | 773 | ············this.enforceStrictColumns(contentValues); |
796 | ········} | 774 | ········} |
797 | ········if·(this.isStrictGrammar())·{ | 775 | ········if·(this.isStrictGrammar())·{ |
798 | ············this.enforceStrictGrammar(s,·null,·null,·null,·null); | 776 | ············this.enforceStrictGrammar(s,·null,·null,·null,·null); |
799 | ········} | 777 | ········} |
800 | ········if·(this.isStrict()·&&·hasUserWhere(s))·{ | 778 | ········if·(this.isStrict())·{ |
801 | ············sqLiteDatabase.validateSql(s2,·(CancellationSignal)null); | 779 | ············sqLiteDatabase.validateSql(s2,·(CancellationSignal)null); |
802 | ············s·=·this.wrap(s); | 780 | ············s·=·this.wrap(s); |
803 | ············s2·=·this.buildUpdate(contentValues,·s); | 781 | ············s2·=·this.buildUpdate(contentValues,·s); |
804 | ········} | 782 | ········} |
805 | ········int·i·=·0; | 783 | ········int·i·=·0; |
806 | ········if·(array·==·null)·{ | 784 | ········if·(array·==·null)·{ |
807 | ············array·=·new·String[0]; | 785 | ············array·=·new·String[0]; |