You are here

function migrate_example_oracle_install in Migrate 6.2

Same name and namespace in other branches
  1. 7.2 migrate_example/migrate_example_oracle/migrate_example_oracle.install \migrate_example_oracle_install()

Implementation of hook_install().

File

migrate_example/migrate_example_oracle/migrate_example_oracle.install, line 9

Code

function migrate_example_oracle_install() {
  global $conf;

  // Should never fail - we can't get here unless hook_requirements passed, right?
  $connection = @oci_connect($conf['oracle_db']['username'], $conf['oracle_db']['password'], $conf['oracle_db']['connection_string'], 'UTF8');
  if (!$connection) {
    $e = oci_error();
    throw new Exception($e['message']);
  }

  // Create a table to hold test data
  $query = "CREATE TABLE ORACLE_CONTENT\n            (OID     NUMBER NOT NULL,\n             TITLE   VARCHAR2(255) NOT NULL,\n             BODY    CLOB NOT NULL,\n             CREATED DATE NOT NULL,\n             UPDATED DATE NOT NULL,\n             CONSTRAINT ORACLE_CONTENT_PK PRIMARY KEY (OID)\n            )";
  $result = oci_parse($connection, $query);
  if (!$result) {
    $e = oci_error($connection);
    throw new Exception($e['message'] . "\n" . $e['sqltext']);
  }
  $status = oci_execute($result);
  if (!$status) {
    $e = oci_error($result);
    throw new Exception($e['message'] . "\n" . $e['sqltext']);
  }

  // Insert a test row or three
  $query = "INSERT INTO ORACLE_CONTENT\n            (OID, TITLE, BODY, CREATED, UPDATED)\n            VALUES(:oid, :title, EMPTY_CLOB(),\n              TO_DATE(:created, 'yyyy/mm/dd hh24:mi:ss'),\n              TO_DATE(:updated, 'yyyy/mm/dd hh24:mi:ss'))\n            RETURNING body INTO :body";
  $result = oci_parse($connection, $query);
  if (!$result) {
    $e = oci_error($connection);
    throw new Exception($e['message'] . "\n" . $e['sqltext']);
  }
  $data = migrate_example_oracle_sample_data();
  oci_bind_by_name($result, ':oid', $oid, 1, SQLT_INT);
  oci_bind_by_name($result, ':title', $title, 255, SQLT_CHR);
  $body_clob = oci_new_descriptor($connection, OCI_D_LOB);
  oci_bind_by_name($result, ':body', $body_clob, -1, SQLT_CLOB);
  oci_bind_by_name($result, ':created', $created, 9, SQLT_CHR);
  oci_bind_by_name($result, ':updated', $updated, 9, SQLT_CHR);
  foreach ($data as $row) {
    extract($row);
    $status = oci_execute($result, OCI_DEFAULT);
    if (!$status) {
      $e = oci_error($result);
      throw new Exception($e['message'] . "\n" . $e['sqltext']);
    }
    $body_clob
      ->save($body);
  }
  oci_commit($connection);
}