org.codehaus.mojo.sql
Class SqlExecMojo

java.lang.Object
  extended by org.apache.maven.plugin.AbstractMojo
      extended by org.codehaus.mojo.sql.SqlExecMojo
All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled, org.apache.maven.plugin.Mojo

public class SqlExecMojo
extends org.apache.maven.plugin.AbstractMojo

Executes SQL against a database.


Nested Class Summary
private  class SqlExecMojo.Transaction
          Contains the definition of a new transaction element.
 
Field Summary
private  boolean append
          Append to an existing file or overwrite it?
private  boolean autocommit
          Set to true to execute none-transactional SQL.
private  Connection conn
          Database connection
private  String delimiter
          Set the delimiter that separates SQL statements.
private  String delimiterType
           The delimiter type takes two values - "normal" and "row".
private  String driver
          Database driver classname.
private  String driverProperties
          Additional key=value pairs separated by comma to be passed into JDBC driver.
private  boolean enableAnonymousPassword
          Ignore the password and use anonymous access.
private  boolean enableBlockMode
          Deprecated. used delimiterType instead.
private  boolean enableFiltering
          Set to true if you want to filter the srcFiles using system-, user- and project properties
private  String encoding
          Encoding to use when reading SQL statements from a file.
private  boolean escapeProcessing
          Argument to Statement.setEscapeProcessing If you want the driver to use regular SQL syntax then set this to false.
static String FILE_SORTING_ASC
          Call setOrderFile(String) with this value to sort in ascendant order the sql files.
static String FILE_SORTING_DSC
          Call setOrderFile(String) with this value to sort in descendant order the sql files.
private  org.apache.maven.shared.filtering.MavenFileFilter fileFilter
           
private  Fileset fileset
          File(s) containing SQL statements to load.
private  boolean forceMojoExecution
          Setting this parameter to true will force the execution of this mojo, even if it would get skipped usually.
private  boolean keepFormat
          Keep the format of an SQL block.
private  org.apache.maven.execution.MavenSession mavenSession
           
static String ON_ERROR_ABORT
          Call setOnError(String) with this value to abort SQL command execution if an error is found.
static String ON_ERROR_ABORT_AFTER
          Call setOnError(String) with this value to continue SQL command execution until all commands have been attempted, then abort the build if an SQL error occurred in any of the commands.
static String ON_ERROR_CONTINUE
          Call setOnError(String) with this value to continue SQL command execution if an error is found.
private  String onError
          Action to perform if an error is found.
private  String orderFile
          Set the order in which the SQL files will be executed.
private  String outputDelimiter
           
private  File outputFile
          Dump the SQL exection's output to a file.
private  String password
          Database password.
private  boolean printResultSet
          Print SQL results.
protected  org.apache.maven.project.MavenProject project
          The Maven Project Object
private  String[] resourceLocations
          List of resources containing SQL statements
private  org.apache.maven.settings.Settings settings
           
private  String settingsKey
          Server's id in settings.xml to look up username and password.
private  boolean showheaders
          Print header columns.
private  boolean skip
          When true, skip the execution.
private  boolean skipOnConnectionError
          Skip execution when there is an error obtaining a connection.
private  String sqlCommand
          SQL input commands separated by ${delimiter}.
private  File[] srcFiles
          List of files containing SQL statements to load.
private  Statement statement
          SQL statement
private  int successfulStatements
          number of successful executed statements
private  int totalStatements
          number of total executed statements
private  Vector<SqlExecMojo.Transaction> transactions
          SQL transactions to perform
private  String url
          Database URL.
private  String username
          Database username.
 
Fields inherited from interface org.apache.maven.plugin.Mojo
ROLE
 
Constructor Summary
SqlExecMojo()
           
 
Method Summary
private  void addCommandToTransactions()
          Add sql command to transactions list.
private  void addFileSetToTransactions()
          Add user sql fileset to transation list
private  void addFilesToTransactions()
          Add user input of srcFiles to transaction list.
