My favorites | Sign in
Project Home Downloads Wiki Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* ============================================================
* This code is part of the "apex-lang" open source project avaiable at:
*
* http://code.google.com/p/apex-lang/
*
* This code is licensed under the Apache License, Version 2.0. You may obtain a
* copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
* ============================================================
*/
global class DatabaseUtils {

global final static List<String> NULL_LIST_OF_STRINGS = null;
global final static Set<String> NULL_SET_OF_STRINGS = null;

global static SObject query(String id){
return first(query(new Set<String>{id}, NULL_SET_OF_STRINGS));
}

global static SObject query(String id, List<String> fields){
return first(query(new Set<String>{id}, fields));
}

global static SObject query(String id, Set<String> fields){
return first(query(new Set<String>{id}, fields));
}

global static List<SObject> query(List<String> ids){
return query(ids, NULL_SET_OF_STRINGS);
}

global static List<SObject> query(Set<String> ids){
return query(ids, NULL_SET_OF_STRINGS);
}

global static List<SObject> query(List<String> ids, List<String> fields){
return query(SetUtils.listToSet(ids), SetUtils.listToSet(fields));
}

global static List<SObject> query(List<String> ids, Set<String> fields){
return query(SetUtils.listToSet(ids), fields);
}

global static List<SObject> query(Set<String> ids, List<String> fields){
return query(ids, SetUtils.listToSet(fields));
}

global static List<SObject> query(Set<String> ids, Set<String> fields){
if(ids == null || ids.size() <= 0){
throw new IllegalArgumentException('ids argument must not be empty: ' + ids);
}
final Set<String> distinctKeyPrefixes = left(trim(ids),3);
if(distinctKeyPrefixes.size() > 1){
//ruh-roh, someone mixed types
throw new IllegalArgumentException('ids argument invalid: multiple SObject types detected: ' + distinctKeyPrefixes);
}
final SoqlBuilder soql =
new SoqlBuilder()
.fromx(retrieveObjectName(SetUtils.setToList(distinctKeyPrefixes).get(0)))
.wherex(new SetCondition('id').inx(SetUtils.setToList(ids)));
if(fields == null || fields.size() <= 0){
soql.selectAll();
} else {
soql.selectx(fields);
}
return Database.query(soql.toSoql());
}

global static String retrieveObjectName(String recordId) {
String returnValue = null;
final String keyPrefixToMatch = StringUtils.left(recordId,3);
String keyPrefix = null;
final Map<String,Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
for(String objectNameKey : globalDescribe.keySet()){
keyPrefix = globalDescribe.get(objectNameKey).getDescribe().getKeyPrefix();
if(StringUtils.isNotBlank(keyPrefix) && StringUtils.equals(keyPrefix,keyPrefixToMatch)){
returnValue = objectNameKey;
break;
}
}
if(StringUtils.isBlank(returnValue)){
throw new IllegalArgumentException('ids argument invalid: key prefix not found: ' + keyPrefixToMatch);
}
return returnValue;
}

global static Set<String> left(Set<String> strs, Integer len) {
Set<String> returnValue = null;
if(strs != null || strs.size() > 0){
returnValue = new Set<String>();
for(String str : strs){
returnValue.add(StringUtils.left(str,len));
}
}
return returnValue;
}

global static Set<String> trim(Set<String> strs) {
Set<String> returnValue = null;
if(strs != null || strs.size() > 0){
returnValue = new Set<String>();
for(String str : strs){
returnValue.add(StringUtils.trim(str));
}
}
return returnValue;
}

global static SObject first(List<SObject> records){
SObject returnValue = null;
if(records != null && records.size() > 0){
returnValue = records.get(0);
}
return returnValue;
}


}

Change log

r117 by Richard.Vanhook on Apr 13, 2011   Diff
Updates
Go to: 
Project members, sign in to write a code review

Older revisions

r116 by Richard.Vanhook on Feb 22, 2011   Diff
Draft version 1.17
r86 by Richard.Vanhook on Jan 24, 2011   Diff
Added DatabaseUtils - a mechanism for
loading any record based on ID
All revisions of this file

File info

Size: 4322 bytes, 119 lines
Powered by Google Project Hosting