Difference between revisions of "API:EPrints/Database"

From EPrints Documentation
Jump to: navigation, search
 
(3 intermediate revisions by the same user not shown)
Line 14: Line 14:
 
<!-- Pod2Wiki=head_synopsis -->
 
<!-- Pod2Wiki=head_synopsis -->
 
==SYNOPSIS==
 
==SYNOPSIS==
  $db = $repo-&gt;database
+
<source lang="perl">$db = $repo->database
  $sth = $db-&gt;prepare("SELECT 'hello, world!'");
+
$sth = $db->prepare("SELECT 'hello, world!'");
  $sth-&gt;execute;
+
$sth->execute;
  print $sth-&gt;fetchrow_arrayref-&gt;[0], "\n";
+
print $sth->fetchrow_arrayref->[0], "\n";</source>
 
+
 
 
<!-- Edit below this comment -->
 
<!-- Edit below this comment -->
  
Line 47: Line 47:
 
By convention variables that contain already quoted values are prefixed with 'Q_' so they can be easily recognised when used in string interpolation:
 
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-&gt;quote_value( "Hello, World!" );
+
<pre>  my $Q_value = $db-&gt;quote_value( "Hello, World!" );
   $db-&gt;do("SELECT $Q_value");
+
   $db-&gt;do("SELECT $Q_value");</pre>
 
+
 
 
Where possible you should avoid quoting values yourself, instead use a method that accepts unquoted values which will (safely) do the work for you.
 
Where possible you should avoid quoting values yourself, instead use a method that accepts unquoted values which will (safely) do the work for you.
  
Line 64: Line 64:
 
<!-- Pod2Wiki=head_database -->
 
<!-- Pod2Wiki=head_database -->
 
===Database===
 
===Database===
<!-- Edit below this comment -->
+
$db = EPrints::Database-&gt;new( $repo )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_new -->
+
====new====
+
 
+
<source lang="perl">$db = EPrints::Database->new( $repo )
+
 
+
</source>
+
 
Create a connection to the database.
 
Create a connection to the database.
  
<!-- Edit below this comment -->
+
$db = $db-&gt;create( $username, $password )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create -->
+
====create====
+
 
+
<source lang="perl">$db = $db->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.
  
<!-- Edit below this comment -->
+
$ok = $db-&gt;connect
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_connect -->
+
====connect====
+
 
+
<source lang="perl">$ok = $db->connect
+
 
+
</source>
+
 
Connects to the database.  
 
Connects to the database.  
  
<!-- Edit below this comment -->
+
$db-&gt;disconnect
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_disconnect -->
+
====disconnect====
+
 
+
<source lang="perl">$db->disconnect
+
 
+
</source>
+
 
Disconnects from the EPrints database.
 
Disconnects from the EPrints database.
  
<!-- Edit below this comment -->
+
$db-&gt;set_debug( $boolean )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_set_debug -->
+
====set_debug====
+
 
+
<source lang="perl">$db->set_debug( $boolean )
+
 
+
</source>
+
 
Set the SQL debug mode to true or false.
 
Set the SQL debug mode to true or false.
  
<!-- Edit below this comment -->
+
$db-&gt;set_version( $versionid );
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_set_version -->
+
====set_version====
+
 
+
<source lang="perl">$db->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).
  
<!-- Edit below this comment -->
+
$version = $db-&gt;get_version
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_version -->
+
====get_version====
+
 
+
<source lang="perl">$version = $db->get_version
+
 
+
</source>
+
 
Returns the current database schema version.
 
Returns the current database schema version.
  
<!-- Edit below this comment -->
+
$boolean = $db-&gt;is_latest_version
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_is_latest_version -->
+
====is_latest_version====
+
 
+
<source lang="perl">$boolean = $db->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.
  
<!-- Edit below this comment -->
+
$version = $db-&gt;get_server_version
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_server_version -->
+
====get_server_version====
+
 
+
<source lang="perl">$version = $db->get_server_version
+
 
+
</source>
+
 
Return the database server version.
 
Return the database server version.
  
<!-- Edit below this comment -->
+
$charset = $db-&gt;get_default_charset( LANGUAGE )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_default_charset -->
+
====get_default_charset====
+
 
+
<source lang="perl">$charset = $db->get_default_charset( LANGUAGE )
+
 
+
</source>
+
 
Return the character set to use for LANGUAGE.
 
Return the character set to use for LANGUAGE.
  
 
Returns undef if character sets are unsupported.
 
Returns undef if character sets are unsupported.
  
<!-- Edit below this comment -->
+
$collation = $db-&gt;get_default_collation( LANGUAGE )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_default_collation -->
+
====get_default_collation====
+
 
+
<source lang="perl">$collation = $db->get_default_collation( LANGUAGE )
+
 
+
</source>
+
 
Return the collation to use for LANGUAGE.
 
Return the collation to use for LANGUAGE.
  
 
Returns undef if collation is unsupported.
 
Returns undef if collation is unsupported.
  
<!-- Edit below this comment -->
+
$driver = $db-&gt;get_driver_name
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_driver_name -->
+
====get_driver_name====
+
 
+
<source lang="perl">$driver = $db->get_driver_name
+
 
+
</source>
+
 
