Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion butane/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ pub mod prelude {
//! Its use is recommended, but not required. If not used, the use
//! of butane's macros may require some of its re-exports to be
//! used manually.
pub use butane_core::db::BackendConnection;

#[doc(no_inline)]
pub use crate::DataObject;
#[doc(no_inline)]
pub use crate::DataResult;
pub use butane_core::db::BackendConnection;
}
3 changes: 1 addition & 2 deletions butane/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
use butane::db::Connection;
use butane::{butane_type, find, model, query, AutoPk, ForeignKey};
use butane::{colname, prelude::*};
use butane_test_helper::*;
#[cfg(feature = "datetime")]
use chrono::{naive::NaiveDateTime, offset::Utc, DateTime};
use serde::Serialize;

use butane_test_helper::*;

#[butane_type]
pub type Whatsit = String;

Expand Down
1 change: 0 additions & 1 deletion butane/tests/common/blog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use butane::{dataresult, model};
use butane::{db::Connection, ForeignKey, Many};
#[cfg(feature = "datetime")]
use chrono::{naive::NaiveDateTime, offset::Utc};

#[cfg(feature = "fake")]
use fake::Dummy;

Expand Down
1 change: 0 additions & 1 deletion butane/tests/custom_enum_derived.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use butane::db::Connection;
use butane::prelude::*;
use butane::{model, query};
use butane::{FieldType, FromSql, SqlVal, ToSql};

use butane_test_helper::*;

