Package astra.util
Class DB
- java.lang.Object
-
- astra.core.Module
-
- astra.util.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
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class astra.core.Module
Module.ACTION, Module.EVENT, Module.FORMULA, Module.SENSOR, Module.SUPPRESS_NOTIFICATIONS, Module.TERM
-
-
Constructor Summary
Constructors Constructor Description DB()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanclose(Connection connection)Action to close an existing SQL Connectionbooleanget(Connection connection, SQLQuery query, ActionParam<ListTerm> results)Action that executes a select query on the database.booleangetConnection(String connectionString, ActionParam<Connection> connection)Action to create a connection to a database.SQLQueryinsert(String table, ListTerm fields, ListTerm values)Term that creates a SELECT query of the form:
INSERT INTO table VALUES (wf1=wv1, wf2=wv2)booleaninstallDriver(String driver)Action that installs a JDBC driver.booleanrawSelectQuery(Connection connection, String sql, ActionParam<ListTerm> results)Action that executes a basic select query on a connection.booleanrawUpdateQuery(Connection connection, String sql)Action to execute a basic SQL Query.SQLQueryselect(String table)Term that creates a SELECT query of the form: SELECT * FROM tableSQLQueryselect(String table, ListTerm fields)Term that creates a SELECT query of the form: SELECT listitem1, listitem2, ...SQLQueryselect(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 ...SQLQueryselect(String table, ListTerm fields, ListTerm where_fields, ListTerm where_values)Term that creates a SELECT query of the form:
SELECT listitem1, listitem2, ...SQLQueryselect(String table, String field)Term that creates a SELECT query of the form: SELECT field FROM tableFormulatableExists(Connection connection, String table)Formula that checks if the provided table exists.booleanupdate(Connection connection, SQLQuery query)Action that executes an update query on the database.booleanwhere(SQLQuery query, ListTerm fields, ListTerm values)Action that allows you to add multiple where constrains in a single step.booleanwhere(SQLQuery query, String field, boolean value)Action to add a where constrain for a boolean valuebooleanwhere(SQLQuery query, String field, char value)Action to add a where constrain for a char valuebooleanwhere(SQLQuery query, String field, double value)Action to add a where constrain for a double valuebooleanwhere(SQLQuery query, String field, float value)Action to add a where constrain for a float valuebooleanwhere(SQLQuery query, String field, int value)Action to add a where constrain for an int valuebooleanwhere(SQLQuery query, String field, long value)Action to add a where constrain for a long valuebooleanwhere(SQLQuery query, String field, Object value)Action to add a where constrain for an objectbooleanwhere(SQLQuery query, String field, String value)Action to add a where constrain for a string value
-
-
-
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 Connectiontable- 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 stringconnection- 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 connectionsql- 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 connectionsql- the SQL Queryresults- 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 objectfield- the field namevalue- 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 objectfield- the field namevalue- 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 objectfield- the field namevalue- 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 objectfield- the field namevalue- 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 objectfield- the field namevalue- 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 objectfield- the field namevalue- 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 objectfield- the field namevalue- 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 objectfield- the field namevalue- 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 objectfields- 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
SQLQueryinstance
-
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 tablefield- the field name- Returns:
- a
SQLQueryinstance
-
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 tablefields- a list of field names- Returns:
- a
SQLQueryinstance
-
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 tablewhere_fields- a list of field names for the where clausewhere_values- a list of values for the where clause- Returns:
- a
SQLQueryinstance
-
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 tablefields- a list of field nameswhere_fields- a list of field names for the where clausewhere_values- a list of values for the where clause- Returns:
- a
SQLQueryinstance
-
get
public boolean get(Connection connection, SQLQuery query, ActionParam<ListTerm> results)
Action that executes a select query on the database.- Parameters:
connection- the database connectionquery- a Query objectresults- 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 tablefields- a list of field names for the where clausevalues- 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 connectionquery- a Query object- Returns:
- true if the action succeeds, false otherwise
-
-