Skip to content

Commit 57b1b7d

Browse files
committed
use BoxeableExpression for some dryness
1 parent 3213376 commit 57b1b7d

File tree

1 file changed

+25
-26
lines changed
  • diesel_cli/src/infer_schema_internals

1 file changed

+25
-26
lines changed

diesel_cli/src/infer_schema_internals/pg.rs

+25-26
Original file line numberDiff line numberDiff line change
@@ -93,44 +93,43 @@ pub fn get_table_data(
9393
domain_types_enabled: bool,
9494
) -> QueryResult<Vec<ColumnInformation>> {
9595
use self::information_schema::columns::dsl::*;
96-
use diesel::sql_types::{Nullable, SingleValue};
96+
use diesel::sql_types::{Nullable, SingleValue, Text};
9797

9898
let schema_name = match table.schema {
9999
Some(ref name) => Cow::Borrowed(name),
100100
None => Cow::Owned(Pg::default_schema(conn)?),
101101
};
102102

103-
let query = if domain_types_enabled {
103+
let (select_name, select_schema) = if domain_types_enabled {
104104
define_sql_function!(#[sql_name = "coalesce"] fn coalesce_maybe<T: SingleValue>(x: Nullable<T>, y: Nullable<T>) -> Nullable<T>);
105105
define_sql_function!(#[sql_name = "coalesce"] fn coalesce_strict<T: SingleValue>(x: Nullable<T>, y: T) -> T);
106106

107-
columns
108-
.select((
109-
column_name,
110-
coalesce_strict(domain_name, udt_name),
111-
coalesce_maybe(domain_schema, udt_schema.nullable()),
112-
__is_nullable,
113-
character_maximum_length,
114-
col_description(regclass(table), ordinal_position),
115-
))
116-
.filter(table_name.eq(&table.sql_name))
117-
.filter(table_schema.eq(schema_name))
118-
.into_boxed()
107+
type BoxedTextExpr = Box<dyn BoxableExpression<columns, Pg, SqlType = Text>>;
108+
type BoxedMaybeTextExpr = Box<dyn BoxableExpression<columns, Pg, SqlType = Text>>;
109+
110+
(
111+
Box::new(coalesce_strict(domain_name, udt_name)) as BoxedTextExpr,
112+
Box::new(coalesce_maybe(domain_schema, udt_schema.nullable())) as BoxedMaybeTextExpr,
113+
)
119114
} else {
120-
columns
121-
.select((
122-
column_name,
123-
udt_name,
124-
udt_schema.nullable(),
125-
__is_nullable,
126-
character_maximum_length,
127-
col_description(regclass(table), ordinal_position),
128-
))
129-
.filter(table_name.eq(&table.sql_name))
130-
.filter(table_schema.eq(schema_name))
131-
.into_boxed()
115+
(
116+
Box::new(udt_name) as BoxedTextExpr,
117+
Box::new(udt_schema.nullable()) as BoxedMaybeTextExpr,
118+
)
132119
};
133120

121+
let query = columns
122+
.select((
123+
column_name,
124+
select_name,
125+
select_schema,
126+
__is_nullable,
127+
character_maximum_length,
128+
col_description(regclass(table), ordinal_position),
129+
))
130+
.filter(table_name.eq(&table.sql_name))
131+
.filter(table_schema.eq(schema_name));
132+
134133
match column_sorting {
135134
ColumnSorting::OrdinalPosition => query.order(ordinal_position).load(conn),
136135
ColumnSorting::Name => query.order(column_name).load(conn),

0 commit comments

Comments
 (0)