You are here

function _install_from_db_read_sql_command_from_file in Open Atrium 7.2

Read a multiline sql command from a file.

Supports the formatting created by mysqldump, but won't handle multiline comments. Taken from backup_migrate module.

1 call to _install_from_db_read_sql_command_from_file()
_install_from_db_read_sql_batch in install_from_db/install_from_db.profile
Read a batch of sql commands (ending in commit)

File

install_from_db/install_from_db.profile, line 328
Install profile helper to add option for importing from database.

Code

function _install_from_db_read_sql_command_from_file($file, $save_line = '') {
  static $save_for_later = '';
  if (!empty($save_for_later)) {

    // Check if a previous line was saved.
    $out = $save_for_later;
    $save_for_later = '';
    return trim($out);
  }
  if (!empty($save_line)) {

    // Save this text for the next time we read from file
    // used to stuff the previous line back into the read buffer.
    $save_for_later = $save_line;
    return;
  }
  $out = '';
  while (($line = fgets($file)) !== FALSE) {
    $line = trim($line);
    if (empty($out) && !empty($line) && substr($line, 0, 2) == '--') {

      // Return single line comments so we can parse what table this was later.
      return trim($line);
    }
    $first2 = substr($line, 0, 2);
    $first3 = substr($line, 0, 3);

    // If a line begins with /*! it is a commented inline sql command
    // only execute lines that don't reference @variables
    if (empty($out) && $first3 == '/*!' && strpos($line, '= @') === FALSE && strpos($line, '=@') === FALSE) {
      return trim($line);
    }

    // Otherwise, ignore single line comments within a sql statement.
    if (!empty($line) && $first2 != '--' && $first2 != '/*') {
      $out .= ' ' . $line;

      // If a line ends in ; it is a sql command.
      if (substr($out, strlen($out) - 1, 1) == ';') {
        return trim($out);
      }
    }
  }
  if (empty($out) && $line === FALSE) {
    return FALSE;
  }
  return trim($out);
}