#[derive(PartialEq, Eq, Debug, Clone, FieldType)]
Expand Down
4 changes: 2 additions & 2 deletions butane/tests/custom_pg.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// We wrap everything in an inner module just so it's easier to have the feature gate in one place
#[cfg(feature = "pg")]
mod custom_pg {
use std::result::Result;

use butane::custom::{SqlTypeCustom, SqlValRefCustom};
use butane::prelude::*;
use butane::{butane_type, db::Connection, model};
use butane::{AutoPk, FieldType, FromSql, SqlType, SqlVal, SqlValRef, ToSql};
use butane_test_helper::{maketest, maketest_pg};

use std::result::Result;

// newtype so we can implement traits for it.
#[butane_type(Custom(POINT))]
#[derive(Debug, PartialEq, Clone)]
Expand Down
1 change: 0 additions & 1 deletion butane/tests/custom_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use butane::db::Connection;
use butane::prelude::*;
use butane::{butane_type, model, query};
use butane::{FieldType, FromSql, SqlType, SqlVal, SqlValRef, ToSql};

use butane_test_helper::*;

#[butane_type(Text)]
Expand Down
7 changes: 2 additions & 5 deletions butane/tests/fake.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use butane::db::Connection;
use butane::{find, DataObject, ForeignKey};

use butane_test_helper::*;
use fake::{Fake, Faker};

mod common;
use common::blog::{Blog, Post, Tag};

fn fake_blog_post(conn: Connection) {
use fake::{Fake, Faker};

use common::blog::{Blog, Post, Tag};

let mut fake_blog: Blog = Faker.fake();
fake_blog.save(&conn).unwrap();

Expand Down
3 changes: 1 addition & 2 deletions butane/tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use std::collections::{BTreeMap, HashMap};
use butane::model;
use butane::prelude::*;
use butane::{db::Connection, FieldType};
use butane_test_helper::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use butane_test_helper::*;

#[model]
#[derive(PartialEq, Eq, Debug, Clone)]
struct FooJJ {
Expand Down
2 changes: 0 additions & 2 deletions butane/tests/many.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use butane::db::Connection;
use butane::prelude::*;
use butane::{model, query::OrderDirection, AutoPk, Many};

use butane_test_helper::testall;

#[cfg(any(feature = "pg", feature = "sqlite"))]
use butane_test_helper::*;

Expand Down
9 changes: 4 additions & 5 deletions butane/tests/migration-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ use butane::migrations::{
};
use butane::{db::Connection, prelude::*, SqlType, SqlVal};
use butane_core::codegen::{butane_type_with_migrations, model_with_migrations};
use proc_macro2::TokenStream;
use quote::quote;
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser as SqlParser;

#[cfg(feature = "pg")]
use butane_test_helper::pg_connection;
#[cfg(feature = "sqlite")]
use butane_test_helper::sqlite_connection;
use proc_macro2::TokenStream;
use quote::quote;
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser as SqlParser;

#[test]
fn current_migration_basic() {
Expand Down
1 change: 0 additions & 1 deletion butane/tests/nullable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use butane::db::Connection;
use butane::prelude::*;
use butane::{model, query};

use butane_test_helper::*;

#[model]
Expand Down
3 changes: 1 addition & 2 deletions butane/tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use butane::db::Connection;
use butane::prelude::*;
use butane::query::BoolExpr;
use butane::{colname, filter, find, query, Many};
use butane_test_helper::*;
#[cfg(feature = "datetime")]
use chrono::{TimeZone, Utc};

use butane_test_helper::*;

mod common;
use common::blog;
use common::blog::{Blog, Post, PostMetadata, Tag};
Expand Down
8 changes: 3 additions & 5 deletions butane/tests/r2d2.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#[cfg(any(feature = "pg", feature = "sqlite"))]
use butane_test_helper::setup_db;

use butane::db;
#[cfg(feature = "pg")]
use butane_test_helper::pg_connspec;
#[cfg(any(feature = "pg", feature = "sqlite"))]
use butane_test_helper::setup_db;
#[cfg(feature = "sqlite")]
use butane_test_helper::sqlite_connspec;

#[cfg(any(feature = "pg", feature = "sqlite"))]
use butane::db;
#[cfg(any(feature = "pg", feature = "sqlite"))]
use r2d2_for_test as r2d2;

Expand Down
3 changes: 1 addition & 2 deletions butane/tests/uuid.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use butane::db::Connection;
use butane::model;
use butane::prelude::*;
use uuid_for_test::Uuid;

use butane_test_helper::*;
use uuid_for_test::Uuid;

#[model]
#[derive(PartialEq, Eq, Debug, Clone)]
Expand Down
3 changes: 1 addition & 2 deletions butane_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::path::PathBuf;

use clap::{value_parser, Arg, ArgMatches};

use butane_cli::{
base_dir, clean, clear_data, collapse_migrations, delete_table, detach_latest_migration, embed,
get_migrations, handle_error, list_migrations, migrate, Result,
};
use clap::{value_parser, Arg, ArgMatches};

fn main() {
let app = clap::Command::new("butane")
Expand Down
2 changes: 1 addition & 1 deletion butane_codegen/src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::*;
use butane_core::make_compile_error;
use proc_macro2::Span;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, quote_spanned, ToTokens};
Expand Down
3 changes: 2 additions & 1 deletion butane_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#![deny(missing_docs)]
extern crate proc_macro;

use std::path::PathBuf;

use butane_core::migrations::adb::{DeferredSqlType, TypeIdentifier};
use butane_core::{codegen, make_compile_error, migrations, SqlType};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::TokenTree;
use quote::quote;
use std::path::PathBuf;
use syn::{Expr, Ident};

mod filter;
Expand Down
6 changes: 4 additions & 2 deletions butane_core/src/autopk.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Contains the [AutoPk] type for autoincrementing primary keys.

use super::{FieldType, FromSql, PrimaryKeyType, Result, SqlType, SqlVal, SqlValRef, ToSql};
use serde::{Deserialize, Serialize};
use std::cmp::{Ordering, PartialOrd};

use serde::{Deserialize, Serialize};

use super::{FieldType, FromSql, PrimaryKeyType, Result, SqlType, SqlVal, SqlValRef, ToSql};

/// Wrapper around a [PrimaryKeyType] to indicate the the primary key
/// will be initialized automatically when the object is created in
/// the database.
Expand Down
14 changes: 9 additions & 5 deletions butane_core/src/codegen/dbobj.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use super::*;
use crate::migrations::adb::{DeferredSqlType, TypeIdentifier, MANY_SUFFIX};
use crate::SqlType;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Ident, Span};
use quote::{quote, quote_spanned};
use syn::{spanned::Spanned, Field, ItemStruct};
use quote::{quote, quote_spanned, ToTokens};
use syn::{spanned::Spanned, Field, ItemStruct, LitStr};

use super::{
fields, get_autopk_sql_type, get_type_argument, is_auto, is_many_to_many, is_row_field,
make_ident_literal_str, make_lit, pk_field, MANY_TYNAMES,
};
use crate::migrations::adb::{DeferredSqlType, TypeIdentifier, MANY_SUFFIX};
use crate::SqlType;

// Configuration that can be specified with attributes to override default behavior
#[derive(Clone, Debug, Default)]
Expand Down
3 changes: 2 additions & 1 deletion butane_core/src/codegen/migration.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use syn::{Field, ItemStruct};

use super::{
dbobj, fields, get_default, get_deferred_sql_type, get_many_sql_type, is_auto, is_foreign_key,
is_many_to_many, is_option, is_row_field, is_unique, pk_field,
};
use crate::migrations::adb::{create_many_table, AColumn, ARef, ATable, DeferredSqlType, TypeKey};
use crate::migrations::{MigrationMut, MigrationsMut};
use crate::Result;
use syn::{Field, ItemStruct};

pub fn write_table_to_disk<M>(
ms: &mut impl MigrationsMut<M = M>,
Expand Down
10 changes: 6 additions & 4 deletions butane_core/src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Code-generation backend

use crate::migrations::adb::{DeferredSqlType, TypeIdentifier, TypeKey};
use crate::migrations::{MigrationMut, MigrationsMut};
use crate::{SqlType, SqlVal};
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Ident, Span, TokenTree};
use quote::{quote, ToTokens};
Expand All @@ -13,6 +10,10 @@ use syn::{
MetaNameValue,
};

use crate::migrations::adb::{DeferredSqlType, TypeIdentifier, TypeKey};
use crate::migrations::{MigrationMut, MigrationsMut};
use crate::{SqlType, SqlVal};

const OPTION_TYNAMES: [&str; 3] = ["Option", "option::Option", "std::option::Option"];
const MANY_TYNAMES: [&str; 2] = ["Many", "butane::Many"];
const FKEY_TYNAMES: [&str; 2] = ["ForeignKey", "butane::ForeignKey"];
Expand Down Expand Up @@ -614,9 +615,10 @@ impl From<TokenStream2> for CompilerErrorMsg {

#[cfg(test)]
mod tests {
use super::*;
use syn::parse::Parser;

use super::*;

#[test]
fn test_get_type_argument_option() {
let expected_type_path: syn::Path = syn::parse_quote!(butane::ForeignKey<Foo>);
Expand Down
3 changes: 2 additions & 1 deletion butane_core/src/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
//! source repository. Not supported for the Sqlite backend as Sqlite
//! supports a very limited set of types to begin with.

use serde::{Deserialize, Serialize};
use std::fmt;

use serde::{Deserialize, Serialize};

/// For use with [SqlType::Custom](crate::SqlType)
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum SqlTypeCustom {
Expand Down
5 changes: 3 additions & 2 deletions butane_core/src/db/connmethods.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Not expected to be called directly by most users. Used by code
//! generated by `#[model]`, `query!`, and other macros.

use crate::query::{BoolExpr, Expr, Order};
use crate::{Result, SqlType, SqlVal, SqlValRef};
use std::ops::{Deref, DerefMut};
use std::vec::Vec;

use crate::query::{BoolExpr, Expr, Order};
use crate::{Result, SqlType, SqlVal, SqlValRef};

/// Methods available on a database connection. Most users do not need
/// to call these methods directly and will instead use methods on
/// [DataObject][crate::DataObject] or the `query!` macro. This trait is
Expand Down
11 changes: 6 additions & 5 deletions butane_core/src/db/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
// may occur if no backends are selected
#![allow(unused)]

use std::borrow::Cow;
use std::fmt::Write;

#[cfg(feature = "datetime")]
use chrono::naive::NaiveDateTime;

use super::Column;
use crate::migrations::adb::{AColumn, TypeIdentifier};
use crate::query::Expr::{Condition, Placeholder, Val};
use crate::query::{BoolExpr::*, Expr, Join, Order, OrderDirection};
use crate::Error;
use crate::{query, Result, SqlType, SqlVal};
use std::borrow::Cow;
use std::fmt::Write;

#[cfg(feature = "datetime")]
use chrono::naive::NaiveDateTime;

pub trait PlaceholderSource {
fn next_placeholder(&mut self) -> Cow<str>;
Expand Down
15 changes: 8 additions & 7 deletions butane_core/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@
//! what a `BackendConnection` can do, but allows using a single concrete type that is not tied to a particular
//! database backend. It is returned by the `connect` method.

use crate::query::BoolExpr;
use crate::{migrations::adb, Error, Result, SqlVal, SqlValRef};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt::Debug;
use std::fs;
use std::io::Write;
use std::ops::{Deref, DerefMut};
use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::query::BoolExpr;
use crate::{migrations::adb, Error, Result, SqlVal, SqlValRef};

mod connmethods;
pub use connmethods::{
BackendRow, BackendRows, Column, ConnectionMethods, MapDeref, QueryResult, RawQueryResult,
};
mod helper;
mod macros;
#[cfg(feature = "pg")]
Expand All @@ -38,10 +43,6 @@ pub use r2::ConnectionManager;
// Macros are always exported at the root of the crate
use crate::connection_method_wrapper;

pub use connmethods::{
BackendRow, BackendRows, Column, ConnectionMethods, MapDeref, QueryResult, RawQueryResult,
};

/// Database connection.
pub trait BackendConnection: ConnectionMethods + Debug + Send + 'static {
/// Begin a database transaction. The transaction object must be
Expand Down
Loading