class MigrateSourceOracle in Migrate 6.2
Same name and namespace in other branches
- 7.2 plugins/sources/oracle.inc \MigrateSourceOracle
Implementation of MigrateSource, to handle imports from remote Oracle servers.
Hierarchy
- class \MigrateSource implements \Iterator
- class \MigrateSourceOracle
Expanded class hierarchy of MigrateSourceOracle
File
- plugins/
sources/ oracle.inc, line 11 - Define a MigrateSource class for importing from Oracle databases.
View source
class MigrateSourceOracle extends MigrateSource {
/**
* Array containing information for connecting to Oracle:
* username - Username to connect as
* password - Password for logging in
* connection_string - See http://us.php.net/manual/en/function.oci-connect.php.
*
* @var array
*/
protected $configuration;
/**
* The active Oracle connection for this source.
*
* @var resource
*/
protected $connection;
public function getConnection() {
return $this->connection;
}
/**
* The SQL query from which to obtain data. Is a string.
*/
protected $query;
/**
* The result object from executing the query - traversed to process the
* incoming data.
*/
protected $result;
/**
* Character set to use in retrieving data.
*
* @var string
*/
protected $characterSet;
/**
* Return an options array for Oracle sources.
*
* @param string $character_set
* Character set to use in retrieving data. Defaults to 'UTF8'.
* @param boolean $cache_counts
* Indicates whether to cache counts of source records.
*/
public static function options($character_set = 'UTF8', $cache_counts = FALSE) {
return compact('character_set', 'cache_counts');
}
/**
* Simple initialization.
*/
public function __construct(array $configuration, $query, $count_query, array $fields, array $options = array()) {
parent::__construct($options);
$this->query = $query;
$this->countQuery = $count_query;
$this->configuration = $configuration;
$this->fields = $fields;
if (empty($options['character_set'])) {
$this->characterSet = 'UTF8';
}
else {
$this->characterSet = $options['character_set'];
}
}
/**
* Return a string representing the source query.
*
* @return string
*/
public function __toString() {
return $this->query;
}
/**
* Connect lazily to the DB server.
*/
protected function connect() {
if (!isset($this->connection)) {
if (!extension_loaded('oci8')) {
throw new Exception(t('You must configure the oci8 extension in PHP.'));
}
$this->connection = oci_connect($this->configuration['username'], $this->configuration['password'], $this->configuration['connection_string'], $this->characterSet);
}
if ($this->connection) {
return TRUE;
}
else {
$e = oci_error();
throw new Exception($e['message']);
return FALSE;
}
}
/**
* Returns a list of fields available to be mapped from the source query.
*
* @return array
* Keys: machine names of the fields (to be passed to addFieldMapping)
* Values: Human-friendly descriptions of the fields.
*/
public function fields() {
// The fields are passed to the constructor for this plugin.
return $this->fields;
}
/**
* Return a count of all available source records.
*/
public function computeCount() {
migrate_instrument_start('MigrateSourceOracle count');
if ($this
->connect()) {
$statement = oci_parse($this->connection, $this->countQuery);
if (!$statement) {
$e = oci_error($this->connection);
throw new Exception($e['message'] . "\n" . $e['sqltext']);
}
$result = oci_execute($statement);
if (!$result) {
$e = oci_error($statement);
throw new Exception($e['message'] . "\n" . $e['sqltext']);
}
$count_array = oci_fetch_array($statement);
$count = reset($count_array);
}
else {
// Do something else?
$count = FALSE;
}
migrate_instrument_stop('MigrateSourceOracle count');
return $count;
}
/**
* Implementation of MigrateSource::performRewind().
*/
public function performRewind() {
$keys = array();
foreach ($this->activeMap
->getSourceKey() as $field_name => $field_schema) {
// Allow caller to provide an alias to table containing the primary key.
if (!empty($field_schema['alias'])) {
$field_name = $field_schema['alias'] . '.' . $field_name;
}
$keys[] = $field_name;
}
/*
* Replace :criteria placeholder with idlist or highwater clauses. We
* considered supporting both but it is not worth the complexity. Run twice
* instead.
*/
if (!empty($this->idList)) {
// TODO: Sanitize. not critical as this is admin supplied data in drush.
$this->query = str_replace(':criteria', $keys[0] . ' IN (' . implode(',', $this->idList) . ')', $this->query);
}
else {
if (isset($this->highwaterField['name']) && ($highwater = $this->activeMigration
->getHighwater())) {
if (empty($this->highwaterField['alias'])) {
$highwater_name = $this->highwaterField['name'];
}
else {
$highwater_name = $this->highwaterField['alias'] . '.' . $this->highwaterField['name'];
}
$this->query = str_replace(':criteria', "{$highwater_name} > '{$highwater}'", $this->query);
}
else {
// No idlist or highwater. Replace :criteria placeholder with harmless WHERE
// clause instead of empty since we don't know if an AND follows.
$this->query = str_replace(':criteria', '1=1', $this->query);
}
}
migrate_instrument_start('oracle_query');
$this
->connect();
$this->result = oci_parse($this->connection, $this->query);
if (!$this->result) {
$e = oci_error($this->connection);
throw new Exception($e['message'] . "\n" . $e['sqltext']);
}
$status = oci_execute($this->result);
if (!$status) {
$e = oci_error($this->result);
throw new Exception($e['message'] . "\n" . $e['sqltext']);
}
migrate_instrument_stop('oracle_query');
}
/**
* Implementation of MigrateSource::getNextRow().
*
* Returns the next row of the result set as an object, making sure NULLs are
* represented as PHP NULLs and that LOBs are returned directly without special
* handling.
*/
public function getNextRow() {
$row = oci_fetch_array($this->result, OCI_ASSOC | OCI_RETURN_NULLS | OCI_RETURN_LOBS);
if (!empty($row)) {
return (object) $row;
}
else {
return FALSE;
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MigrateSource:: |
protected | property | The MigrateMap class for the current migration. | |
MigrateSource:: |
protected | property | The Migration class currently invoking us, during rewind() and next(). | |
MigrateSource:: |
protected | property | Whether this instance should cache the source count. | |
MigrateSource:: |
protected | property | Key to use for caching counts. | |
MigrateSource:: |
protected | property | The primary key of the current row | |
MigrateSource:: |
protected | property | The current row from the quey | |
MigrateSource:: |
protected | property | Information on the highwater mark for the current migration, if any. | |
MigrateSource:: |
protected | property | List of source IDs to process. | |
MigrateSource:: |
protected | property | By default, next() will directly read the map row and add it to the data row. A source plugin implementation may do this itself (in particular, the SQL source can incorporate the map table into the query) - if so, it should set this TRUE so we… | |
MigrateSource:: |
protected | property | Number of rows intentionally ignored (prepareRow() returned FALSE) | |
MigrateSource:: |
protected | property | Number of rows we've at least looked at. | 1 |
MigrateSource:: |
protected | property | Whether this instance should not attempt to count the source. | |
MigrateSource:: |
public | function | Return a count of available source records, from the cache if appropriate. Returns -1 if the source is not countable. | |
MigrateSource:: |
public | function | Implementation of Iterator::current() - called when entering a loop iteration, returning the current row | |
MigrateSource:: |
public | function | ||
MigrateSource:: |
public | function | ||
MigrateSource:: |
public | function | ||
MigrateSource:: |
public | function | Implementation of Iterator::key - called when entering a loop iteration, returning the key of the current row. It must be a scalar - we will serialize to fulfill the requirement, but using getCurrentKey() is preferable. | |
MigrateSource:: |
public | function | Implementation of Iterator::next() - subclasses of MigrateSource should implement getNextRow() to retrieve the next valid source rocord to process. | |
MigrateSource:: |
protected | function | Give the calling migration a shot at manipulating, and possibly rejecting, the source row. | |
MigrateSource:: |
public | function | Reset numIgnored back to 0. | |
MigrateSource:: |
public | function | Implementation of Iterator::rewind() - subclasses of MigrateSource should implement performRewind() to do any class-specific setup for iterating source records. | |
MigrateSource:: |
public | function | Implementation of Iterator::valid() - called at the top of the loop, returning TRUE to process the loop and FALSE to terminate it | |
MigrateSourceOracle:: |
protected | property | Character set to use in retrieving data. | |
MigrateSourceOracle:: |
protected | property | Array containing information for connecting to Oracle: username - Username to connect as password - Password for logging in connection_string - See http://us.php.net/manual/en/function.oci-connect.php. | |
MigrateSourceOracle:: |
protected | property | The active Oracle connection for this source. | |
MigrateSourceOracle:: |
protected | property | The SQL query from which to obtain data. Is a string. | |
MigrateSourceOracle:: |
protected | property | The result object from executing the query - traversed to process the incoming data. | |
MigrateSourceOracle:: |
public | function | Return a count of all available source records. | |
MigrateSourceOracle:: |
protected | function | Connect lazily to the DB server. | |
MigrateSourceOracle:: |
public | function |
Returns a list of fields available to be mapped from the source query. Overrides MigrateSource:: |
|
MigrateSourceOracle:: |
public | function | ||
MigrateSourceOracle:: |
public | function | Implementation of MigrateSource::getNextRow(). | |
MigrateSourceOracle:: |
public static | function | Return an options array for Oracle sources. | |
MigrateSourceOracle:: |
public | function | Implementation of MigrateSource::performRewind(). | |
MigrateSourceOracle:: |
public | function |
Simple initialization. Overrides MigrateSource:: |
|
MigrateSourceOracle:: |
public | function | Return a string representing the source query. |