migration.mssql.class.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. *
  4. */
  5. class MigrationMSSQL extends Migration {
  6. public function __construct($dbhost, $dbport = '1433', $dbuser, $dbpass, $dbname, $boost=false) {
  7. parent::__construct($dbhost, $dbport, $dbuser, $dbpass, $dbname, $boost);
  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 100000 ";
  32. if (in_array($table, array('Empleado', 'Alumno'))) {
  33. //$top = " TOP 2000 ";
  34. }
  35. if ($table == 'ProgramaAcademico') {
  36. //$top = ' TOP 1000 ';
  37. }
  38. if ($table == 'Matricula') {
  39. //$top = " TOP 1000 ";
  40. }
  41. //$top = null;
  42. //$top = " TOP 10 " ;
  43. $extra = null;
  44. if (isset($options) && !empty($options['inner_join'])) {
  45. $extra = ' '.$options['alias_orig_table'].' INNER JOIN '.$options['inner_join'].' '.$options['alias_join_table'].' ON '.$options['on'];
  46. }
  47. $order = isset($options['order']) ? $options['order'] : null;
  48. $sql = "SELECT $top $fields_sql FROM $table $extra $order";
  49. $sql = isset($options['query']) ? sprintf($options['query'], "$top $fields_sql") : $sql;
  50. //Remove
  51. $this->rows_iterator = mssql_query($sql, $this->c);
  52. if ($this->rows_iterator === false) {
  53. error_log("--- Error with query $sql MSSQL error: ".mssql_get_last_message()."-- \n");
  54. }
  55. }
  56. public function fetch_row() {
  57. return mssql_fetch_row($this->rows_iterator);
  58. }
  59. public function fetch_array() {
  60. return mssql_fetch_array($this->rows_iterator, MSSQL_ASSOC);
  61. }
  62. public function num_rows() {
  63. return mssql_num_rows($this->rows_iterator);
  64. }
  65. }