KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > FetchStatement


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: FetchStatement.java,v 1.2 2002/10/17 21:00:55 pierreg0 Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16
17 class FetchStatement
18 {
19     protected final ClassBaseTable mainTable;
20     protected final ArrayList JavaDoc selected = new ArrayList JavaDoc();
21     protected final ArrayList JavaDoc supertables = new ArrayList JavaDoc();
22
23     protected StringBuffer JavaDoc conditionList = new StringBuffer JavaDoc();
24
25     public FetchStatement(ClassBaseTable mainTable)
26     {
27         this.mainTable = mainTable;
28     }
29
30     public int select(Column col)
31     {
32         String JavaDoc columnID = referenceColumn(col);
33
34         if (!selected.contains(columnID))
35             selected.add(columnID);
36
37         return selected.indexOf(columnID) + 1;
38     }
39
40     public String JavaDoc referenceColumn(Column col)
41     {
42         ClassBaseTable t = (ClassBaseTable)col.getTable();
43
44         if (!t.equals(mainTable) && !supertables.contains(t))
45             supertables.add(t);
46
47         return t.getName().toString() + '.' + col.getName().toString();
48     }
49
50     public void andCondition(String JavaDoc condition)
51     {
52         if (conditionList.length() > 0)
53             conditionList.append(" AND ");
54
55         conditionList.append('(').append(condition).append(')');
56     }
57
58     public String JavaDoc toString()
59     {
60         StringBuffer JavaDoc stmt = new StringBuffer JavaDoc("SELECT ");
61
62         Iterator JavaDoc i = selected.iterator();
63
64         while (i.hasNext())
65         {
66             stmt.append(i.next());
67
68             if (i.hasNext())
69                 stmt.append(',');
70         }
71
72         SQLIdentifier mainTableName = mainTable.getName();
73         String JavaDoc mainTableColumnID = referenceColumn(mainTable.getIDMapping().getColumn());
74         StringBuffer JavaDoc joinConditions = new StringBuffer JavaDoc();
75
76         stmt.append(" FROM ").append(mainTableName);
77
78         i = supertables.iterator();
79
80         while (i.hasNext())
81         {
82             ClassBaseTable supertable = (ClassBaseTable)i.next();
83             String JavaDoc supertableColumnID = referenceColumn(supertable.getIDMapping().getColumn());
84
85             stmt.append(',').append(supertable.getName());
86
87             if (joinConditions.length() > 0)
88                 joinConditions.append(" AND ");
89                 
90             joinConditions.append(mainTableColumnID).append(" = ").append(supertableColumnID);
91         }
92
93         if (joinConditions.length() > 0)
94         {
95             if (conditionList.length() > 0)
96                 conditionList = joinConditions.append(" AND (").append(conditionList).append(')');
97             else
98                 conditionList = joinConditions;
99         }
100
101         if (conditionList.length() > 0)
102             stmt.append(" WHERE ").append(conditionList);
103
104         return stmt.toString();
105     }
106 }
107
Popular Tags