Difference between revisions of "API:EPrints/Database"

From EPrints Documentation
Jump to: navigation, search
Line 71: Line 71:
 
====new====
 
====new====
  
$db = EPrints::Database->new( $repo )
+
<source lang="perl">$db = EPrints::Database-&gt;new( $repo )</source>
 
Create a connection to the database.
 
Create a connection to the database.
  
Line 81: Line 81:
 
====create====
 
====create====
  
$db = $db-&gt;create( $username, $password )
+
<source lang="perl">$db = $db-&gt;create( $username, $password )</source>
 
Create and connect to a new database using super user account $username and $password.
 
Create and connect to a new database using super user account $username and $password.
  
Line 91: Line 91:
 
====connect====
 
====connect====
  
$ok = $db-&gt;connect
+
<source lang="perl">$ok = $db-&gt;connect</source>
 
Connects to the database.  
 
Connects to the database.  
  
Line 101: Line 101:
 
====disconnect====
 
====disconnect====
  
$db-&gt;disconnect
+
<source lang="perl">$db-&gt;disconnect</source>
 
Disconnects from the EPrints database.
 
Disconnects from the EPrints database.
  
Line 111: Line 111:
 
====set_debug====
 
====set_debug====
  
$db-&gt;set_debug( $boolean )
+
<source lang="perl">$db-&gt;set_debug( $boolean )</source>
 
Set the SQL debug mode to true or false.
 
Set the SQL debug mode to true or false.
  
Line 121: Line 121:
 
====set_version====
 
====set_version====
  
$db-&gt;set_version( $versionid );
+
<source lang="perl">$db-&gt;set_version( $versionid );</source>
 
Set the version id table in the SQL database to the given value (used by the upgrade script).
 
Set the version id table in the SQL database to the given value (used by the upgrade script).
  
Line 131: Line 131:
 
====get_version====
 
====get_version====
  
$version = $db-&gt;get_version
+
<source lang="perl">$version = $db-&gt;get_version</source>
 
Returns the current database schema version.
 
Returns the current database schema version.
  
Line 141: Line 141:
 
====is_latest_version====
 
====is_latest_version====
  
$boolean = $db-&gt;is_latest_version
+
<source lang="perl">$boolean = $db-&gt;is_latest_version</source>
 
Return true if the database schema is in the correct configuration for this version of eprints. Otherwise false.
 
Return true if the database schema is in the correct configuration for this version of eprints. Otherwise false.
  
Line 151: Line 151:
 
====get_server_version====
 
====get_server_version====
  
$version = $db-&gt;get_server_version
+
<source lang="perl">$version = $db-&gt;get_server_version</source>
 
Return the database server version.
 
Return the database server version.
  
Line 161: Line 161:
 
====get_default_charset====
 
====get_default_charset====
  
$charset = $db-&gt;get_default_charset( LANGUAGE )
+
<source lang="perl">$charset = $db-&gt;get_default_charset( LANGUAGE )</source>
 
Return the character set to use for LANGUAGE.
 
Return the character set to use for LANGUAGE.
  
Line 173: Line 173:
 
====get_default_collation====
 
====get_default_collation====
  
$collation = $db-&gt;get_default_collation( LANGUAGE )
+
<source lang="perl">$collation = $db-&gt;get_default_collation( LANGUAGE )</source>
 
Return the collation to use for LANGUAGE.
 
Return the collation to use for LANGUAGE.
  
Line 185: Line 185:
 
====get_driver_name====
 
====get_driver_name====
  
$driver = $db-&gt;get_driver_name
+
<source lang="perl">$driver = $db-&gt;get_driver_name</source>
 
Return the database driver name.
 
Return the database driver name.
  
Line 195: Line 195:
 
====error====
 
====error====
  
$errstr = $db-&gt;error
+
<source lang="perl">$errstr = $db-&gt;error</source>
 
Return a string describing the last SQL error.
 
Return a string describing the last SQL error.
  
Line 205: Line 205:
 
====retry_error====
 
====retry_error====
  
$bool = $db-&gt;retry_error
+
<source lang="perl">$bool = $db-&gt;retry_error</source>
 
Returns true if the current error is a retry error.
 
Returns true if the current error is a retry error.
  
Line 215: Line 215:
 
====duplicate_error====
 
====duplicate_error====
  
$bool = $db-&gt;duplicate_error
+
<source lang="perl">$bool = $db-&gt;duplicate_error</source>
 
Returns true if the current error is a PRIMARY KEY or UNIQUE error.
 
Returns true if the current error is a PRIMARY KEY or UNIQUE error.
  
Line 225: Line 225:
 
====begin====
 
====begin====
  
$db-&gt;begin
+
<source lang="perl">$db-&gt;begin</source>
 
Begin a transaction.
 
Begin a transaction.
  
Line 235: Line 235:
 
====commit====
 
====commit====
  
$db-&gt;commit
+
<source lang="perl">$db-&gt;commit</source>
 
Commit the previously begun transaction.
 
Commit the previously begun transaction.
  
Line 245: Line 245:
 
====rollback====
 
====rollback====
  
$db-&gt;rollback
+
<source lang="perl">$db-&gt;rollback</source>
 
Roll-back the current transaction.
 
Roll-back the current transaction.
  
Line 255: Line 255:
 
====type_info====
 
====type_info====
  
$type_info = $db-&gt;type_info( DATA_TYPE )
+
<source lang="perl">$type_info = $db-&gt;type_info( DATA_TYPE )</source>
 
See {{API:PodLink|file=DBI|package_name=DBI|section=type_info|text=DBI/type_info}}.
 
See {{API:PodLink|file=DBI|package_name=DBI|section=type_info|text=DBI/type_info}}.
  