private  void addResourcesToTransactions()
          Add user input of srcFiles to transaction list.
 void addText(String sql)
          Set an inline SQL command to execute.
protected  void copy(org.springframework.core.io.Resource resource, File file)
           
 SqlExecMojo.Transaction createTransaction()
          Add a SQL transaction to execute
private  void execSQL(String sql, PrintStream out)
          Exec the sql statement.
 void execute()
          Load the sql file and then execute it
private  Connection getConnection()
          Creates a new Connection as using the driver, url, userid and password specified.
 String getDriver()
           
protected  Properties getDriverProperties()
          parse driverProperties into Properties set
(package private)  int getGoodSqls()
          Deprecated. use getSuccessfulStatements()
 String getOnError()
           
 String getOrderFile()
           
 String getPassword()
           
 String[] getResourceLocations()
           
protected  org.springframework.core.io.Resource[] getResources(String[] locations)
           
 String getSqlCommand()
           
 File[] getSrcFiles()
           
 int getSuccessfulStatements()
          Number of SQL statements executed so far that caused errors.
 int getTotalStatements()
          Number of SQL statements executed so far, including the ones that caused errors.
 Vector<SqlExecMojo.Transaction> getTransactions()
           
 String getUrl()
           
 String getUsername()
           
 boolean isEnableBlockMode()
           
private  void loadUserInfoFromSettings()
          Load username password from settings if user has not set them in JVM properties
private  void printResultSet(ResultSet rs, PrintStream out)
          print any results in the result set.
private  void runStatements(Reader reader, PrintStream out)
          read in lines and execute them
 void setAppend(boolean append)
          whether output should be appended to or overwrite an existing file.
(package private)  void setAutocommit(boolean autocommit)
           
 void setDelimiter(String delimiter)
          Set the delimiter that separates SQL statements.
 void setDelimiterType(String delimiterType)
          Set the delimiter type: "normal" or "row" (default "normal").
 void setDriver(String driver)
           
 void setDriverProperties(String driverProperties)
           
 void setEnableBlockMode(boolean enableBlockMode)
           
 void setEncoding(String encoding)
          Set the file encoding to use on the SQL files read in
 void setEscapeProcessing(boolean enable)
          Set escape processing for statements.
 void setFileFilter(org.apache.maven.shared.filtering.MavenFileFilter filter)
           
(package private)  void setFileset(Fileset fileset)
           
 void setKeepFormat(boolean keepformat)
          whether or not format should be preserved.
 void setOnError(String action)
           
 void setOrderFile(String orderFile)
           
 void setOutputFile(File output)
          Set the output file;
 void setPassword(String password)
           
 void setPrintResultSet(boolean print)
          Print result sets from the statements; optional, default false
 void setPrintResutlSet(boolean print)
          Deprecated. typo, use setPrintResultSet()
 void setResourceLocations(String[] resourceLocations)
           
(package private)  void setSettings(org.apache.maven.settings.Settings settings)
           
(package private)  void setSettingsKey(String key)
           
 void setShowheaders(boolean showheaders)
          Print headers for result sets from the statements; optional, default true.
(package private)  void setSkip(boolean skip)
           
 void setSqlCommand(String sqlCommand)
           
 void setSrcFiles(File[] files)
           
 void setTransactions(Vector<SqlExecMojo.Transaction> transactions)
           
 void setUrl(String url)
           
 void setUsername(String username)
           
protected  boolean skipMojo()
           Determine if the mojo execution should get skipped.
private  void sortTransactions()
          Sort the transaction list.
 
Methods inherited from class org.apache.maven.plugin.AbstractMojo
getLog, getPluginContext, setLog, setPluginContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ON_ERROR_ABORT

public static final String ON_ERROR_ABORT
Call setOnError(String) with this value to abort SQL command execution if an error is found.

See Also:
Constant Field Values

ON_ERROR_ABORT_AFTER

public static final String ON_ERROR_ABORT_AFTER
Call setOnError(String) with this value to continue SQL command execution until all commands have been attempted, then abort the build if an SQL error occurred in any of the commands.

