-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve data type parsing and formatting
Add a C module to delegate the parsing of data types by `col_type_is()` to the Postgres core `parseTypeString()` function, which is the canonical type parser. This ensure that no matter the aliasing or typemod specified for a data type, we end up with exactly the same spelling as the core uses, ensuring more accurate comparisons. This was necessitated by the change in 1e1d745 that added support for type aliases but broke complicated typmods such as ` second(0)` in `interval second(0)`. Resolves #315. Document the two new functions, `parse_type()` and `format_type_string()` and remove the documentation for `pg_typeof()`, which hasn't shipped with pgTAP in several years, since it has been in the Postgres core since 8.3. Also, fix the name of the v1.3.9 upgrade file.
- Loading branch information
Showing
10 changed files
with
607 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
CREATE FUNCTION parse_type(type text, OUT typid oid, OUT typmod int4) | ||
RETURNS RECORD | ||
AS '$libdir/pgtap' | ||
LANGUAGE C STABLE STRICT; | ||
|
||
CREATE OR REPLACE FUNCTION format_type_string ( TEXT ) | ||
RETURNS TEXT AS $$ | ||
BEGIN RETURN format_type(p.typid, p.typmod) from parse_type($1) p; | ||
EXCEPTION WHEN OTHERS THEN RETURN NULL; | ||
END; | ||
$$ LANGUAGE PLPGSQL STABLE; | ||
|
||
-- col_type_is( schema, table, column, schema, type, description ) | ||
CREATE OR REPLACE FUNCTION col_type_is ( NAME, NAME, NAME, NAME, TEXT, TEXT ) | ||
RETURNS TEXT AS $$ | ||
DECLARE | ||
have_type TEXT := _get_col_ns_type($1, $2, $3); | ||
want_type TEXT; | ||
BEGIN | ||
IF have_type IS NULL THEN | ||
RETURN fail( $6 ) || E'\n' || diag ( | ||
' Column ' || COALESCE(quote_ident($1) || '.', '') | ||
|| quote_ident($2) || '.' || quote_ident($3) || ' does not exist' | ||
); | ||
END IF; | ||
|
||
IF quote_ident($4) = ANY(current_schemas(true)) THEN | ||
want_type := quote_ident($4) || '.' || format_type_string($5); | ||
ELSE | ||
want_type := format_type_string(quote_ident($4) || '.' || $5); | ||
END IF; | ||
|
||
IF want_type IS NULL THEN | ||
RETURN fail( $6 ) || E'\n' || diag ( | ||
' Type ' || quote_ident($4) || '.' || $5 || ' does not exist' | ||
); | ||
END IF; | ||
|
||
IF have_type = want_type THEN | ||
-- We're good to go. | ||
RETURN ok( true, $6 ); | ||
END IF; | ||
|
||
-- Wrong data type. tell 'em what we really got. | ||
RETURN ok( false, $6 ) || E'\n' || diag( | ||
' have: ' || have_type || | ||
E'\n want: ' || want_type | ||
); | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
-- col_type_is( schema, table, column, schema, type ) | ||
CREATE OR REPLACE FUNCTION col_type_is ( NAME, NAME, NAME, NAME, TEXT ) | ||
RETURNS TEXT AS $$ | ||
SELECT col_type_is( $1, $2, $3, $4, $5, 'Column ' || quote_ident($1) || '.' || quote_ident($2) | ||
|| '.' || quote_ident($3) || ' should be type ' || quote_ident($4) || '.' || $5); | ||
$$ LANGUAGE SQL; | ||
|
||
-- col_type_is( schema, table, column, type, description ) | ||
CREATE OR REPLACE FUNCTION col_type_is ( NAME, NAME, NAME, TEXT, TEXT ) | ||
RETURNS TEXT AS $$ | ||
DECLARE | ||
have_type TEXT; | ||
want_type TEXT; | ||
BEGIN | ||
-- Get the data type. | ||
IF $1 IS NULL THEN | ||
have_type := _get_col_type($2, $3); | ||
ELSE | ||
have_type := _get_col_type($1, $2, $3); | ||
END IF; | ||
|
||
IF have_type IS NULL THEN | ||
RETURN fail( $5 ) || E'\n' || diag ( | ||
' Column ' || COALESCE(quote_ident($1) || '.', '') | ||
|| quote_ident($2) || '.' || quote_ident($3) || ' does not exist' | ||
); | ||
END IF; | ||
|
||
want_type := format_type_string($4); | ||
IF want_type IS NULL THEN | ||
RETURN fail( $5 ) || E'\n' || diag ( | ||
' Type ' || $4 || ' does not exist' | ||
); | ||
END IF; | ||
|
||
IF have_type = want_type THEN | ||
-- We're good to go. | ||
RETURN ok( true, $5 ); | ||
END IF; | ||
|
||
-- Wrong data type. tell 'em what we really got. | ||
RETURN ok( false, $5 ) || E'\n' || diag( | ||
' have: ' || have_type || | ||
E'\n want: ' || want_type | ||
); | ||
END; | ||
$$ LANGUAGE plpgsql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.