class MigrateSourceDB2 in Migrate 7.2
Implementation of MigrateSource, to handle imports from remote DB2 servers.
Hierarchy
- class \MigrateSource implements \Iterator
- class \MigrateSourceDB2
Expanded class hierarchy of MigrateSourceDB2
File
- plugins/
sources/ db2.inc, line 11 - Define a MigrateSource class for importing from IBM DB2 databases.
View source
class MigrateSourceDB2 extends MigrateSource {
/**
* Array containing information for connecting to DB2:
* database - Database to connect to
* username - Username to connect as
* password - Password for authentication
*
* @var array
*/
protected $configuration;
/**
* The active DB2 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 statement resource from executing the query - traversed to process the
* incoming data.
*/
protected $stmt;
/**
* Return an options array for DB2 sources.
*
* @param boolean $cache_counts
* Indicates whether to cache counts of source records.
*/
public static function options($cache_counts = FALSE) {
return compact('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;
}
/**
* 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)) {
// Check for the ibm_db2 extension before attempting to connect with it.
if (!extension_loaded('ibm_db2')) {
throw new Exception(t('You must configure the ibm_db2 extension in PHP.'));
}
// Connect to db2.
$this->connection = db2_connect($this->configuration['database'], $this->configuration['username'], $this->configuration['password']);
}
if ($this->connection) {
return TRUE;
}
else {
$e = db2_conn_errormsg();
throw new Exception($e);
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('MigrateSourceDB2 count');
// Make sure we're connected.
if ($this
->connect()) {
// Execute the count query.
$stmt = db2_exec($this->connection, $this->countQuery);
// If something went wrong, throw an exception with the error message.
if (!$stmt) {
$e = db2_stmt_errormsg($stmt);
throw new Exception($e);
}
// Grab the first row as an array.
$count_array = db2_fetch_array($stmt);
// The first item in this array will be our count.
$count = reset($count_array);
}
else {
// Connection failed.
$count = FALSE;
}
migrate_instrument_stop('MigrateSourceDB2 count');
return $count;
}
/**
* Implementation of MigrateSource::performRewind().
*/
public function performRewind() {
migrate_instrument_start('db2_query');
// Ensure we're connected to the database.
$this
->connect();
// Execute the query.
$this->stmt = db2_exec($this->connection, $this->query);
// Throw an exception with the error message if something went wrong.
if (!$this->stmt) {
$e = db2_stmt_errormsg($this->stmt);
throw new Exception($e);
}
migrate_instrument_stop('db2_query');
}
/**
* Implementation of MigrateSource::getNextRow().
*
* @return object
*/
public function getNextRow() {
return db2_fetch_object($this->stmt);
}
}
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 | Used in the case of multiple key sources that need to use idlist. | |
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 | The highwater mark at the beginning of the import operation. | |
MigrateSource:: |
protected | property | Whether this instance should not attempt to count the source. | |
MigrateSource:: |
protected | property | If TRUE, we will maintain hashed source rows to determine whether incoming data has changed. | |
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:: |
protected | function | Determine whether this row has changed, and therefore whether it should be processed. | |
MigrateSource:: |
public | function | ||
MigrateSource:: |
public | function | ||
MigrateSource:: |
public | function | ||
MigrateSource:: |
protected | function | Generate a hash of the source row. | 3 |
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 | |
MigrateSourceDB2:: |
protected | property | Array containing information for connecting to DB2: database - Database to connect to username - Username to connect as password - Password for authentication | |
MigrateSourceDB2:: |
protected | property | The active DB2 connection for this source. | |
MigrateSourceDB2:: |
protected | property | The SQL query from which to obtain data. Is a string. | |
MigrateSourceDB2:: |
protected | property | The statement resource from executing the query - traversed to process the incoming data. | |
MigrateSourceDB2:: |
public | function | Return a count of all available source records. | |
MigrateSourceDB2:: |
protected | function | Connect lazily to the DB server. | |
MigrateSourceDB2:: |
public | function |
Returns a list of fields available to be mapped from the source query. Overrides MigrateSource:: |
|
MigrateSourceDB2:: |
public | function | ||
MigrateSourceDB2:: |
public | function | Implementation of MigrateSource::getNextRow(). | |
MigrateSourceDB2:: |
public static | function | Return an options array for DB2 sources. | |
MigrateSourceDB2:: |
public | function | Implementation of MigrateSource::performRewind(). | |
MigrateSourceDB2:: |
public | function |
Simple initialization. Overrides MigrateSource:: |
|
MigrateSourceDB2:: |
public | function | Return a string representing the source query. |