Line 265: Line 265:
 
====get_column_type====
 
====get_column_type====
  
$real_type = $db-&gt;get_column_type( NAME, TYPE, NOT_NULL, [ LENGTH/PRECISION ], [ SCALE ], %opts )
+
<source lang="perl">$real_type = $db-&gt;get_column_type( NAME, TYPE, NOT_NULL, [ LENGTH/PRECISION ], [ SCALE ], %opts )</source>
 
Returns a SQL column definition for NAME of type TYPE, usually something like:
 
Returns a SQL column definition for NAME of type TYPE, usually something like:
  
Line 312: Line 312:
 
====create_archive_tables====
 
====create_archive_tables====
  
$success = $db-&gt;create_archive_tables
+
<source lang="perl">$success = $db-&gt;create_archive_tables</source>
 
Create all the SQL tables for each dataset.
 
Create all the SQL tables for each dataset.
  
Line 322: Line 322:
 
====drop_archive_tables====
 
====drop_archive_tables====
  
$db-&gt;drop_archive_tables()
+
<source lang="perl">$db-&gt;drop_archive_tables()</source>
 
Destroy all tables used by eprints in the database.
 
Destroy all tables used by eprints in the database.
  
Line 332: Line 332:
 
====create_version_table====
 
====create_version_table====
  
$db-&gt;create_version_table
+
<source lang="perl">$db-&gt;create_version_table</source>
 
Make the version table (and set the only value to be the current version of eprints).
 
Make the version table (and set the only value to be the current version of eprints).
  
Line 342: Line 342:
 
====drop_version_table====
 
====drop_version_table====
  
$db-&gt;drop_version_table
+
<source lang="perl">$db-&gt;drop_version_table</source>
 
Drop the version table.
 
Drop the version table.
  
Line 352: Line 352:
 
====create_dataset_tables====
 
====create_dataset_tables====
  
$success = $db-&gt;create_dataset_tables( $dataset )
+
<source lang="perl">$success = $db-&gt;create_dataset_tables( $dataset )</source>
 
Create all the SQL tables for a single dataset.
 
Create all the SQL tables for a single dataset.
  
Line 362: Line 362:
 
====drop_dataset_tables====
 
====drop_dataset_tables====
  
$db-&gt;drop_dataset_tables( $dataset )
+
<source lang="perl">$db-&gt;drop_dataset_tables( $dataset )</source>
 
Drop all the SQL tables for a single dataset.
 
Drop all the SQL tables for a single dataset.
  
Line 372: Line 372:
 
====create_dataset_index_tables====
 
====create_dataset_index_tables====
  
$success = $db-&gt;create_dataset_index_tables( $dataset )
+
<source lang="perl">$success = $db-&gt;create_dataset_index_tables( $dataset )</source>
 
Create all the index tables for a single dataset.
 
Create all the index tables for a single dataset.
  
Line 382: Line 382:
 
====create_dataset_ordervalues_tables====
 
====create_dataset_ordervalues_tables====
  
$success = $db-&gt;create_dataset_ordervalues_tables( $dataset )
+
<source lang="perl">$success = $db-&gt;create_dataset_ordervalues_tables( $dataset )</source>
 
Create all the ordervalues tables for a single dataset.
 
Create all the ordervalues tables for a single dataset.
  
Line 392: Line 392:
 
====has_dataset====
 
====has_dataset====
  
$db-&gt;has_dataset( $dataset )
+
<source lang="perl">$db-&gt;has_dataset( $dataset )</source>
 
Returns true if $dataset exists in the database or has no database tables.
 
Returns true if $dataset exists in the database or has no database tables.
  
Line 404: Line 404:
 
====has_field====
 
====has_field====
  
$db-&gt;has_field( $dataset, $field )
+
<source lang="perl">$db-&gt;has_field( $dataset, $field )</source>
 
Returns true if $field is in the database for $dataset.
 
Returns true if $field is in the database for $dataset.
  
Line 414: Line 414:
 
====add_field====
 
====add_field====
  
$db-&gt;add_field( $dataset, $field [, $force ] )
+
<source lang="perl">$db-&gt;add_field( $dataset, $field [, $force ] )</source>
 
Add $field to $dataset's tables.
 
Add $field to $dataset's tables.
  
Line 426: Line 426:
 
====remove_field====
 
====remove_field====
  
$db-&gt;remove_field( $dataset, $field )
+
<source lang="perl">$db-&gt;remove_field( $dataset, $field )</source>
 
Remove $field from $dataset's tables.
 
Remove $field from $dataset's tables.
  
Line 436: Line 436:
 
====rename_field====
 
====rename_field====
  
$ok = $db-&gt;rename_field( $dataset, $field, $old_name )
+
<source lang="perl">$ok = $db-&gt;rename_field( $dataset, $field, $old_name )</source>
 
Rename a $field in the database from it's old name $old_name.
 
Rename a $field in the database from it's old name $old_name.
  
Line 448: Line 448:
 
====create_table====
 
====create_table====
  
$success = $db-&gt;create_table( $tablename, $setkey, @fields );
+
<source lang="perl">$success = $db-&gt;create_table( $tablename, $setkey, @fields );</source>
 
Creates a new table $tablename based on @fields.
 
Creates a new table $tablename based on @fields.
  
Line 460: Line 460:
 
====create_foreign_key====
 
====create_foreign_key====
  
$ok = $db-&gt;create_foreign_key( $main_table, $aux_table, $key_field )
+
<source lang="perl">$ok = $db-&gt;create_foreign_key( $main_table, $aux_table, $key_field )</source>
 
Create a foreign key relationship between $main_table and $aux_table using the $key_field.
 
Create a foreign key relationship between $main_table and $aux_table using the $key_field.
  
