You are here

public function MigrateSourceMSSQL::performRewind in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/sources/mssql.inc \MigrateSourceMSSQL::performRewind()

Implementation of MigrateSource::performRewind().

File

plugins/sources/sqlsrv.inc, line 150
Define a MigrateSource for importing from Microsoft SQL Server databases.

Class

MigrateSourceMSSQL
Implementation of MigrateSource, to handle imports from remote MS SQL Server db servers.

Code

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');
}