See Also:
Constant Field Values

ON_ERROR_CONTINUE

public static final String ON_ERROR_CONTINUE
Call setOnError(String) with this value to continue SQL command execution if an error is found.

See Also:
Constant Field Values

FILE_SORTING_ASC

public static final String FILE_SORTING_ASC
Call setOrderFile(String) with this value to sort in ascendant order the sql files.

See Also:
Constant Field Values

FILE_SORTING_DSC

public static final String FILE_SORTING_DSC
Call setOrderFile(String) with this value to sort in descendant order the sql files.

See Also:
Constant Field Values

username

private String username
Database username. If not given, it will be looked up through settings.xml's server with ${settingsKey} as key.

Since:
1.0

password

private String password
Database password. If not given, it will be looked up through settings.xml's server with ${settingsKey} as key.

Since:
1.0

enableAnonymousPassword

private boolean enableAnonymousPassword
Ignore the password and use anonymous access. This may be useful for databases like MySQL which do not allow empty password parameters in the connection initialization.

Since:
1.4

driverProperties

private String driverProperties
Additional key=value pairs separated by comma to be passed into JDBC driver.

Since:
1.0

settings

private org.apache.maven.settings.Settings settings
Since:
1.0

settingsKey

private String settingsKey
Server's id in settings.xml to look up username and password. Defaults to ${url} if not given.

Since:
1.0

skipOnConnectionError

private boolean skipOnConnectionError
Skip execution when there is an error obtaining a connection. This is a special case to support databases, such as embedded Derby, that can shutdown the database via the URL (i.e. shutdown=true).

Since:
1.1

forceMojoExecution

private boolean forceMojoExecution
Setting this parameter to true will force the execution of this mojo, even if it would get skipped usually.


project

protected org.apache.maven.project.MavenProject project
The Maven Project Object


mavenSession

private org.apache.maven.execution.MavenSession mavenSession

sqlCommand

private String sqlCommand
SQL input commands separated by ${delimiter}.

Since:
1.0

srcFiles

private File[] srcFiles
List of files containing SQL statements to load.

Since:
1.0

resourceLocations

private String[] resourceLocations
List of resources containing SQL statements

Since:
1.5

fileset

private Fileset fileset
File(s) containing SQL statements to load.

Since:
1.0

skip

private boolean skip
When true, skip the execution.

Since:
1.0

url

private String url
Database URL.

Since:
1.0-beta-1

driver

private String driver
Database driver classname.

Since:
1.0

autocommit

private boolean autocommit
Set to true to execute none-transactional SQL.

Since:
1.0

onError

private String onError
Action to perform if an error is found. Possible values are abort and continue.

Since:
1.0

delimiter

private String delimiter
Set the delimiter that separates SQL statements.

Since:
1.0

delimiterType

private String delimiterType

The delimiter type takes two values - "normal" and "row". Normal means that any occurrence of the delimiter terminate the SQL command whereas with row, only a line containing just the delimiter is recognized as the end of the command.

For example, set this to "go" and delimiterType to "row" for Sybase ASE or MS SQL Server.

Since:
1.2

orderFile

private String orderFile
Set the order in which the SQL files will be executed. Possible values are ascending and descending. Any other value means that no sorting will be performed.

Since:
1.1

enableBlockMode

private boolean enableBlockMode
Deprecated. used delimiterType instead.
When true, the whole SQL content in sqlCommand, srcFiles and fileset are sent directly to JDBC in one SQL statement. This option is for executing database stored procedures/functions.

Since:
1.1

keepFormat

private boolean keepFormat
Keep the format of an SQL block.

Since:
1.1

printResultSet

private boolean printResultSet
Print SQL results.

Since:
1.3

showheaders

private boolean showheaders
Print header columns.


outputFile

private File outputFile
Dump the SQL exection's output to a file. Default is stdout.

Since:
1.3

outputDelimiter

private String outputDelimiter
Since:
1.4

encoding