Line 472: Line 472:
 
====create_counters====
 
====create_counters====
  
$success = $db-&gt;create_counters
+
<source lang="perl">$success = $db-&gt;create_counters</source>
 
Create the counters used to store the highest current id of eprints, users etc.
 
Create the counters used to store the highest current id of eprints, users etc.
  
Line 482: Line 482:
 
====has_counter====
 
====has_counter====
  
$success = $db-&gt;has_counter( $counter )
+
<source lang="perl">$success = $db-&gt;has_counter( $counter )</source>
 
Returns true if $counter exists.
 
Returns true if $counter exists.
  
Line 492: Line 492:
 
====create_counter====
 
====create_counter====
  
$success = $db-&gt;create_counter( $name )
+
<source lang="perl">$success = $db-&gt;create_counter( $name )</source>
 
Create and initialise to zero a new counter called $name.
 
Create and initialise to zero a new counter called $name.
  
Line 502: Line 502:
 
====remove_counters====
 
====remove_counters====
  
$success = $db-&gt;remove_counters
+
<source lang="perl">$success = $db-&gt;remove_counters</source>
 
Destroy all counters.
 
Destroy all counters.
  
Line 512: Line 512:
 
====drop_counter====
 
====drop_counter====
  
$success = $db-&gt;drop_counter( $name )
+
<source lang="perl">$success = $db-&gt;drop_counter( $name )</source>
 
Destroy the counter named $name.
 
Destroy the counter named $name.
  
Line 522: Line 522:
 
====counter_current====
 
====counter_current====
  
$n = $db-&gt;counter_current( $counter )
+
<source lang="perl">$n = $db-&gt;counter_current( $counter )</source>
 
Return the value of the previous counter_next on $counter.
 
Return the value of the previous counter_next on $counter.
  
Line 532: Line 532:
 
====counter_next====
 
====counter_next====
  
$n = $db-&gt;counter_next( $counter )
+
<source lang="perl">$n = $db-&gt;counter_next( $counter )</source>
 
Return the next unused value for the named counter. Returns undef if  the counter doesn't exist.
 
Return the next unused value for the named counter. Returns undef if  the counter doesn't exist.
  
Line 542: Line 542:
 
====counter_minimum====
 
====counter_minimum====
  
$db-&gt;counter_minimum( $counter, $value )
+
<source lang="perl">$db-&gt;counter_minimum( $counter, $value )</source>
 
Ensure that the counter is set no lower than $value. This is used when importing eprints which may not be in scrict sequence.
 
Ensure that the counter is set no lower than $value. This is used when importing eprints which may not be in scrict sequence.
  
Line 552: Line 552:
 
====counter_reset====
 
====counter_reset====
  
$db-&gt;counter_reset( $counter )
+
<source lang="perl">$db-&gt;counter_reset( $counter )</source>
 
Reset the counter. Use with caution.
 
Reset the counter. Use with caution.
  
Line 562: Line 562:
 
====next_doc_pos====
 
====next_doc_pos====
  
$n = $db-&gt;next_doc_pos( $eprintid )
+
<source lang="perl">$n = $db-&gt;next_doc_pos( $eprintid )</source>
 
Return the next unused document pos for the given eprintid.
 
Return the next unused document pos for the given eprintid.
  
Line 578: Line 578:
 
====do====
 
====do====
  
$success = $db-&gt;do( $sql )
+
<source lang="perl">$success = $db-&gt;do( $sql )</source>
 
Execute the given SQL.
 
Execute the given SQL.
  
Line 588: Line 588:
 
====prepare====
 
====prepare====
  
$sth = $db-&gt;prepare( $sql )
+
<source lang="perl">$sth = $db-&gt;prepare( $sql )</source>
 
Prepare the SQL statement $sql for execution.
 
Prepare the SQL statement $sql for execution.
  
Line 603: Line 603:
 
====prepare_select====
 
====prepare_select====
  
$sth = $db-&gt;prepare_select( $sql [, %options ] )
+
<source lang="perl">$sth = $db-&gt;prepare_select( $sql [, %options ] )</source>
 
Prepare a SELECT statement $sql for execution.
 
Prepare a SELECT statement $sql for execution.
  
Line 622: Line 622:
 
====has_sequence====
 
====has_sequence====
  
$boolean = $db-&gt;has_sequence( $name )
+
<source lang="perl">$boolean = $db-&gt;has_sequence( $name )</source>
 
Return true if a sequence of the given name exists in the database.
 
Return true if a sequence of the given name exists in the database.
  
Line 632: Line 632:
 
====create_sequence====
 
====create_sequence====
  
$success = $db-&gt;create_sequence( $seq_name )
+
<source lang="perl">$success = $db-&gt;create_sequence( $seq_name )</source>
 
Creates a new sequence object initialised to zero.
 
Creates a new sequence object initialised to zero.
  
Line 642: Line 642:
 
====drop_sequence====
 
====drop_sequence====
  
$success = $db-&gt;drop_sequence( $seq_name )
+
<source lang="perl">$success = $db-&gt;drop_sequence( $seq_name )</source>
 
Deletes a sequence object.
 
Deletes a sequence object.
  
Line 652: Line 652:
 
====drop_column====
 
====drop_column====
  
$success = $db-&gt;drop_column( $table, $column )
+
<source lang="perl">$success = $db-&gt;drop_column( $table, $column )</source>
 
Drops a column from a table.
 
Drops a column from a table.
  
Line 662: Line 662:
 
====get_primary_key====
 
====get_primary_key====
  
@columns = $db-&gt;get_primary_key( $tablename )
+
<source lang="perl">@columns = $db-&gt;get_primary_key( $tablename )</source>
 
