API:EPrints/Database
EPrints 3 Reference: Directory Structure - Metadata Fields - Repository Configuration - XML Config Files - XML Export Format - EPrints data structure - Core API - Data Objects
Latest Source Code (3.4, 3.3) | Revision Log | Before editing this page please read Pod2Wiki
Contents
- 1 NAME
- 2 DESCRIPTION
- 3 METHODS
- 3.1 build_connection_string
- 3.2 create
- 3.3 new
- 3.4 connect
- 3.5 disconnect
- 3.6 error
- 3.7 begin
- 3.8 commit
- 3.9 rollback
- 3.10 create_archive_tables
- 3.11 drop_archive_tables
- 3.12 create_dataset_tables
- 3.13 drop_dataset_tables
- 3.14 create_dataset_index_tables
- 3.15 create_dataset_ordervalues_tables
- 3.16 type_info
- 3.17 get_column_type
- 3.18 create_table
- 3.19 has_sequence
- 3.20 create_sequence
- 3.21 drop_sequence
- 3.22 drop_column
- 3.23 get_primary_key
- 3.24 create_index
- 3.25 create_unique_index
- 3.26 _update
- 3.27 _update_quoted
- 3.28 insert
- 3.29 insert_quoted
- 3.30 delete_from
- 3.31 add_record
- 3.32 prep_int
- 3.33 prep_value
- 3.34 prep_like_value
- 3.35 quote_value
- 3.36 quote_int
- 3.37 quote_binary
- 3.38 quote_ordervalue
- 3.39 quote_identifier
- 3.40 update
- 3.41 remove
- 3.42 create_counters
- 3.43 has_counter
- 3.44 create_counter
- 3.45 remove_counters
- 3.46 drop_counter
- 3.47 next_doc_pos
- 3.48 counter_current
- 3.49 counter_next
- 3.50 counter_minimum
- 3.51 counter_reset
- 3.52 cache_exp
- 3.53 cache
- 3.54 cache_table
- 3.55 get_index_ids
- 3.56 search
- 3.57 drop_cache
- 3.58 count_table
- 3.59 from_cache
- 3.60 drop_orphan_cache_tables
- 3.61 get_single
- 3.62 get_all
- 3.63 get_cache_ids
- 3.64 get_dataobjs
- 3.65 get_values
- 3.66 sort_values
- 3.67 get_ids_by_field_values
- 3.68 do
- 3.69 prepare
- 3.70 prepare_select
- 3.71 execute
- 3.72 has_dataset
- 3.73 has_field
- 3.74 add_field
- 3.75 remove_field
- 3.76 rename_field
- 3.77 exists
- 3.78 set_debug
- 3.79 create_version_table
- 3.80 drop_version_table
- 3.81 set_version
- 3.82 has_table
- 3.83 has_column
- 3.84 index_name
- 3.85 drop_table
- 3.86 clear_table
- 3.87 rename_table
- 3.88 swap_table
- 3.89 get_tables
- 3.90 get_version
- 3.91 is_latest_version
- 3.92 valid_login
- 3.93 get_server_version
- 3.94 get_default_charset
- 3.95 get_default_collation
- 3.96 get_driver_name
- 3.97 dequeue_events
- 3.98 prepare_regexp
- 3.99 sql_as
- 3.100 sql_like
- 4 COPYRIGHT
NAME
EPrints::Database - a connection to the SQL database for an eprints session.
DESCRIPTION
EPrints Database Access Module
Provides access to the backend database. All database access done via this module, in the hope that the backend can be replaced as easily as possible.
The database object is created automatically when you start a new eprints session. To get a handle on it use:
$db = $session->get_database
Cross-database Support
Any use of SQL must use quote_identifier to quote database tables and columns. The only exception to this are the Database::* modules which provide database-driver specific extensions.
Variables that are database quoted are prefixed with 'Q_'.
METHODS
build_connection_string
$dbstr = EPrints::Database::build_connection_string( %params )
Build the string to use to connect to the database via DBI. %params must contain dbname, and may also contain dbport, dbhost and dbsock.
create
$db = $db->create( $username, $password )
Create and connect to a new database using super user account $username and $password.
new
$db = EPrints::Database->new( $session )
Create a connection to the database.
connect
$foo = $db->connect
Connects to the database.
disconnect
$foo = $db->disconnect
Disconnects from the EPrints database. Should always be done before any script exits.
error
$errstr = $db->error
Return a string describing the last SQL error.
begin
$db->begin
Begin a transaction.
commit
$db->commit
Commit the previous begun transaction.
rollback
$db->rollback
Rollback the partially completed transaction.
create_archive_tables
$success = $db->create_archive_tables
Create all the SQL tables for each dataset.
drop_archive_tables
$db->drop_archive_tables()
Destroy all tables used by eprints in the database.
create_dataset_tables
$success = $db->create_dataset_tables( $dataset )
Create all the SQL tables for a single dataset.
drop_dataset_tables
$db->drop_dataset_tables( $dataset )
Drop all the SQL tables for a single dataset.
create_dataset_index_tables
$success = $db->create_dataset_index_tables( $dataset )
Create all the index tables for a single dataset.
create_dataset_ordervalues_tables
$success = $db->create_dataset_ordervalues_tables( $dataset )
Create all the ordervalues tables for a single dataset.
type_info
$type_info = $db->type_info( DATA_TYPE )
See DBI/type_info.
get_column_type
$real_type = $db->get_column_type( NAME, TYPE, NOT_NULL, [ LENGTH/PRECISION ], [ SCALE ], %opts )
Returns a SQL column definition for NAME of type TYPE, usually something like:
$name $type($length,$scale) [ NOT NULL ]
If NOT_NULL is true column will be set "not null".
LENGTH/PRECISION and SCALE control the maximum lengths of character or decimal types (see below).
Other options available to refine the column definition:
langid - character set/collation to use sorted - whether this column will be used to order by
langid is mapped to real database values by the "dblanguages" configuration option. The database may not be able to order the request column type in which case, if sorted is true, the database may use a substitute column type.
TYPE is the SQL type. The types are constants defined by this module, to import them use:
use EPrints::Database qw( :sql_types );
Supported types (n = requires LENGTH argument):
Character data: SQL_VARCHAR(n), SQL_LONGVARCHAR, SQL_CLOB.
Binary data: SQL_VARBINARY(n), SQL_LONGVARBINARY.
Integer data: SQL_TINYINT, SQL_SMALLINT, SQL_INTEGER, SQL_BIGINT.
Floating-point data: SQL_REAL, SQL_DOUBLE.
Time data: SQL_DATE, SQL_TIME.
The actual column types used will be database-specific.
create_table
$success = $db->create_table( $tablename, $dataset, $setkey, @fields );
Create the tables used to store metadata for this dataset: the main table and any required for multiple or mulitlang fields.
The first $setkey number of fields are used for a primary key.
has_sequence
$boolean = $db->has_sequence( $name )
Return true if a sequence of the given name exists in the database.
create_sequence
$success = $db->create_sequence( $seq_name )
Creates a new sequence object initialised to zero.
drop_sequence
$success = $db->drop_sequence( $seq_name )
Deletes a sequence object.
drop_column
$success = $db->drop_column( $table, $column )
Drops a column from a table.
get_primary_key
@columns = $db->get_primary_key( $tablename )
Returns the list of column names that comprise the primary key for $tablename.
Returns empty list if no primary key exists.
create_index
$success = $db->create_index( $tablename, @columns )
Creates an index over @columns for $tablename. Returns true on success.
create_unique_index
$success = $db->create_unique_index( $tablename, @columns )
Creates a unique index over @columns for $tablename. Returns true on success.
_update
$rows = $db->_update( $tablename, $keycols, $keyvals, $columns, @values )
UPDATES $tablename where $keycols equals $keyvals and returns the number of rows affected.
Note! If no rows are affected the result is still 'true', see DBI's execute() method.
This method is internal.
_update_quoted
$success = $db->_update_quoted( $tablename, $keycols, $keyvals, $columns, @qvalues )
UPDATES $tablename where $keycols equals $keyvals. Won't quote $keyvals or @qvalues before use - use this method with care!
This method is internal.
insert
$success = $db->insert( $table, $columns, @values )
Inserts values into the table $table. If $columns is defined it will be used as a list of columns to insert into. @values is a list of arrays containing values to insert.
Values will be quoted before insertion.
insert_quoted
$success = $db->insert_quoted( $table, $columns, @qvalues )
Inserts values into the table $table. If $columns is defined it will be used as a list of columns to insert into. @qvalues is a list of arrays containing values to insert.
Values will NOT be quoted before insertion - care must be exercised!
delete_from
$success = $db->delete_from( $table, $columns, @values )
Perform a SQL DELETE FROM $table using $columns to build a where clause. @values is a list of array references of values in the same order as $columns.
If you want to clear a table completely use clear_table().
add_record
$success = $db->add_record( $dataset, $data )
Add the given data as a new record in the given dataset. $data is a reference to a hash containing values structured for a record in the that dataset.
prep_int
$mungedvalue = EPrints::Database::prep_int( $value )
Escape a numerical value for SQL. undef becomes NULL. Anything else becomes a number (zero if needed).
prep_value
$mungedvalue = EPrints::Database::prep_value( $value )
Escape a value for SQL. Modify value such that " becomes \" and \ becomes \\ and ' becomes \'
prep_like_value
$mungedvalue = EPrints::Database::prep_like_value( $value )
Escape an value for an SQL like field. In addition to ' " and \ also escapes % and _
quote_value
$str = $db->quote_value( $value )
Return a quoted value. To quote a 'like' value you should do:
my $str = $database->quote_value( EPrints::Database::prep_like_value( $foo ) . '%' );
quote_int
$str = $db->quote_int( $value )
Return a quoted integer value
quote_binary
$str = $db->quote_binary( $bytes )
Some databases (Oracle/PostgreSQL) require transforms of binary data to work correctly.
This method should be called on data containing nul bytes or back-slashes before being passed on quote_value.
quote_ordervalue
$str = $db->quote_ordervalue( $field, $value )
Some databases (Oracle) can't order by CLOBS so need special treatment when creating the ordervalues tables. This method allows any fixing-up required for string data before it's inserted.
quote_identifier
$str = $db->quote_identifier( @parts )
Quote a database identifier (e.g. table names). Multiple @parts will be joined by dot.
update
$success = $db->update( $dataset, $data, $changed, $insert )
Updates a record in the database with the given $data. Obviously the value of the primary key must be set.
This also updates the text indexes and the ordering keys.
remove
$success = $db->remove( $dataset, $id )
Attempts to remove the record with the primary key $id from the specified dataset.
create_counters
$success = $db->create_counters
Create the counters used to store the highest current id of eprints, users etc.
has_counter
$success = $db->has_counter( $counter )
Returns true if $counter exists.
create_counter
$success = $db->create_counter( $name )
Create and initialise to zero a new counter called $name.
remove_counters
$success = $db->remove_counters
Destroy all counters.
drop_counter
$success = $db->drop_counter( $name )
Destroy the counter named $name.
next_doc_pos
$n = $db->next_doc_pos( $eprintid )
Return the next unused document pos for the given eprintid.
counter_current
$n = $db->counter_current( $counter )
Return the value of the previous counter_next on $counter.
counter_next
$n = $db->counter_next( $counter )
Return the next unused value for the named counter. Returns undef if the counter doesn't exist.
counter_minimum
$db->counter_minimum( $counter, $value )
Ensure that the counter is set no lower than $value. This is used when importing eprints which may not be in scrict sequence.
counter_reset
$db->counter_reset( $counter )
Reset the counter. Use with caution.
cache_exp
$searchexp = $db->cache_exp( $cacheid )
Return the serialised Search of a the cached search with id $cacheid. Return undef if the id is invalid or expired.
cache
$cacheid = $db->cache( $searchexp, $dataset, $srctable, [$order], [$list] )
Create a cache of the specified search expression from the SQL table $srctable.
If $order is set then the cache is ordered by the specified fields. For example "-year/title" orders by year (descending). Records with the same year are ordered by title.
If $srctable is set to "LIST" then order is ignored and the list of ids is taken from the array reference $list.
If $srctable is set to "ALL" every matching record from $dataset is added to the cache, optionally ordered by $order.
cache_table
$tablename = $db->cache_table( $id )
Return the SQL table used to store the cache with id $id.
get_index_ids
$ids = $db->get_index_ids( $table, $condition )
Return a reference to an array of the distinct primary keys from the given SQL table which match the specified condition.
search
$ids = $db->search( $keyfield, $tables, $conditions, [$main_table_alias] )
Return a reference to an array of ids - the results of the search specified by $conditions accross the tables specified in the $tables hash where keys are tables aliases and values are table names.
If no table alias is passed then M is assumed.
drop_cache
$db->drop_cache( $id )
Remove the cached search with the given id.
count_table
$n = $db->count_table( $tablename )
Return the number of rows in the specified SQL table.
from_cache
$foo = $db->from_cache( $dataset, $cacheid, [$offset], [$count], [$justids] )
Return a reference to an array containing all the items from the given dataset that have id's in the specified cache. The cache may be specified either by id or serialised search expression.
$offset is an offset from the start of the cache and $count is the number of records to return.
If $justids is true then it returns just an ref to an array of the record ids, not the objects.
drop_orphan_cache_tables
$c = $db->drop_orphan_cache_tables
Drop tables called "cacheXXX" where XXX is an integer. Returns the number of tables dropped.
get_single
$obj = $db->get_single( $dataset, $id )
Return a single item from the given dataset. The one with the specified id.
get_all
$items = $db->get_all( $dataset )
Returns a reference to an array with all the items from the given dataset.
get_cache_ids
@ids = $db->get_cache_ids( $dataset, $cachemap, $offset, $count )
Returns a list of $count ids from $cache_id starting at $offset and in the order in the cachemap.
get_dataobjs
@dataobjs = $db->get_dataobjs( $dataset [, $id [, $id ] ] )
Retrieves the records in $dataset with the given $id(s). If an $id doesn't exist in the database it will be ignored.
get_values
$foo = $db->get_values( $field, $dataset )
Return a reference to an array of all the distinct values of the EPrints::MetaField specified.
sort_values
$values = $db->sort_values( $field, $values [, $langid ] )
ALPHA!!! Liable to API change!!!
Sorts and returns the list of $values using the database.
$field is used to get the order value for each value. $langid (or $session->get_langid if unset) is used to determine the database collation to use when sorting the resulting order values.
get_ids_by_field_values
$ids = $db->get_ids_by_field_values( $field, $dataset [ %opts ] )
Return a reference to a hash table where the keys are field value ids and the value is a reference to an array of ids.
do
$success = $db->do( $sql )
Execute the given SQL.
prepare
$sth = $db->prepare( $sql )
Prepare the given $sql and return a handle on it.
prepare_select
$sth = $db->prepare_select( $sql [, %options ] )
Prepare a SELECT statement $sql and return a handle to it. After preparing a statement use execute() to execute it.
The LIMIT SQL keyword is not universally supported, to specify a LIMIT you must use the limit option.
Options:
limit - limit the number of rows returned offset - return limit number of rows after offset
execute
$success = $db->execute( $sth, $sql )
Execute the SQL prepared earlier. $sql is only passed in for debugging purposes.
has_dataset
$db->has_dataset( $dataset )
Returns true if $dataset exists in the database or has no database tables.
This does not check that all fields are configured - see has_field().
has_field
$db->has_field( $dataset, $field )
Returns true if $field is in the database for $dataset.
add_field
$db->add_field( $dataset, $field [, $force ] )
Add $field to $dataset's tables.
If $force is true will modify/replace an existing column (use with care!).
remove_field
$db->remove_field( $dataset, $field )
Remove $field from $dataset's tables.
rename_field
$ok = $db->rename_field( $dataset, $field, $old_name )
Rename a $field in the database from it's old name $old_name.
Returns true if the field was successfully renamed.
exists
$boolean = $db->exists( $dataset, $id )
Return true if a record with the given primary key exists in the dataset, otherwise false.
set_debug
$db->set_debug( $boolean )
Set the SQL debug mode to true or false.
create_version_table
$db->create_version_table
Make the version table (and set the only value to be the current version of eprints).
drop_version_table
$db->drop_version_table
Drop the version table.
set_version
$db->set_version( $versionid );
Set the version id table in the SQL database to the given value (used by the upgrade script).
has_table
$boolean = $db->has_table( $tablename )
Return true if a table of the given name exists in the database.
has_column
$boolean = $db->has_column( $tablename, $columnname )
Return true if the a table of the given name has a column named $columnname in the database.
index_name
$name = $db->index_name( $table, @columns )
Returns the name of the first index that starts with @columns on the $table table.
Returns undef if no index exists.
drop_table
$db->drop_table( $tablename )
Delete the named table. Use with caution!
clear_table
$db->clear_table( $tablename )
Clears all records from the given table, use with caution!
rename_table
$db->rename_table( $tablename, $newtablename )
Renames the table from the old name to the new one.
swap_table
$db->swap_table( $table_a, $table_b )
Swap table a and table b.
get_tables
@tables = $db->get_tables
Return a list of all the tables in the database.
get_version
$version = $db->get_version
Return the version of eprints which the database is compatable with or undef if unknown (before v2.1).
is_latest_version
$boolean = $db->is_latest_version
Return true if the SQL tables are in the correct configuration for this edition of eprints. Otherwise false.
valid_login
$db->valid_login( $username, $password )
Returns whether the clear-text $password matches the stored crypted password for $username.
get_server_version
$version = $db->get_server_version
Return the database server version.
get_default_charset
$charset = $db->get_default_charset( LANGUAGE )
Return the character set to use for LANGUAGE.
Returns undef if character sets are unsupported.
get_default_collation
$collation = $db->get_default_collation( LANGUAGE )
Return the collation to use for LANGUAGE.
Returns undef if collation is unsupported.
get_driver_name
$driver = $db->get_driver_name
Return the database driver name.
dequeue_events
@events = $db->dequeue_events( $n )
Attempt to dequeue upto $n events. May return between 0 and $n events depending on parallel processes and how many events are remaining on the queue.
prepare_regexp
$sql = $db->prepare_regexp( $quoted_column, $quoted_value )
The syntax used for regular expressions varies across databases. This method takes two quoted values and returns a SQL expression that will apply the regexp ($quoted_value) to the column ($quoted_column).
sql_as
$sql = $db->sql_AS()
Returns the syntactic glue to use when aliasing. SQL 92 DBs will happilly use " AS " but some DBs (Oracle!) won't accept it.
sql_like
$sql = $db->sql_LIKE()
Returns the syntactic glue to use when making a case-insensitive LIKE. PostgreSQL requires "ILIKE" while everything else uses "LIKE" and the column collation.
COPYRIGHT