Package astra.util

Class DB


  • public class DB
    extends Module
    This class implements support for Database Acccess via JDBC.

    To use this class, you must also have the correct JDBC driver jar, and this must be installed on the project classpath.

    This is an early release and so its functionality may change, but will definitely be extended in future releases.

    Example Usage:

    module DB db;

    rule +!main(list args) {
        db.installDriver("org.sqlite.JDBC");
        db.getConnection("jdbc:sqlite:test.db", object<java.sql.Connection> connection);
        if (db.tableExists(connection, "users")) {
            db.rawUpdateQuery(connection, "DROP TABLE users");
        }
        db.rawUpdateQuery(connection, "CREATE TABLE users (id INT PRIMARY KEY, username CHAR(50) NOT NULL, password CHAR(50) NOT NULL, email CHAR(50))");
        db.rawUpdateQuery(connection, "INSERT INTO users (username, password, email) VALUES('user', 'pass', 'user@mail.com')");
        db.rawSelectQuery(connection, "SELECT * FROM users", list results);
        forall( list row : results) {
            C.println("row: " + row);
        }
        db.close(connection);
    }
    Author:
    Rem Collier
    • Constructor Detail

      • DB

        public DB()
    • Method Detail

      • installDriver

        public boolean installDriver​(String driver)
        Action that installs a JDBC driver.

        The parameter is the fully qualified class name of the JDBC Driver

        Parameters:
        driver - a qualified class name for a driver
        Returns:
        true if the action succeeds, false otherwise
      • tableExists

        public Formula tableExists​(Connection connection,
                                   String table)
        Formula that checks if the provided table exists.
        Parameters:
        connection - a SQL Connection
        table - the name of the table
        Returns:
        the formula TRUE if the table exists, the formula FALSE otherwise
      • getConnection

        public boolean getConnection​(String connectionString,
                                     ActionParam<Connection> connection)
        Action to create a connection to a database.
        Parameters:
        connectionString - the connection string
        connection - a reference to an SQL Collection object.
        Returns:
        true if the action succeeds, false otherwise
      • close

        public boolean close​(Connection connection)
        Action to close an existing SQL Connection
        Parameters:
        connection - the SQL connection
        Returns:
        true if the action succeeds, false otherwise
      • rawUpdateQuery

        public boolean rawUpdateQuery​(Connection connection,
                                      String sql)
        Action to execute a basic SQL Query.

        This action is considered raw because it takes an SQL statement in string form as the input.

        Parameters:
        connection - and SQL connection
        sql - the SQL update query
        Returns:
        true if the action succeeds, false otherwise
      • rawSelectQuery

        public boolean rawSelectQuery​(Connection connection,
                                      String sql,
                                      ActionParam<ListTerm> results)
        Action that executes a basic select query on a connection.

        This action is considered raw because it takes an SQL statement in string form as the input.

        Parameters:
        connection - a SQL connection
        sql - the SQL Query
        results - a list that contains the results of the query (list of list)
        Returns:
        true if the action succeeds, false otherwise
      • where

        public boolean where​(SQLQuery query,
                             String field,
                             String value)
        Action to add a where constrain for a string value
        Parameters:
        query - the Query object
        field - the field name
        value - the value
        Returns:
        true if the action succeeds, false otherwise
      • where

        public boolean where​(SQLQuery query,
                             String field,
                             int value)
        Action to add a where constrain for an int value
        Parameters:
        query - the Query object
        field - the field name
        value - the value
        Returns:
        true if the action succeeds, false otherwise
      • where

        public boolean where​(SQLQuery query,
                             String field,
                             long value)
        Action to add a where constrain for a long value
        Parameters:
        query - the Query object
        field - the field name
        value - the value
        Returns:
        true if the action succeeds, false otherwise
      • where

        public boolean where​(SQLQuery query,
                             String field,
                             float value)
        Action to add a where constrain for a float value
        Parameters:
        query - the Query object
        field - the field name
        value - the value
        Returns:
        true if the action succeeds, false otherwise
      • where

        public boolean where​(SQLQuery query,
                             String field,
                             double value)
        Action to add a where constrain for a double value
        Parameters:
        query - the Query object
        field - the field name
        value - the value
        Returns:
        true if the action succeeds, false otherwise
      • where

        public boolean where​(SQLQuery query,
                             String field,
                             boolean value)
        Action to add a where constrain for a boolean value
        Parameters:
        query - the Query object
        field - the field name
        value - the value
        Returns:
        true if the action succeeds, false otherwise
      • where

        public boolean where​(SQLQuery query,
                             String field,
                             char value)
        Action to add a where constrain for a char value
        Parameters:
        query - the Query object
        field - the field name
        value - the value
        Returns:
        true if the action succeeds, false otherwise
      • where

        public boolean where​(SQLQuery query,
                             String field,
                             Object value)
        Action to add a where constrain for an object
        Parameters:
        query - the Query object
        field - the field name
        value - the value
        Returns:
        true if the action succeeds, false otherwise
      • where

        public boolean where​(SQLQuery query,
                             ListTerm fields,
                             ListTerm values)
        Action that allows you to add multiple where constrains in a single step.

        The number of fields and values should match.

        All fields should be of type string

        Values should not be lists

        Parameters:
        query - the Query object
        fields - a list of field names (strings)
        values - a list of values
        Returns:
        true if the action succeeds, false otherwise
      • select

        public SQLQuery select​(String table)
        Term that creates a SELECT query of the form: SELECT * FROM table
        Parameters:
        table - the name of the table
        Returns:
        a SQLQuery instance
      • select

        public SQLQuery select​(String table,
                               String field)
        Term that creates a SELECT query of the form: SELECT field FROM table
        Parameters:
        table - the name of the table
        field - the field name
        Returns:
        a SQLQuery instance
      • select

        public SQLQuery select​(String table,
                               ListTerm fields)
        Term that creates a SELECT query of the form: SELECT listitem1, listitem2, ... FROM table
        Parameters:
        table - the name of the table
        fields - a list of field names
        Returns:
        a SQLQuery instance
      • select

        public SQLQuery select​(String table,
                               ListTerm where_fields,
                               ListTerm where_values)
        Term that creates a SELECT query of the form:
        SELECT * FROM table WHERE wf1=wv1 AND wf2=wv2 AND ...
        Parameters:
        table - the name of the table
        where_fields - a list of field names for the where clause
        where_values - a list of values for the where clause
        Returns:
        a SQLQuery instance
      • select

        public SQLQuery select​(String table,
                               ListTerm fields,
                               ListTerm where_fields,
                               ListTerm where_values)
        Term that creates a SELECT query of the form:
        SELECT listitem1, listitem2, ... FROM table WHERE wf1=wv1 AND wf2=wv2 AND ...
        Parameters:
        table - the name of the table
        fields - a list of field names
        where_fields - a list of field names for the where clause
        where_values - a list of values for the where clause
        Returns:
        a SQLQuery instance
      • get

        public boolean get​(Connection connection,
                           SQLQuery query,
                           ActionParam<ListTerm> results)
        Action that executes a select query on the database.
        Parameters:
        connection - the database connection
        query - a Query object
        results - a container for the results
        Returns:
        true if the action succeeds, false otherwise
      • insert

        public SQLQuery insert​(String table,
                               ListTerm fields,
                               ListTerm values)
        Term that creates a SELECT query of the form:
        INSERT INTO table VALUES (wf1=wv1, wf2=wv2)
        Parameters:
        table - the name of the table
        fields - a list of field names for the where clause
        values - a list of values for the where clause
        Returns:
        true if the action succeeds, false otherwise
      • update

        public boolean update​(Connection connection,
                              SQLQuery query)
        Action that executes an update query on the database.
        Parameters:
        connection - the database connection
        query - a Query object
        Returns:
        true if the action succeeds, false otherwise