Returns the list of column names that comprise the primary key for $tablename.
 
Returns the list of column names that comprise the primary key for $tablename.
  
Line 674: Line 674:
 
====create_primary_key====
 
====create_primary_key====
  
$db-&gt;create_primary_key( $tablename, @cols )
+
<source lang="perl">$db-&gt;create_primary_key( $tablename, @cols )</source>
 
Create a PRIMARY KEY on $tablename over @cols.
 
Create a PRIMARY KEY on $tablename over @cols.
  
Line 684: Line 684:
 
====create_index====
 
====create_index====
  
$success = $db-&gt;create_index( $tablename, @columns )
+
<source lang="perl">$success = $db-&gt;create_index( $tablename, @columns )</source>
 
Creates an index over @columns for $tablename. Returns true on success.
 
Creates an index over @columns for $tablename. Returns true on success.
  
Line 694: Line 694:
 
====create_unique_index====
 
====create_unique_index====
  
$success = $db-&gt;create_unique_index( $tablename, @columns )
+
<source lang="perl">$success = $db-&gt;create_unique_index( $tablename, @columns )</source>
 
Creates a unique index over @columns for $tablename. Returns true on success.
 
Creates a unique index over @columns for $tablename. Returns true on success.
  
Line 704: Line 704:
 
====index_name====
 
====index_name====
  
$name = $db-&gt;index_name( $table, @columns )
+
<source lang="perl">$name = $db-&gt;index_name( $table, @columns )</source>
 
Returns the name of the first index that starts with @columns on the $table table.
 
Returns the name of the first index that starts with @columns on the $table table.
  
Line 716: Line 716:
 
====_update====
 
====_update====
  
$rows = $db-&gt;_update( $tablename, $keycols, $keyvals, $columns, @values )
+
<source lang="perl">$rows = $db-&gt;_update( $tablename, $keycols, $keyvals, $columns, @values )</source>
 
UPDATES $tablename where $keycols equals $keyvals and returns the number of rows affected.
 
UPDATES $tablename where $keycols equals $keyvals and returns the number of rows affected.
  
Line 730: Line 730:
 
====insert====
 
====insert====
  
$success = $db-&gt;insert( $table, $columns, @values )
+
<source lang="perl">$success = $db-&gt;insert( $table, $columns, @values )</source>
 
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.
 
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.
  
Line 742: Line 742:
 
====insert_quoted====
 
====insert_quoted====
  
$success = $db-&gt;insert_quoted( $table, $columns, @qvalues )
+
<source lang="perl">$success = $db-&gt;insert_quoted( $table, $columns, @qvalues )</source>
 
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.
 
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.
  
Line 754: Line 754:
 
====delete_from====
 
====delete_from====
  
$success = $db-&gt;delete_from( $table, $columns, @values )
+
<source lang="perl">$success = $db-&gt;delete_from( $table, $columns, @values )</source>
 
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.
 
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.
  
Line 766: Line 766:
 
====count_table====
 
====count_table====
  
$n = $db-&gt;count_table( $tablename )
+
<source lang="perl">$n = $db-&gt;count_table( $tablename )</source>
 
Return the number of rows in the specified SQL table.
 
Return the number of rows in the specified SQL table.
  
Line 776: Line 776:
 
====has_table====
 
====has_table====
  
$boolean = $db-&gt;has_table( $tablename )
+
<source lang="perl">$boolean = $db-&gt;has_table( $tablename )</source>
 
Return true if a table of the given name exists in the database.
 
Return true if a table of the given name exists in the database.
  
Line 786: Line 786:
 
====has_column====
 
====has_column====
  
$boolean = $db-&gt;has_column( $tablename, $columnname )
+
<source lang="perl">$boolean = $db-&gt;has_column( $tablename, $columnname )</source>
 
Return true if the a table of the given name has a column named $columnname in the database.
 
Return true if the a table of the given name has a column named $columnname in the database.
  
Line 796: Line 796:
 
====drop_table====
 
====drop_table====
  
$db-&gt;drop_table( $tablename [, $tablename2 ] )
+
<source lang="perl">$db-&gt;drop_table( $tablename [, $tablename2 ] )</source>
 
Delete the named table(s). Use with caution!
 
Delete the named table(s). Use with caution!
  
Line 806: Line 806:
 
====clear_table====
 
====clear_table====
  
$db-&gt;clear_table( $tablename )
+
<source lang="perl">$db-&gt;clear_table( $tablename )</source>
 
Clears all records from the given table, use with caution!
 
Clears all records from the given table, use with caution!
  
Line 816: Line 816:
 
====rename_table====
 
====rename_table====
  
$db-&gt;rename_table( $tablename, $newtablename )
+
<source lang="perl">$db-&gt;rename_table( $tablename, $newtablename )</source>
 
Renames the table from the old name to the new one.
 
Renames the table from the old name to the new one.
  
Line 826: Line 826:
 
====swap_table====
 
====swap_table====
  
$db-&gt;swap_table( $table_a, $table_b )
+
<source lang="perl">$db-&gt;swap_table( $table_a, $table_b )</source>
 
Swap table a and table b.  
 
Swap table a and table b.  
  
Line 836: Line 836:
 
====get_tables====
 
====get_tables====
  
@tables = $db-&gt;get_tables
+
<source lang="perl">@tables = $db-&gt;get_tables</source>
 
Return a list of all the tables in the database.
 
Return a list of all the tables in the database.
  
Line 852: Line 852:
 
====prep_int====
 
====prep_int====
  
$mungedvalue = EPrints::Database::prep_int( $value )
+
<source lang="perl">$mungedvalue = EPrints::Database::prep_int( $value )</source>
 
