|
| 1 | +use sqlx::{Error, FromRow, PgConnection}; |
| 2 | + |
| 3 | +const QUERY: &str = r#" |
| 4 | +SELECT |
| 5 | + table_catalog, |
| 6 | + table_schema, |
| 7 | + table_name, |
| 8 | + column_name, |
| 9 | + ordinal_position, |
| 10 | + column_default, |
| 11 | + is_nullable, |
| 12 | + data_type, |
| 13 | + character_maximum_length, |
| 14 | + character_octet_length, |
| 15 | + numeric_precision, |
| 16 | + numeric_precision_radix, |
| 17 | + numeric_scale, |
| 18 | + datetime_precision, |
| 19 | + interval_type, |
| 20 | + interval_precision, |
| 21 | + character_set_catalog, |
| 22 | + character_set_schema, |
| 23 | + character_set_name, |
| 24 | + collation_catalog, |
| 25 | + collation_schema, |
| 26 | + collation_name, |
| 27 | + domain_catalog, |
| 28 | + domain_schema, |
| 29 | + domain_name, |
| 30 | + udt_catalog, |
| 31 | + udt_schema, |
| 32 | + udt_name, |
| 33 | + scope_catalog, |
| 34 | + scope_schema, |
| 35 | + scope_name, |
| 36 | + maximum_cardinality, |
| 37 | + dtd_identifier, |
| 38 | + is_self_referencing, |
| 39 | + is_identity, |
| 40 | + identity_generation, |
| 41 | + identity_start, |
| 42 | + identity_increment, |
| 43 | + identity_maximum, |
| 44 | + identity_minimum, |
| 45 | + identity_cycle, |
| 46 | + is_generated, |
| 47 | + generation_expression, |
| 48 | + is_updatable |
| 49 | +FROM |
| 50 | + information_schema.columns |
| 51 | +WHERE |
| 52 | + table_schema = ANY($1) |
| 53 | +ORDER BY |
| 54 | + table_catalog, |
| 55 | + table_schema, |
| 56 | + table_name, |
| 57 | + column_name;"#; |
| 58 | + |
| 59 | +pub async fn columns(connection: &mut PgConnection, schema_names: &[String]) -> Result<Vec<Column>, Error> { |
| 60 | + sqlx::query_as(QUERY) |
| 61 | + .bind(&schema_names[..]) |
| 62 | + .fetch_all(connection).await |
| 63 | +} |
| 64 | + |
| 65 | +#[derive(Debug, Clone, PartialEq, FromRow)] |
| 66 | +pub struct Column { |
| 67 | + pub table_catalog: String, |
| 68 | + pub table_schema: String, |
| 69 | + pub table_name: String, |
| 70 | + pub column_name: String, |
| 71 | + pub ordinal_position: i32, |
| 72 | + pub column_default: Option<String>, |
| 73 | + pub is_nullable: String, |
| 74 | + pub data_type: String, |
| 75 | + pub character_maximum_length: Option<i32>, |
| 76 | + pub character_octet_length: Option<i32>, |
| 77 | + pub numeric_precision: Option<i32>, |
| 78 | + pub numeric_precision_radix: Option<i32>, |
| 79 | + pub numeric_scale: Option<i32>, |
| 80 | + pub datetime_precision: Option<i32>, |
| 81 | + pub interval_type: Option<String>, |
| 82 | + pub interval_precision: Option<i32>, |
| 83 | + pub character_set_catalog: Option<String>, |
| 84 | + pub character_set_schema: Option<String>, |
| 85 | + pub character_set_name: Option<String>, |
| 86 | + pub collation_catalog: Option<String>, |
| 87 | + pub collation_schema: Option<String>, |
| 88 | + pub collation_name: Option<String>, |
| 89 | + pub domain_catalog: Option<String>, |
| 90 | + pub domain_schema: Option<String>, |
| 91 | + pub domain_name: Option<String>, |
| 92 | + pub udt_catalog: Option<String>, |
| 93 | + pub udt_schema: Option<String>, |
| 94 | + pub udt_name: Option<String>, |
| 95 | + pub scope_catalog: Option<String>, |
| 96 | + pub scope_schema: Option<String>, |
| 97 | + pub scope_name: Option<String>, |
| 98 | + pub maximum_cardinality: Option<i32>, |
| 99 | + pub dtd_identifier: Option<String>, |
| 100 | + pub is_self_referencing: String, |
| 101 | + pub is_identity: String, |
| 102 | + pub identity_generation: Option<String>, |
| 103 | + pub identity_start: Option<String>, |
| 104 | + pub identity_increment: Option<String>, |
| 105 | + pub identity_maximum: Option<String>, |
| 106 | + pub identity_minimum: Option<String>, |
| 107 | + pub identity_cycle: Option<String>, |
| 108 | + pub is_generated: String, |
| 109 | + pub generation_expression: Option<String>, |
| 110 | + pub is_updatable: String, |
| 111 | +} |
| 112 | + |
0 commit comments