KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > exp > InlineSqlExp


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.exp;
13
14 import com.versant.core.util.CharBuf;
15 import com.versant.core.jdbc.sql.SqlDriver;
16
17 import java.util.Map JavaDoc;
18
19 import com.versant.core.common.BindingSupportImpl;
20
21 /**
22  * Inline SQL with parameter replacement for argument column names.
23  */

24 public class InlineSqlExp extends SqlExp {
25
26     private String JavaDoc template;
27
28     public InlineSqlExp(String JavaDoc template, SqlExp children) {
29         super(children);
30         this.template = template;
31     }
32
33     public InlineSqlExp() {
34     }
35
36     public SqlExp createInstance() {
37         return new InlineSqlExp();
38     }
39
40     public SqlExp getClone(SqlExp clone, Map JavaDoc cloneMap) {
41         super.getClone(clone, cloneMap);
42
43         ((InlineSqlExp) clone).template = template;
44
45         return clone;
46     }
47
48     /**
49      * Append SQL for this node to s.
50      *
51      * @param driver The driver being used
52      * @param s Append the SQL here
53      * @param leftSibling
54      */

55     public void appendSQLImp(SqlDriver driver, CharBuf s, SqlExp leftSibling) {
56         int n = template.length();
57         int state = 0;
58         for (int i = 0; i < n; i++) {
59             char c = template.charAt(i);
60             int pn;
61             switch (state) {
62                 case 0:
63                     if (c == '$')
64                         state = '$';
65                     else
66                         s.append(c);
67                     break;
68                 case '$':
69                     pn = 0;
70                     switch (c) {
71                         default:
72                             s.append('$');
73                         case '$':
74                             s.append(c);
75                             break;
76                         case '1':
77                             pn = 1;
78                             break;
79                         case '2':
80                             pn = 2;
81                             break;
82                         case '3':
83                             pn = 3;
84                             break;
85                         case '4':
86                             pn = 4;
87                             break;
88                         case '5':
89                             pn = 5;
90                             break;
91                         case '6':
92                             pn = 6;
93                             break;
94                         case '7':
95                             pn = 7;
96                             break;
97                         case '8':
98                             pn = 8;
99                             break;
100                         case '9':
101                             pn = 9;
102                             break;
103                         case '0':
104                             pn = 10;
105                             break;
106                     }
107                     if (pn > 0) get(pn).appendSQL(driver, s, null);
108                     state = 0;
109                     break;
110                 default:
111                     throw BindingSupportImpl.getInstance().internal("Unknown state: " + state);
112             }
113         }
114         if (state == '$') s.append('$');
115     }
116
117     /**
118      * Get the n'th entry in list with the first having index 1.
119      */

120     private SqlExp get(int idx) {
121         SqlExp list = childList;
122         for (int n = idx; list != null && --n > 0; list = list.next) ;
123         if (list == null) {
124             throw BindingSupportImpl.getInstance().invalidOperation("Invalid expression index: $" +
125                     (idx == 10 ? "0 (10)" : Integer.toString(idx)) +
126                     " in '" + template + "'");
127         }
128         return list;
129     }
130
131 }
132
133
Popular Tags