Escape a numerical value for SQL. undef becomes NULL. Anything else becomes a number (zero if needed).
 
Escape a numerical value for SQL. undef becomes NULL. Anything else becomes a number (zero if needed).
  
Line 862: Line 862:
 
====prep_value====
 
====prep_value====
  
$mungedvalue = EPrints::Database::prep_value( $value )
+
<source lang="perl">$mungedvalue = EPrints::Database::prep_value( $value )</source>
 
Escape a value for SQL. Modify value such that " becomes \" and \  becomes \\ and ' becomes \'
 
Escape a value for SQL. Modify value such that " becomes \" and \  becomes \\ and ' becomes \'
  
Line 872: Line 872:
 
====prep_like_value====
 
====prep_like_value====
  
$mungedvalue = EPrints::Database::prep_like_value( $value )
+
<source lang="perl">$mungedvalue = EPrints::Database::prep_like_value( $value )</source>
 
Escape an value for an SQL like field. In addition to ' " and \ also  escapes % and _
 
Escape an value for an SQL like field. In addition to ' " and \ also  escapes % and _
  
Line 882: Line 882:
 
====quote_value====
 
====quote_value====
  
$str = $db-&gt;quote_value( $value )
+
<source lang="perl">$str = $db-&gt;quote_value( $value )</source>
 
Return a quoted value. To quote a 'like' value you should do:
 
Return a quoted value. To quote a 'like' value you should do:
  
Line 894: Line 894:
 
====quote_int====
 
====quote_int====
  
$str = $db-&gt;quote_int( $value )
+
<source lang="perl">$str = $db-&gt;quote_int( $value )</source>
 
Return a quoted integer value
 
Return a quoted integer value
  
Line 904: Line 904:
 
====quote_binary====
 
====quote_binary====
  
$str = $db-&gt;quote_binary( $bytes )
+
<source lang="perl">$str = $db-&gt;quote_binary( $bytes )</source>
 
Some databases (Oracle/PostgreSQL) require transforms of binary data to work correctly.
 
Some databases (Oracle/PostgreSQL) require transforms of binary data to work correctly.
  
Line 916: Line 916:
 
====quote_ordervalue====
 
====quote_ordervalue====
  
$str = $db-&gt;quote_ordervalue( $field, $value )
+
<source lang="perl">$str = $db-&gt;quote_ordervalue( $field, $value )</source>
 
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.
 
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.
  
Line 926: Line 926:
 
====quote_identifier====
 
====quote_identifier====
  
$str = $db-&gt;quote_identifier( @parts )
+
<source lang="perl">$str = $db-&gt;quote_identifier( @parts )</source>
 
Quote a database identifier (e.g. table names). Multiple @parts will be joined by dot.
 
Quote a database identifier (e.g. table names). Multiple @parts will be joined by dot.
  
Line 936: Line 936:
 
====prepare_regexp====
 
====prepare_regexp====
  
$sql = $db-&gt;prepare_regexp( $quoted_column, $quoted_value )
+
<source lang="perl">$sql = $db-&gt;prepare_regexp( $quoted_column, $quoted_value )</source>
 
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).
 
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).
  
Line 946: Line 946:
 
====sql_as====
 
====sql_as====
  
$sql = $db-&gt;sql_AS()
+
<source lang="perl">$sql = $db-&gt;sql_AS()</source>
 
Returns the syntactic glue to use when aliasing. SQL 92 DBs will happilly use " AS " but some DBs (Oracle!) won't accept it.
 
Returns the syntactic glue to use when aliasing. SQL 92 DBs will happilly use " AS " but some DBs (Oracle!) won't accept it.
  
Line 956: Line 956:
 
====sql_like====
 
====sql_like====
  
$sql = $db-&gt;sql_LIKE()
+
<source lang="perl">$sql = $db-&gt;sql_LIKE()</source>
 
Returns the syntactic glue to use when making a case-insensitive LIKE. PostgreSQL requires "ILIKE" while everything else uses "LIKE" and the column collation.
 
Returns the syntactic glue to use when making a case-insensitive LIKE. PostgreSQL requires "ILIKE" while everything else uses "LIKE" and the column collation.
  
Line 972: Line 972:
 
====insert_data====
 
====insert_data====
  
$success = $db-&gt;insert_data( $dataset, $data )
+
<source lang="perl">$success = $db-&gt;insert_data( $dataset, $data )</source>
 
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.
 
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.
  
Line 986: Line 986:
 
====update_data====
 
====update_data====
  
$success = $db-&gt;update_data( $dataset, $data, $changed )
+
<source lang="perl">$success = $db-&gt;update_data( $dataset, $data, $changed )</source>
 
Updates a record in the given {{API:PodLink|file=EPrints/DataSet|package_name=EPrints::DataSet|section=|text=$dataset}}. $data is a copy of the entire record's data and $changed the values that have changed.
 
Updates a record in the given {{API:PodLink|file=EPrints/DataSet|package_name=EPrints::DataSet|section=|text=$dataset}}. $data is a copy of the entire record's data and $changed the values that have changed.
  
Line 1,000: Line 1,000:
 
====exists====
 
====exists====
  
$boolean = $db-&gt;exists( $dataset, $id )
+
<source lang="perl">$boolean = $db-&gt;exists( $dataset, $id )</source>
 
Return true if a record with the given primary key exists in the dataset, otherwise false.
 
Return true if a record with the given primary key exists in the dataset, otherwise false.
  
Line 1,010: Line 1,010:
 
====remove====
 
====remove====
  
$success = $db-&gt;remove( $dataset, $id )
+
<source lang="perl">$success = $db-&gt;remove( $dataset, $id )</source>
 
Remove the record, index terms and order value with the key field value $id from the specified dataset.
 
