Using Database Access
Database features of GAMA provide a set of actions on Database Management Systems (DBMS). Database features are implemented in the irit.gaml.extensions.database plug-in with these features:
- Agents can execute SQL queries (create, Insert, select, update, drop, delete) to various kinds of DBMS.
These features are implemented in two kinds of components: skill (SQLSKILL
) and agent (AgentDB
).
SQLSKILL
and AgentDB
provide almost the same features (the same set of actions on DBMS) but with certain slight differences:
- An agent of species
AgentDB
will maintain a unique connection to the database during the whole simulation. The connection is thus initialized when the agent is created and destroyed when it is killed. - In contrast, an agent of a species with the
SQLSKILL
skill will open a connection each time it wants to execute a query. This means that each action will be composed of three running steps:- Make a database connection.
- Execute an SQL statement.
- Close database connection.
An agent with the
SQLSKILL
spends a lot of time to create/close the connection each time it needs to send a query; it saves the database connection (a DBMS often limits the number of simultaneous connections). In contrast, anAgentDB
agent only needs to establish one database connection that can be used for any action. Because it does not need to create and close the database connection for each action, therefore actions ofAgentDB
agents are executed faster than the actions ofSQLSKILL
ones but we must pay a connection for each agent.
With an inheritance agent of species AgentDB
or an agent of a species using SQLSKILL
, we can query data from relational database to create agents, define the environment, or analyze or store simulation results in RDBMS. The database features help us to have more flexibility in the management of simulation models and analysis of simulation results.
Description​
- Plug-in: irit.gaml.extensions.database
- Author: TRUONG Minh Thai, Frederic AMBLARD, Benoit GAUDOU, Christophe SIBERTIN-BLANC
Supported DBMS​
The following DBMS are currently supported:
- SQLite
- MySQL
- PostgreSQL: The GIS extension needs to be installed and activated in the database.
Note that, MySQL and Postgres DBMSs require a dedicated server to work while SQLite only needs a file to be accessed.
All the actions are independent from the chosen DBMS. Only the connection parameters are DBMS-dependent.
We chose to implement 3 main query actions:
select
: that will execute the SELECT SQL queries. It will return a result dataset.insert
: that will execute the INSERT SQL queries. It will return the number of records that are affected by the SQL query.executeUpdate
: that can execute any CREATE/INSERT/DROP/DELETE SQL queries (basically all the queries that do not return a dataset. It generalizes theinsert
action.
SQLSKILL skill​
Define a species that uses the SQLSKILL
skill​
Example of declaration:
species toto skills: [SQLSKILL] {
//insert your descriptions here
}
Agents with such a skill can use new actions defined in the skill. All these actions need information for the database connection.
Map of connection parameters for SQL​
In the actions defined in the SQLSKILL
, a parameter containing the connection parameters is required. It is a map with key::value pairs with the following keys:
dbtype
(mandatory): DBMS type value. Its value is a string. We must use "mysql" when we want to connect to a MySQL. That is the same for "postgres", "sqlite" (ignore case sensitive)host
(optional): Host name or IP address of data server. It is absent when we work with SQlite.port
(optional): Port of connection. It is not required when we work with SQLite.database
(mandatory): Name of database. It is the file name including the path when we work with SQLite.user
(optional): Username. It is not required when we work with SQLite.passwd
(optional): Password. It is not required when we work with SQLite.srid
(optional): srid (Spatial Reference Identifier) corresponds to a spatial reference system. This value is specified when GAMA connects to spatial database. If it is absent then GAMA uses spatial reference system defined in Preferences->External configuration.
Example: Definitions of connection parameters
// POSTGRES connection parameter
map <string, string> POSTGRES <- [
'host'::'localhost',
'dbtype'::'postgres',
'database'::'BPH',
'port'::'5432',
'user'::'postgres',
'passwd'::'abc'];
//SQLite
map <string, string> SQLITE <- [
'dbtype'::'sqlite',
'database'::'../includes/meteo.db'];
// MySQL connection parameter
map <string, string> MySQL <- [
'host'::'localhost',
'dbtype'::'MySQL',
'database'::'', // it may be a empty string
'port'::'3306',
'user'::'root',
'passwd'::'abc'];
Action testConnection
: test a connection to a database​
Syntax:
testConnection (params: map <string, string>) The action tests the connection to a given database.
- Return: boolean. It is:
true
: the agent can connect to the DBMS (to the given Database with the given name and password).false
: the agent cannot connect (either the server is not started, the database does not exist or the user/password are not correct).
- Arguments:
params
(type =map <string, string>
): map of connection parameters
- Exceptions: GamaRuntimeException
Example: Check a connection to a MySQL database.
// Needs to be executed in the context of an agent with the SQLSKILL skill.
// MySQL is the connection parameters map defined above.
if (testConnection(MySQL)){
write "Connection is OK" ;
}else{
write "Connection is false" ;
}
Action select
: select data from a database​
Syntax:
select (params: map <string, string>, select: string, values: list) The action creates a connection to a DBMS and executes the select statement. If the connection or selection fails then it throws a GamaRuntimeException.
- Return:
list<list>
. If the selection succeeds, it returns a list with three elements:- The first element is a list of column names.
- The second element is a list of column types.
- The third element is a data set.
- Arguments:
params
(type = map<string,string>): map containing the connection parametersselect
(type = string): A SQL query returning values, i.e. a SELECT query. The selection query can be a parametric query (i.e. it can contain question marks).values
(type = list): List of values that are used to replace question marks. This is an optional parameter.
- Exceptions: GamaRuntimeException
Example: select data from table points.
map <string, string> PARAMS <- ['dbtype'::'sqlite', 'database'::'../includes/meteo.db'];
list<list> t <- select(PARAMS, "SELECT * FROM points ;");
Example: select data from table point with question marks from table points.
map <string, string> PARAMS <- ['dbtype'::'sqlite', 'database'::'../includes/meteo.db'];
list<list> t <- select(params: PARAMS,
select: "SELECT temp_min FROM points where (day>? and day<?);",
values: [10,20] );
Action insert
: Insert data into a database​
Syntax:
insert (param: map<string,string>, into: string, columns: list<string>, values: list) The action creates a connection to a DBMS and executes the insert statement. If the connection or insertion fails then it throws a_GamaRuntimeException_.
- Return: int
If the insertion succeeds, it returns a number of records inserted by the insert.
- Arguments:
*
params
(type = map<string,string>): map containing the connection parameters. *into
(type = string): the table name. *columns
(type=list<string>): list of column names of the table. It is an optional argument. If it is not specified then all columns of table are selected. *values
(type=list): list of values that are used to insert into the table chosen columns. Hence the columns and values must have same size. - Exceptions:_GamaRuntimeException
Example: Insert data into table registration.
map<string, string> PARAMS <- ['dbtype'::'sqlite', 'database'::'../../includes/Student.db'];
do insert (params: PARAMS,
into: "registration",
values: [102, 'Mahnaz', 'Fatma', 25]);
do insert (params: PARAMS,
into: "registration",
columns: ["id", "first", "last"],
values: [103, 'Zaid tim', 'Kha']);
int n <- insert (params: PARAMS,
into: "registration",
columns: ["id", "first", "last"],
values: [104, 'Bill', 'Clark']);