1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * LIKE文中检索_、%等特殊字符串时的处理 */ public class CMM_COB_Utils { public static String escapeWildcard( String pCondition ) { if(String.isBlank(pCondition)){ return pCondition; } pCondition = pCondition.replace('%', '%'); pCondition = pCondition.replace('_', '_'); pCondition = pCondition.replace('%', '\\%'); pCondition = pCondition.replace('_', '\\_'); return pCondition; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// 動的SOQLにおいてはescapeSingleQuotesメソッドの利用とワイルドカード対策 String searchKey = 'search%'; searchKey = String.escapeSingleQuotes(searchKey); searchKey = CMM_COB_Utils.escapeWildcard(searchKey); String query = 'select id , ModuleID__c from BatchLogInfo__c where ModuleID__c like \'%'+searchKey+'%\''; list<BatchLogInfo__c> BatchLogInfo_list = database.query(query); System.debug('size'+BatchLogInfo_list.size()); for(BatchLogInfo__c item : BatchLogInfo_list){ System.debug('00000 '+item.ModuleID__c); } // 静的SOQLにおいてはワイルドカード対 String searchKey002 = 'search%'; searchKey002 = CMM_COB_Utils.escapeWildcard(searchKey002); searchKey002 = '%' + searchKey002 + '%'; list<BatchLogInfo__c> BatchLogInfo_list002 = [select id , ModuleID__c from BatchLogInfo__c where ModuleID__c like : searchKey002]; System.debug('size'+BatchLogInfo_list002.size()); for(BatchLogInfo__c item : BatchLogInfo_list002){ System.debug('11111 '+item.ModuleID__c); } |