All pastes #641047 Raw Edit

Something

public php v1 · immutable
#641047 ·published 2007-07-31 21:52 UTC
rendered paste body
/** * Find the table alias for a table in a query. * * @param $query *   The query to examine for the alias. * @param *   $table The table to examine for the alias. * @return *   The alias if it exists, or the name of the table if it's present but not *   aliased, or FALSE if the table is not present. */function project_find_alias($query, $table) {  // See if {$table} is already in the query.  $match = array();  // This regexp handles many cases:  // - {$table} can be either in FROM or in a JOIN clause  // - Query might end immediately after {$table}  // - Might not have a table alias for {$table}  $pattern = "@.*(FROM|JOIN)\s+\{$table\}\s*(\S+)?@";  if (preg_match($pattern, $query, $match)) {    $keywords = '@(ON|INNER|LEFT|RIGHT|WHERE|ORDER|GROUP|HAVING|LIMIT)@';    if (!isset($match[2]) || preg_match($keywords, $match[2])) {      // No alias found, just use $table.      $alias = $table;    }    else {      // Alias found.      $alias = $match[2];    }  }  else {    // Table not in query.    $alias = FALSE;  }  return $alias;}