migration.mssql.class.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. *
  4. */
  5. class MigrationMSSQL extends Migration {
  6. public function __construct($dbhost, $dbport = '1433', $dbuser, $dbpass, $dbname) {
  7. parent::__construct($dbhost, $dbport, $dbuser, $dbpass, $dbname);
  8. ini_set('display_errors', 1);
  9. ini_set('mssql.datetimeconvert', 0);
  10. $this->odbtype = 'mssql';
  11. }
  12. public function connect() {
  13. $this->c = mssql_connect($this->odbhost, $this->odbuser, $this->odbpass, TRUE);
  14. if ($this->c === false) {
  15. $this->errors_stack[] = 'Could not connect. MSSQL error: ' . mssql_get_last_message();
  16. return false;
  17. }
  18. mssql_select_db($this->odbname, $this->c);
  19. return true;
  20. }
  21. public function select_all($table, $fields, $options = array()) {
  22. $fields_sql = '';
  23. foreach ($fields as $field) {
  24. $fields_sql .= $field . ', ';
  25. }
  26. if (!empty($fields_sql)) {
  27. $fields_sql = substr($fields_sql, 0, -2);
  28. }
  29. //In order to process X item of each table add TOP X
  30. $top = null;
  31. $top = " TOP 1000 ";
  32. if (in_array($table, array('Empleado', 'Alumno'))) {
  33. $top = " TOP 1000 ";
  34. }
  35. if (in_array($table, array('ProgramaAcademico', 'Matricula'))) {
  36. $top = " TOP 1000 ";
  37. }
  38. //$top = null;
  39. // $top = " TOP 25000 ";
  40. $extra = null;
  41. if (isset($options) && !empty($options['inner_join'])) {
  42. $extra = ' '.$options['alias_orig_table'].' INNER JOIN '.$options['inner_join'].' '.$options['alias_join_table'].' ON '.$options['on'];
  43. }
  44. $order = isset($options['order']) ? $options['order'] : null;
  45. $sql = "SELECT $top $fields_sql FROM $table $extra $order";
  46. $sql = isset($options['query']) ? sprintf($options['query'], "$top $fields_sql") : $sql;
  47. if (!empty($extra)) {
  48. error_log(print_r($options,1));
  49. error_log($sql);
  50. }
  51. //Remove
  52. $this->rows_iterator = mssql_query($sql, $this->c);
  53. if ($this->rows_iterator === false) {
  54. error_log("--- Error with query $sql MSSQL error: ".mssql_get_last_message()."-- \n");
  55. }
  56. }
  57. public function fetch_row() {
  58. return mssql_fetch_row($this->rows_iterator);
  59. }
  60. public function fetch_array() {
  61. return mssql_fetch_array($this->rows_iterator, MSSQL_ASSOC);
  62. }
  63. public function num_rows() {
  64. return mssql_num_rows($this->rows_iterator);
  65. }
  66. }