Return the database driver name.
 
Return the database driver name.
  
<!-- Edit below this comment -->
+
$errstr = $db-&gt;error
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_error -->
+
====error====
+
 
+
<source lang="perl">$errstr = $db->error
+
 
+
</source>
+
 
Return a string describing the last SQL error.
 
Return a string describing the last SQL error.
  
<!-- Edit below this comment -->
+
$bool = $db-&gt;retry_error
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_retry_error -->
+
====retry_error====
+
 
+
<source lang="perl">$bool = $db->retry_error
+
 
+
</source>
+
 
Returns true if the current error is a retry error.
 
Returns true if the current error is a retry error.
  
<!-- Edit below this comment -->
+
$bool = $db-&gt;duplicate_error
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_duplicate_error -->
+
====duplicate_error====
+
 
+
<source lang="perl">$bool = $db->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.
  
<!-- Edit below this comment -->
+
$db-&gt;begin
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_begin -->
+
====begin====
+
 
+
<source lang="perl">$db->begin
+
 
+
</source>
+
 
Begin a transaction.
 
Begin a transaction.
  
<!-- Edit below this comment -->
+
$db-&gt;commit
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_commit -->
+
====commit====
+
 
+
<source lang="perl">$db->commit
+
 
+
</source>
+
 
Commit the previously begun transaction.
 
Commit the previously begun transaction.
  
<!-- Edit below this comment -->
+
$db-&gt;rollback
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_rollback -->
+
====rollback====
+
 
+
<source lang="perl">$db->rollback
+
 
+
</source>
+
 
Roll-back the current transaction.
 
Roll-back the current transaction.
  
<!-- Edit below this comment -->
+
$type_info = $db-&gt;type_info( DATA_TYPE )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_type_info -->
+
====type_info====
+
 
+
<source lang="perl">$type_info = $db->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}}.
  
<!-- Edit below this comment -->
+
$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:
  
 +
<pre>  $name $type($length,$scale) [ NOT NULL ]</pre>
  
<!-- Pod2Wiki= -->
 
<!-- Pod2Wiki=item_get_column_type -->
 
====get_column_type====
 
 
<source lang="perl">$real_type = $db->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:
 
 
  $name $type($length,$scale) [ NOT NULL ]
 
 
 
 
If NOT_NULL is true column will be set "not null".
 
If NOT_NULL is true column will be set "not null".
  
Line 316: Line 136:
 
Other options available to refine the column definition:
 
Other options available to refine the column definition:
  
  langid - character set/collation to use
+
<pre>  langid - character set/collation to use
   sorted - whether this column will be used to order by
+
   sorted - whether this column will be used to order by</pre>
 
+
 
 
'''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.
 
'''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:
 
TYPE is the SQL type. The types are constants defined by this module, to import them use:
  
  use EPrints::Database qw( :sql_types );
+
<pre>  use EPrints::Database qw( :sql_types );</pre>
 
+
 
 
Supported types (n = requires LENGTH argument):
 
Supported types (n = requires LENGTH argument):
  
Line 345: Line 165:
 
<!-- Pod2Wiki=head_basic_sql_operations -->
 
<!-- Pod2Wiki=head_basic_sql_operations -->
 
===Basic SQL operations===
 
===Basic SQL operations===
<!-- Edit below this comment -->
+
$success = $db-&gt;do( $sql )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_do -->
+
====do====
+
 
+
<source lang="perl">$success = $db->do( $sql )
+
 
+
</source>
+
 
Execute the given SQL.
 
Execute the given SQL.
  
<!-- Edit below this comment -->
+
$sth = $db-&gt;prepare( $sql )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_prepare -->
+
====prepare====
+
 
+
<source lang="perl">$sth = $db->prepare( $sql )
+
 
+
</source>
+
 
Prepare the SQL statement $sql for execution.
 
Prepare the SQL statement $sql for execution.
  
Use the <tt>execute</tt> method on the returned {{API:PodLink|file=DBI|package_name=DBI|section=|text=DBI}} handle to execute the SQL:
+
Use the <code>execute</code> method on the returned {{API:PodLink|file=DBI|package_name=DBI|section=|text=DBI}} handle to execute the SQL:
  
  my $sth = $db-&gt;prepare_select( "SELECT 'Hello, World'" );
+
<pre>  my $sth = $db-&gt;prepare_select( "SELECT 'Hello, World'" );
   $sth-&gt;execute;
+
   $sth-&gt;execute;</pre>
 
+
<!-- Edit below this comment -->
+
  
 
+
$sth = $db-&gt;prepare_select( $sql [, %options ] )
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_prepare_select -->
+
====prepare_select====
+
 
+
<source lang="perl">$sth = $db->prepare_select( $sql [, %options ] )
+
 
+
</source>
+
 
Prepare a SELECT statement $sql for execution. This method provides cross-database support for getting a part of the result list based on an offset/limit.
 
Prepare a SELECT statement $sql for execution. This method provides cross-database support for getting a part of the result list based on an offset/limit.
  
Line 390: Line 183:
 
Options:
 
Options:
  
  limit - limit the number of rows returned
+
<pre>  limit - limit the number of rows returned
   offset - return limit number of rows after offset
