Skip navigation links

Package scriptella.driver.csv

CSV Driver for Scriptella.

See: Description

Package scriptella.driver.csv Description

CSV Driver for Scriptella.

The driver to write and query CSV files.

Note: The driver doesn't use SQL syntax

General information

Driver class: scriptella.driver.csv.Driver
URL: CSV file URL. URIs are resolved relative to a script file directory. If url has no value the output is read from/printed to the console (System.out).
Runtime dependencies: None

Driver Specific Properties

Name Description Required
encoding Specifies charset encoding of CSV files. No, the system default encoding is used.
separator The delimiter to use for separating entries when reading from or writing to files. No, the default value is ,.
quote The character to use for quoted elements when reading from or writing to files. Use empty string to suppress quoting. No, the default value is ".
headers Value of true means the first line contains headers.

Only valid for <query> elements.

No, the default value is true(first line contains column names).
eol End-Of-Line suffix.

Only valid for <script> elements.

No, the default value is \n.
trim Value of true specifies that the leading and trailing whitespaces in CSV fields should be omitted. No, the default value is true.
skip_lines The number of lines to skip before start reading. Please note that if headers=true, the actual number of skipped lines is skipped_lines+1 No, the default value is 0 (no lines are skipped).
null_string Specifies string token to represent Java null literal.

When querying a CSV file, cells with content equal to null_string are parsed to Java nulls.
When outputting content, if null_string is set, all the missing variables, or the ones with a null value are substituted with null_string.

Specify an empty string (null_string=) to automatically convert between nulls in memory and empty strings in files. For example: CSV line 1,,5 is parsed to a set of 3 variables with the following values {"1", null, "5"} as opposed to the default behaviour {"1","","5"}.

No, by default strings are preserved, i.e. empty strings are not converted to nulls and null variables references are not expanded in the output, i.e. ${nullvalue}.

Query Syntax

The driver utilizes query by example approach for CSV content filtering. If you want to read the whole CSV simply use empty query element, otherwise specify a set of lines containing comma-separated case insensitive regular expressions.
Example:
,food
,^Beverages$
    
This query selects rows where the second column equals "Beverages" or contains "food" substring.

The columns of the matched row can be referenced by name in nested queries/scripts. It is also possible to reference columns by an index, i.e. $1, $2, ...

Script Syntax

The script syntax is simple, just specify a comma-separated set of columns.
Example:
<script>
id,priority,summary,status
1,Critical,NullPointerException in Main class,Open
</script>
    
This script writes 2 lines to the output file.

IMPORTANT: Always use commas as a column separator inside both <script> and <query> elements no matter what separator is used in files being parsed or produced. This decision allows switching between different formats like tab or semicolumn while keeping scripts and queries unchanged.

Properties substitution

In CSV script and query elements use ${property} syntax for properties/variables substition.

NOTE:

By default NULL variables and expressions are preserved, use null_string connection property to specify a string token for nulls. For example setting null_string to empty string in the connection properties section will enable parsing empty strings as nulls:
<connection driver="csv" url="report.csv">
    null_string=
    </connection>
Scriptella properties substitution engine cannot distinguish null value from an unused variable or some unintended usage of $var syntax, therefore such blocks are preserved by default until user explicitly specify the value of null_string.

Examples

<connection id="in" driver="csv" url="data.csv">

</connection>
<connection id="out" driver="csv" url="report.csv">
    #Use empty quote to turn off quoting
    quote=
    separator=;
</connection>

<script connection-id="out">
    ID,Priority,Summary,Status
</script>

<query connection-id="in">
    <!--Empty query means select all-->
    <script connection-id="out">
        $rownum,$priority,$summary,$status
    </script>
</query>

Copies rows from data.csv file to report.csv, additionally the ID column is added. The result file will be semicolon separated.

Custom formatting/parsing rules for properties

See Text driver for a description of formatting/parsing rules.
Skip navigation links

Copyright © Copyright 2006-2019 The Scriptella Project Team.