Remove the record, index terms and order value with the key field value $id from the specified dataset.
  
Line 1,028: Line 1,028:
 
====cache_exp====
 
====cache_exp====
  
$searchexp = $db-&gt;cache_exp( $cacheid )
+
<source lang="perl">$searchexp = $db-&gt;cache_exp( $cacheid )</source>
 
Return the serialised Search of a the cached search with id $cacheid. Return undef if the id is invalid or expired.
 
Return the serialised Search of a the cached search with id $cacheid. Return undef if the id is invalid or expired.
  
Line 1,038: Line 1,038:
 
====cache====
 
====cache====
  
$cacheid = $db-&gt;cache( $searchexp, $dataset, $srctable, [$order], [$list] )
+
<source lang="perl">$cacheid = $db-&gt;cache( $searchexp, $dataset, $srctable, [$order], [$list] )</source>
 
Create a cache of the specified search expression from the SQL table $srctable.
 
Create a cache of the specified search expression from the SQL table $srctable.
  
Line 1,054: Line 1,054:
 
====cache_table====
 
====cache_table====
  
$tablename = $db-&gt;cache_table( $id )
+
<source lang="perl">$tablename = $db-&gt;cache_table( $id )</source>
 
Return the SQL table used to store the cache with id $id.
 
Return the SQL table used to store the cache with id $id.
  
Line 1,064: Line 1,064:
 
====search====
 
====search====
  
$ids = $db-&gt;search( $keyfield, $tables, $conditions, [$main_table_alias] )
+
<source lang="perl">$ids = $db-&gt;search( $keyfield, $tables, $conditions, [$main_table_alias] )</source>
 
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.  
 
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.  
  
Line 1,076: Line 1,076:
 
====drop_cache====
 
====drop_cache====
  
$db-&gt;drop_cache( $id )
+
<source lang="perl">$db-&gt;drop_cache( $id )</source>
 
Remove the cached search with the given id.
 
Remove the cached search with the given id.
  
Line 1,086: Line 1,086:
 
====from_cache====
 
====from_cache====
  
$foo = $db-&gt;from_cache( $dataset, $cacheid, [$offset], [$count], [$justids] )
+
<source lang="perl">$foo = $db-&gt;from_cache( $dataset, $cacheid, [$offset], [$count], [$justids] )</source>
 
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.  
 
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.  
  
Line 1,100: Line 1,100:
 
====drop_orphan_cache_tables====
 
====drop_orphan_cache_tables====
  
$c = $db-&gt;drop_orphan_cache_tables
+
<source lang="perl">$c = $db-&gt;drop_orphan_cache_tables</source>
 
Drop tables called "cacheXXX" where XXX is an integer. Returns the number of tables dropped.
 
Drop tables called "cacheXXX" where XXX is an integer. Returns the number of tables dropped.
  
Line 1,110: Line 1,110:
 
====get_single====
 
====get_single====
  
$obj = $db-&gt;get_single( $dataset, $id )
+
<source lang="perl">$obj = $db-&gt;get_single( $dataset, $id )</source>
 
Return a single item from the given dataset. The one with the specified id.
 
Return a single item from the given dataset. The one with the specified id.
  
Line 1,120: Line 1,120:
 
====get_all====
 
====get_all====
  
$items = $db-&gt;get_all( $dataset )
+
<source lang="perl">$items = $db-&gt;get_all( $dataset )</source>
 
Returns a reference to an array with all the items from the given dataset.
 
Returns a reference to an array with all the items from the given dataset.
  
Line 1,130: Line 1,130:
 
====get_cache_ids====
 
====get_cache_ids====
  
@ids = $db-&gt;get_cache_ids( $dataset, $cachemap, $offset, $count )
+
<source lang="perl">@ids = $db-&gt;get_cache_ids( $dataset, $cachemap, $offset, $count )</source>
 
Returns a list of $count ids from $cache_id starting at $offset and in the order in the cachemap.
 
Returns a list of $count ids from $cache_id starting at $offset and in the order in the cachemap.
  
Line 1,140: Line 1,140:
 
====get_dataobjs====
 
====get_dataobjs====
  
@dataobjs = $db-&gt;get_dataobjs( $dataset [, $id [, $id ] ] )
+
<source lang="perl">@dataobjs = $db-&gt;get_dataobjs( $dataset [, $id [, $id ] ] )</source>
 
Retrieves the records in $dataset with the given $id(s). If an $id doesn't exist in the database it will be ignored.
 
Retrieves the records in $dataset with the given $id(s). If an $id doesn't exist in the database it will be ignored.
  
Line 1,150: Line 1,150:
 
====get_values====
 
====get_values====
  
$foo = $db-&gt;get_values( $field, $dataset )
+
<source lang="perl">$foo = $db-&gt;get_values( $field, $dataset )</source>
 
Return a reference to an array of all the distinct values of the  [[API:EPrints/MetaField|EPrints::MetaField]] specified.
 
Return a reference to an array of all the distinct values of the  [[API:EPrints/MetaField|EPrints::MetaField]] specified.
  
Line 1,160: Line 1,160:
 
====get_ids_by_field_values====
 
====get_ids_by_field_values====
  
$ids = $db-&gt;get_ids_by_field_values( $field, $dataset [ %opts ] )
+
<source lang="perl">$ids = $db-&gt;get_ids_by_field_values( $field, $dataset [ %opts ] )</source>
 
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.
 
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.
  
Line 1,170: Line 1,170:
 
====dequeue_events====
 
====dequeue_events====
  
@events = $db-&gt;dequeue_events( $n )
+
<source lang="perl">@events = $db-&gt;dequeue_events( $n )</source>
 
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.
 
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.
  