private String encoding
Encoding to use when reading SQL statements from a file.

Since:
1.1

append

private boolean append
Append to an existing file or overwrite it?


escapeProcessing

private boolean escapeProcessing
Argument to Statement.setEscapeProcessing If you want the driver to use regular SQL syntax then set this to false.

Since:
1.4

successfulStatements

private int successfulStatements
number of successful executed statements


totalStatements

private int totalStatements
number of total executed statements


conn

private Connection conn
Database connection


statement

private Statement statement
SQL statement


transactions

private Vector<SqlExecMojo.Transaction> transactions
SQL transactions to perform


fileFilter

private org.apache.maven.shared.filtering.MavenFileFilter fileFilter
Since:
1.4

enableFiltering

private boolean enableFiltering
Set to true if you want to filter the srcFiles using system-, user- and project properties

Since:
1.4
Constructor Detail

SqlExecMojo

public SqlExecMojo()
Method Detail

createTransaction

public SqlExecMojo.Transaction createTransaction()
Add a SQL transaction to execute

Returns:
a new SqlExecMojo.Transaction

addText

public void addText(String sql)
Set an inline SQL command to execute. NB: Properties are not expanded in this text.

Parameters:
sql - the sql statement to add

setEncoding

public void setEncoding(String encoding)
Set the file encoding to use on the SQL files read in

Parameters:
encoding - the encoding to use on the files

setDelimiter

public void setDelimiter(String delimiter)
Set the delimiter that separates SQL statements. Defaults to ";";

Parameters:
delimiter - the new delimiter

setDelimiterType

public void setDelimiterType(String delimiterType)
Set the delimiter type: "normal" or "row" (default "normal").

Parameters:
delimiterType - the new delimiterType

setPrintResutlSet

public void setPrintResutlSet(boolean print)
Deprecated. typo, use setPrintResultSet()

Print result sets from the statements; optional, default false

Parameters:
print - true to print the resultset, otherwise false

setPrintResultSet

public void setPrintResultSet(boolean print)
Print result sets from the statements; optional, default false

Parameters:
print - true to print the resultset, otherwise false

setShowheaders

public void setShowheaders(boolean showheaders)
Print headers for result sets from the statements; optional, default true.

Parameters:
showheaders - true to show the headers, otherwise false

setOutputFile

public void setOutputFile(File output)
Set the output file;

Parameters:
output - the output file

setAppend

public void setAppend(boolean append)
whether output should be appended to or overwrite an existing file. Defaults to false.

Parameters:
append - true to append, otherwise false to overwrite

setKeepFormat

public void setKeepFormat(boolean keepformat)
whether or not format should be preserved. Defaults to false.

Parameters:
keepformat - The keepformat to set

setEscapeProcessing

public void setEscapeProcessing(boolean enable)
Set escape processing for statements.

Parameters:
enable - true to escape, otherwiser false

skipMojo

protected boolean skipMojo()

Determine if the mojo execution should get skipped.

This is the case if:
  • skip is true
  • if the mojo gets executed on a project with packaging type 'pom' and forceMojoExecution is false

Returns:
true if the mojo execution should be skipped.

execute

public void execute()
             throws org.apache.maven.plugin.MojoExecutionException
Load the sql file and then execute it

Throws:
org.apache.maven.plugin.MojoExecutionException

addCommandToTransactions

private void addCommandToTransactions()
Add sql command to transactions list.


addFileSetToTransactions

private void addFileSetToTransactions()
Add user sql fileset to transation list


getResources

protected org.springframework.core.io.Resource[] getResources(String[] locations)
                                                       throws org.apache.maven.plugin.MojoExecutionException
Throws:
org.apache.maven.plugin.MojoExecutionException

copy

protected void copy(org.springframework.core.io.Resource resource,
                    File file)
             throws IOException
Throws:
IOException

addResourcesToTransactions

private void addResourcesToTransactions()
                                 throws org.apache.maven.plugin.MojoExecutionException
Add user input of srcFiles to transaction list.

