Skip to content

Commit

Permalink
fix: add more types from sqlx docs (#10)
Browse files Browse the repository at this point in the history
* fix: add more types from sqlx docs

* fix: include array types

* fix: use underscore internal type

* fix: vecs for updates

---------

Co-authored-by: James Holman <james.holman@betashares.com.au>
  • Loading branch information
jayy-lmao and jayy-lmao authored Apr 1, 2024
1 parent c3f9757 commit d260906
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/db_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ SELECT
c.table_name,
c.column_name,
c.udt_name,
c.data_type,
c.table_schema,
c.is_nullable = 'YES' AS is_nullable,
CASE
Expand Down
2 changes: 1 addition & 1 deletion src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub async fn generate_migration_code(
if let Some(table_row) = matching_column {
let existing_nullable = table_row.is_nullable;
let existing_type = &table_row.udt_name;
if data_type != convert_data_type(existing_type) {
if data_type != &convert_data_type(existing_type) {
panic!("Data type {} does not match {}", data_type, existing_type);
}
// Compare data types and nullability
Expand Down
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub struct TableColumn {
pub(crate) table_name: String,
pub(crate) column_name: String,
pub(crate) udt_name: String,
pub(crate) data_type: String,
pub(crate) is_nullable: bool,
pub(crate) is_unique: bool,
pub(crate) is_primary_key: bool,
Expand Down
54 changes: 36 additions & 18 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn generate_struct_code(table_name: &str, rows: &Vec<TableColumn>) -> String
let mut data_type = convert_data_type(&row.udt_name);
let optional_type = format!("Option<{}>", data_type);
if row.is_nullable {
data_type = optional_type.as_str();
data_type = optional_type;
}

struct_code.push_str(&format!(" pub {}: {},\n", column_name, data_type));
Expand All @@ -46,35 +46,52 @@ pub fn generate_struct_code(table_name: &str, rows: &Vec<TableColumn>) -> String
struct_code
}

pub fn convert_data_type(data_type: &str) -> &str {
pub fn convert_data_type(data_type: &str) -> String {
if data_type.to_lowercase().contains("char(") {
return "String".to_string();
}
if data_type.starts_with("_") {
let array_of_type = convert_data_type(&data_type[1..]);
let vec_type = format!("Vec<{}>", array_of_type);
return vec_type;
}

match data_type {
"int8" => "i64",
"int4" => "i32",
"int2" => "i16",
"text" => "String",
"varchar" => "String",
"jsonb" => "sqlx::Json",
"timestamptz" => "chrono::DateTime<chrono::Utc>",
"timestamp" => "chrono::NaiveDateTime",
"time" => "chrono::NaiveTime",
"bool" | "boolean" => "bool",
"bytea" => "Vec<u8>", // is this right?
"char" => "i8",
"date" => "chrono::NaiveDate",
"float4" => "f32",
"float8" => "f64",
"float4" | "real" => "f32",
"float8" | "double precision" => "f64",
"int2" | "smallint" | "smallserial" => "i16",
"int4" | "int" | "serial" => "i32",
"int8" | "bigint" | "bigserial" => "i64",
"void" => "()",
"jsonb" | "json" => "serde_json::Value",
"text" | "varchar" | "name" | "citext" => "String",
"time" => "chrono::NaiveTime",
"timestamp" => "chrono::NaiveDateTime",
"timestamptz" => "chrono::DateTime<chrono::Utc>",
"uuid" => "uuid::Uuid",
"boolean" => "bool",
"bool" => "bool",
"bytea" => "Vec<u8>", // is this right?
_ => panic!("Unknown type: {}", data_type),
}
.to_string()
}

pub fn convert_data_type_from_pg(data_type: &str) -> &str {
pub fn convert_data_type_from_pg(data_type: &str) -> String {
if data_type.contains("Json<") {
return "jsonb".to_string();
}
if data_type.contains("Vec<") {
let array_type = convert_data_type_from_pg(&data_type[4..data_type.len() - 1]);
return format!("{}[]", array_type);
}
match data_type {
"i64" => "int8",
"i32" => "int4",
"i16" => "int2",
"String" => "text",
"sqlx::Json" => "jsonb",
"serde_json::Value" => "jsonb",
"chrono::DateTime<chrono::Utc>" => "timestamptz",
"chrono::NaiveDateTime" => "timestamp",
"DateTime<Utc>" => "timestamptz",
Expand All @@ -86,6 +103,7 @@ pub fn convert_data_type_from_pg(data_type: &str) -> &str {
"Vec<u8>" => "bytea", // is this right ?
_ => panic!("Unknown type: {}", data_type),
}
.to_string()
}

fn generate_query_code(_row: &TableColumn) -> String {
Expand Down

0 comments on commit d260906

Please sign in to comment.