Line 1,186: Line 1,186:
 
====valid_login====
 
====valid_login====
  
$real_username = $db-&gt;valid_login( $username, $password )
+
<source lang="perl">$real_username = $db-&gt;valid_login( $username, $password )</source>
 
Tests whether the clear-text $password matches the stored encrypted password for $username.
 
Tests whether the clear-text $password matches the stored encrypted password for $username.
  

Revision as of 08:55, 2 March 2012

EPrints 3 Reference: Directory Structure - Metadata Fields - Repository Configuration - XML Config Files - XML Export Format - EPrints data structure - Core API - Data Objects


API: Core API

Latest Source Code (3.3, 3.2) | Revision Log | Before editing this page please read Pod2Wiki


Contents

NAME

EPrints::Database - a connection to the SQL database for an eprints session


SYNOPSIS

 $db = $repo->database
 $sth = $db->prepare("SELECT 'hello, world!'");
 $sth->execute;
 print $sth->fetchrow_arrayref->[0], "\n";
 


DESCRIPTION

EPrints Database Access Module

Provides access to the backend database. All database access is performed via this module to 1) provide cross-database support and 2) improve security through enforcing proper quoting of SQL values.

In most use-cases it should not be necessary to use the database module directly. Instead you should use EPrints::DataSet or EPrints::MetaField accessor methods to access objects and field values respectively.


Cross-database Support

Any use of SQL statements must use quote_identifier to quote database tables and columns and quote_value to quote values. The only exception to this are the EPrints::Database::* modules which provide database-driver specific extensions.


Quoting SQL Values

By convention variables that contain already quoted values are prefixed with 'Q_' so they can be easily recognised when used in string interpolation:

 my $Q_value = $db->quote_value( "Hello, World!" );
 $db->do("SELECT $Q_value");
 

Where possible you should avoid quoting values yourself, instead use a method that accepts unquoted values which will (safely) do the work for you.


METHODS

Database

new

$db = EPrints::Database-&gt;new( $repo )

Create a connection to the database.


create

$db = $db-&gt;create( $username, $password )

Create and connect to a new database using super user account $username and $password.


connect

$ok = $db-&gt;connect

Connects to the database.


disconnect

$db-&gt;disconnect

Disconnects from the EPrints database.


set_debug

$db-&gt;set_debug( $boolean )

Set the SQL debug mode to true or false.


set_version

$db-&gt;set_version( $versionid );

Set the version id table in the SQL database to the given value (used by the upgrade script).


get_version

$version = $db-&gt;get_version

Returns the current database schema version.


is_latest_version

$boolean = $db-&gt;is_latest_version

Return true if the database schema is in the correct configuration for this version of eprints. Otherwise false.


get_server_version

$version = $db-&gt;get_server_version

Return the database server version.


get_default_charset

$charset = $db-&gt;get_default_charset( LANGUAGE )

Return the character set to use for LANGUAGE.

Returns undef if character sets are unsupported.


get_default_collation

$collation = $db-&gt;get_default_collation( LANGUAGE )

Return the collation to use for LANGUAGE.

Returns undef if collation is unsupported.


get_driver_name

$driver = $db-&gt;get_driver_name

Return the database driver name.


error

$errstr = $db-&gt;error

Return a string describing the last SQL error.


retry_error

$bool = $db-&gt;retry_error

Returns true if the current error is a retry error.


duplicate_error

$bool = $db-&gt;duplicate_error

Returns true if the current error is a PRIMARY KEY or UNIQUE error.


begin

$db-&gt;begin

Begin a transaction.


commit

$db-&gt;commit

Commit the previously begun transaction.


rollback

$db-&gt;rollback

Roll-back the current transaction.


type_info

$type_info = $db-&gt;type_info( DATA_TYPE )

See DBI/type_info.


get_column_type

$real_type = $db-&gt;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.


Schema Manipulation

create_archive_tables

$success = $db-&gt;create_archive_tables

Create all the SQL tables for each dataset.


drop_archive_tables

$db-&gt;drop_archive_tables()

Destroy all tables used by eprints in the database.


create_version_table

$db-&gt;create_version_table

Make the version table (and set the only value to be the current version of eprints).


drop_version_table

$db-&gt;drop_version_table

Drop the version table.


create_dataset_tables

$success = $db-&gt;create_dataset_tables( $dataset )

Create all the SQL tables for a single dataset.


drop_dataset_tables

$db-&gt;drop_dataset_tables( $dataset )

Drop all the SQL tables for a single dataset.


create_dataset_index_tables

$success = $db-&gt;create_dataset_index_tables( $dataset )

Create all the index tables for a single dataset.


create_dataset_ordervalues_tables

$success = $db-&gt;create_dataset_ordervalues_tables( $dataset )

Create all the ordervalues tables for a single dataset.


has_dataset

$db-&gt;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-&gt;has_field( $dataset, $field )

Returns true if $field is in the database for $dataset.


add_field

$db-&gt;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-&gt;remove_field( $dataset, $field )

Remove $field from $dataset's tables.


rename_field

$ok = $db-&gt;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.


create_table

$success = $db-&gt;create_table( $tablename, $setkey, @fields );

Creates a new table $tablename based on @fields.

The first $setkey number of fields are used for a primary key.


create_foreign_key

$ok = $db-&gt;create_foreign_key( $main_table, $aux_table, $key_field )

Create a foreign key relationship between $main_table and $aux_table using the $key_field.

This will cause records in $aux_table to be deleted if the equivalent record is deleted from $main_table.


create_counters

$success = $db-&gt;create_counters

Create the counters used to store the highest current id of eprints, users etc.


has_counter

$success = $db-&gt;has_counter( $counter )

Returns true if $counter exists.


