You are here

public function SQLFIReader::init in Feed Import 8

Here you'll init your reader.

Overrides FeedImportReader::init

File

feed_import_base/src/SQLFIReader.php, line 12

Class

SQLFIReader
SQL Reader class, used te read data with SQL queries.

Namespace

Drupal\feed_import_base

Code

public function init() {

  // Require dsn and query.
  if (empty($this->options['dsn']) || empty($this->options['query'])) {
    return FALSE;
  }

  // Set default options.
  $this->options += array(
    'user' => 'root',
    'pass' => NULL,
    'params' => array(),
  );

  // Parse params if needed.
  if (!is_array($this->options['params'])) {
    $params = static::cleanLines($this->options['params']);
    $this->options['params'] = array();
    foreach ($params as &$param) {
      $param = explode('=', $param, 2);
      if (count($param) == 2) {
        $this->options['params'][$param[0]] = $param[1];
      }
      else {
        $this->options['params'][] = $param[0];
      }
    }
    unset($param, $params);
  }

  // Connect to db.
  $db = new PDO($this->options['dsn'], $this->options['user'], $this->options['pass']);
  if (!$db) {
    return FALSE;
  }

  // Prepare query.
  $this->items = $db
    ->prepare($this->options['query'], array(
    PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY,
  ));

  // Execute query
  if (!$this->items
    ->execute($this->options['params'])) {
    return FALSE;
  }
  return TRUE;
}