+
   offset - return limit number of rows after offset</pre>
 
+
<!-- Edit below this comment -->
+
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_update -->
+
====update====
+
 
+
<source lang="perl">$rows = $db->update( $tablename, $keycols, $keyvals, $columns, $values )
+
  
</source>
+
$rows = $db-&gt;update( $tablename, $keycols, $keyvals, $columns, $values )
 
UPDATEs $columns with the equivalent slot from $values where the rows in $tablename match the $keycols/$keyvals.
 
UPDATEs $columns with the equivalent slot from $values where the rows in $tablename match the $keycols/$keyvals.
  
 
Returns the number of rows affected or false on error, see {{API:PodLink|file=DBI|package_name=DBI|section=|text=DBI}}'s execute() method.
 
Returns the number of rows affected or false on error, see {{API:PodLink|file=DBI|package_name=DBI|section=|text=DBI}}'s execute() method.
  
  $db-&gt;update(
+
<pre>  $db-&gt;update(
 
     'eprint',
 
     'eprint',
 
     ['eprintid'],
 
     ['eprintid'],
Line 413: Line 197:
 
     ['title','description'],
 
     ['title','description'],
 
     ['Hello, World!', undef]
 
     ['Hello, World!', undef]
   );
+
   );</pre>
 
+
<!-- Edit below this comment -->
+
  
 
+
$success = $db-&gt;update_quoted( $tablename, $keycols, $keyvals, $columns, $Q_values )
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_update_quoted -->
+
====update_quoted====
+
 
+
<source lang="perl">$success = $db->update_quoted( $tablename, $keycols, $keyvals, $columns, $Q_values )
+
 
+
</source>
+
 
UPDATEs $tablename where $keycols equals $keyvals.
 
UPDATEs $tablename where $keycols equals $keyvals.
  
 
Returns the number of rows affected or false on error, see {{API:PodLink|file=DBI|package_name=DBI|section=|text=DBI}}'s execute() method.
 
Returns the number of rows affected or false on error, see {{API:PodLink|file=DBI|package_name=DBI|section=|text=DBI}}'s execute() method.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;insert( $table, $columns, @values )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_insert -->
+
====insert====
+
 
+
<source lang="perl">$success = $db->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.
  
 
Values will be quoted before insertion.
 
Values will be quoted before insertion.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;insert_quoted( $table, $columns, @Q_values )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_insert_quoted -->
+
====insert_quoted====
+
 
+
<source lang="perl">$success = $db->insert_quoted( $table, $columns, @Q_values )
+
 
+
</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.
  
 
Values will NOT be quoted before insertion - care must be exercised!
 
Values will NOT be quoted before insertion - care must be exercised!
  
<!-- Edit below this comment -->
+
$success = $db-&gt;delete_from( $table, $columns, @values )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_delete_from -->
+
====delete_from====
+
 
+
<source lang="perl">$success = $db->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.
  
 
If you want to clear a table completely use clear_table().
 
If you want to clear a table completely use clear_table().
  
<!-- Edit below this comment -->
+
$n = $db-&gt;count_table( $tablename )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_count_table -->
+
====count_table====
+
 
+
<source lang="perl">$n = $db->count_table( $tablename )
+
 
+
</source>
+
 
Return the number of rows in the specified SQL table.
 
Return the number of rows in the specified SQL table.
  
<!-- Edit below this comment -->
+
$db-&gt;clear_table( $tablename )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_clear_table -->
+
====clear_table====
+
 
+
<source lang="perl">$db->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 501: Line 231:
 
<!-- Pod2Wiki=head_quoting -->
 
<!-- Pod2Wiki=head_quoting -->
 
===Quoting===
 
===Quoting===
<!-- Edit below this comment -->
+
$mungedvalue = EPrints::Database::prep_int( $value )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_prep_int -->
+
====prep_int====
+
 
+
<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).
  
<!-- Edit below this comment -->
+
$mungedvalue = EPrints::Database::prep_value( $value )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_prep_value -->
+
====prep_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 \'
  
<!-- Edit below this comment -->
+
$mungedvalue = EPrints::Database::prep_like_value( $value )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_prep_like_value -->
+
====prep_like_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 _
  
<!-- Edit below this comment -->
+
$str = $db-&gt;quote_value( $value )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_quote_value -->
+
====quote_value====
+
 
+
<source lang="perl">$str = $db->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:
  
my $str = $database-&gt;quote_value( EPrints::Database::prep_like_value( $foo ) . '%' );
+
<pre> my $str = $database-&gt;quote_value( EPrints::Database::prep_like_value( $foo ) . '%' );</pre>
 
+
<!-- Edit below this comment -->
+
  
 
+
$str = $db-&gt;quote_int( $value )
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_quote_int -->
+
====quote_int====
+
 
+
<source lang="perl">$str = $db->quote_int( $value )
+
 
+
</source>
+
 
Return a quoted integer value
 
Return a quoted integer value
  
<!-- Edit below this comment -->
+
$str = $db-&gt;quote_binary( $bytes )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_quote_binary -->
+
====quote_binary====
+
 