create_counter

$success = $db-&gt;create_counter( $name )

Create and initialise to zero a new counter called $name.


remove_counters

$success = $db-&gt;remove_counters

Destroy all counters.


drop_counter

$success = $db-&gt;drop_counter( $name )

Destroy the counter named $name.


counter_current

$n = $db-&gt;counter_current( $counter )

Return the value of the previous counter_next on $counter.


counter_next

$n = $db-&gt;counter_next( $counter )

Return the next unused value for the named counter. Returns undef if the counter doesn't exist.


counter_minimum

$db-&gt;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-&gt;counter_reset( $counter )

Reset the counter. Use with caution.


next_doc_pos

$n = $db-&gt;next_doc_pos( $eprintid )

Return the next unused document pos for the given eprintid.


SQL utility methods

do

$success = $db-&gt;do( $sql )

Execute the given SQL.


prepare

$sth = $db-&gt;prepare( $sql )

Prepare the SQL statement $sql for execution.

Use the execute method on the returned DBI handle to execute the SQL:

 my $sth = $db->prepare_select( "SELECT 'Hello, World'" );
 $sth->execute;
 


prepare_select

$sth = $db-&gt;prepare_select( $sql [, %options ] )

Prepare a SELECT statement $sql for execution.

Returns a DBI statement handle.

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
 


has_sequence

$boolean = $db-&gt;has_sequence( $name )

Return true if a sequence of the given name exists in the database.


create_sequence

$success = $db-&gt;create_sequence( $seq_name )

Creates a new sequence object initialised to zero.


drop_sequence

$success = $db-&gt;drop_sequence( $seq_name )

Deletes a sequence object.


drop_column

$success = $db-&gt;drop_column( $table, $column )

Drops a column from a table.


get_primary_key

@columns = $db-&gt;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_primary_key

$db-&gt;create_primary_key( $tablename, @cols )

Create a PRIMARY KEY on $tablename over @cols.


create_index

$success = $db-&gt;create_index( $tablename, @columns )

Creates an index over @columns for $tablename. Returns true on success.


create_unique_index

$success = $db-&gt;create_unique_index( $tablename, @columns )

Creates a unique index over @columns for $tablename. Returns true on success.


index_name

$name = $db-&gt;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.


_update

$rows = $db-&gt;_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.


insert

$success = $db-&gt;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-&gt;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-&gt;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().


count_table

$n = $db-&gt;count_table( $tablename )

Return the number of rows in the specified SQL table.


has_table

$boolean = $db-&gt;has_table( $tablename )

Return true if a table of the given name exists in the database.


has_column

$boolean = $db-&gt;has_column( $tablename, $columnname )

Return true if the a table of the given name has a column named $columnname in the database.


drop_table

$db-&gt;drop_table( $tablename [, $tablename2 ] )

Delete the named table(s). Use with caution!


clear_table

$db-&gt;clear_table( $tablename )

Clears all records from the given table, use with caution!


rename_table

$db-&gt;rename_table( $tablename, $newtablename )

Renames the table from the old name to the new one.


swap_table

$db-&gt;swap_table( $table_a, $table_b )

Swap table a and table b.


get_tables

@tables = $db-&gt;get_tables

Return a list of all the tables in the database.


Quoting Values

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-&gt;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-&gt;quote_int( $value )

Return a quoted integer value


quote_binary

$str = $db-&gt;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-&gt;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-&gt;quote_identifier( @parts )

Quote a database identifier (e.g. table names). Multiple @parts will be joined by dot.


prepare_regexp

$sql = $db-&gt;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-&gt;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-&gt;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.


Dataset Data

insert_data

$success = $db-&gt;insert_data( $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.

Returns undef if a record already exists with the key field value in $data.

Inserts ordervalues if the dataset is ordered.


update_data

$success = $db-&gt;update_data( $dataset, $data, $changed )

Updates a record in the given $dataset. $data is a copy of the entire record's data and $changed the values that have changed.

If the record does not already exist or the key field is unset in $data no changes will be written.

Updates ordervalues if the dataset is ordered.


exists

$boolean = $db-&gt;exists( $dataset, $id )

Return true if a record with the given primary key exists in the dataset, otherwise false.


remove

$success = $db-&gt;remove( $dataset, $id )

Remove the record, index terms and order value with the key field value $id from the specified dataset.

Returns true on success.


Searching, Caching and Retrieval

cache_exp

$searchexp = $db-&gt;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-&gt;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-&gt;cache_table( $id )

Return the SQL table used to store the cache with id $id.


search

$ids = $db-&gt;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-&gt;drop_cache( $id )

Remove the cached search with the given id.


from_cache

$foo = $db-&gt;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-&gt;drop_orphan_cache_tables

Drop tables called "cacheXXX" where XXX is an integer. Returns the number of tables dropped.


get_single

$obj = $db-&gt;get_single( $dataset, $id )

Return a single item from the given dataset. The one with the specified id.


get_all

$items = $db-&gt;get_all( $dataset )

Returns a reference to an array with all the items from the given dataset.


get_cache_ids

@ids = $db-&gt;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-&gt;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-&gt;get_values( $field, $dataset )

Return a reference to an array of all the distinct values of the EPrints::MetaField specified.


get_ids_by_field_values

$ids = $db-&gt;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.


dequeue_events

@events = $db-&gt;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.


Password Validation

valid_login

$real_username = $db-&gt;valid_login( $username, $password )

Tests whether the clear-text $password matches the stored encrypted password for $username.

Returns the user's real (case-sensitive) username or undef if the passwords don't match.


SEE ALSO

To access database-stored objects use the methods provided by the following modules: EPrints::Repository, EPrints::DataSet.


COPYRIGHT