/** * Outputs DDL for differences in functions * * @param $ofs1 stage1 output pointer * @param $ofs3 stage3 output pointer * @param $old_schema original schema * @param $new_schema new schema */ public static function diff_functions($ofs1, $ofs3, $old_schema, $new_schema) { // drop functions that no longer exist in stage 3 if ($old_schema != null) { foreach (dbx::get_functions($old_schema) as $old_function) { if (!pgsql8_schema::contains_function($new_schema, pgsql8_function::get_declaration($new_schema, $old_function, FALSE))) { $ofs3->write(pgsql8_function::get_drop_sql($old_schema, $old_function) . "\n"); } } } // Add new functions and replace modified functions foreach (dbx::get_functions($new_schema) as $new_function) { $old_function = null; if ($old_schema != null) { $old_function = dbx::get_function($old_schema, $new_function['name'], pgsql8_function::get_declaration($new_schema, $new_function)); } if ($old_function == null || !pgsql8_function::equals($new_schema, $new_function, $old_function, pgsql8_diff::$ignore_function_whitespace)) { $ofs1->write(pgsql8_function::get_creation_sql($new_schema, $new_function) . "\n"); } else { if (isset($new_function['forceRedefine']) && strcasecmp($new_function['forceRedefine'], 'true') == 0) { $ofs1->write("-- DBSteward insists on function recreation: " . $new_schema['name'] . "." . $new_function['name'] . " has forceRedefine set to true\n"); $ofs1->write(pgsql8_function::get_creation_sql($new_schema, $new_function) . "\n"); } else { if (pgsql8_schema::contains_type($new_schema, $new_function['returns']) && pgsql8_schema::contains_type($old_schema, $new_function['returns']) && !pgsql8_type::equals(dbx::get_type($old_schema, $new_function['returns']), dbx::get_type($new_schema, $new_function['returns']))) { $ofs1->write("-- Force function re-creation " . $new_function['name'] . " for type: " . $new_function['returns'] . "\n"); $ofs1->write(pgsql8_function::get_creation_sql($new_schema, $new_function) . "\n"); } } } } }
/** * Outputs DDL for differences in functions * * @param $ofs1 stage1 output pointer * @param $ofs3 stage3 output pointer * @param $old_schema original schema * @param $new_schema new schema */ public static function diff_functions($ofs1, $ofs3, $old_schema, $new_schema) { // drop functions that no longer exist in stage 3 if ($old_schema != NULL) { foreach (dbx::get_functions($old_schema) as $old_function) { if (!mssql10_schema::contains_function($new_schema, mssql10_function::get_declaration($new_schema, $old_function))) { $ofs3->write(mssql10_function::get_drop_sql($old_schema, $old_function) . "\n"); } } } // Add new functions and replace modified functions foreach (dbx::get_functions($new_schema) as $new_function) { $old_function = NULL; if ($old_schema != NULL) { $old_function = dbx::get_function($old_schema, $new_function['name'], mssql10_function::get_declaration($new_schema, $new_function)); } if ($old_function == NULL) { $ofs1->write(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n"); } else { if (!mssql10_function::equals($new_schema, $new_function, $old_function, mssql10_diff::$ignore_function_whitespace)) { // functions are not equal, old_function is not null, it previously existed // for MSSQL, there is no CREATE OR REPLACE FUNCTION, so drop the function explicitly $ofs1->write(mssql10_function::get_drop_sql($old_schema, $old_function) . "\n"); $ofs1->write(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n"); } else { if (isset($new_function['forceRedefine']) && strcasecmp($new_function['forceRedefine'], 'true') == 0) { $ofs1->write("-- DBSteward insists on function recreation: " . $new_schema['name'] . "." . $new_function['name'] . " has forceRedefine set to true\n"); $ofs1->write(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n"); } else { if (mssql10_schema::contains_type($new_schema, $new_function['returns']) && mssql10_schema::contains_type($old_schema, $new_function['returns']) && !mssql10_type::equals(dbx::get_type($old_schema, $new_function['returns']), dbx::get_type($new_schema, $new_function['returns']))) { $ofs1->write("-- dbstward insisting on function re-creation " . $new_function['name'] . " for type " . $new_function['returns'] . " definition change\n"); $ofs1->write(mssql10_function::get_drop_sql($old_schema, $old_function) . "\n"); $ofs1->write(mssql10_function::get_creation_sql($new_schema, $new_function) . "\n"); } } } } } }
/** * Parses ALTER FUNCTION command. * * @param database database * @param command ALTER FUNCTION command * */ public static function parse($database, $command) { if (preg_match(self::PATTERN_OWNER, $command, $matches) > 0) { $line = $command; $function_name = trim($matches[1]); // make all functionName's fully qualified // default_schema will make set search path induced schemas come through correctly $function_name = sql_parser::get_schema_name($function_name, $database) . '.' . sql_parser::get_object_name($function_name); $arguments = $matches[2]; $owner_name = trim($matches[3]); $node_schema = dbx::get_schema($database, sql_parser::get_schema_name($function_name, $database)); if ($node_schema == null) { throw new exception("Failed to find function schema for " . $function_name); } $node_function = dbx::get_function($node_schema, sql_parser::get_object_name($function_name)); if ($node_function == null) { throw new exception("Failed to find function " . $function_name . " in schema " . $node_schema['name']); } dbx::set_attribute($node_function, 'owner', $owner_name); } else { throw new exception("Cannot parse command: " . $command); } }
protected static function update_permissions($ofs1, $ofs3) { foreach (dbx::get_schemas(dbsteward::$new_database) as $new_schema) { $old_schema = dbx::get_schema(dbsteward::$old_database, $new_schema['name']); foreach (dbx::get_permissions($new_schema) as $new_permission) { if ($old_schema == NULL || !mssql10_permission::has_permission($old_schema, $new_permission)) { $ofs1->write(mssql10_permission::get_sql(dbsteward::$new_database, $new_schema, $new_schema, $new_permission) . "\n"); } } foreach (dbx::get_tables($new_schema) as $new_table) { $old_table = NULL; if ($old_schema != NULL) { $old_table = dbx::get_table($old_schema, $new_table['name']); } if (!dbsteward::$ignore_oldnames && mssql10_diff_tables::is_renamed_table($new_schema, $new_table)) { // oldTableName renamed table ? skip permission diffing on it, it is the same continue; } foreach (dbx::get_permissions($new_table) as $new_permission) { if ($old_table == NULL || !mssql10_permission::has_permission($old_table, $new_permission)) { $ofs1->write(mssql10_permission::get_sql(dbsteward::$new_database, $new_schema, $new_table, $new_permission) . "\n"); } } } foreach (dbx::get_sequences($new_schema) as $new_sequence) { $old_sequence = NULL; if ($old_schema != NULL) { $old_sequence = dbx::get_sequence($old_schema, $new_sequence['name']); } foreach (dbx::get_permissions($new_sequence) as $new_permission) { if ($old_sequence == NULL || !mssql10_permission::has_permission($old_sequence, $new_permission)) { $ofs1->write(mssql10_permission::get_sql(dbsteward::$new_database, $new_schema, $new_sequence, $new_permission) . "\n"); } } } foreach (dbx::get_functions($new_schema) as $new_function) { $old_function = NULL; if ($old_schema != NULL) { $old_function = dbx::get_function($old_schema, $new_function['name'], mssql10_function::get_declaration($new_schema, $new_function)); } foreach (dbx::get_permissions($new_function) as $new_permission) { if ($old_function == NULL || !mssql10_permission::has_permission($old_function, $new_permission)) { $ofs1->write(mssql10_permission::get_sql(dbsteward::$new_database, $new_schema, $new_function, $new_permission) . "\n"); } } } foreach (dbx::get_views($new_schema) as $new_view) { $old_view = NULL; if ($old_schema != NULL) { $old_view = dbx::get_view($old_schema, $new_view['name']); } foreach (dbx::get_permissions($new_view) as $new_permission) { // if always_recreate_views flag is on, always grant all view permissions, as the view was recreated if (dbsteward::$always_recreate_views || $old_view == NULL || !mssql10_permission::has_permission($old_view, $new_permission) || mssql10_diff_views::is_view_modified($old_view, $new_view)) { // view permissions are in schema stage 3 file because views are (re)created in that stage for SELECT * expansion $ofs3->write(mssql10_permission::get_sql(dbsteward::$new_database, $new_schema, $new_view, $new_permission) . "\n"); } } } } }
/** * Parses CREATE FUNCTION and CREATE OR REPLACE FUNCTION command. * * @param database database * @param command CREATE FUNCTION command */ public static function parse($database, $command) { if (preg_match(self::PATTERN, trim($command), $matches) > 0) { $function_name = trim($matches[1]); // make all functionName's fully qualified // default_schema will make set search path induced schemas come through correctly $function_name = sql_parser::get_schema_name($function_name, $database) . '.' . sql_parser::get_object_name($function_name); $arguments = $matches[2]; $node_schema = dbx::get_schema($database, sql_parser::get_schema_name($function_name, $database)); if ($node_schema == null) { throw new exception("Failed to find function schema for " . $function_name); } $node_function = dbx::get_function($node_schema, sql_parser::get_object_name($function_name), null, true); //@TODO: this may be a problem when there is more than one prototype for a function $function_declaration = pgsql8_function::set_declaration($node_schema, $node_function, $arguments); // check remaining definition for function modifiers by chopping of function declaration $function_close_position = stripos($command, ')'); $function_modifiers = str_replace("\n", ' ', substr($command, $function_close_position + 1)); // kill extra whitespace by regex match $function_modifiers = preg_replace("/\\s+/", " ", $function_modifiers); // kill trailing semicolon $function_modifiers = trim($function_modifiers); if (substr($function_modifiers, -1) == ';') { $function_modifiers = trim(substr($function_modifiers, 0, -1)); } $function_modifiers = ' ' . $function_modifiers . ' '; // AS token (definition) token // AS $_$ BEGIN DO STUFF END $_$ if (($as_pos = stripos($function_modifiers, ' AS ')) !== false) { $end_as_token_pos = strpos($function_modifiers, ' ', $as_pos + 4); $as_token = substr($function_modifiers, $as_pos + 4, $end_as_token_pos - ($as_pos + 4)); $definition_start = strpos($function_modifiers, $as_token, $as_pos) + strlen($as_token); $definition_end = strpos($function_modifiers, $as_token, $definition_start + strlen($as_token)); $definition = substr($function_modifiers, $definition_start, $definition_end - $definition_start); $definition = trim($definition); pgsql8_function::set_definition($node_function, $definition); // cut out what we just found $function_modifiers = substr($function_modifiers, 0, $as_pos) . ' ' . substr($function_modifiers, $definition_end + strlen($as_token)); } // now that the AS <token> (definition) <token> section is gone, parsing is simpler: // RETURNS (type) if (preg_match(self::PATTERN_RETURNS, $function_modifiers, $matches) > 0) { dbx::set_attribute($node_function, 'returns', trim($matches[1])); } // LANGUAGE (languagename) if (preg_match(self::PATTERN_LANGUAGE, $function_modifiers, $matches) > 0) { dbx::set_attribute($node_function, 'language', trim($matches[1])); } // check for IMMUTABLE | STABLE | VOLATILE modifiers if (preg_match('/.*\\s+IMMUTABLE\\s+.*/i', $function_modifiers, $matches) > 0) { dbx::set_attribute($node_function, 'cachePolicy', 'IMMUTABLE'); } if (preg_match('/.*\\s+STABLE\\s+.*/i', $function_modifiers, $matches) > 0) { dbx::set_attribute($node_function, 'cachePolicy', 'STABLE'); } if (preg_match('/.*\\s+VOLATILE\\s+.*/i', $function_modifiers, $matches) > 0) { dbx::set_attribute($node_function, 'cachePolicy', 'VOLATILE'); } // check for SECURITY DEFINER modifier if (preg_match('/.*\\s+SECURITY DEFINER\\s+.*/i', $function_modifiers, $matches) > 0) { dbx::set_attribute($node_function, 'securityDefiner', 'true'); } } else { throw new exception("Cannot parse command: " . $command); } }