+
<source lang="perl">$str = $db->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.
  
 
This method should be called on data containing nul bytes or back-slashes before being passed on [[API:EPrints/Database#quote_value|quote_value]].
 
This method should be called on data containing nul bytes or back-slashes before being passed on [[API:EPrints/Database#quote_value|quote_value]].
  
<!-- Edit below this comment -->
+
$str = $db-&gt;quote_ordervalue( $field, $value )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_quote_ordervalue -->
+
====quote_ordervalue====
+
 
+
<source lang="perl">$str = $db->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.
  
<!-- Edit below this comment -->
+
$str = $db-&gt;quote_identifier( @parts )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_quote_identifier -->
+
====quote_identifier====
+
 
+
<source lang="perl">$str = $db->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.
  
<!-- Edit below this comment -->
+
$sql = $db-&gt;prepare_regexp( $quoted_column, $quoted_value )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_prepare_regexp -->
+
====prepare_regexp====
+
 
+
<source lang="perl">$sql = $db->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).
  
<!-- Edit below this comment -->
+
$sql = $db-&gt;sql_AS()
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_sql_as -->
+
====sql_as====
+
 
+
<source lang="perl">$sql = $db->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.
  
<!-- Edit below this comment -->
+
$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.
  
 +
$sql = $db-&gt;sql_IS_SET( $qname )
 +
Return the SQL to test whether $qname is set. For most databases this will be:
  
<!-- Pod2Wiki= -->
+
<pre>  $qname is not null AND $qname != ''</pre>
<!-- Pod2Wiki=item_sql_like -->
+
====sql_like====
+
  
<source lang="perl">$sql = $db->sql_LIKE()
+
$qname is a quoted identifier or value.
 
+
</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.
+
  
 
<!-- Edit below this comment -->
 
<!-- Edit below this comment -->
Line 643: Line 281:
 
<!-- Pod2Wiki=head_counters -->
 
<!-- Pod2Wiki=head_counters -->
 
===Counters===
 
===Counters===
<!-- Edit below this comment -->
+
$n = $db-&gt;counter_current( $counter )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_counter_current -->
+
====counter_current====
+
 
+
<source lang="perl">$n = $db->counter_current( $counter )
+
 
+
</source>
+
 
Return the value of the previous counter_next on $counter.
 
Return the value of the previous counter_next on $counter.
  
<!-- Edit below this comment -->
+
$n = $db-&gt;counter_next( $counter )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_counter_next -->
+
====counter_next====
+
 
+
<source lang="perl">$n = $db->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.
  
<!-- Edit below this comment -->
+
$db-&gt;counter_minimum( $counter, $value )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_counter_minimum -->
+
====counter_minimum====
+
 
+
<source lang="perl">$db->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.
  
<!-- Edit below this comment -->
+
$db-&gt;counter_reset( $counter )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_counter_reset -->
+
====counter_reset====
+
 
+
<source lang="perl">$db->counter_reset( $counter )
+
 
+
</source>
+
 
Reset the counter. Use with caution.
 
Reset the counter. Use with caution.
  
<!-- Edit below this comment -->
+
$n = $db-&gt;next_doc_pos( $eprintid )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_next_doc_pos -->
+
====next_doc_pos====
+
 
+
<source lang="perl">$n = $db->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 709: Line 302:
 
<!-- Pod2Wiki=head_dataset_data -->
 
<!-- Pod2Wiki=head_dataset_data -->
 
===Dataset data===
 
===Dataset data===
<!-- Edit below this comment -->
+
$boolean = $db-&gt;exists( $dataset, $id )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_exists -->
+
====exists====
+
 
+
<source lang="perl">$boolean = $db->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.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;insert_data( $dataset, $data )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_insert_data -->
+
====insert_data====
+
 
+
<source lang="perl">$success = $db->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.
  
 
Returns undef if a record already exists with the key field value in $data.
 
Returns undef if a record already exists with the key field value in $data.
  
Inserts ordervalues if the dataset is {{API:PodLink|file=EPrints/DataSet|package_name=EPrints::DataSet|section=ordered|text=ordered}}.
+
Inserts ordervalues if the dataset is [[API:EPrints/DataSet#ordered|ordered]].
  
<!-- Edit below this comment -->
+
$success = $db-&gt;update_data( $dataset, $data, $changed )
 
+
Updates a record in the given [[API:EPrints/DataSet|$dataset]]. $data is a copy of the entire record's data and $changed the values that have changed.
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_update_data -->
+
====update_data====
+
 
+
<source lang="perl">$success = $db->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.
+
  
 
If the record does not already exist or the key field is unset in $data no changes will be written.
 
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 {{API:PodLink|file=EPrints/DataSet|package_name=EPrints::DataSet|section=ordered|text=ordered}}.
+
Updates ordervalues if the dataset is [[API:EPrints/DataSet#ordered|ordered]].
 
+
<!-- Edit below this comment -->
+
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_remove -->
+
====remove====
+
 
+
<source lang="perl">$success = $db->remove( $dataset, $id )
+
  
</source>
+
$success = $db-&gt;remove( $dataset, $id )
 
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 773: Line 330:
 
<!-- Pod2Wiki=head_searching_caching_and_object_retrieval -->
 
<!-- Pod2Wiki=head_searching_caching_and_object_retrieval -->
 
===Searching, caching and object retrieval===
 
===Searching, caching and object retrieval===
<!-- Edit below this comment -->
+
$searchexp = $db-&gt;cache_exp( $cacheid )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_cache_exp -->
+
====cache_exp====
+
 
+
<source lang="perl">$searchexp = $db->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.
  
<!-- Edit below this comment -->
+
$cacheid = $db-&gt;cache( $searchexp, $dataset, $srctable, [$order], [$list] )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_cache -->
+
====cache====
+
 
+
<source lang="perl">$cacheid = $db->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 803: Line 342:
 
If $srctable is set to "ALL" every matching record from $dataset is added to the cache, optionally ordered by $order.
 
If $srctable is set to "ALL" every matching record from $dataset is added to the cache, optionally ordered by $order.
  
<!-- Edit below this comment -->
+
$tablename = $db-&gt;cache_table( $id )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_cache_table -->
+
====cache_table====
+
 
+
<source lang="perl">$tablename = $db->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.
  
<!-- Edit below this comment -->
+
$ids = $db-&gt;search( $keyfield, $tables, $conditions, [$main_table_alias] )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_search -->
+
====search====
+
 
+
<source lang="perl">$ids = $db->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.  
  
 
If no table alias is passed then M is assumed.  
 
If no table alias is passed then M is assumed.  
  
<!-- Edit below this comment -->
+
$db-&gt;drop_cache( $id )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_drop_cache -->
+
====drop_cache====
+
 
+
<source lang="perl">$db->drop_cache( $id )
+
 
+
</source>
+
 
Remove the cached search with the given id.
 
Remove the cached search with the given id.
  
<!-- Edit below this comment -->
+
$foo = $db-&gt;from_cache( $dataset, $cacheid, [$offset], [$count], [$justids] )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_from_cache -->
+
====from_cache====
+
 
+
<source lang="perl">$foo = $db->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 857: Line 360:
 
If $justids is true then it returns just an ref to an array of the record ids, not the objects.
 
If $justids is true then it returns just an ref to an array of the record ids, not the objects.
  
<!-- Edit below this comment -->
+
$c = $db-&gt;drop_orphan_cache_tables
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_drop_orphan_cache_tables -->
+
====drop_orphan_cache_tables====
+
 
+
<source lang="perl">$c = $db->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.
  
<!-- Edit below this comment -->
+
$obj = $db-&gt;get_single( $dataset, $id )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_single -->
+
====get_single====
+
 
+
<source lang="perl">$obj = $db->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.
  
<!-- Edit below this comment -->
+
$items = $db-&gt;get_all( $dataset )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_all -->
+
====get_all====
+
 
+
<source lang="perl">$items = $db->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.
  
<!-- Edit below this comment -->
+
@ids = $db-&gt;get_cache_ids( $dataset, $cachemap, $offset, $count )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_cache_ids -->
+
====get_cache_ids====
+
 
+
<source lang="perl">@ids = $db->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.
  
<!-- Edit below this comment -->
+
@dataobjs = $db-&gt;get_dataobjs( $dataset [, $id [, $id ] ] )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_dataobjs -->
+
====get_dataobjs====
+
 
+
<source lang="perl">@dataobjs = $db->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.
  
<!-- Edit below this comment -->
+
$foo = $db-&gt;get_values( $field, $dataset )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_values -->
+
====get_values====
+
 
+
<source lang="perl">$foo = $db->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.
  
<!-- Edit below this comment -->
+
$ids = $db-&gt;get_ids_by_field_values( $field, $dataset [ %opts ] )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_ids_by_field_values -->
+
====get_ids_by_field_values====
+
 
+
<source lang="perl">$ids = $db->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.
  
<!-- Edit below this comment -->
+
@events = $db-&gt;dequeue_events( $n )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_dequeue_events -->
+
====dequeue_events====
+
 
+
<source lang="perl">@events = $db->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 959: Line 390:
 
<!-- Pod2Wiki=head_password_validation -->
 
<!-- Pod2Wiki=head_password_validation -->
 
===Password Validation===
 
===Password Validation===
<!-- Edit below this comment -->
+
$real_username = $db-&gt;valid_login( $username, $password )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_valid_login -->
+
====valid_login====
+
 
+
<source lang="perl">$real_username = $db->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.
  
Line 979: Line 401:
 
<!-- Pod2Wiki=head_database_schema_manipulation -->
 
<!-- Pod2Wiki=head_database_schema_manipulation -->
 
===Database schema manipulation===
 
===Database schema manipulation===
<!-- Edit below this comment -->
+
$boolean = $db-&gt;has_sequence( $name )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_has_sequence -->
+
====has_sequence====
+
 
+
<source lang="perl">$boolean = $db->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.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;create_sequence( $seq_name )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_sequence -->
+
====create_sequence====
+
 
+
<source lang="perl">$success = $db->create_sequence( $seq_name )
+
 
+
</source>
+
 
Creates a new sequence object initialised to zero.
 
Creates a new sequence object initialised to zero.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;drop_sequence( $seq_name )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_drop_sequence -->
+
====drop_sequence====
+
 
+
<source lang="perl">$success = $db->drop_sequence( $seq_name )
+
 
+
</source>
+
 
Deletes a sequence object.
 
Deletes a sequence object.
  
<!-- Edit below this comment -->
+
$boolean = $db-&gt;has_column( $tablename, $columnname )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_has_column -->
+
====has_column====
+
 
+
<source lang="perl">$boolean = $db->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.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;drop_column( $table, $column )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_drop_column -->
+
====drop_column====
+
 
+
<source lang="perl">$success = $db->drop_column( $table, $column )
+
 
+
</source>
+
 
Drops a column from a table.
 
Drops a column from a table.
  
<!-- Edit below this comment -->
+
@columns = $db-&gt;get_primary_key( $tablename )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_primary_key -->
+
====get_primary_key====
+
 
+
<source lang="perl">@columns = $db->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.
  
 
Returns empty list if no primary key exists.
 
Returns empty list if no primary key exists.
  
<!-- Edit below this comment -->
+
$db-&gt;create_primary_key( $tablename, @cols )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_primary_key -->
+
====create_primary_key====
+
 
+
<source lang="perl">$db->create_primary_key( $tablename, @cols )
+
 
+
</source>
+
 
Create a PRIMARY KEY on $tablename over @cols.
 
Create a PRIMARY KEY on $tablename over @cols.
  
<!-- Edit below this comment -->
+
$name = $db-&gt;index_name( $table, @columns )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_index_name -->
+
====index_name====
+
 
+
<source lang="perl">$name = $db->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.
  
 
Returns undef if no index exists.
 
Returns undef if no index exists.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;create_index( $tablename, @columns )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_index -->
+
====create_index====
+
 
+
<source lang="perl">$success = $db->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.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;create_unique_index( $tablename, @columns )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_unique_index -->
+
====create_unique_index====
+
 
+
<source lang="perl">$success = $db->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.
  
<!-- Edit below this comment -->
+
$ok = $db-&gt;create_foreign_key( $main_table, $aux_table, $key_field )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_foreign_key -->
+
====create_foreign_key====
+
 
+
<source lang="perl">$ok = $db->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.
  
 
This will cause records in $aux_table to be deleted if the equivalent record is deleted from $main_table.
 
This will cause records in $aux_table to be deleted if the equivalent record is deleted from $main_table.
  
<!-- Edit below this comment -->
+
@tables = $db-&gt;get_tables
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_get_tables -->
+
====get_tables====
+
 
+
<source lang="perl">@tables = $db->get_tables
+
 
+
</source>
+
 
Return a list of all the tables in the database.
 
Return a list of all the tables in the database.
  
<!-- Edit below this comment -->
+
$boolean = $db-&gt;has_table( $tablename )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_has_table -->
+
====has_table====
+
 
+
<source lang="perl">$boolean = $db->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.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;create_table( $tablename, $setkey, @fields );
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_table -->
+
====create_table====
+
 
+
<source lang="perl">$success = $db->create_table( $tablename, $setkey, @fields );
+
 
+
</source>
+
 
Creates a new table $tablename based on @fields.
 
Creates a new table $tablename based on @fields.
  
 
The first $setkey number of fields are used for a primary key.
 
The first $setkey number of fields are used for a primary key.
  
<!-- Edit below this comment -->
+
$db-&gt;drop_table( $tablename [, $tablename2 ] )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_drop_table -->
+
====drop_table====
+
 
+
<source lang="perl">$db->drop_table( $tablename [, $tablename2 ] )
+
 
+
</source>
+
 
Delete the named table(s). Use with caution!
 
Delete the named table(s). Use with caution!
  
<!-- Edit below this comment -->
+
$db-&gt;rename_table( $tablename, $newtablename )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_rename_table -->
+
====rename_table====
+
 
+
<source lang="perl">$db->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.
  
<!-- Edit below this comment -->
+
$db-&gt;swap_table( $table_a, $table_b )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_swap_table -->
+
====swap_table====
+
 
+
<source lang="perl">$db->swap_table( $table_a, $table_b )
+
 
+
</source>
+
 
Swap table a and table b.  
 
Swap table a and table b.  
  
Line 1,197: Line 466:
 
<!-- Pod2Wiki=head_eprints_schema_manipulation -->
 
<!-- Pod2Wiki=head_eprints_schema_manipulation -->
 
===EPrints schema manipulation===
 
===EPrints schema manipulation===
<!-- Edit below this comment -->
+
$success = $db-&gt;create_archive_tables
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_archive_tables -->
+
====create_archive_tables====
+
 
+
<source lang="perl">$success = $db->create_archive_tables
+
 
+
</source>
+
 
Create all the SQL tables for each dataset.
 
Create all the SQL tables for each dataset.
  
<!-- Edit below this comment -->
+
$db-&gt;drop_archive_tables()
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_drop_archive_tables -->
+
====drop_archive_tables====
+
 
+
<source lang="perl">$db->drop_archive_tables()
+
 
+
</source>
+
 
Destroy all tables used by eprints in the database.
 
Destroy all tables used by eprints in the database.
  
<!-- Edit below this comment -->
+
$db-&gt;create_version_table
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_version_table -->
+
====create_version_table====
+
 
+
<source lang="perl">$db->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).
  
<!-- Edit below this comment -->
+
$db-&gt;drop_version_table
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_drop_version_table -->
+
====drop_version_table====
+
 
+
<source lang="perl">$db->drop_version_table
+
 
+
</source>
+
 
Drop the version table.
 
Drop the version table.
  
<!-- Edit below this comment -->
+
$db-&gt;has_dataset( $dataset )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_has_dataset -->
+
====has_dataset====
+
 
+
<source lang="perl">$db->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.
  
 
This does not check that all fields are configured - see has_field().
 
This does not check that all fields are configured - see has_field().
  
<!-- Edit below this comment -->
+
$success = $db-&gt;create_dataset_tables( $dataset )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_dataset_tables -->
+
====create_dataset_tables====
+
 
+
<source lang="perl">$success = $db->create_dataset_tables( $dataset )
+
 
+
</source>
+
 
Create all the SQL tables for a single dataset.
 
Create all the SQL tables for a single dataset.
  
Line 1,275: Line 490:
 
Order values tables are created for each configured languaged if the dataset is ordered.
 
Order values tables are created for each configured languaged if the dataset is ordered.
  
<!-- Edit below this comment -->
+
$db-&gt;drop_dataset_tables( $dataset )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_drop_dataset_tables -->
+
====drop_dataset_tables====
+
 
+
<source lang="perl">$db->drop_dataset_tables( $dataset )
+
 
+
</source>
+
 
Drop all the SQL tables for a single dataset.
 
Drop all the SQL tables for a single dataset.
  
<!-- Edit below this comment -->
+
$db-&gt;has_field( $dataset, $field )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_has_field -->
+
====has_field====
+
 
+
<source lang="perl">$db->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.
  
<!-- Edit below this comment -->
+
$db-&gt;add_field( $dataset, $field [, $force ] )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_add_field -->
+
====add_field====
+
 
+
<source lang="perl">$db->add_field( $dataset, $field [, $force ] )
+
 
+
</source>
+
 
Add $field to $dataset's tables.
 
Add $field to $dataset's tables.
  
 
If $force is true will modify/replace an existing column (use with care!).
 
If $force is true will modify/replace an existing column (use with care!).
  
<!-- Edit below this comment -->
+
$db-&gt;remove_field( $dataset, $field )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_remove_field -->
+
====remove_field====
+
 
+
<source lang="perl">$db->remove_field( $dataset, $field )
+
 
+
</source>
+
 
Remove $field from $dataset's tables.
 
Remove $field from $dataset's tables.
  
<!-- Edit below this comment -->
+
$ok = $db-&gt;rename_field( $dataset, $field, $old_name )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_rename_field -->
+
====rename_field====
+
 
+
<source lang="perl">$ok = $db->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.
  
 
Returns true if the field was successfully renamed.
 
Returns true if the field was successfully renamed.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;create_counters
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_counters -->
+
====create_counters====
+
 
+
<source lang="perl">$success = $db->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.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;has_counter( $counter )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_has_counter -->
+
====has_counter====
+
 
+
<source lang="perl">$success = $db->has_counter( $counter )
+
 
+
</source>
+
 
Returns true if $counter exists.
 
Returns true if $counter exists.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;create_counter( $name )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_create_counter -->
+
====create_counter====
+
 
+
<source lang="perl">$success = $db->create_counter( $name )
+
 
+
</source>
+
 
Create and initialise to zero a new counter called $name.
 
Create and initialise to zero a new counter called $name.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;remove_counters
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_remove_counters -->
+
====remove_counters====
+
 
+
<source lang="perl">$success = $db->remove_counters
+
 
+
</source>
+
 
Destroy all counters.
 
Destroy all counters.
  
<!-- Edit below this comment -->
+
$success = $db-&gt;drop_counter( $name )
 
+
 
+
<!-- Pod2Wiki= -->
+
<!-- Pod2Wiki=item_drop_counter -->
+
====drop_counter====
+
 
+
<source lang="perl">$success = $db->drop_counter( $name )
+
 
+
</source>
+
 
Destroy the counter named $name.
 
Destroy the counter named $name.
  

Latest revision as of 09:56, 22 January 2013

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


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

$db = EPrints::Database->new( $repo )

Create a connection to the database.

$db = $db->create( $username, $password )

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

$ok = $db->connect

Connects to the database.

$db->disconnect

Disconnects from the EPrints database.

$db->set_debug( $boolean )

Set the SQL debug mode to true or false.

$db->set_version( $versionid );

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

$version = $db->get_version

Returns the current database schema version.

$boolean = $db->is_latest_version

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

$version = $db->get_server_version

Return the database server version.

$charset = $db->get_default_charset( LANGUAGE )

Return the character set to use for LANGUAGE.

Returns undef if character sets are unsupported.

$collation = $db->get_default_collation( LANGUAGE )

Return the collation to use for LANGUAGE.

Returns undef if collation is unsupported.

$driver = $db->get_driver_name

Return the database driver name.

$errstr = $db->error

Return a string describing the last SQL error.

$bool = $db->retry_error

Returns true if the current error is a retry error.

$bool = $db->duplicate_error

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

$db->begin

Begin a transaction.

$db->commit

Commit the previously begun transaction.

$db->rollback

Roll-back the current transaction.

$type_info = $db->type_info( DATA_TYPE )

See DBI/type_info.

$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.


Basic SQL operations

$success = $db->do( $sql )

Execute the given SQL.

$sth = $db->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;
$sth = $db->prepare_select( $sql [, %options ] )

Prepare a SELECT statement $sql for execution. This method provides cross-database support for getting a part of the result list based on an offset/limit.

Returns a DBI statement handle.

Options:

  limit - limit the number of rows returned
  offset - return limit number of rows after offset
$rows = $db->update( $tablename, $keycols, $keyvals, $columns, $values )

UPDATEs $columns with the equivalent slot from $values where the rows in $tablename match the $keycols/$keyvals.

Returns the number of rows affected or false on error, see DBI's execute() method.

  $db->update(
    'eprint',
    ['eprintid'],
    [23],
    ['title','description'],
    ['Hello, World!', undef]
  );
$success = $db->update_quoted( $tablename, $keycols, $keyvals, $columns, $Q_values )

UPDATEs $tablename where $keycols equals $keyvals.

Returns the number of rows affected or false on error, see DBI's execute() method.

$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.

$success = $db->insert_quoted( $table, $columns, @Q_values )

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!

$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().

$n = $db->count_table( $tablename )

Return the number of rows in the specified SQL table.

$db->clear_table( $tablename )

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


Quoting

$mungedvalue = EPrints::Database::prep_int( $value )

Escape a numerical value for SQL. undef becomes NULL. Anything else becomes a number (zero if needed).

$mungedvalue = EPrints::Database::prep_value( $value )

Escape a value for SQL. Modify value such that " becomes \" and \ becomes \\ and ' becomes \'

$mungedvalue = EPrints::Database::prep_like_value( $value )

Escape an value for an SQL like field. In addition to ' " and \ also escapes % and _

$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 ) . '%' );
$str = $db->quote_int( $value )

Return a quoted integer value

$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.

$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.

$str = $db->quote_identifier( @parts )

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

$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 = $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 = $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.

$sql = $db->sql_IS_SET( $qname )

Return the SQL to test whether $qname is set. For most databases this will be:

  $qname is not null AND $qname != ''

$qname is a quoted identifier or value.


Counters

$n = $db->counter_current( $counter )

Return the value of the previous counter_next on $counter.

$n = $db->counter_next( $counter )

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

$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.

$db->counter_reset( $counter )

Reset the counter. Use with caution.

$n = $db->next_doc_pos( $eprintid )

Return the next unused document pos for the given eprintid.


Dataset data

$boolean = $db->exists( $dataset, $id )

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

$success = $db->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.

$success = $db->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.

$success = $db->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 object retrieval

$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.

$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.

$tablename = $db->cache_table( $id )

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

$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.

$db->drop_cache( $id )

Remove the cached search with the given id.

$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.

$c = $db->drop_orphan_cache_tables

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

$obj = $db->get_single( $dataset, $id )

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

$items = $db->get_all( $dataset )

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

@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.

@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.

$foo = $db->get_values( $field, $dataset )

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

$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.

@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.


Password Validation

$real_username = $db->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.


Database schema manipulation

$boolean = $db->has_sequence( $name )

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

$success = $db->create_sequence( $seq_name )

Creates a new sequence object initialised to zero.

$success = $db->drop_sequence( $seq_name )

Deletes a sequence object.

$boolean = $db->has_column( $tablename, $columnname )

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

$success = $db->drop_column( $table, $column )

Drops a column from a table.

@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.

$db->create_primary_key( $tablename, @cols )

Create a PRIMARY KEY on $tablename over @cols.

$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.

$success = $db->create_index( $tablename, @columns )

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

$success = $db->create_unique_index( $tablename, @columns )

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

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

@tables = $db->get_tables

Return a list of all the tables in the database.

$boolean = $db->has_table( $tablename )

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

$success = $db->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.

$db->drop_table( $tablename [, $tablename2 ] )

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

$db->rename_table( $tablename, $newtablename )

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

$db->swap_table( $table_a, $table_b )

Swap table a and table b.


EPrints schema manipulation

$success = $db->create_archive_tables

Create all the SQL tables for each dataset.

$db->drop_archive_tables()

Destroy all tables used by eprints in the database.

$db->create_version_table

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

$db->drop_version_table

Drop the version table.

$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().

$success = $db->create_dataset_tables( $dataset )

Create all the SQL tables for a single dataset.

Index tables are created in the dataset is indexable.

Order values tables are created for each configured languaged if the dataset is ordered.

$db->drop_dataset_tables( $dataset )

Drop all the SQL tables for a single dataset.

$db->has_field( $dataset, $field )

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

$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!).

$db->remove_field( $dataset, $field )

Remove $field from $dataset's tables.

$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.

$success = $db->create_counters

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

$success = $db->has_counter( $counter )

Returns true if $counter exists.

$success = $db->create_counter( $name )

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

$success = $db->remove_counters

Destroy all counters.

$success = $db->drop_counter( $name )

Destroy the counter named $name.


SEE ALSO

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


COPYRIGHT

Copyright 2000-2011 University of Southampton.

This file is part of EPrints http://www.eprints.org/.

EPrints is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

EPrints is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with EPrints. If not, see http://www.gnu.org/licenses/.