|
NameOpenXPKI::Server::Database - Handles database connections and encapsulates DB specific drivers/functions.DescriptionThis class contains the API to interact with the configured OpenXPKI database.Database driversWhile OpenXPKI supports several database types out of the box it still allows you to include new DBMS specific drivers without the need to change existing code.For more details see OpenXPKI::Server::Database::Role::Driver. Class structure.----------------------------. .----| OpenXPKI::Server::Database |---.--------------------. | '----------------------------' | | | | | | | | v v | | .---------------------. .---------------. | .-----' | SQL::Abstract::More | | DBIx::Handler | | | '---------------------' '---------------' | | . | v injected | .---------------------. . | | O:S:D::QueryBuilder |<...........' | '---------------------' | | .--------------. | '---->| O:S:D::Query | | '--------------' | | .------------------. '->| O:S:D::Driver::* | '------------------' . consumes . .---------------------. ....>| O:S:D::Role::Driver | . '---------------------' . .------------------------------. .--------------------------------. ....>| O:S:D::Role::SequenceSupport | or | O:S:D::Role::SequenceEmulation | . '------------------------------' '--------------------------------' . .------------------------------. .--------------------------------. '...>| O:S:D::Role::MergeSupport | or | O:S:D::Role::MergeEmulation | '------------------------------' '--------------------------------' AttributesConstructor parameters
Others
MethodsNote: all methods might throw an OpenXPKI::Exception if there are errors in the query or during it's execution.newConstructor.Named parameters: see attributes section above. selectSelects rows from the database and returns the results as a DBI::st statement handle.Please note that "NULL" values will be converted to Perl "undef". Subqueries can be realized using "subselect". Named parameters:
subselectBuilds a subquery to be used within another query and returns a reference to an ArrayRef.The returned structure is understood by SQL::Abstract which is used internally. E.g. to create the following query: SELECT title FROM books WHERE ( author_id IN ( SELECT id FROM authors WHERE ( legs > 2 ) ) ) you can use "subselect()" as follows: CTX('dbi')->select( from => "books", columns => [ "title" ], where => { author_id => CTX('dbi')->subselect("IN" => { from => "authors", columns => [ "id" ], where => { legs => { '>' => 2 } }, }), }, ); Positional parameters:
select_oneSelects one row from the database and returns the results as a HashRef (column name => value) by calling "$sth->fetchrow_hashref".For parameters see "select". Returns "undef" if the query had no results. Please note that "NULL" values will be converted to Perl "undef". select_arraysSelects all rows from the database and returns them as an ArrayRef[ArrayRef]. This is a shortcut to "$dbi->select(...)->fetchall_arrayref([])".For parameters see "select". Please note that "NULL" values will be converted to Perl "undef". select_hashesSelects all rows from the database and returns them as an ArrayRef[HashRef]. This is a shortcut to "$dbi->select(...)->fetchall_arrayref({})".For parameters see "select". Please note that "NULL" values will be converted to Perl "undef". countTakes the same arguments as "select", wraps them into a subquery and return the number of rows the select would return. The parameters "order_by", "limit" and "offset" are ignored.insertInserts rows into the database and returns the number of affected rows.$db->insert( into => "certificate", values => { identifier => AUTO_ID, # use the sequence associated with this table cert_key => $key, ... } ); To automatically set a primary key to the next serial number (i.e. sequence associated with this table) set it to "AUTO_ID". You need to "use OpenXPKI::Server::Database;" to be able to use "AUTO_ID". Named parameters:
updateUpdates rows in the database and returns the number of affected rows.A WHERE clause is required to prevent accidential updates of all rows in a table. Please note that "NULL" values will be converted to Perl "undef". Named parameters:
mergeEither directly executes or emulates an SQL MERGE (you could also call it REPLACE) function and returns the number of affected rows.Please note that e.g. MySQL returns 2 (not 1) if an update was performed. So you should only use the return value to test for 0 / FALSE. Named parameters:
deleteDeletes rows in the database and returns the results as a DBI::st statement handle.To prevent accidential deletion of all rows of a table you must specify parameter "all" if you want to do that: CTX('dbi')->delete( from => "mytab", all => 1, ); Named parameters:
start_txnRecords the start of a new transaction (i.e. sets a flag) without database interaction.If the flag was already set (= another transaction is running), a "ROLLBACK" is performed first and an error message is logged. Please note that after a "fork()" the flag is be reset as the "DBI" handle is also reset (so there cannot be a running transaction). in_txnReturns "true" if a transaction is currently running, i.e. after "start_txn" was called but before "commit" or "rollback" where called.commitCommits a transaction.Logs an error if "start_txn" was not called first. rollbackRolls back a transaction.Logs an error if "start_txn" was not called first. insert_and_commitCalling this method is the same as:$db->start_txn; $db->insert(...); $db->commit; For more informations see "insert" in OpenXPKI::Server::Database. update_and_commitCalling this method is the same as:$db->start_txn; $db->update(...); $db->commit; For more informations see "update" in OpenXPKI::Server::Database. merge_and_commitCalling this method is the same as:$db->start_txn; $db->merge(...); $db->commit; For more informations see "merge" in OpenXPKI::Server::Database. delete_and_commitCalling this method is the same as:$db->start_txn; $db->delete(...); $db->commit; For more informations see "delete" in OpenXPKI::Server::Database. ################################################################################ Low level methodsThe following methods allow more fine grained control over the query processing.dbhReturns a fork safe DBI handle. Connects to the database if neccessary.To remain fork safe DO NOT CACHE this (also do not convert into a lazy attribute). runExecutes the given query and returns a DBI statement handle. Throws an exception in case of errors.my $sth; eval { $sth = $db->run($query); }; if (my $e = OpenXPKI::Exception->caught) { die "OpenXPKI exception executing query: $e"; } elsif ($@) { die "Unknown error: $e"; }; Parameters:
disconnectDisconnects from the database. Might be useful to e.g. remove file locks when using SQLite.
Visit the GSP FreeBSD Man Page Interface. |