Skip to content

Commit

Permalink
postgresql tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjcowan committed Nov 2, 2024
1 parent 20b3540 commit 11f6efd
Show file tree
Hide file tree
Showing 5 changed files with 462 additions and 233 deletions.
138 changes: 70 additions & 68 deletions src/DapperMatic/Providers/PostgreSql/PostgreSqlMethods.Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ protected override (string sql, object parameters) SqlGetSchemaNames(
? ""
: ToLikeString(schemaNameFilter);

var sql =
$"""
SELECT DISTINCT nspname
FROM pg_catalog.pg_namespace
{(string.IsNullOrWhiteSpace(where) ? "" : "WHERE lower(nspname) LIKE @where")}
ORDER BY nspname
""";
var sql = $"""
SELECT DISTINCT nspname
FROM pg_catalog.pg_namespace
{(string.IsNullOrWhiteSpace(where) ? "" : "WHERE lower(nspname) LIKE @where")}
ORDER BY nspname
""";

return (sql, new { where });
}
Expand Down Expand Up @@ -56,17 +55,17 @@ protected override (string sql, object parameters) SqlDoesTableExist(
string tableName
)
{
var sql =
$"""
SELECT COUNT(*)
FROM pg_class
JOIN pg_catalog.pg_namespace n ON n.oid = pg_class.relnamespace
WHERE
relkind = 'r'
{(string.IsNullOrWhiteSpace(schemaName) ? "" : " AND lower(nspname) = @schemaName")}
AND lower(relname) = @tableName
""";
var sql = $"""
SELECT COUNT(*)
FROM pg_class pgc
JOIN pg_catalog.pg_namespace n ON n.oid = pgc.relnamespace
WHERE
pgc.relkind = 'r'
{(
string.IsNullOrWhiteSpace(schemaName) ? "" : " AND lower(n.nspname) = @schemaName"
)}
AND lower(pgc.relname) = @tableName
""";

return (
sql,
Expand All @@ -85,18 +84,18 @@ protected override (string sql, object parameters) SqlGetTableNames(
{
var where = string.IsNullOrWhiteSpace(tableNameFilter) ? "" : ToLikeString(tableNameFilter);

var sql =
$"""
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE'
AND lower(TABLE_SCHEMA) = @schemaName
AND TABLE_NAME NOT IN ('spatial_ref_sys', 'geometry_columns', 'geography_columns', 'raster_columns', 'raster_overviews')
{(string.IsNullOrWhiteSpace(where) ? null : " AND lower(TABLE_NAME) LIKE @where")}
ORDER BY TABLE_NAME
""";
var sql = $"""
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE = 'BASE TABLE'
AND lower(TABLE_SCHEMA) = @schemaName
AND TABLE_NAME NOT IN ('spatial_ref_sys', 'geometry_columns', 'geography_columns', 'raster_columns', 'raster_overviews')
{(
string.IsNullOrWhiteSpace(where) ? null : " AND lower(TABLE_NAME) LIKE @where"
)}
ORDER BY TABLE_NAME
""";

return (
sql,
Expand Down Expand Up @@ -132,11 +131,11 @@ string expression
var schemaQualifiedTableName = GetSchemaQualifiedIdentifierName(schemaName, tableName);

return $"""
ALTER TABLE {schemaQualifiedTableName}
ALTER COLUMN {NormalizeName(columnName)} SET DEFAULT {expression}
""";
ALTER TABLE {schemaQualifiedTableName}
ALTER COLUMN {NormalizeName(columnName)} SET DEFAULT {expression}
""";
}

protected override string SqlDropDefaultConstraint(
Expand Down Expand Up @@ -170,25 +169,27 @@ protected override string SqlDropIndex(string? schemaName, string tableName, str

protected override (string sql, object parameters) SqlGetViewNames(
string? schemaName,
string? viewNameFilter = null)
string? viewNameFilter = null
)
{
var where = string.IsNullOrWhiteSpace(viewNameFilter) ? "" : ToLikeString(viewNameFilter);

var sql =
$"""
SELECT
v.viewname as ViewName
from pg_views as v
where
v.schemaname not like 'pg_%'
and v.schemaname != 'information_schema'
and v.viewname not in ('geography_columns', 'geometry_columns', 'raster_columns', 'raster_overviews')
and lower(v.schemaname) = @schemaName
{(string.IsNullOrWhiteSpace(where) ? "" : " AND lower(v.viewname) LIKE @where")}
ORDER BY
v.schemaname, v.viewname
""";
var sql = $"""
SELECT
v.viewname as ViewName
from pg_views as v
where
v.schemaname not like 'pg_%'
and v.schemaname != 'information_schema'
and v.viewname not in ('geography_columns', 'geometry_columns', 'raster_columns', 'raster_overviews')
and lower(v.schemaname) = @schemaName
{(
string.IsNullOrWhiteSpace(where) ? "" : " AND lower(v.viewname) LIKE @where"
)}
ORDER BY
v.schemaname, v.viewname
""";

return (
sql,
Expand All @@ -207,23 +208,24 @@ protected override (string sql, object parameters) SqlGetViews(
{
var where = string.IsNullOrWhiteSpace(viewNameFilter) ? "" : ToLikeString(viewNameFilter);

var sql =
$"""
SELECT
v.schemaname as SchemaName,
v.viewname as ViewName,
v.definition as Definition
from pg_views as v
where
v.schemaname not like 'pg_%'
and v.schemaname != 'information_schema'
and v.viewname not in ('geography_columns', 'geometry_columns', 'raster_columns', 'raster_overviews')
and lower(v.schemaname) = @schemaName
{(string.IsNullOrWhiteSpace(where) ? "" : " AND lower(v.viewname) LIKE @where")}
ORDER BY
v.schemaname, v.viewname
""";
var sql = $"""
SELECT
v.schemaname as SchemaName,
v.viewname as ViewName,
v.definition as Definition
from pg_views as v
where
v.schemaname not like 'pg_%'
and v.schemaname != 'information_schema'
and v.viewname not in ('geography_columns', 'geometry_columns', 'raster_columns', 'raster_overviews')
and lower(v.schemaname) = @schemaName
{(
string.IsNullOrWhiteSpace(where) ? "" : " AND lower(v.viewname) LIKE @where"
)}
ORDER BY
v.schemaname, v.viewname
""";

return (
sql,
Expand Down
Loading

0 comments on commit 11f6efd

Please sign in to comment.