Working with SQL (SQL plugin)

In this recipe, we will explore the SQL plugin to work with SQL. This is a bit different from the JDBC recipe.

Getting ready

Obviously, we need a database. This database can be PostgreSQL, MS SQL, Oracle, or MySQL. For testing, you can use the PostgreSQL database that is implemented in the appliance (refer to the Tuning the appliance recipe in Chapter 2, Optimizing Orchestrator Configuration). Also, take a look at the There's more... section of this recipe.

We will use a Microsoft SQL 2008 R2 database in this example; however, the steps are the same for all databases. The database we will be using is called testDB.

You will need an existing database and a user who is able to create/drop tables as well as insert/delete information, for example, the DBO role.

How to do it...

See the following sections.

Add an SQL DB to Orchestrator

In this section, we will connect an SQL server to Orchestrator:

  1. Start the Library | SQL | Configuration | Add a database workflow.
  2. Enter a name for the connection.
  3. Select the kind of database you like.
  4. Enter the JDBC connection string to your database. You can use the Library | JDBC | JDBC URL generator workflow to create and test the string:

    Add an SQL DB to Orchestrator

  5. When the workflow has finished, go and check the inventory:

    Add an SQL DB to Orchestrator

Run SQL statement

We will now run a SQL statement:

  1. Start the Library | SQL | Execute a custom query on a database workflow.
  2. Select the database.
  3. Enter the following as a statement: INSERT INTO testtbl VALUES (2,'some lastname','some firstname');.
  4. Run the workflow and check the database table.

Run an SQL query

We will now run an SQL query:

  1. Create a new workflow.
  2. Add the workflow and create the inputs as in-parameters. Assign the output variable (array of SQL:ActiveRecord) as an attribute.
  3. Add a scriptable task and assign the attribute as in-parameter.
  4. Enter the following script:
          for each (result in resultRecords){ 
             columns=(result.getFieldNames()); 
             for each (column in columns){ 
                   System.log("Column :"+column+" content:"+result.getProperty(column)); 
             } 
          } 
    
  5. Save and run the script with a SQL query such as select * from testtbl.

How it works...

The SQL plugin is a bit different than the JDBC connector as it uses Orchestrator objects. There are the following Orchestrator scripting classes:

  • SQLActiveRecord
  • SQLColumn
  • SQLDatabase
  • SQLDatabaseManager
  • SQLTable

Also extremely valuable are the Generate CRUD workflows for a table. CRUD stands for Create, Read, Update and Delete and will create these four workflows a given table.

See also

The example workflow 10.03.1 Execute SQL query (SQL plugin).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset