db2.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @file
  4. * Define a MigrateSource class for importing from IBM DB2 databases.
  5. */
  6. /**
  7. * Implementation of MigrateSource, to handle imports from remote DB2 servers.
  8. */
  9. class MigrateSourceDB2 extends MigrateSource {
  10. /**
  11. * Array containing information for connecting to DB2:
  12. * database - Database to connect to
  13. * username - Username to connect as
  14. * password - Password for authentication
  15. *
  16. * @var array
  17. */
  18. protected $configuration;
  19. /**
  20. * The active DB2 connection for this source.
  21. *
  22. * @var resource
  23. */
  24. protected $connection;
  25. public function getConnection() {
  26. return $this->connection;
  27. }
  28. /**
  29. * The SQL query from which to obtain data. Is a string.
  30. */
  31. protected $query;
  32. /**
  33. * The statement resource from executing the query - traversed to process the
  34. * incoming data.
  35. */
  36. protected $stmt;
  37. /**
  38. * Return an options array for DB2 sources.
  39. *
  40. * @param boolean $cache_counts
  41. * Indicates whether to cache counts of source records.
  42. */
  43. static public function options($cache_counts = FALSE) {
  44. return compact('cache_counts');
  45. }
  46. /**
  47. * Simple initialization.
  48. */
  49. public function __construct(array $configuration, $query, $count_query,
  50. array $fields, array $options = array()) {
  51. parent::__construct($options);
  52. $this->query = $query;
  53. $this->countQuery = $count_query;
  54. $this->configuration = $configuration;
  55. $this->fields = $fields;
  56. }
  57. /**
  58. * Return a string representing the source query.
  59. *
  60. * @return string
  61. */
  62. public function __toString() {
  63. return $this->query;
  64. }
  65. /**
  66. * Connect lazily to the DB server.
  67. */
  68. protected function connect() {
  69. if (!isset($this->connection)) {
  70. // Check for the ibm_db2 extension before attempting to connect with it.
  71. if (!extension_loaded('ibm_db2')) {
  72. throw new Exception(t('You must configure the ibm_db2 extension in PHP.'));
  73. }
  74. // Connect to db2.
  75. $this->connection = db2_connect($this->configuration['database'],
  76. $this->configuration['username'], $this->configuration['password']);
  77. }
  78. if ($this->connection) {
  79. return TRUE;
  80. }
  81. // If we failed to connect, throw an exception with the connection error
  82. // message.
  83. else {
  84. $e = db2_conn_errormsg();
  85. throw new Exception($e);
  86. return FALSE;
  87. }
  88. }
  89. /**
  90. * Returns a list of fields available to be mapped from the source query.
  91. *
  92. * @return array
  93. * Keys: machine names of the fields (to be passed to addFieldMapping)
  94. * Values: Human-friendly descriptions of the fields.
  95. */
  96. public function fields() {
  97. // The fields are passed to the constructor for this plugin.
  98. return $this->fields;
  99. }
  100. /**
  101. * Return a count of all available source records.
  102. */
  103. public function computeCount() {
  104. migrate_instrument_start('MigrateSourceDB2 count');
  105. // Make sure we're connected.
  106. if ($this->connect()) {
  107. // Execute the count query.
  108. $stmt = db2_exec($this->connection, $this->countQuery);
  109. // If something went wrong, throw an exception with the error message.
  110. if (!$stmt) {
  111. $e = db2_stmt_errormsg($stmt);
  112. throw new Exception($e);
  113. }
  114. // Grab the first row as an array.
  115. $count_array = db2_fetch_array($stmt);
  116. // The first item in this array will be our count.
  117. $count = reset($count_array);
  118. }
  119. else {
  120. // Connection failed.
  121. $count = FALSE;
  122. }
  123. migrate_instrument_stop('MigrateSourceDB2 count');
  124. return $count;
  125. }
  126. /**
  127. * Implementation of MigrateSource::performRewind().
  128. */
  129. public function performRewind() {
  130. migrate_instrument_start('db2_query');
  131. // Ensure we're connected to the database.
  132. $this->connect();
  133. // Execute the query.
  134. $this->stmt = db2_exec($this->connection, $this->query);
  135. // Throw an exception with the error message if something went wrong.
  136. if (!$this->stmt) {
  137. $e = db2_stmt_errormsg($this->stmt);
  138. throw new Exception($e);
  139. }
  140. migrate_instrument_stop('db2_query');
  141. }
  142. /**
  143. * Implementation of MigrateSource::getNextRow().
  144. *
  145. * @return object
  146. */
  147. public function getNextRow() {
  148. return db2_fetch_object($this->stmt);
  149. }
  150. }