Throws:
org.apache.maven.plugin.MojoExecutionException

addFilesToTransactions

private void addFilesToTransactions()
                             throws org.apache.maven.plugin.MojoExecutionException
Add user input of srcFiles to transaction list.

Throws:
org.apache.maven.plugin.MojoExecutionException

sortTransactions

private void sortTransactions()
Sort the transaction list.


loadUserInfoFromSettings

private void loadUserInfoFromSettings()
                               throws org.apache.maven.plugin.MojoExecutionException
Load username password from settings if user has not set them in JVM properties

Throws:
org.apache.maven.plugin.MojoExecutionException

getConnection

private Connection getConnection()
                          throws org.apache.maven.plugin.MojoExecutionException,
                                 SQLException
Creates a new Connection as using the driver, url, userid and password specified. The calling method is responsible for closing the connection.

Returns:
Connection the newly created connection.
Throws:
org.apache.maven.plugin.MojoExecutionException - if the UserId/Password/Url is not set or there is no suitable driver or the driver fails to load.
SQLException - if there is problem getting connection with valid url

getDriverProperties

protected Properties getDriverProperties()
                                  throws org.apache.maven.plugin.MojoExecutionException
parse driverProperties into Properties set

Returns:
the driver properties
Throws:
org.apache.maven.plugin.MojoExecutionException

runStatements

private void runStatements(Reader reader,
                           PrintStream out)
                    throws SQLException,
                           IOException
read in lines and execute them

Parameters:
reader - the reader
out - the outputstream
Throws:
SQLException
IOException

execSQL

private void execSQL(String sql,
                     PrintStream out)
              throws SQLException
Exec the sql statement.

Parameters:
sql - query to execute
out - the outputstream
Throws:
SQLException

printResultSet

private void printResultSet(ResultSet rs,
                            PrintStream out)
                     throws SQLException
print any results in the result set.

Parameters:
rs - the resultset to print information about
out - the place to print results
Throws:
SQLException - on SQL problems.

getUsername

public String getUsername()

setUsername

public void setUsername(String username)

getPassword

public String getPassword()

setPassword

public void setPassword(String password)

getUrl

public String getUrl()

setUrl

public void setUrl(String url)

getDriver

public String getDriver()

setDriver

public void setDriver(String driver)

setAutocommit

void setAutocommit(boolean autocommit)

setFileset

void setFileset(Fileset fileset)

getSrcFiles

public File[] getSrcFiles()

setSrcFiles

public void setSrcFiles(File[] files)

getOrderFile

public String getOrderFile()

setOrderFile

public void setOrderFile(String orderFile)

getGoodSqls

int getGoodSqls()
Deprecated. use getSuccessfulStatements()


getSuccessfulStatements

public int getSuccessfulStatements()
Number of SQL statements executed so far that caused errors.

Returns:
the number

getTotalStatements

public int getTotalStatements()
Number of SQL statements executed so far, including the ones that caused errors.

Returns:
the number

getOnError

public String getOnError()

setOnError

public void setOnError(String action)

setSettings

void setSettings(org.apache.maven.settings.Settings settings)

setSettingsKey

void setSettingsKey(String key)

setSkip

void setSkip(boolean skip)

setDriverProperties

public void setDriverProperties(String driverProperties)

isEnableBlockMode

public boolean isEnableBlockMode()

setEnableBlockMode

public void setEnableBlockMode(boolean enableBlockMode)

getSqlCommand

public String getSqlCommand()

setSqlCommand

public void setSqlCommand(String sqlCommand)

getTransactions

public Vector<SqlExecMojo.Transaction> getTransactions()

setTransactions

public void setTransactions(Vector<SqlExecMojo.Transaction> transactions)

setFileFilter

public void setFileFilter(org.apache.maven.shared.filtering.MavenFileFilter filter)

getResourceLocations

public String[] getResourceLocations()

setResourceLocations

public void setResourceLocations(String[] resourceLocations)


Copyright © 2006-2011 The Kuali Foundation. All Rights Reserved.