1 package org.apache.torque.mojo; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.maven.plugin.AbstractMojo; 23 import org.apache.maven.plugin.MojoExecutionException; 24 import org.apache.tools.ant.Project; 25 import org.apache.torque.task.TorqueSQLExec; 26 import org.apache.torque.task.TorqueSQLExec.OnError; 27 28 /** 29 * Executes the generated SQL. 30 * 31 * @author Raphael Pieroni (rafale_at_codehaus.org) 32 * @author <a href="fischer@seitenbau.de">Thomas Fischer</a> 33 * 34 * @goal sqlExec 35 * @phase generate-sources 36 */ 37 public class SqlExecMojo 38 extends AbstractMojo 39 { 40 /** 41 * The ant task this mojo is wrapping. 42 */ 43 private TorqueSQLExec antTask = new TorqueSQLExec(); 44 45 /** 46 * The ant project the ant task is running in. 47 */ 48 private Project antProject = new Project(); 49 50 /** 51 * Autocommit flag. Default value is true. 52 * 53 * @parameter expression="true" 54 */ 55 private boolean autocommit = true; 56 57 /** 58 * Tells the Mojo what to do if an sql error occurs during execution. 59 * Can be either "continue", "stop" or "abort". 60 * 61 * @parameter expression="continue". 62 * @required 63 */ 64 private String onError; 65 66 /** 67 * The fully qualified class name of the database driver. 68 * 69 * @parameter 70 * @required 71 */ 72 private String driver = null; 73 74 /** 75 * The connect URL of the database. 76 * 77 * @parameter 78 * @required 79 */ 80 private String url = null; 81 82 /** 83 * The user name to connect to the database. 84 * 85 * @parameter 86 * @required 87 */ 88 private String user = null; 89 90 /** 91 * The password for the database user. 92 * 93 * @parameter 94 */ 95 private String password = ""; 96 97 /** 98 * The SQL Statement delimiter. 99 * 100 * @parameter expression=";" 101 */ 102 private String delimiter = ";"; 103 104 /** 105 * Whether the delimiter can be anywhere in the sql ("normal") 106 * or needs to be in an extra row ("row"). 107 * 108 * @parameter expression="normal" 109 */ 110 private String delimiterType = "normal"; 111 112 /** 113 * The path to the properties file containing the mapping 114 * sql file -> target database. 115 * 116 * @parameter expression="${project.build.directory}/torque/sqldbmap.properties" 117 */ 118 private String sqlDbMap; 119 120 /** 121 * The source directory where to find the SQL files. 122 * 123 * @parameter expression="${project.build.directory}/generated-sql/torque" 124 */ 125 private String srcDir; 126 127 /** 128 * Creates and initializes a SqlExecMojo. 129 */ 130 public SqlExecMojo() 131 { 132 antProject.init(); 133 antTask.setProject(antProject); 134 } 135 136 /** 137 * Returns whether autocommit is turned on. 138 * 139 * @return true if autocommit is on, false otherwise. 140 */ 141 public boolean isAutocommit() 142 { 143 return autocommit; 144 } 145 146 /** 147 * Sets whether autocommit is turned on. 148 * 149 * @param autocommit true to turn autocommit on, false to turn it off. 150 */ 151 public void setAutocommit(boolean autocommit) 152 { 153 this.autocommit = autocommit; 154 } 155 156 /** 157 * The delimiter used to separate SQL commands. 158 * 159 * @return the delimiter used to separate SQL commands. 160 */ 161 public String getDelimiter() 162 { 163 return delimiter; 164 } 165 166 /** 167 * Sets the delimiter used to separate SQL commands. 168 * 169 * @param delimiter the delimiter used to separate SQL commands. 170 */ 171 public void setDelimiter(String delimiter) 172 { 173 this.delimiter = delimiter; 174 } 175 176 /** 177 * Returns whether the delimiter can be anywhere in the sql ("normal") 178 * or needs to be in an extra row ("row"). 179 * 180 * @return the delimiter type. 181 */ 182 public String getDelimiterType() 183 { 184 return delimiterType; 185 } 186 187 /** 188 * Sets whether the delimiter can be anywhere in the sql ("normal") 189 * or needs to be in an extra row ("row"). 190 * 191 * @param delimiterType the delimiter type, should either be "normal" 192 * or "row". 193 */ 194 public void setDelimiterType(String delimiterType) 195 { 196 this.delimiterType = delimiterType; 197 } 198 199 200 /** 201 * Returns the fully qualified class name of the database driver. 202 * 203 * @return the fully qualified class name of the database driver. 204 */ 205 public String getDriver() 206 { 207 return driver; 208 } 209 210 /** 211 * Sets the fully qualified class name of the database driver. 212 * 213 * @param driver the fully qualified class name of the database driver. 214 */ 215 public void setDriver(String driver) 216 { 217 this.driver = driver; 218 } 219 220 /** 221 * Returns the password of the database user. 222 * 223 * @return the password of the database user. 224 */ 225 public String getPassword() 226 { 227 return password; 228 } 229 230 /** 231 * Sets the password of the database user. 232 * 233 * @param password the password of the database user. 234 */ 235 public void setPassword(String password) 236 { 237 this.password = password; 238 } 239 240 /** 241 * Returns the connect URL to the database. 242 * 243 * @return the connect URL to the database. 244 */ 245 public String getUrl() 246 { 247 return url; 248 } 249 250 /** 251 * Sets the connect URL to the database. 252 * 253 * @param url the connect URL to the database. 254 */ 255 public void setUrl(String url) 256 { 257 this.url = url; 258 } 259 260 /** 261 * Returns the database user. 262 * 263 * @return the userId of the database user. 264 */ 265 public String getUser() 266 { 267 return user; 268 } 269 270 /** 271 * Sets the database user. 272 * 273 * @param user the userId of the database user. 274 */ 275 public void setUser(String user) 276 { 277 this.user = user; 278 } 279 280 /** 281 * Returns the path to the mapping SQL Files -> database. 282 * 283 * @return the path to the mapping SQL Files -> database. 284 */ 285 public String getSqlDbMap() 286 { 287 return sqlDbMap; 288 } 289 290 /** 291 * Sets the path to the mapping SQL Files -> database. 292 * 293 * @param sqlDbMap the absolute path to the mapping SQL Files -> database. 294 */ 295 public void setSqlDbMap(String sqlDbMap) 296 { 297 this.sqlDbMap = sqlDbMap; 298 } 299 300 /** 301 * Returns whether to procede if an sql error occurs 302 * during execution. 303 * 304 * @return onError what to do in case of an sql error, can be one of 305 * "continue", "stop" or "abort". 306 */ 307 public String getOnError() 308 { 309 return onError; 310 } 311 312 /** 313 * Tells the task whether to procede if an sql error occurs 314 * during execution. 315 * Can be either "continue", "stop" or "abort". 316 * 317 * @param onError what to do in case of an sql error. 318 */ 319 public void setOnError(String onError) 320 { 321 this.onError = onError; 322 } 323 324 /** 325 * Returns the path to the directory where the sql files can be found. 326 * 327 * @return the source directory where to find the SQL files. 328 */ 329 public String getSrcDir() 330 { 331 return srcDir; 332 } 333 334 /** 335 * Sets the path to the directory where the sql files can be found. 336 * 337 * @param srcDir the source directory where to find the SQL files. 338 */ 339 public void setSrcDir(String srcDir) 340 { 341 this.srcDir = srcDir; 342 } 343 344 /** 345 * Executes the goal of this mojo. 346 * 347 * @throws MojoExecutionException if the execution fails. 348 * 349 * @see org.apache.maven.plugin.Mojo#execute() 350 */ 351 public void execute() throws MojoExecutionException 352 { 353 configureTask(); 354 antTask.execute(); 355 } 356 357 /** 358 * Transfers the settings in this Mojo to the encapsulated ant task. 359 */ 360 protected void configureTask() 361 { 362 antTask.setDelimiter(getDelimiter()); 363 getLog().debug("Delimiter = " + getDelimiter()); 364 antTask.setDelimiterType(getDelimiterTypeForAnt()); 365 getLog().debug("DelimiterType = " + getDelimiterType()); 366 antTask.setAutocommit(isAutocommit()); 367 getLog().debug("Autocommit = " + isAutocommit()); 368 antTask.setDriver(getDriver()); 369 getLog().debug("Driver = " + getDriver()); 370 antTask.setUrl(getUrl()); 371 getLog().debug("Url = " + getUrl()); 372 antTask.setUserid(getUser()); 373 getLog().debug("Userid = " + getUser()); 374 antTask.setPassword(getPassword()); 375 getLog().debug("Password = " + getPassword()); 376 antTask.setSqlDbMap(getSqlDbMap()); 377 getLog().debug("SqlDbMap = " + getSqlDbMap()); 378 antTask.setSrcDir(getSrcDir()); 379 getLog().info("executing SQL files in src dir: " + getSrcDir()); 380 antTask.setOnerror(getOnErrorAction()); 381 getLog().debug("onErrorAction = " + getOnErrorAction()); 382 } 383 384 /** 385 * Returns the onError setting as a OnError object. 386 * 387 * @return the onError setting as a OnError object, never null. 388 */ 389 private OnError getOnErrorAction() 390 { 391 OnError onErrorAction = new OnError(); 392 onErrorAction.setValue(onError); 393 return onErrorAction; 394 } 395 396 /** 397 * Returns the delimiter type in a form acceptable by the ant task. 398 * 399 * @return the delimiter type setting as a DelimiterType object, 400 * never null. 401 */ 402 private TorqueSQLExec.DelimiterType getDelimiterTypeForAnt() 403 { 404 TorqueSQLExec.DelimiterType delimiterType 405 = new TorqueSQLExec.DelimiterType(); 406 delimiterType.setValue(this.delimiterType); 407 return delimiterType; 408 } 409 410 }