This database library provides a light abstraction layer over any number of third-party database libraries. It doesn't actually perform any database operations itself. You must include at least one database plugin library for this to library to work correctly.
include mvc/database.e
include mvc/db_mysql.e
atom conn = db_connect( "mysql://user:password@localhost/mydb" )
atom result = db_query( "SELECT * FROM mytable" )
object row = db_fetch( result )
while sequence( row ) do
row = db_fetch( result )
end while
db_free( result )
db_disconnect()
You'll notice in the above example that conn
isn't actually reused by any of the other database routines. That's
because each call to db_connect()
will update the global connection state for your application and, as long as you
only need one connection, you don't need to continue passing conn
around to other routines. You can also use
db_select()
to update the global connection state at any time, or pass conn
manually as the last parameter of each
database routine.
Each plugin must register a unique "protocol" for use in the db_connect()
connection URL. To remain consistent, this
should be the same string as found in the plugin file name, e.g. db_mysql.e
registers mysql
as its protocol.
Include at least one of these libraries to in order to connect to a database. Currently only the MySQL is implemented.
Third-party wrappers are stored in the db/
directory and shared libraries (.so, .dll) should be placed in the bin/
directory whenever necessary (usually just on Windows).
-
mvc/db_eusql.e
-
mvc/db_mysql.e
-
mvc/db_postgres.e
-
mvc/db_sqlite3.e
add_handler
add_protocol
db_affected_rows
db_connect
db_disconnect
db_error
db_fetch
db_free
db_insert_id
db_query
db_select
include mvc/database.e
public procedure add_handler( integer proto_id, integer func_id, integer rtn_id )
Adds a new function handler. Used internally by database libraries.
Parameters
proto_id
- the protocol ID registered byadd_protocol()
func_id
- the database function IDDB_CONNECT
DB_DISCONNECT
DB_TABLE_EXISTS
DB_QUERY
DB_FETCH
DB_FREE
DB_ERROR
DB_INSERT_ID
DB_AFFECTED_ROWS
rtn_id
- the routine ID of the function for this protocol and ID
include mvc/database.e
public function add_protocol( sequence proto )
Adds a new protocol handler. Used internally by database libraries.
Parameters
proto
- the protocol name to register
Returns
The unique ID of the registered protocol.
include mvc/database.e
public function db_affected_rows( atom conn = current_conn )
Get the number of rows affected by the last query.
Parameters
conn
the database connection handle
Returns
Depends on the underlying database. Should return the number of rows affected by the last query.
include mvc/database.e
public function db_connect( sequence url, integer timeout = DEFAULT_TIMEOUT )
Connect to a database.
Parameters
url
- a connection URL in the formatprotocol://username:password@hostname/database
timeout
- the connection timeout in milliseconds (default is5000
)
Returns
The connection handle, or NULL
if a connection could not be made. Also stores this as the global connection state.
include mvc/database.e
public procedure db_disconnect( atom conn = current_conn )
Disconnect from a database.
Parameters
conn
- the connection handle to disconnect
include mvc/database.e
public function db_error( atom conn = current_conn )
Returns the last error from the database.
Parameters
- none
Returns
Depends on the underlying database. Should return a sequence containing the description of the last error.
include mvc/database.e
public function db_fetch( atom result, atom conn = current_conn )
Fetch the next row from the query result.
Parameters
result
- the query result handleconn
the database connection handle
Returns
Depends on the underlying database. Should return a sequence of row fields or NULL
when there are no more rows in the result.
include mvc/database.e
public procedure db_free( atom result, atom conn = current_conn )
Free the query results.
Parameters
result
- the query result handleconn
the database connection handle
include mvc/database.e
public function db_insert_id( atom conn = current_conn )
Get the last inserted row ID value.
Parameters
conn
the database connection handle
Returns
Depends on the underlying database. Should return the last inserted row ID value.
include mvc/database.e
public function db_query( sequence query, object params = {}, atom conn = current_conn )
Execute a database query.
Parameters
query
- the databse query to executeparams
- parameters to be used in the queryconn
- the database connection handle
Returns
Depends on the underlying database. Usually returns a result handle for SELECT
queries to be used with db_fetch()
, or the number of affected rows for INSERT
, UPDATE
, and DELETE
queries. Will return -1
if there was an internal error.
include mvc/database.e
public function db_select( atom conn )
Select another already connected database.
Parameters
conn
- the connection handle to select as the global connection
Returns
Returns TRUE
if the database connection handle was changed.