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 package org.melati.poem.odmg;
45
46 import java.util.Iterator;
47 import java.util.Collection;
48 import java.util.Enumeration;
49
50 import org.melati.poem.Persistent;
51 import org.melati.poem.Table;
52
53
54
55
56
57 class PoemTableAsDCollection<P extends Persistent> implements org.odmg.DCollection {
58
59 private Table<P> _wrappedTable = null;
60
61 PoemTableAsDCollection(Table<P> aTable) {
62 _wrappedTable = aTable;
63 }
64
65 public boolean isEmpty() { return size() == 0; }
66 public int size() { return _wrappedTable.count(); }
67
68 @SuppressWarnings("unchecked")
69 public boolean add(Object obj) {
70 _wrappedTable.create((P)obj);
71 return true;
72 }
73
74 public boolean removeAll(@SuppressWarnings("rawtypes") Collection coll) {
75 Iterator<?> iter = coll.iterator();
76 while (iter.hasNext()) {
77 if (!remove(iter.next()))
78 return false;
79 }
80 return true;
81 }
82
83
84
85
86
87
88
89
90 public boolean remove(Object obj) {
91 @SuppressWarnings("unchecked")
92 P p = (P)obj;
93
94 Enumeration<Persistent> refs = _wrappedTable.getDatabase().referencesTo(p);
95 while (refs.hasMoreElements()) {
96 Persistent q = (Persistent)refs.nextElement();
97 q.deleteAndCommit();
98 }
99
100 p.deleteAndCommit();
101 return true;
102 }
103
104 public Iterator<P> iterator() {
105 return new EnumerationIterator<P>(_wrappedTable.selection());
106 }
107
108
109
110
111
112
113 public Iterator<P> select(String queryString) {
114 String lowerCaseQueryString = queryString.toLowerCase();
115 int whereStart = lowerCaseQueryString.indexOf("where ");
116 if (whereStart<0)
117 whereStart = 0;
118 else
119 whereStart += 6;
120
121 int orderByStart = lowerCaseQueryString.indexOf("order by ");
122 int whereEnd = 0;
123 if (orderByStart<0)
124 whereEnd = queryString.length();
125 else if (orderByStart==0) {
126 whereStart = -1;
127 orderByStart += 9;
128 }
129 else {
130 whereEnd = orderByStart;
131 orderByStart += 9;
132 }
133
134 String whereClause = "";
135 String orderByClause = "";
136 if (whereStart>=0)
137 whereClause = queryString.substring(whereStart,whereEnd);
138 if (orderByStart>=0)
139 orderByClause = queryString.substring(orderByStart,queryString.length());
140
141
142
143
144
145
146
147
148
149 return new EnumerationIterator<P>(
150 _wrappedTable.selection(whereClause,orderByClause,true));
151 }
152
153 public Object selectElement(String queryString) {
154 Iterator<P> iter = select(queryString);
155 if (iter.hasNext())
156 return iter.next();
157 return null;
158 }
159
160
161 public org.odmg.DCollection query(String queryString) {
162 throw new org.odmg.NotImplementedException();
163 }
164 public Object[] toArray(Object[] obj) {
165 throw new org.odmg.NotImplementedException();
166 }
167 public Object[] toArray() {
168 throw new org.odmg.NotImplementedException();
169 }
170 public boolean existsElement(String obj) {
171 throw new org.odmg.NotImplementedException();
172 }
173 public boolean contains(Object obj) {
174 throw new org.odmg.NotImplementedException();
175 }
176 public boolean addAll(@SuppressWarnings("rawtypes") Collection coll) {
177 throw new org.odmg.NotImplementedException();
178 }
179 public boolean containsAll(@SuppressWarnings("rawtypes") Collection coll) {
180 throw new org.odmg.NotImplementedException();
181 }
182
183 public boolean retainAll(@SuppressWarnings("rawtypes") Collection coll) {
184 throw new org.odmg.NotImplementedException();
185 }
186 public void clear() {
187 throw new org.odmg.NotImplementedException();
188 }
189
190
191
192 private class EnumerationIterator<T> implements Iterator<T> {
193 private Enumeration<T> _enum = null;
194 EnumerationIterator(Enumeration<T> en) {
195 _enum = en;
196 }
197
198 public boolean hasNext() { return _enum.hasMoreElements(); }
199 public T next() { return _enum.nextElement(); }
200 public void remove() { throw new org.odmg.NotImplementedException(); }
201 }
202
203
204 }