class MigrateSourceMSSQL in Migrate 7.2
Same name and namespace in other branches
- 6.2 plugins/sources/mssql.inc \MigrateSourceMSSQL
Implementation of MigrateSource, to handle imports from remote MS SQL Server db servers.
Hierarchy
- class \MigrateSource implements \Iterator
- class \MigrateSourceMSSQL
Expanded class hierarchy of MigrateSourceMSSQL
File
- plugins/
sources/ sqlsrv.inc, line 11 - Define a MigrateSource for importing from Microsoft SQL Server databases.
View source
class MigrateSourceMSSQL extends MigrateSource {
/**
* Array containing information for connecting to SQL Server:
* servername - Hostname of the SQL Server
* username - Username to connect as
* password - Password for logging in
* database (optional) - Database to select after connecting
* character_set (optional) - set to "UTF-8" for multibyte chars support
*
* @var array
*/
protected $configuration;
/**
* The active MS SQL Server connection for this source.
*
* @var resource
*/
protected $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;
/**
* By default, sqlsrv_query fetches all results - severe memory problems with
* big tables. So, we will fetch a batch at a time.
*
* @var int
*/
protected $batchSize;
/**
* Return an options array for MS SQL sources.
*
* @param int $batch_size
* Number of rows to pull at once (defaults to 500).
* @param boolean $cache_counts
* Indicates whether to cache counts of source records.
*/
public static function options($batch_size, $cache_counts) {
return compact('batch_size', '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;
$this->batchSize = isset($options['batch_size']) ? $options['batch_size'] : 500;
}
/**
* 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('sqlsrv')) {
throw new Exception(t('You must configure the sqlsrv extension in PHP.'));
}
if (isset($this->configuration['port'])) {
$host = $this->configuration['servername'] . ',' . $this->configuration['port'];
}
else {
$host = $this->configuration['servername'];
}
$connectionInfo = array(
"Database" => $this->configuration['database'],
"UID" => $this->configuration['username'],
"PWD" => $this->configuration['password'],
);
// Add CharacterSet option.
if (!empty($this->configuration['character_set'])) {
$connectionInfo["CharacterSet"] = $this->configuration['character_set'];
}
$this->connection = sqlsrv_connect($host, $connectionInfo);
if ($this->connection !== FALSE) {
return $this->connection;
}
}
}
/**
* 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('MigrateSourceMSSQL count');
if ($connect = $this
->connect()) {
$result = sqlsrv_query($connect, $this->countQuery);
$result_array = sqlsrv_fetch_array($result);
if ($result_array) {
$count = reset($result_array);
}
}
else {
// Do something else?
$count = FALSE;
}
migrate_instrument_stop('MigrateSourceMSSQL count');
return $count;
}
/**
* Implementation of MigrateSource::performRewind().
*/
public function performRewind() {
/*
* 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)) {
$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;
}
// 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('sqlsrv_query');
$this
->connect();
$this->result = sqlsrv_query($this->connection, $this->query, array(
$this->batchSize,
));
migrate_instrument_stop('sqlsrv_query');
}
/**
* Implementation of MigrateSource::getNextRow().
*
* Returns the next row of the result set as an object, dealing with the
* difference between the end of the batch and the end of all data.
*/
public function getNextRow() {
$row = sqlsrv_fetch_object($this->result);
// Might be totally out of data, or just out of this batch - request another
// batch and see
if (!is_object($row)) {
sqlsrv_num_rows($this->result);
$row = sqlsrv_fetch_object($this->result);
}
if (is_object($row)) {
return $row;
}
else {
return NULL;
}
}
}
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 | |
MigrateSourceMSSQL:: |
protected | property | By default, sqlsrv_query fetches all results - severe memory problems with big tables. So, we will fetch a batch at a time. | |
MigrateSourceMSSQL:: |
protected | property | Array containing information for connecting to SQL Server: servername - Hostname of the SQL Server username - Username to connect as password - Password for logging in database (optional) - Database to select after connecting character_set (optional)… | |
MigrateSourceMSSQL:: |
protected | property | The active MS SQL Server connection for this source. | |
MigrateSourceMSSQL:: |
protected | property | The SQL query from which to obtain data. Is a string. | |
MigrateSourceMSSQL:: |
protected | property | The result object from executing the query - traversed to process the incoming data. | |
MigrateSourceMSSQL:: |
public | function | Return a count of all available source records. | |
MigrateSourceMSSQL:: |
protected | function | Connect lazily to the DB server. | |
MigrateSourceMSSQL:: |
public | function |
Returns a list of fields available to be mapped from the source query. Overrides MigrateSource:: |
|
MigrateSourceMSSQL:: |
public | function | Implementation of MigrateSource::getNextRow(). | |
MigrateSourceMSSQL:: |
public static | function | Return an options array for MS SQL sources. | |
MigrateSourceMSSQL:: |
public | function | Implementation of MigrateSource::performRewind(). | |
MigrateSourceMSSQL:: |
public | function |
Simple initialization. Overrides MigrateSource:: |
|
MigrateSourceMSSQL:: |
public | function | Return a string representing the source query. |