%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.task.TorqueSQLTask |
|
|
1 | package org.apache.torque.task; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.io.File; |
|
20 | import java.io.FileInputStream; |
|
21 | import java.io.FileOutputStream; |
|
22 | ||
23 | import java.util.Iterator; |
|
24 | import java.util.Properties; |
|
25 | ||
26 | import org.apache.tools.ant.BuildException; |
|
27 | ||
28 | import org.apache.velocity.context.Context; |
|
29 | ||
30 | import org.apache.torque.engine.EngineException; |
|
31 | import org.apache.torque.engine.database.transform.XmlToAppData; |
|
32 | import org.apache.torque.engine.database.model.Database; |
|
33 | ||
34 | ||
35 | /** |
|
36 | * An extended Texen task used for generating SQL source from |
|
37 | * an XML schema describing a database structure. |
|
38 | * |
|
39 | * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a> |
|
40 | * @author <a href="mailto:jmcnally@collab.net>John McNally</a> |
|
41 | * @version $Id: TorqueSQLTask.java 239624 2005-08-24 12:18:03Z henning $ |
|
42 | */ |
|
43 | 0 | public class TorqueSQLTask extends TorqueDataModelTask |
44 | { |
|
45 | // if the database is set than all generated sql files |
|
46 | // will be placed in the specified database, the database |
|
47 | // will not be taken from the data model schema file. |
|
48 | ||
49 | private String database; |
|
50 | 0 | private String suffix = ""; |
51 | ||
52 | 0 | private String idTableXMLFile = null; |
53 | ||
54 | /** |
|
55 | * |
|
56 | * @param database |
|
57 | */ |
|
58 | public void setDatabase(String database) |
|
59 | { |
|
60 | 0 | this.database = database; |
61 | 0 | } |
62 | ||
63 | /** |
|
64 | * |
|
65 | * @return |
|
66 | */ |
|
67 | public String getDatabase() |
|
68 | { |
|
69 | 0 | return database; |
70 | } |
|
71 | ||
72 | /** |
|
73 | * |
|
74 | * @param suffix |
|
75 | */ |
|
76 | public void setSuffix(String suffix) |
|
77 | { |
|
78 | 0 | this.suffix = suffix; |
79 | 0 | } |
80 | ||
81 | /** |
|
82 | * |
|
83 | * @return |
|
84 | */ |
|
85 | public String getSuffix() |
|
86 | { |
|
87 | 0 | return suffix; |
88 | } |
|
89 | ||
90 | /** |
|
91 | * Set the path to the xml schema file that defines the id-table, used |
|
92 | * by the idbroker method. |
|
93 | * |
|
94 | * @param idXmlFile xml schema file |
|
95 | */ |
|
96 | public void setIdTableXMLFile(String idXmlFile) |
|
97 | { |
|
98 | 0 | idTableXMLFile = idXmlFile; |
99 | 0 | } |
100 | ||
101 | /** |
|
102 | * Gets the id-table xml schema file path. |
|
103 | * |
|
104 | * @return Path to file. |
|
105 | */ |
|
106 | public String getIdTableXMLFile() |
|
107 | { |
|
108 | 0 | return idTableXMLFile; |
109 | } |
|
110 | ||
111 | /** |
|
112 | * create the sql -> database map. |
|
113 | * |
|
114 | * @throws Exception |
|
115 | */ |
|
116 | private void createSqlDbMap() throws Exception |
|
117 | { |
|
118 | 0 | if (getSqlDbMap() == null) |
119 | { |
|
120 | 0 | return; |
121 | } |
|
122 | ||
123 | // Produce the sql -> database map |
|
124 | 0 | Properties sqldbmap = new Properties(); |
125 | ||
126 | // Check to see if the sqldbmap has already been created. |
|
127 | 0 | File file = new File(getSqlDbMap()); |
128 | ||
129 | 0 | if (file.exists()) |
130 | { |
|
131 | 0 | FileInputStream fis = new FileInputStream(file); |
132 | 0 | sqldbmap.load(fis); |
133 | 0 | fis.close(); |
134 | } |
|
135 | ||
136 | 0 | Iterator i = getDataModelDbMap().keySet().iterator(); |
137 | ||
138 | 0 | while (i.hasNext()) |
139 | { |
|
140 | 0 | String dataModelName = (String) i.next(); |
141 | 0 | String sqlFile = dataModelName + suffix + ".sql"; |
142 | ||
143 | String databaseName; |
|
144 | ||
145 | 0 | if (getDatabase() == null) |
146 | { |
|
147 | 0 | databaseName = (String) getDataModelDbMap().get(dataModelName); |
148 | } |
|
149 | else |
|
150 | { |
|
151 | 0 | databaseName = getDatabase(); |
152 | } |
|
153 | ||
154 | 0 | sqldbmap.setProperty(sqlFile, databaseName); |
155 | } |
|
156 | ||
157 | 0 | sqldbmap.store(new FileOutputStream(getSqlDbMap()), |
158 | "Sqlfile -> Database map"); |
|
159 | 0 | } |
160 | ||
161 | /** |
|
162 | * Create the database model necessary for the IDBroker tables. |
|
163 | * We use the model to generate the necessary SQL to create |
|
164 | * these tables. This method adds an AppData object containing |
|
165 | * the model to the context under the name "idmodel". |
|
166 | */ |
|
167 | public void loadIdBrokerModel() |
|
168 | throws EngineException |
|
169 | { |
|
170 | // Transform the XML database schema into |
|
171 | // data model object. |
|
172 | 0 | XmlToAppData xmlParser = new XmlToAppData(getTargetDatabase(), null); |
173 | 0 | Database ad = xmlParser.parseFile(getIdTableXMLFile()); |
174 | ||
175 | 0 | ad.setName("idmodel"); |
176 | 0 | context.put("idmodel", ad); |
177 | 0 | } |
178 | ||
179 | /** |
|
180 | * Place our target database and target platform |
|
181 | * values into the context for use in the templates. |
|
182 | * |
|
183 | * @return the context |
|
184 | * @throws Exception |
|
185 | */ |
|
186 | public Context initControlContext() throws Exception |
|
187 | { |
|
188 | 0 | super.initControlContext(); |
189 | try |
|
190 | { |
|
191 | 0 | createSqlDbMap(); |
192 | ||
193 | // If the load path for the id broker table xml schema is |
|
194 | // defined then load it. |
|
195 | 0 | String f = getIdTableXMLFile(); |
196 | 0 | if (f != null && f.length() > 0) |
197 | { |
|
198 | 0 | loadIdBrokerModel(); |
199 | } |
|
200 | } |
|
201 | 0 | catch (EngineException ee) |
202 | { |
|
203 | 0 | throw new BuildException(ee); |
204 | 0 | } |
205 | ||
206 | 0 | return context; |
207 | } |
|
208 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |