migration.mssql.class.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 = " TOP 1000 ";
  31. if (in_array($table, array('Empleado', 'Alumno'))) {
  32. $top = " TOP 5000 ";
  33. }
  34. if ($table == 'ProgramaAcademico') {
  35. $top = " TOP 1000 ";
  36. }
  37. //$top = null;
  38. //$top = " TOP 2500 ";
  39. $extra = null;
  40. if (isset($options) && !empty($options['inner_join'])) {
  41. $extra = ' '.$options['alias_orig_table'].' INNER JOIN '.$options['inner_join'].' '.$options['alias_join_table'].' ON '.$options['on'];
  42. }
  43. $order = isset($options['order']) ? $options['order'] : null;
  44. $sql = "SELECT $top $fields_sql FROM $table $extra $order";
  45. if (!empty($extra)) {
  46. error_log(print_r($options,1));
  47. error_log($sql);
  48. }
  49. //remove
  50. $this->rows_iterator = mssql_query($sql, $this->c);
  51. if ($this->rows_iterator === false) {
  52. error_log("--- Error with query $sql MSSQL error: ".mssql_get_last_message()."-- \n");
  53. }
  54. }
  55. public function fetch_row() {
  56. return mssql_fetch_row($this->rows_iterator);
  57. }
  58. public function fetch_array() {
  59. return mssql_fetch_array($this->rows_iterator, MSSQL_ASSOC);
  60. }
  61. public function num_rows() {
  62. return mssql_num_rows($this->rows_iterator);
  63. }
  64. }