Skip to content

Commit

Permalink
wtf is going on
Browse files Browse the repository at this point in the history
  • Loading branch information
eboody committed Dec 6, 2024
1 parent 16b5db8 commit 393cb0e
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 82 deletions.
2 changes: 1 addition & 1 deletion attr/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,6 @@ pub enum Privacy {
panic!()
};
let attr = DeriveParser::from_attributes(&item.attrs);
assert_eq!(attr.has_derive("ormlite", "ManualType"), true);
assert!(attr.has_derive("ormlite", "ManualType"));
}
}
4 changes: 2 additions & 2 deletions attr/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use quote::TokenStreamExt;
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Ident(String);

impl Ident {
pub fn as_ref(&self) -> &String {
impl AsRef<String> for Ident {
fn as_ref(&self) -> &String {
&self.0
}
}
Expand Down
9 changes: 6 additions & 3 deletions attr/src/metadata/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ impl ColumnMeta {
}

pub fn from_fields<'a>(fields: impl Iterator<Item = &'a Field>) -> Vec<Self> {
fields.map(|f| ColumnMeta::from_field(f)).collect()
fn fun_name(f: &Field) -> ColumnMeta {
ColumnMeta::from_field(f)
}
fields.map(fun_name).collect()
}

pub fn from_syn(ident: &syn::Ident, ty: &syn::Type) -> Self {
Expand Down Expand Up @@ -268,8 +271,8 @@ pub name: String
let column = ColumnMeta::from_field(field);
assert_eq!(column.name, "name");
assert_eq!(column.ty, "String");
assert_eq!(column.marked_primary_key, false);
assert_eq!(column.has_database_default, false);
assert!(!column.marked_primary_key);
assert!(!column.has_database_default);
assert_eq!(column.rust_default, Some("\"foo\".to_string()".to_string()));
assert_eq!(column.ident, "name");
}
Expand Down
23 changes: 15 additions & 8 deletions attr/src/metadata/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ impl ModelMeta {
pub fn from_derive(ast: &DeriveInput) -> Self {
let attrs = TableAttr::from_attrs(&ast.attrs);
let table = TableMeta::new(ast, &attrs);
let pkey = table.pkey.as_deref().expect(&format!(
"No column marked with #[ormlite(primary_key)], and no column named id, uuid, {0}_id, or {0}_uuid",
table.name,
));
let pkey = table.pkey.as_deref().unwrap_or_else(|| {
panic!(
"No column marked with #[ormlite(primary_key)], and no column named id, uuid, {0}_id, or {0}_uuid",
table.name
)
});
let mut insert_struct = None;
let mut extra_derives: Option<Vec<syn::Ident>> = None;
for attr in attrs {
Expand All @@ -48,13 +50,18 @@ impl ModelMeta {
}
}
let pkey = table.columns.iter().find(|&c| c.name == pkey).unwrap().clone();
let insert_struct = insert_struct.map(|v| Ident::from(v));
let extra_derives = extra_derives.take().map(|vec| vec.into_iter().map(|v| v.to_string()).map(Ident::from).collect());

fn fun_name(v: String) -> Ident {
Ident::from(v)
}
let insert_struct = insert_struct.map(fun_name);
let extra_derives = extra_derives
.take()
.map(|vec| vec.into_iter().map(|v| v.to_string()).map(Ident::from).collect());

Self {
table,
insert_struct,
extra_derives,
extra_derives,
pkey,
}
}
Expand Down
2 changes: 1 addition & 1 deletion attr/src/metadata/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl TableMeta {
let mut pkey = columns
.iter()
.find(|&c| c.marked_primary_key)
.map(|c| c.clone())
.cloned()
.map(|c| c.name.clone());
if pkey.is_none() {
let candidates = sqlmo::util::pkey_column_names(&name);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/down.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use std::path::Path;

use crate::command::{get_executed_migrations, get_pending_migrations, MigrationType};
use crate::util::{create_runtime, CommandSuccess};
use anyhow::anyhow;
use ormlite::postgres::{PgArguments, PgConnection};
use ormlite::Arguments;
use ormlite::{Acquire, Connection, Executor};
use ormlite_core::config::{get_var_database_url, get_var_migration_folder, get_var_snapshot_folder};
use url::Url;
use anyhow::anyhow;

#[derive(Parser, Debug)]
pub struct Down {
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Down {
let target = if let Some(target) = self.target {
target
} else if executed.len() > 1 {
executed.iter().nth(1).unwrap().name.clone()
executed.get(1).unwrap().name.clone()
} else if executed.len() == 1 {
"0_empty".to_string()
} else {
Expand Down
8 changes: 4 additions & 4 deletions cli/src/command/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ fn check_for_pending_migrations(

fn check_reversible_compatibility(reversible: bool, migration_environment: Option<MigrationType>) -> Result<()> {
if let Some(migration_environment) = migration_environment {
if reversible && migration_environment == MigrationType::Simple {
return Err(anyhow!("You cannot mix reversible and non-reversible migrations"));
} else if !reversible && migration_environment != MigrationType::Simple {
if (reversible && migration_environment == MigrationType::Simple)
|| (!reversible && migration_environment != MigrationType::Simple)
{
return Err(anyhow!("You cannot mix reversible and non-reversible migrations"));
}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ fn autogenerate_migration(
let mut current = runtime.block_on(Schema::try_from_postgres(conn, "public"))?;
current.tables.retain(|t| t.name != "_sqlx_migrations");

let mut desired = schema_from_ormlite_project(codebase_path, &c)?;
let mut desired = schema_from_ormlite_project(codebase_path, c)?;
experimental_modifications_to_schema(&mut desired)?;

let migration = current.migrate_to(
Expand Down
22 changes: 13 additions & 9 deletions cli/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::HashMap;
use std::path::Path;
use sqlmo::{Constraint, Schema, Table};
use crate::config::Config;
use anyhow::Result as AnyResult;
use ormlite_attr::{schema_from_filepaths, Ident, InnerType, Type};
use ormlite_core::schema::FromMeta;
use anyhow::Result as AnyResult;
use crate::config::Config;
use sqlmo::{Constraint, Schema, Table};
use std::collections::HashMap;
use std::path::Path;

pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Schema> {
let mut schema = Schema::default();
Expand All @@ -29,7 +29,7 @@ pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Sch
let model_name = c.ty.inner_type_name();
let pkey = primary_key_type
.get(&model_name)
.expect(&format!("Could not find model {} for join", model_name));
.unwrap_or_else(|| panic!("Could not find model {} for join", model_name));
c.ty = Type::Inner(pkey.clone());
}
}
Expand All @@ -38,8 +38,11 @@ pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Sch
let table = Table::from_meta(&table);
schema.tables.push(table);
}
let mut table_names: HashMap<String, (String, String)> =
schema.tables.iter().map(|t| (t.name.clone(), (t.name.clone(), t.primary_key().unwrap().name.clone()))).collect();
let mut table_names: HashMap<String, (String, String)> = schema
.tables
.iter()
.map(|t| (t.name.clone(), (t.name.clone(), t.primary_key().unwrap().name.clone())))
.collect();
for (alias, real) in &c.table.aliases {
let Some(real) = table_names.get(real) else {
continue;
Expand All @@ -63,4 +66,5 @@ pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Sch
}
}
Ok(schema)
}
}

6 changes: 5 additions & 1 deletion core/src/query_builder/args.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::default::Default;
use sqlx::{Arguments, Database, IntoArguments};

pub struct QueryBuilderArgs<'q, DB: Database>(pub Box<DB::Arguments<'q>>, usize);
pub struct QueryBuilderArgs<'q, DB: Database>(pub Box<DB::Arguments<'q>>, pub usize);

impl<'q, DB: Database> QueryBuilderArgs<'q, DB> {
pub fn add<T: 'q + Send + sqlx::Encode<'q, DB> + sqlx::Type<DB>>(&mut self, arg: T) {
Expand All @@ -12,6 +12,10 @@ impl<'q, DB: Database> QueryBuilderArgs<'q, DB> {
pub fn len(&self) -> usize {
self.1
}

pub fn is_empty(&self) -> bool {
self.1 == 0
}
}

impl<'q, DB: Database> IntoArguments<'q, DB> for QueryBuilderArgs<'q, DB> {
Expand Down
5 changes: 4 additions & 1 deletion core/src/query_builder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ where
let q = self.query.to_sql(DB::dialect());
let args = self.arguments;
let (q, placeholder_count) = util::replace_placeholders(&q, &mut self.gen)?;
if placeholder_count != args.len() {
if placeholder_count != {
let this = &args;
this.1
} {
return Err(Error::OrmliteError(format!(
"Failing to build query. {} placeholders were found in the query, but \
{} arguments were provided.",
Expand Down
39 changes: 18 additions & 21 deletions core/src/query_builder/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,27 @@ pub fn replace_placeholders<T: Iterator<Item = String>>(
buf.push_str(&placeholder_generator.next().unwrap());
placeholder_count += 1;
}
Token::Char(c) => {
match c {
'?' => {
buf.push_str(&placeholder_generator.next().unwrap());
placeholder_count += 1;
}
'$' => {
let next_tok = it.next();
if let Some(next_tok) = next_tok {
match next_tok {
Token::Number(text, _) => {
let n = text.parse::<usize>().map_err(|_| Error::OrmliteError(
format!("Failed to parse number after a $ during query tokenization. Value was: {text}"
)))?;
buf.push_str(&format!("${next_tok}"));
placeholder_count = std::cmp::max(placeholder_count, n);
}
_ => {}
}
Token::Char(c) => match c {
'?' => {
buf.push_str(&placeholder_generator.next().unwrap());
placeholder_count += 1;
}
'$' => {
let next_tok = it.next();
if let Some(next_tok) = next_tok {
if let Token::Number(text, _) = next_tok {
let n = text.parse::<usize>().map_err(|_| {
Error::OrmliteError(format!(
"Failed to parse number after a $ during query tokenization. Value was: {text}"
))
})?;
buf.push_str(&format!("${next_tok}"));
placeholder_count = std::cmp::max(placeholder_count, n);
}
}
_ => buf.push(*c),
}
}
_ => buf.push(*c),
},
_ => buf.push_str(&tok.to_string()),
}
}
Expand Down
21 changes: 12 additions & 9 deletions core/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashMap;
use std::path::Path;
use ormlite_attr::{schema_from_filepaths, ColumnMeta, Ident, InnerType};
use crate::config::Config;
use anyhow::Result as AnyResult;
use ormlite_attr::ModelMeta;
use ormlite_attr::Type;
use ormlite_attr::{schema_from_filepaths, ColumnMeta, Ident, InnerType};
use sqlmo::{schema::Column, Constraint, Schema, Table};
use anyhow::Result as AnyResult;
use crate::config::Config;
use std::collections::HashMap;
use std::path::Path;

pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Schema> {
let mut schema = Schema::default();
Expand All @@ -30,7 +30,7 @@ pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Sch
let model_name = c.ty.inner_type_name();
let pkey = primary_key_type
.get(&model_name)
.expect(&format!("Could not find model {} for join", model_name));
.unwrap_or_else(|| panic!("Could not find model {} for join", model_name));
c.ty = Type::Inner(pkey.clone());
}
}
Expand All @@ -39,8 +39,11 @@ pub fn schema_from_ormlite_project(paths: &[&Path], c: &Config) -> AnyResult<Sch
let table = Table::from_meta(&table);
schema.tables.push(table);
}
let mut table_names: HashMap<String, (String, String)> =
schema.tables.iter().map(|t| (t.name.clone(), (t.name.clone(), t.primary_key().unwrap().name.clone()))).collect();
let mut table_names: HashMap<String, (String, String)> = schema
.tables
.iter()
.map(|t| (t.name.clone(), (t.name.clone(), t.primary_key().unwrap().name.clone())))
.collect();
for (alias, real) in &c.table.aliases {
let Some(real) = table_names.get(real) else {
continue;
Expand Down Expand Up @@ -207,10 +210,10 @@ impl Nullable {
#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;
use assert_matches::assert_matches;
use ormlite_attr::Type;
use syn::parse_str;
use anyhow::Result;

#[test]
fn test_convert_type() -> Result<()> {
Expand Down
6 changes: 2 additions & 4 deletions macro/src/codegen/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ fn recursive_primitive_types<'a>(table: &'a ModelMeta, cache: &'a MetadataCache)
table
.columns
.iter()
.map(|c| recursive_primitive_types_ty(&c.ty, cache))
.flatten()
.flat_map(|c| recursive_primitive_types_ty(&c.ty, cache))
.collect()
}

Expand All @@ -85,8 +84,7 @@ pub(crate) fn table_primitive_types<'a>(attr: &'a TableMeta, cache: &'a Metadata
.iter()
.filter(|c| !c.skip)
.filter(|c| !c.json)
.map(|c| recursive_primitive_types_ty(&c.ty, cache))
.flatten()
.flat_map(|c| recursive_primitive_types_ty(&c.ty, cache))
.unique()
.collect()
}
Expand Down
4 changes: 2 additions & 2 deletions macro/src/codegen/from_row.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::codegen::common::{from_row_bounds, OrmliteCodegen};
use crate::MetadataCache;
use ormlite_attr::{ColumnMeta, Type};
use ormlite_attr::Ident;
use ormlite_attr::TableMeta;
use ormlite_attr::{ColumnMeta, Type};
use proc_macro2::TokenStream;
use quote::quote;

Expand Down Expand Up @@ -96,7 +96,7 @@ pub fn impl_from_row_using_aliases(
) -> TokenStream {
let row = db.row();
let fields = attr.all_fields();
let bounds = from_row_bounds(db, attr, &metadata_cache);
let bounds = from_row_bounds(db, attr, metadata_cache);
let mut incrementer = 0usize..;
let columns = attr
.columns
Expand Down
14 changes: 10 additions & 4 deletions macro/src/codegen/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ pub fn impl_Model__insert(db: &dyn OrmliteCodegen, attr: &ModelMeta, metadata_ca
placeholder.next().unwrap()
}
});
fn fun_name(c: &ColumnMeta) -> TokenStream {
insertion_binding(c)
}

let query_bindings = attr
.database_columns()
.filter(|c| attr.pkey.name == c.name || !c.has_database_default)
.map(|c| insertion_binding(c));
.map(fun_name);

let insert_join = attr.many_to_one_joins().map(|c| insert_join(c));
let insert_join = attr.many_to_one_joins().map(insert_join);

let late_bind = attr.many_to_one_joins().map(|c| {
let id = &c.ident;
Expand Down Expand Up @@ -141,15 +144,18 @@ pub fn impl_Insert(db: &dyn OrmliteCodegen, meta: &TableMeta, model: &Ident, ret
);
let query_bindings = meta.database_columns().filter(|&c| !c.has_database_default).map(|c| {
if let Some(rust_default) = &c.rust_default {
let default: syn::Expr = syn::parse_str(&rust_default).expect("Failed to parse default_value");
let default: syn::Expr = syn::parse_str(rust_default).expect("Failed to parse default_value");
return quote! {
q = q.bind(#default);
};
}
insertion_binding(c)
});
fn fun_name(c: &ColumnMeta) -> TokenStream {
insert_join(c)
}

let insert_join = meta.many_to_one_joins().map(|c| insert_join(c));
let insert_join = meta.many_to_one_joins().map(fun_name);

let late_bind = meta.many_to_one_joins().map(|c| {
let id = &c.ident;
Expand Down
Loading

0 comments on commit 393cb0e

Please sign in to comment.