├── rust-toolchain ├── examples ├── mysql │ ├── all_about_inserts │ │ ├── migrations │ │ │ ├── .gitkeep │ │ │ └── 2017-12-16-173626_create_users │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ └── Cargo.toml │ ├── getting_started_step_1 │ │ ├── migrations │ │ │ └── 20160815133237_create_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ ├── diesel.toml │ │ ├── src │ │ │ ├── models.rs │ │ │ ├── schema.rs │ │ │ ├── lib.rs │ │ │ └── bin │ │ │ │ └── show_posts.rs │ │ └── Cargo.toml │ ├── getting_started_step_2 │ │ ├── migrations │ │ │ └── 20160815133237_create_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ ├── diesel.toml │ │ ├── src │ │ │ ├── schema.rs │ │ │ ├── models.rs │ │ │ ├── bin │ │ │ │ ├── show_posts.rs │ │ │ │ └── write_post.rs │ │ │ └── lib.rs │ │ └── Cargo.toml │ ├── getting_started_step_3 │ │ ├── migrations │ │ │ └── 20160815133237_create_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ ├── diesel.toml │ │ ├── src │ │ │ ├── schema.rs │ │ │ ├── models.rs │ │ │ ├── bin │ │ │ │ ├── delete_post.rs │ │ │ │ ├── show_posts.rs │ │ │ │ ├── publish_post.rs │ │ │ │ └── write_post.rs │ │ │ └── lib.rs │ │ └── Cargo.toml │ └── test_all ├── sqlite │ ├── all_about_inserts │ │ ├── migrations │ │ │ ├── .gitkeep │ │ │ └── 2017-12-16-173626_create_users │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ └── Cargo.toml │ ├── getting_started_step_3 │ │ ├── migrations │ │ │ ├── .gitkeep │ │ │ └── 20170124012402_create_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ ├── .gitignore │ │ ├── diesel.toml │ │ ├── src │ │ │ ├── schema.rs │ │ │ ├── models.rs │ │ │ ├── bin │ │ │ │ ├── delete_post.rs │ │ │ │ ├── show_posts.rs │ │ │ │ ├── publish_post.rs │ │ │ │ └── write_post.rs │ │ │ └── lib.rs │ │ ├── Cargo.toml │ │ └── README.md │ ├── getting_started_step_1 │ │ ├── migrations │ │ │ └── 20170124012402_create_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ ├── diesel.toml │ │ ├── src │ │ │ ├── models.rs │ │ │ ├── schema.rs │ │ │ ├── lib.rs │ │ │ └── bin │ │ │ │ └── show_posts.rs │ │ └── Cargo.toml │ ├── getting_started_step_2 │ │ ├── migrations │ │ │ └── 20170124012402_create_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ ├── diesel.toml │ │ ├── src │ │ │ ├── schema.rs │ │ │ ├── models.rs │ │ │ ├── bin │ │ │ │ ├── show_posts.rs │ │ │ │ └── write_post.rs │ │ │ └── lib.rs │ │ └── Cargo.toml │ └── test_all ├── postgres │ ├── advanced-blog-cli │ │ ├── migrations │ │ │ ├── .gitkeep │ │ │ ├── 2017-12-20-144812_create_users │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ │ ├── 2017-12-20-163829_create_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ │ ├── 2017-12-20-183155_create_comments │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ │ ├── 2017-12-20-164453_make_username_unique │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ │ ├── 2017-12-20-171042_add_published_at_to_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ │ └── 00000000000000_diesel_initial_setup │ │ │ │ └── down.sql │ │ ├── .env.sample │ │ ├── diesel.toml │ │ ├── Cargo.toml │ │ └── src │ │ │ ├── test_helpers.rs │ │ │ ├── comment.rs │ │ │ └── schema.rs │ ├── all_about_inserts │ │ ├── migrations │ │ │ ├── .gitkeep │ │ │ ├── 2017-12-16-173626_create_users │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ │ └── 00000000000000_diesel_initial_setup │ │ │ │ └── down.sql │ │ └── Cargo.toml │ ├── getting_started_step_1 │ │ ├── migrations │ │ │ ├── 20160815133237_create_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ │ └── 00000000000000_diesel_initial_setup │ │ │ │ └── down.sql │ │ ├── diesel.toml │ │ ├── src │ │ │ ├── models.rs │ │ │ ├── schema.rs │ │ │ ├── lib.rs │ │ │ └── bin │ │ │ │ └── show_posts.rs │ │ └── Cargo.toml │ ├── getting_started_step_2 │ │ ├── migrations │ │ │ ├── 20160815133237_create_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ │ └── 00000000000000_diesel_initial_setup │ │ │ │ └── down.sql │ │ ├── diesel.toml │ │ ├── src │ │ │ ├── schema.rs │ │ │ ├── models.rs │ │ │ ├── bin │ │ │ │ ├── show_posts.rs │ │ │ │ └── write_post.rs │ │ │ └── lib.rs │ │ └── Cargo.toml │ ├── getting_started_step_3 │ │ ├── migrations │ │ │ ├── 20160815133237_create_posts │ │ │ │ ├── down.sql │ │ │ │ └── up.sql │ │ │ └── 00000000000000_diesel_initial_setup │ │ │ │ └── down.sql │ │ ├── diesel.toml │ │ ├── src │ │ │ ├── schema.rs │ │ │ ├── models.rs │ │ │ ├── bin │ │ │ │ ├── delete_post.rs │ │ │ │ ├── show_posts.rs │ │ │ │ ├── publish_post.rs │ │ │ │ └── write_post.rs │ │ │ └── lib.rs │ │ └── Cargo.toml │ ├── all_about_updates │ │ └── Cargo.toml │ └── test_all └── test_all ├── diesel_compile_tests ├── rust-toolchain ├── tests │ ├── ui │ │ ├── insertable_empty_struct.rs │ │ ├── as_expression_bad_sql_type.rs │ │ ├── queryable_by_name_requires_table_name_or_sql_type_annotation.rs │ │ ├── as_changeset_on_non_struct.rs │ │ ├── belongs_to_second_parent.stderr │ │ ├── belongs_to_missing_parent_import.rs │ │ ├── as_changeset_bad_column_name_syntax.rs │ │ ├── as_changeset_missing_table_import.rs │ │ ├── as_changeset_bad_primary_key_syntax.rs │ │ ├── insertable_empty_struct.stderr │ │ ├── as_changeset_missing_column_name_tuple_struct.rs │ │ ├── belongs_to_missing_parent_import.stderr │ │ ├── as_changeset_deprecated_column_name.rs │ │ ├── belongs_to_incorrect_lifetime_syntax.stderr │ │ ├── as_changeset_struct_with_only_primary_key.rs │ │ ├── belongs_to_missing_foreign_key_field.rs │ │ ├── as_changeset_bad_column_name.rs │ │ ├── as_changeset_deprecated_column_name.stderr │ │ ├── as_changeset_on_non_struct.stderr │ │ ├── belongs_to_incorrect_lifetime_syntax.rs │ │ ├── sql_type_bad_options.rs │ │ ├── belongs_to_second_parent.rs │ │ ├── insertable_missing_table_or_column.rs │ │ ├── as_changeset_missing_table_import.stderr │ │ ├── as_expression_bad_sql_type.stderr │ │ ├── belongs_to_invalid_option_syntax.rs │ │ ├── as_changeset_bad_primary_key_syntax.stderr │ │ ├── belongs_to_missing_foreign_key_column.rs │ │ ├── belongs_to_missing_foreign_key_field.stderr │ │ └── identifiable_missing_pk_field.rs │ ├── compile-fail │ │ ├── table_invalid_syntax_1.rs │ │ ├── table_invalid_syntax_2.rs │ │ ├── cannot_pass_aggregate_to_where.rs │ │ ├── order_requires_column_from_same_table.rs │ │ ├── cannot_mix_aggregate_and_non_aggregate_selects.rs │ │ ├── numeric_ops_require_numeric_column.rs │ │ ├── boxed_queries_must_be_used_with_proper_connection.rs │ │ ├── update_requires_left_side_of_eq_to_be_a_column.rs │ │ ├── update_requires_set.rs │ │ ├── expressions_can_only_be_compared_for_equality_to_expressions_of_same_type.rs │ │ ├── select_requires_column_from_same_table.rs │ │ ├── table_column_names.rs │ │ ├── ordering_functions_require_ord.rs │ │ ├── select_for_update_cannot_be_used_on_sqlite.rs │ │ ├── insert_requires_value_of_same_type_as_column.rs │ │ ├── boxed_queries_require_selectable_expression_for_order.rs │ │ ├── boxed_queries_require_selectable_expression_for_filter.rs │ │ ├── select_for_update_no_wait_cannot_be_used_on_sqlite.rs │ │ ├── sqlite_upsert_cannot_be_used_on_pg.rs │ │ ├── array_only_usable_with_pg.rs │ │ ├── select_for_update_skip_locked_cannot_be_used_on_sqlite.rs │ │ ├── select_sql_still_ensures_result_type.rs │ │ ├── sqlite_insert_or_ignore_cannot_be_used_on_pg.rs │ │ ├── columns_cannot_be_rhs_of_insert.rs │ │ ├── filter_requires_bool_nonaggregate_expression.rs │ │ ├── subselect_requires_correct_type.rs │ │ ├── update_requires_column_be_from_same_table.rs │ │ ├── cannot_update_target_with_methods_other_than_filter_called.rs │ │ ├── cannot_use_expression_methods_on_tuples.rs │ │ ├── array_expressions_must_be_correct_type.rs │ │ ├── exists_can_only_take_subselects.rs │ │ ├── delete_statement_does_not_support_returning_methods_on_sqlite.rs │ │ ├── distinct_on_clause_only_supported_for_pg.rs │ │ ├── selecting_multiple_columns_requires_all_must_be_from_selectable_table.rs │ │ ├── find_requires_correct_type.rs │ │ ├── ilike_only_compiles_for_pg.rs │ │ ├── update_requires_valid_where_clause.rs │ │ ├── any_is_only_selectable_if_inner_expr_is_selectable.rs │ │ ├── update_statement_does_not_support_returning_methods_on_sqlite.rs │ │ ├── codegen_does_not_add_save_changes_method_on_structs_without_primary_key.rs │ │ ├── distinct_on_allows_only_fields_of_table.rs │ │ ├── custom_returning_requires_nonaggregate.rs │ │ ├── select_carries_correct_result_type_info.rs │ │ ├── aggregate_expression_requires_column_from_same_table.rs │ │ ├── insert_cannot_reference_columns_from_other_table.rs │ │ ├── custom_returning_requires_selectable_expression.rs │ │ ├── array_expressions_must_be_same_type.rs │ │ ├── insert_from_select_cant_be_used_with_tuples_or_arrays.rs │ │ ├── user_defined_functions_follow_same_selection_rules.rs │ │ └── cannot_join_to_non_joinable_table.rs │ └── compile_tests.rs └── Cargo.toml ├── docker ├── postgres │ └── init │ │ └── init.sql └── mysql │ └── init │ └── init.sql ├── .gitignore ├── migrations ├── mysql │ ├── 20170603131224_add_likes │ │ ├── down.sql │ │ └── up.sql │ ├── 20170215170122_create_trees │ │ ├── down.sql │ │ └── up.sql │ ├── 20170811104602_add_blob_types │ │ ├── down.sql │ │ └── up.sql │ ├── 2020-01-25-033332_add unsigned table │ │ ├── down.sql │ │ └── up.sql │ ├── 20170407152306_add_nullable_table │ │ ├── down.sql │ │ └── up.sql │ ├── 20170816164349_add_keywords_table │ │ ├── down.sql │ │ └── up.sql │ ├── 20170219200159_add_foreign_key │ │ ├── down.sql │ │ └── up.sql │ ├── 20170211150830_index_columns_used_in_benchmarks │ │ ├── down.sql │ │ └── up.sql │ ├── 20170207193805_initial_schema │ │ └── down.sql │ ├── 20170209180355_add_one_off_tables_from_integration_tests │ │ ├── down.sql │ │ └── up.sql │ ├── 20170805195107_tables_which_make_infer_joinable_blow_up │ │ └── down.sql │ └── 20170804172328_add_foreign_keys │ │ ├── down.sql │ │ └── up.sql ├── sqlite │ ├── 20170603131224_add_likes │ │ ├── down.sql │ │ └── up.sql │ ├── 20170215170122_create_trees │ │ ├── down.sql │ │ └── up.sql │ ├── 20160825135747_create_followings │ │ ├── down.sql │ │ └── up.sql │ ├── 20170407152306_add_nullable_table │ │ ├── down.sql │ │ └── up.sql │ ├── 20170816164356_add_keywords_table │ │ ├── down.sql │ │ └── up.sql │ ├── 20170219200159_add_foreign_key │ │ ├── down.sql │ │ └── up.sql │ ├── 20170513174919_infer_all_the_datetime_types │ │ ├── down.sql │ │ └── up.sql │ ├── 20170211150830_index_columns_used_in_benchmarks │ │ ├── down.sql │ │ └── up.sql │ ├── 20151219180527_create_users_and_posts_and_comments │ │ ├── down.sql │ │ └── up.sql │ ├── 20160116104628_create_special_posts_and_special_comments │ │ ├── down.sql │ │ └── up.sql │ ├── 20160416143735_infer_all_the_types │ │ └── down.sql │ ├── 20170209180355_add_one_off_tables_from_integration_tests │ │ ├── down.sql │ │ └── up.sql │ └── 20170805195107_tables_which_make_infer_joinable_blow_up │ │ └── down.sql └── postgresql │ ├── 20170603131224_add_likes │ ├── down.sql │ └── up.sql │ ├── 20170215170122_create_trees │ ├── down.sql │ └── up.sql │ ├── 20170717152137_add_ranges │ ├── down.sql │ └── up.sql │ ├── 20160825135747_create_followings │ ├── down.sql │ └── up.sql │ ├── 2017-09-27-135928_remove_citext_table │ ├── up.sql │ └── down.sql │ ├── 20170407152306_add_nullable_table │ ├── down.sql │ └── up.sql │ ├── 20170721184144_create_citext_table │ ├── down.sql │ └── up.sql │ ├── 20170816164352_add_keywords_table │ ├── down.sql │ └── up.sql │ ├── 20160107090901_add_tags_to_posts │ ├── down.sql │ └── up.sql │ ├── 20161206123630_create_table_custom_schema │ ├── down.sql │ └── up.sql │ ├── 20170219200159_add_foreign_key │ ├── down.sql │ └── up.sql │ ├── 2017-09-26-151545_fix_posts_tags │ ├── up.sql │ └── down.sql │ ├── 20170211150830_index_columns_used_in_benchmarks │ ├── down.sql │ └── up.sql │ ├── 20151219180527_create_users_and_posts_and_comments │ ├── down.sql │ └── up.sql │ ├── 20160116104628_create_special_posts_and_special_comments │ ├── down.sql │ └── up.sql │ ├── 00000000000000_diesel_initial_setup │ └── down.sql │ ├── 20170209180355_add_one_off_tables_from_integration_tests │ ├── down.sql │ └── up.sql │ ├── 20170805195107_tables_which_make_infer_joinable_blow_up │ └── down.sql │ └── 20170804172328_add_foreign_keys │ ├── down.sql │ └── up.sql ├── diesel ├── src │ ├── pg │ │ ├── serialize │ │ │ └── mod.rs │ │ ├── upsert │ │ │ ├── on_conflict_docs_setup.rs │ │ │ └── mod.rs │ │ └── expression │ │ │ ├── extensions │ │ │ └── mod.rs │ │ │ ├── helper_types.rs │ │ │ ├── operators.rs │ │ │ └── mod.rs │ ├── query_builder │ │ ├── limit_clause.rs │ │ ├── offset_clause.rs │ │ ├── group_by_clause.rs │ │ ├── returning_clause.rs │ │ ├── order_clause.rs │ │ ├── distinct_clause.rs │ │ └── insert_statement │ │ │ └── column_list.rs │ ├── type_impls │ │ ├── mod.rs │ │ └── decimal.rs │ ├── migration │ │ └── setup_migration_table.sql │ ├── util.rs │ ├── sqlite │ │ ├── connection │ │ │ └── diesel_manage_updated_at.sql │ │ ├── mod.rs │ │ └── types │ │ │ └── numeric.rs │ ├── data_types.rs │ ├── query_dsl │ │ ├── nullable_select_dsl.rs │ │ ├── limit_dsl.rs │ │ ├── offset_dsl.rs │ │ └── boxed_dsl.rs │ ├── mysql │ │ ├── mod.rs │ │ └── value.rs │ └── expression │ │ ├── grouped.rs │ │ ├── not.rs │ │ └── functions │ │ └── helper_types.rs └── README.md ├── diesel_cli ├── tests │ ├── print_schema │ │ ├── print_schema_simple_without_docs │ │ │ ├── diesel.toml │ │ │ ├── mysql │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ ├── postgres │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ └── sqlite │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ ├── print_schema_order │ │ │ ├── diesel.toml │ │ │ ├── mysql │ │ │ │ └── schema.sql │ │ │ ├── sqlite │ │ │ │ └── schema.sql │ │ │ └── postgres │ │ │ │ └── schema.sql │ │ ├── print_schema_simple │ │ │ ├── diesel.toml │ │ │ ├── mysql │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ ├── postgres │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ └── sqlite │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ ├── print_schema_unsigned │ │ │ ├── diesel.toml │ │ │ └── mysql │ │ │ │ └── schema.sql │ │ ├── print_schema_column_renaming │ │ │ ├── diesel.toml │ │ │ ├── postgres │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ ├── mysql │ │ │ │ └── schema.sql │ │ │ └── sqlite │ │ │ │ └── schema.sql │ │ ├── print_schema_type_renaming │ │ │ ├── diesel.toml │ │ │ └── postgres │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ ├── print_schema_compound_primary_key │ │ │ ├── diesel.toml │ │ │ ├── mysql │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ ├── sqlite │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ └── postgres │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ ├── print_schema_patch_file │ │ │ ├── diesel.toml │ │ │ ├── mysql │ │ │ │ ├── schema.sql │ │ │ │ ├── schema.patch │ │ │ │ └── expected.rs │ │ │ ├── postgres │ │ │ │ ├── schema.sql │ │ │ │ ├── schema.patch │ │ │ │ └── expected.rs │ │ │ └── sqlite │ │ │ │ ├── schema.sql │ │ │ │ ├── expected.rs │ │ │ │ └── schema.patch │ │ ├── print_schema_with_foreign_keys │ │ │ ├── diesel.toml │ │ │ └── postgres │ │ │ │ └── schema.sql │ │ ├── print_schema_with_compound_foreign_keys │ │ │ ├── diesel.toml │ │ │ └── postgres │ │ │ │ └── schema.sql │ │ ├── print_schema_custom_types │ │ │ ├── diesel.toml │ │ │ ├── mysql │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ ├── postgres │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ └── sqlite │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ ├── print_schema_with_foreign_keys_reserved_names │ │ │ ├── diesel.toml │ │ │ └── postgres │ │ │ │ └── schema.sql │ │ ├── print_schema_except_tables │ │ │ ├── mysql │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ ├── postgres │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ ├── sqlite │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ └── diesel.toml │ │ ├── print_schema_only_tables │ │ │ ├── mysql │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ ├── postgres │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ ├── sqlite │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ │ └── diesel.toml │ │ ├── print_schema_specifying_schema_name │ │ │ ├── diesel.toml │ │ │ └── postgres │ │ │ │ ├── schema.sql │ │ │ │ └── expected.rs │ │ └── print_schema_specifying_schema_name_with_foreign_keys │ │ │ ├── diesel.toml │ │ │ └── postgres │ │ │ └── schema.sql │ ├── support │ │ └── database.rs │ ├── tests.rs │ └── exit_codes.rs └── src │ ├── default_files │ └── diesel.toml │ ├── setup_sql │ └── postgres │ │ └── initial_setup │ │ └── down.sql │ └── infer_schema_internals │ └── mod.rs ├── diesel_derives ├── tests │ ├── schema.rs │ └── tests.rs ├── src │ ├── resolved_at_shim.rs │ └── non_aggregate.rs └── Cargo.toml ├── diesel_tests └── tests │ ├── schema_dsl │ └── mod.rs │ ├── backend_specifics.rs │ ├── deserialization.rs │ └── postgres_specific_schema.rs ├── diesel_migrations ├── migrations_internals │ ├── src │ │ └── schema.rs │ └── Cargo.toml ├── migrations_macros │ └── Cargo.toml └── Cargo.toml ├── bin ├── diesel └── bench ├── clippy.toml ├── guide_drafts └── README.md ├── .editorconfig ├── docker-compose.yml ├── _build ├── install-rust.yml └── failable_step.yml └── .env.sample /rust-toolchain: -------------------------------------------------------------------------------- 1 | 1.37.0 2 | -------------------------------------------------------------------------------- /examples/mysql/all_about_inserts/migrations/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /examples/sqlite/all_about_inserts/migrations/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /diesel_compile_tests/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2019-08-01 2 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /examples/postgres/all_about_inserts/migrations/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/migrations/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docker/postgres/init/init.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE diesel_test; 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | !diesel_cli/Cargo.lock 4 | .env 5 | -------------------------------------------------------------------------------- /migrations/mysql/20170603131224_add_likes/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE likes; 2 | -------------------------------------------------------------------------------- /migrations/sqlite/20170603131224_add_likes/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE likes; 2 | -------------------------------------------------------------------------------- /migrations/mysql/20170215170122_create_trees/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE trees; 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20170603131224_add_likes/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE likes; 2 | -------------------------------------------------------------------------------- /migrations/sqlite/20170215170122_create_trees/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE trees; 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20170215170122_create_trees/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE trees; 2 | -------------------------------------------------------------------------------- /migrations/mysql/20170811104602_add_blob_types/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE all_the_blobs; 2 | -------------------------------------------------------------------------------- /migrations/mysql/2020-01-25-033332_add unsigned table/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE unsigned_table; -------------------------------------------------------------------------------- /migrations/postgresql/20170717152137_add_ranges/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE all_the_ranges 2 | -------------------------------------------------------------------------------- /migrations/sqlite/20160825135747_create_followings/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE followings; 2 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | .env 4 | *.db 5 | -------------------------------------------------------------------------------- /migrations/mysql/20170407152306_add_nullable_table/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE nullable_table; 2 | -------------------------------------------------------------------------------- /migrations/mysql/20170816164349_add_keywords_table/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE with_keywords; 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20160825135747_create_followings/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE followings; 2 | -------------------------------------------------------------------------------- /migrations/sqlite/20170407152306_add_nullable_table/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE nullable_table; 2 | -------------------------------------------------------------------------------- /migrations/sqlite/20170816164356_add_keywords_table/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE with_keywords; 2 | -------------------------------------------------------------------------------- /migrations/postgresql/2017-09-27-135928_remove_citext_table/up.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE citext_table; 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20170407152306_add_nullable_table/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE nullable_table; 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20170721184144_create_citext_table/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE citext_table; 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20170816164352_add_keywords_table/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE with_keywords; 2 | -------------------------------------------------------------------------------- /diesel/src/pg/serialize/mod.rs: -------------------------------------------------------------------------------- 1 | mod write_tuple; 2 | 3 | pub use self::write_tuple::WriteTuple; 4 | -------------------------------------------------------------------------------- /diesel/src/query_builder/limit_clause.rs: -------------------------------------------------------------------------------- 1 | simple_clause!(NoLimitClause, LimitClause, " LIMIT "); 2 | -------------------------------------------------------------------------------- /diesel/src/query_builder/offset_clause.rs: -------------------------------------------------------------------------------- 1 | simple_clause!(NoOffsetClause, OffsetClause, " OFFSET "); 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20160107090901_add_tags_to_posts/down.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE posts DROP COLUMN tags; 2 | -------------------------------------------------------------------------------- /examples/mysql/all_about_inserts/migrations/2017-12-16-173626_create_users/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE users; 2 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_1/migrations/20160815133237_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts 2 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_2/migrations/20160815133237_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts 2 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/migrations/20160815133237_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts 2 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/2017-12-20-144812_create_users/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE users; 2 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/2017-12-20-163829_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts; 2 | -------------------------------------------------------------------------------- /examples/postgres/all_about_inserts/migrations/2017-12-16-173626_create_users/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE users; 2 | -------------------------------------------------------------------------------- /examples/sqlite/all_about_inserts/migrations/2017-12-16-173626_create_users/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE users; 2 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_1/migrations/20170124012402_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts 2 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_2/migrations/20170124012402_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts 2 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/migrations/20170124012402_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts 2 | -------------------------------------------------------------------------------- /migrations/mysql/20170219200159_add_foreign_key/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE fk_tests; 2 | DROP TABLE fk_inits; 3 | -------------------------------------------------------------------------------- /migrations/sqlite/20170219200159_add_foreign_key/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE fk_tests; 2 | DROP TABLE fk_inits; 3 | -------------------------------------------------------------------------------- /diesel/src/query_builder/group_by_clause.rs: -------------------------------------------------------------------------------- 1 | simple_clause!(NoGroupByClause, GroupByClause, " GROUP BY "); 2 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_1/migrations/20160815133237_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts 2 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_2/migrations/20160815133237_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts 2 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/migrations/20160815133237_create_posts/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE posts 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20161206123630_create_table_custom_schema/down.sql: -------------------------------------------------------------------------------- 1 | DROP SCHEMA custom_schema CASCADE; 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20170219200159_add_foreign_key/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE fk_tests; 2 | DROP TABLE fk_inits; 3 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/2017-12-20-183155_create_comments/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE comments; 2 | -------------------------------------------------------------------------------- /migrations/postgresql/2017-09-26-151545_fix_posts_tags/up.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE posts ALTER COLUMN tags TYPE text[]; 2 | -------------------------------------------------------------------------------- /migrations/sqlite/20170513174919_infer_all_the_datetime_types/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE infer_all_the_datetime_types; 2 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple_without_docs/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | -------------------------------------------------------------------------------- /migrations/postgresql/2017-09-26-151545_fix_posts_tags/down.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE posts ALTER COLUMN tags TYPE varchar[]; 2 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_order/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/2017-12-20-164453_make_username_unique/down.sql: -------------------------------------------------------------------------------- 1 | DROP INDEX users_username; 2 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_unsigned/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | -------------------------------------------------------------------------------- /migrations/postgresql/20160107090901_add_tags_to_posts/up.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE posts ADD COLUMN tags varchar[] NOT NULL DEFAULT '{}'; 2 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_column_renaming/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_type_renaming/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | -------------------------------------------------------------------------------- /migrations/mysql/20170211150830_index_columns_used_in_benchmarks/down.sql: -------------------------------------------------------------------------------- 1 | DROP INDEX users_hair_color; 2 | DROP INDEX posts_user_id; 3 | -------------------------------------------------------------------------------- /migrations/mysql/20170215170122_create_trees/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE trees ( 2 | id INTEGER PRIMARY KEY, 3 | parent_id INTEGER 4 | ); 5 | -------------------------------------------------------------------------------- /migrations/sqlite/20170211150830_index_columns_used_in_benchmarks/down.sql: -------------------------------------------------------------------------------- 1 | DROP INDEX users_hair_color; 2 | DROP INDEX posts_user_id; 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_compound_primary_key/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_patch_file/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | patch_file = "schema.patch" 4 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_with_foreign_keys/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | -------------------------------------------------------------------------------- /migrations/postgresql/20170211150830_index_columns_used_in_benchmarks/down.sql: -------------------------------------------------------------------------------- 1 | DROP INDEX users_hair_color; 2 | DROP INDEX posts_user_id; 3 | -------------------------------------------------------------------------------- /migrations/postgresql/20170215170122_create_trees/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE trees ( 2 | id SERIAL PRIMARY KEY, 3 | parent_id INTEGER 4 | ); 5 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_with_compound_foreign_keys/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/2017-12-20-171042_add_published_at_to_posts/down.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE posts DROP COLUMN published_at; 2 | -------------------------------------------------------------------------------- /examples/test_all: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | for dir in $(find . -maxdepth 1 -mindepth 1 -type d); do 5 | (cd $dir && ./test_all) 6 | done 7 | -------------------------------------------------------------------------------- /migrations/sqlite/20151219180527_create_users_and_posts_and_comments/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE users; 2 | DROP TABLE posts; 3 | DROP TABLE comments; 4 | -------------------------------------------------------------------------------- /migrations/sqlite/20160116104628_create_special_posts_and_special_comments/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE special_posts; 2 | DROP TABLE special_comments; 3 | -------------------------------------------------------------------------------- /migrations/sqlite/20170215170122_create_trees/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE trees ( 2 | id INTEGER PRIMARY KEY NOT NULL, 3 | parent_id INTEGER 4 | ); 5 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_compound_primary_key/mysql/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE asd (id INTEGER, qsd INTEGER, PRIMARY KEY (id, qsd)); 2 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_compound_primary_key/sqlite/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE asd (id INTEGER, qsd INTEGER, PRIMARY KEY (id, qsd)); 2 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_custom_types/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | import_types = ["foo::*", "bar::*"] 4 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/.env.sample: -------------------------------------------------------------------------------- 1 | BLOG_USERNAME=sgrif 2 | BLOG_PASSWORD=hunter2 3 | DATABASE_URL=postgres://localhost/diesel_example 4 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/2017-12-20-164453_make_username_unique/up.sql: -------------------------------------------------------------------------------- 1 | CREATE UNIQUE INDEX users_username ON users (username); 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20151219180527_create_users_and_posts_and_comments/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE users; 2 | DROP TABLE posts; 3 | DROP TABLE comments; 4 | -------------------------------------------------------------------------------- /migrations/postgresql/20160116104628_create_special_posts_and_special_comments/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE special_posts; 2 | DROP TABLE special_comments; 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_compound_primary_key/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE asd (id INTEGER, qsd INTEGER, PRIMARY KEY (id, qsd)); 2 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_with_foreign_keys_reserved_names/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | -------------------------------------------------------------------------------- /diesel_cli/tests/support/database.rs: -------------------------------------------------------------------------------- 1 | // This file only exists so rustfmt can find it. The path to this module is always overridden by a 2 | // cfg attr 3 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/2017-12-20-171042_add_published_at_to_posts/up.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE posts ADD COLUMN published_at TIMESTAMP; 2 | -------------------------------------------------------------------------------- /migrations/postgresql/20170407152306_add_nullable_table/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE nullable_table ( 2 | id SERIAL PRIMARY KEY, 3 | value INTEGER 4 | ); 5 | -------------------------------------------------------------------------------- /diesel/src/type_impls/mod.rs: -------------------------------------------------------------------------------- 1 | mod date_and_time; 2 | mod decimal; 3 | pub mod floats; 4 | mod integers; 5 | pub mod option; 6 | mod primitives; 7 | mod tuples; 8 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple/mysql/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id SERIAL PRIMARY KEY); 2 | CREATE TABLE users2 (id SERIAL PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple/sqlite/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /migrations/postgresql/20161206123630_create_table_custom_schema/up.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA custom_schema; 2 | CREATE TABLE custom_schema.users (id SERIAL PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_custom_types/mysql/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_custom_types/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id SERIAL PRIMARY KEY); 2 | CREATE TABLE users2 (id SERIAL PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_custom_types/sqlite/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_except_tables/mysql/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_only_tables/mysql/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_only_tables/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id SERIAL PRIMARY KEY); 2 | CREATE TABLE users2 (id SERIAL PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_only_tables/sqlite/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_patch_file/mysql/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_patch_file/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id SERIAL PRIMARY KEY); 2 | CREATE TABLE users2 (id SERIAL PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_patch_file/sqlite/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_specifying_schema_name/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | schema = "custom_schema" 5 | -------------------------------------------------------------------------------- /diesel_derives/tests/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | users { 3 | id -> Integer, 4 | name -> Text, 5 | hair_color -> Nullable, 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /migrations/postgresql/20170721184144_create_citext_table/up.sql: -------------------------------------------------------------------------------- 1 | CREATE EXTENSION IF NOT EXISTS citext; 2 | CREATE TABLE citext_table (citext_field CITEXT PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_except_tables/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id SERIAL PRIMARY KEY); 2 | CREATE TABLE users2 (id SERIAL PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_except_tables/sqlite/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_only_tables/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | filter = { only_tables = ["users1"] } 5 | -------------------------------------------------------------------------------- /docker/mysql/init/init.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE IF NOT EXISTS diesel_test; 2 | CREATE DATABASE IF NOT EXISTS diesel_unit_test; 3 | GRANT ALL ON `diesel_%`.* TO 'root'@'localhost'; 4 | -------------------------------------------------------------------------------- /migrations/postgresql/2017-09-27-135928_remove_citext_table/down.sql: -------------------------------------------------------------------------------- 1 | CREATE EXTENSION IF NOT EXISTS citext; 2 | CREATE TABLE citext_table (citext_field CITEXT PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /migrations/sqlite/20170407152306_add_nullable_table/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE nullable_table ( 2 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 3 | value INTEGER 4 | ); 5 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_except_tables/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | filter = { except_tables = ["users1"] } 5 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple_without_docs/mysql/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple_without_docs/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id SERIAL PRIMARY KEY); 2 | CREATE TABLE users2 (id SERIAL PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple_without_docs/sqlite/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER PRIMARY KEY); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /diesel_tests/tests/schema_dsl/mod.rs: -------------------------------------------------------------------------------- 1 | #![cfg_attr(not(feature = "postgres"), allow(dead_code))] 2 | 3 | mod functions; 4 | mod structures; 5 | 6 | pub use self::functions::*; 7 | -------------------------------------------------------------------------------- /migrations/sqlite/20170211150830_index_columns_used_in_benchmarks/up.sql: -------------------------------------------------------------------------------- 1 | CREATE INDEX users_hair_color ON users (hair_color); 2 | CREATE INDEX posts_user_id ON posts (user_id); 3 | -------------------------------------------------------------------------------- /migrations/mysql/20170211150830_index_columns_used_in_benchmarks/up.sql: -------------------------------------------------------------------------------- 1 | CREATE INDEX users_hair_color ON users (hair_color(255)); 2 | CREATE INDEX posts_user_id ON posts (user_id); 3 | -------------------------------------------------------------------------------- /migrations/mysql/20170603131224_add_likes/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE likes ( 2 | comment_id INTEGER NOT NULL, 3 | user_id INTEGER NOT NULL, 4 | PRIMARY KEY (comment_id, user_id) 5 | ); 6 | -------------------------------------------------------------------------------- /migrations/postgresql/00000000000000_diesel_initial_setup/down.sql: -------------------------------------------------------------------------------- 1 | DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); 2 | DROP FUNCTION IF EXISTS diesel_set_updated_at(); 3 | -------------------------------------------------------------------------------- /migrations/postgresql/20170211150830_index_columns_used_in_benchmarks/up.sql: -------------------------------------------------------------------------------- 1 | CREATE INDEX users_hair_color ON users (hair_color); 2 | CREATE INDEX posts_user_id ON posts (user_id); 3 | -------------------------------------------------------------------------------- /migrations/sqlite/20170603131224_add_likes/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE likes ( 2 | comment_id INTEGER NOT NULL, 3 | user_id INTEGER NOT NULL, 4 | PRIMARY KEY (comment_id, user_id) 5 | ); 6 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_specifying_schema_name_with_foreign_keys/diesel.toml: -------------------------------------------------------------------------------- 1 | [print_schema] 2 | file = "src/schema.rs" 3 | with_docs = true 4 | schema = "custom_schema" 5 | -------------------------------------------------------------------------------- /diesel_migrations/migrations_internals/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | __diesel_schema_migrations (version) { 3 | version -> VarChar, 4 | run_on -> Timestamp, 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /migrations/mysql/20170407152306_add_nullable_table/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE nullable_table ( 2 | id INTEGER PRIMARY KEY AUTO_INCREMENT, 3 | value INTEGER 4 | ) CHARACTER SET utf8mb4; 5 | -------------------------------------------------------------------------------- /migrations/postgresql/20170603131224_add_likes/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE likes ( 2 | comment_id INTEGER NOT NULL, 3 | user_id INTEGER NOT NULL, 4 | PRIMARY KEY (comment_id, user_id) 5 | ); 6 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_unsigned/mysql/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users1 (id INTEGER UNSIGNED PRIMARY KEY, x INTEGER UNSIGNED); 2 | CREATE TABLE users2 (id INTEGER PRIMARY KEY); 3 | -------------------------------------------------------------------------------- /migrations/postgresql/20170816164352_add_keywords_table/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE with_keywords ( 2 | fn INTEGER PRIMARY KEY, 3 | let INTEGER NOT NULL, 4 | extern INTEGER NOT NULL 5 | ); 6 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | -------------------------------------------------------------------------------- /migrations/mysql/20170816164349_add_keywords_table/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE with_keywords ( 2 | fn INTEGER NOT NULL PRIMARY KEY, 3 | let INTEGER NOT NULL, 4 | extern INTEGER NOT NULL 5 | ); 6 | -------------------------------------------------------------------------------- /migrations/sqlite/20170816164356_add_keywords_table/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE with_keywords ( 2 | fn INTEGER NOT NULL PRIMARY KEY, 3 | let INTEGER NOT NULL, 4 | extern INTEGER NOT NULL 5 | ); 6 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_order/mysql/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE def (id INTEGER PRIMARY KEY); 2 | CREATE TABLE abc (id INTEGER PRIMARY KEY); 3 | CREATE TABLE ghi (id INTEGER PRIMARY KEY); 4 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_order/sqlite/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE def (id INTEGER PRIMARY KEY); 2 | CREATE TABLE abc (id INTEGER PRIMARY KEY); 3 | CREATE TABLE ghi (id INTEGER PRIMARY KEY); 4 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_1/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_1/src/models.rs: -------------------------------------------------------------------------------- 1 | #[derive(Queryable)] 2 | pub struct Post { 3 | pub id: i32, 4 | pub title: String, 5 | pub body: String, 6 | pub published: bool, 7 | } 8 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_2/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_1/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_2/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_1/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_1/src/models.rs: -------------------------------------------------------------------------------- 1 | #[derive(Queryable)] 2 | pub struct Post { 3 | pub id: i32, 4 | pub title: String, 5 | pub body: String, 6 | pub published: bool, 7 | } 8 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_2/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | -------------------------------------------------------------------------------- /migrations/sqlite/20160416143735_infer_all_the_types/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE infer_all_the_ints; 2 | DROP TABLE infer_all_the_bools; 3 | DROP TABLE infer_all_the_strings; 4 | DROP TABLE infer_all_the_floats; 5 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_column_renaming/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE with_keywords ( 2 | fn INTEGER PRIMARY KEY, 3 | let INTEGER NOT NULL, 4 | extern INTEGER NOT NULL 5 | ); 6 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_order/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE def (id INTEGER PRIMARY KEY); 2 | CREATE TABLE abc (id INTEGER PRIMARY KEY); 3 | CREATE TABLE ghi (id INTEGER PRIMARY KEY); 4 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/00000000000000_diesel_initial_setup/down.sql: -------------------------------------------------------------------------------- 1 | DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); 2 | DROP FUNCTION IF EXISTS diesel_set_updated_at(); 3 | -------------------------------------------------------------------------------- /examples/postgres/all_about_inserts/migrations/00000000000000_diesel_initial_setup/down.sql: -------------------------------------------------------------------------------- 1 | DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); 2 | DROP FUNCTION IF EXISTS diesel_set_updated_at(); 3 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_1/src/models.rs: -------------------------------------------------------------------------------- 1 | #[derive(Queryable)] 2 | pub struct Post { 3 | pub id: i32, 4 | pub title: String, 5 | pub body: String, 6 | pub published: bool, 7 | } 8 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_1/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | posts (id) { 3 | id -> Integer, 4 | title -> Text, 5 | body -> Text, 6 | published -> Bool, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_2/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | posts (id) { 3 | id -> Integer, 4 | title -> Text, 5 | body -> Text, 6 | published -> Bool, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | posts (id) { 3 | id -> Integer, 4 | title -> Text, 5 | body -> Text, 6 | published -> Bool, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_column_renaming/mysql/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE with_keywords ( 2 | fn INTEGER NOT NULL PRIMARY KEY, 3 | let INTEGER NOT NULL, 4 | extern INTEGER NOT NULL 5 | ); 6 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_1/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | posts (id) { 3 | id -> Integer, 4 | title -> Varchar, 5 | body -> Text, 6 | published -> Bool, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_2/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | posts (id) { 3 | id -> Integer, 4 | title -> Varchar, 5 | body -> Text, 6 | published -> Bool, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | posts (id) { 3 | id -> Integer, 4 | title -> Varchar, 5 | body -> Text, 6 | published -> Bool, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_1/migrations/00000000000000_diesel_initial_setup/down.sql: -------------------------------------------------------------------------------- 1 | DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); 2 | DROP FUNCTION IF EXISTS diesel_set_updated_at(); 3 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_1/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | posts (id) { 3 | id -> Int4, 4 | title -> Varchar, 5 | body -> Text, 6 | published -> Bool, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_2/migrations/00000000000000_diesel_initial_setup/down.sql: -------------------------------------------------------------------------------- 1 | DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); 2 | DROP FUNCTION IF EXISTS diesel_set_updated_at(); 3 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_2/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | posts (id) { 3 | id -> Int4, 4 | title -> Varchar, 5 | body -> Text, 6 | published -> Bool, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/migrations/00000000000000_diesel_initial_setup/down.sql: -------------------------------------------------------------------------------- 1 | DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); 2 | DROP FUNCTION IF EXISTS diesel_set_updated_at(); 3 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | posts (id) { 3 | id -> Int4, 4 | title -> Varchar, 5 | body -> Text, 6 | published -> Bool, 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /migrations/mysql/20170207193805_initial_schema/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE followings; 2 | DROP TABLE special_comments; 3 | DROP TABLE special_posts; 4 | DROP TABLE comments; 5 | DROP TABLE posts; 6 | DROP TABLE users; 7 | -------------------------------------------------------------------------------- /migrations/mysql/2020-01-25-033332_add unsigned table/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE unsigned_table ( 2 | id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT, 3 | value INTEGER UNSIGNED NOT NULL 4 | ) CHARACTER SET utf8mb4; -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_column_renaming/sqlite/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE with_keywords ( 2 | fn INTEGER NOT NULL PRIMARY KEY, 3 | let INTEGER NOT NULL, 4 | extern INTEGER NOT NULL 5 | ); 6 | -------------------------------------------------------------------------------- /diesel/src/migration/setup_migration_table.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS __diesel_schema_migrations ( 2 | version VARCHAR(50) PRIMARY KEY NOT NULL, 3 | run_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP 4 | ); 5 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_with_foreign_keys_reserved_names/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users (id SERIAL PRIMARY KEY); 2 | CREATE TABLE posts (id SERIAL PRIMARY KEY, type INTEGER NOT NULL REFERENCES users); 3 | -------------------------------------------------------------------------------- /migrations/mysql/20170209180355_add_one_off_tables_from_integration_tests/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE numbers; 2 | DROP TABLE precision_numbers; 3 | DROP TABLE nullable_doubles; 4 | DROP TABLE users_with_name_pk; 5 | DROP TABLE points; 6 | -------------------------------------------------------------------------------- /migrations/sqlite/20170209180355_add_one_off_tables_from_integration_tests/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE numbers; 2 | DROP TABLE precision_numbers; 3 | DROP TABLE nullable_doubles; 4 | DROP TABLE users_with_name_pk; 5 | DROP TABLE points; 6 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_specifying_schema_name/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA custom_schema; 2 | CREATE TABLE in_public (id SERIAL PRIMARY KEY); 3 | CREATE TABLE custom_schema.in_schema (id SERIAL PRIMARY KEY); 4 | -------------------------------------------------------------------------------- /migrations/postgresql/20170209180355_add_one_off_tables_from_integration_tests/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE numbers; 2 | DROP TABLE precision_numbers; 3 | DROP TABLE nullable_doubles; 4 | DROP TABLE users_with_name_pk; 5 | DROP TABLE points; 6 | -------------------------------------------------------------------------------- /diesel/src/util.rs: -------------------------------------------------------------------------------- 1 | /// Treats tuples as a list which can be appended to. e.g. 2 | /// `(a,).tuple_append(b) == (a, b)` 3 | pub trait TupleAppend { 4 | type Output; 5 | 6 | fn tuple_append(self, right: T) -> Self::Output; 7 | } 8 | -------------------------------------------------------------------------------- /diesel_cli/src/default_files/diesel.toml: -------------------------------------------------------------------------------- 1 | # For documentation on how to configure this file, 2 | # see diesel.rs/guides/configuring-diesel-cli 3 | 4 | [print_schema] 5 | file = "src/schema.rs" 6 | 7 | [migrations_directory] 8 | dir = "migrations" -------------------------------------------------------------------------------- /diesel/src/query_builder/returning_clause.rs: -------------------------------------------------------------------------------- 1 | use crate::backend::SupportsReturningClause; 2 | 3 | simple_clause!( 4 | NoReturningClause, 5 | ReturningClause, 6 | " RETURNING ", 7 | backend_bounds = SupportsReturningClause 8 | ); 9 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_1/migrations/20160815133237_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id SERIAL PRIMARY KEY, 3 | title VARCHAR NOT NULL, 4 | body TEXT NOT NULL, 5 | published BOOLEAN NOT NULL DEFAULT FALSE 6 | ) 7 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_2/migrations/20160815133237_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id SERIAL PRIMARY KEY, 3 | title VARCHAR NOT NULL, 4 | body TEXT NOT NULL, 5 | published BOOLEAN NOT NULL DEFAULT FALSE 6 | ) 7 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/migrations/20160815133237_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id SERIAL PRIMARY KEY, 3 | title VARCHAR NOT NULL, 4 | body TEXT NOT NULL, 5 | published BOOLEAN NOT NULL DEFAULT FALSE 6 | ) 7 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_1/migrations/20170124012402_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id INTEGER NOT NULL PRIMARY KEY, 3 | title VARCHAR NOT NULL, 4 | body TEXT NOT NULL, 5 | published BOOLEAN NOT NULL DEFAULT 0 6 | ) 7 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_2/migrations/20170124012402_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id INTEGER NOT NULL PRIMARY KEY, 3 | title VARCHAR NOT NULL, 4 | body TEXT NOT NULL, 5 | published BOOLEAN NOT NULL DEFAULT 0 6 | ) 7 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/migrations/20170124012402_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id INTEGER NOT NULL PRIMARY KEY, 3 | title VARCHAR NOT NULL, 4 | body TEXT NOT NULL, 5 | published BOOLEAN NOT NULL DEFAULT 0 6 | ) 7 | -------------------------------------------------------------------------------- /migrations/postgresql/20170219200159_add_foreign_key/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE fk_inits ( 2 | id INTEGER PRIMARY KEY 3 | ); 4 | 5 | CREATE TABLE fk_tests ( 6 | id INTEGER PRIMARY KEY, 7 | fk_id INTEGER NOT NULL REFERENCES fk_inits(id) 8 | ); 9 | -------------------------------------------------------------------------------- /migrations/sqlite/20160825135747_create_followings/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE followings ( 2 | user_id INTEGER NOT NULL, 3 | post_id INTEGER NOT NULL, 4 | email_notifications BOOLEAN NOT NULL DEFAULT 0, 5 | PRIMARY KEY (user_id, post_id) 6 | ); 7 | -------------------------------------------------------------------------------- /examples/postgres/all_about_updates/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "all_about_updates" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | diesel = { version = "1.4.0", features = ["postgres"] } 8 | -------------------------------------------------------------------------------- /migrations/postgresql/20160825135747_create_followings/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE followings ( 2 | user_id INTEGER NOT NULL, 3 | post_id INTEGER NOT NULL, 4 | email_notifications BOOLEAN NOT NULL DEFAULT 'f', 5 | PRIMARY KEY (user_id, post_id) 6 | ); 7 | -------------------------------------------------------------------------------- /migrations/sqlite/20170513174919_infer_all_the_datetime_types/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE infer_all_the_datetime_types ( 2 | dt DATETIME PRIMARY KEY NOT NULL, 3 | date DATE NOT NULL, 4 | time TIME NOT NULL, 5 | timestamp TIMESTAMP NOT NULL 6 | ); 7 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/insertable_empty_struct.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | users { 6 | id -> Integer, 7 | } 8 | } 9 | 10 | #[derive(Insertable)] 11 | struct User; 12 | 13 | fn main() {} 14 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_type_renaming/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TYPE user_job AS ENUM ('programmer', 'director', 'writer', 'mathematician'); 2 | 3 | CREATE TABLE users ( 4 | id INTEGER PRIMARY KEY, 5 | job user_job NOT NULL 6 | ); 7 | 8 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_1/migrations/20160815133237_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id INTEGER AUTO_INCREMENT PRIMARY KEY, 3 | title VARCHAR(255) NOT NULL, 4 | body TEXT NOT NULL, 5 | published BOOLEAN NOT NULL DEFAULT FALSE 6 | ); 7 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_2/migrations/20160815133237_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id INTEGER AUTO_INCREMENT PRIMARY KEY, 3 | title VARCHAR(255) NOT NULL, 4 | body TEXT NOT NULL, 5 | published BOOLEAN NOT NULL DEFAULT FALSE 6 | ); 7 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/migrations/20160815133237_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id INTEGER AUTO_INCREMENT PRIMARY KEY, 3 | title VARCHAR(255) NOT NULL, 4 | body TEXT NOT NULL, 5 | published BOOLEAN NOT NULL DEFAULT FALSE 6 | ); 7 | -------------------------------------------------------------------------------- /diesel/src/pg/upsert/on_conflict_docs_setup.rs: -------------------------------------------------------------------------------- 1 | include!("../../doctest_setup.rs"); 2 | use schema::users; 3 | 4 | #[derive(Clone, Copy, Insertable, AsChangeset)] 5 | #[table_name="users"] 6 | struct User<'a> { 7 | id: i32, 8 | name: &'a str, 9 | } 10 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_expression_bad_sql_type.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | #[derive(AsExpression)] 5 | #[sql_type(Foo)] 6 | #[sql_type] 7 | #[sql_type = "@%&&*"] 8 | #[sql_type = "1omg"] 9 | struct Lol; 10 | 11 | fn main() {} 12 | -------------------------------------------------------------------------------- /examples/sqlite/test_all: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | export DATABASE_URL="/tmp/test_examples.db" 5 | 6 | for dir in $(find . -maxdepth 1 -mindepth 1 -type d); do 7 | cd $dir 8 | ../../../bin/diesel database reset 9 | cargo build 10 | cd .. 11 | done 12 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_demo_step_1_mysql" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | diesel = { version = "1.4.0", features = ["mysql"] } 8 | dotenv = "0.10" 9 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_2/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_demo_step_2_mysql" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | diesel = { version = "1.4.0", features = ["mysql"] } 8 | dotenv = "0.10" 9 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_demo_step_3_mysql" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | diesel = { version = "1.4.0", features = ["mysql"] } 8 | dotenv = "0.10" 9 | -------------------------------------------------------------------------------- /migrations/mysql/20170811104602_add_blob_types/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE all_the_blobs ( 2 | id INTEGER PRIMARY KEY, -- Can't use a blob as a pk 3 | tiny TINYBLOB NOT NULL, 4 | normal BLOB NOT NULL, 5 | medium MEDIUMBLOB NOT NULL, 6 | big LONGBLOB NOT NULL 7 | ) 8 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_demo_step_1_pg" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | diesel = { version = "1.4.0", features = ["postgres"] } 8 | dotenv = "0.10" 9 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_2/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_demo_step_2_pg" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | diesel = { version = "1.4.0", features = ["postgres"] } 8 | dotenv = "0.10" 9 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_demo_step_3_pg" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | diesel = { version = "1.4.0", features = ["postgres"] } 8 | dotenv = "0.10" 9 | -------------------------------------------------------------------------------- /migrations/mysql/20170219200159_add_foreign_key/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE fk_inits ( 2 | id INTEGER PRIMARY KEY 3 | ); 4 | 5 | CREATE TABLE fk_tests ( 6 | id INTEGER PRIMARY KEY, 7 | fk_id INTEGER NOT NULL, 8 | FOREIGN KEY (fk_id) REFERENCES fk_inits (id) 9 | ); 10 | -------------------------------------------------------------------------------- /migrations/postgresql/20170717152137_add_ranges/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE all_the_ranges ( 2 | int4 INT4RANGE PRIMARY KEY, 3 | int8 INT8RANGE NOT NULL, 4 | num NUMRANGE NOT NULL, 5 | ts TSRANGE NOT NULL, 6 | tstz TSTZRANGE NOT NULL, 7 | date DATERANGE NOT NULL 8 | ) 9 | -------------------------------------------------------------------------------- /migrations/sqlite/20170219200159_add_foreign_key/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE fk_inits ( 2 | id INTEGER PRIMARY KEY 3 | ); 4 | 5 | CREATE TABLE fk_tests ( 6 | id INTEGER PRIMARY KEY, 7 | fk_id INTEGER NOT NULL, 8 | FOREIGN KEY(fk_id) REFERENCES fk_inits(id) 9 | ); 10 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_patch_file/postgres/schema.patch: -------------------------------------------------------------------------------- 1 | @@ -5,8 +5,9 @@ 2 | } 3 | 4 | table! { 5 | - users2 (id) { 6 | - id -> Int4, 7 | + users2 (myid) { 8 | + #[sql_name = "id"] 9 | + myid -> Int4, 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_patch_file/mysql/schema.patch: -------------------------------------------------------------------------------- 1 | @@ -5,8 +5,9 @@ table! { 2 | } 3 | 4 | table! { 5 | - users2 (id) { 6 | - id -> Integer, 7 | + users2 (myid) { 8 | + #[sql_name = "id"] 9 | + myid -> Integer, 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /migrations/mysql/20170805195107_tables_which_make_infer_joinable_blow_up/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE self_referential_fk; 2 | DROP TABLE fk_doesnt_reference_pk; 3 | DROP TABLE composite_fk; 4 | DROP TABLE multiple_fks_to_same_table; 5 | DROP TABLE cyclic_fk_1 CASCADE; 6 | DROP TABLE cyclic_fk_2 CASCADE; 7 | -------------------------------------------------------------------------------- /bin/diesel: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | SCRIPT_DIR=$(dirname "$0") 5 | ABSOLUTE_SCRIPT_DIR=$(cd "$SCRIPT_DIR" && pwd) 6 | BASE_DIR=$(dirname "$ABSOLUTE_SCRIPT_DIR") 7 | $(cd $BASE_DIR/diesel_cli && cargo build --quiet --no-default-features --features "$BACKEND") 8 | $BASE_DIR/target/debug/diesel $@ 9 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/table_invalid_syntax_1.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | table! { 4 | 12 5 | } 6 | // error-pattern: Invalid `table!` syntax. Please see the `table!` macro docs for more info. `https://docs.diesel.rs/diesel/macro.table.html` 7 | 8 | fn main() {} 9 | -------------------------------------------------------------------------------- /clippy.toml: -------------------------------------------------------------------------------- 1 | cognitive-complexity-threshold = 30 2 | doc-valid-idents = [ 3 | "MiB", "GiB", "TiB", "PiB", "EiB", 4 | "DirectX", "OpenGL", "TrueType", 5 | "GPLv2", "GPLv3", 6 | "GitHub", 7 | "IPv4", "IPv6", 8 | "JavaScript", "NaN", "OAuth", 9 | "SQLite", "PostgreSQL", "MySQL" 10 | ] 11 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_with_foreign_keys/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users (id SERIAL PRIMARY KEY); 2 | CREATE TABLE posts (id SERIAL PRIMARY KEY, user_id INTEGER NOT NULL REFERENCES users); 3 | CREATE TABLE comments (id SERIAL PRIMARY KEY, post_id INTEGER NOT NULL REFERENCES posts); 4 | -------------------------------------------------------------------------------- /diesel/src/pg/expression/extensions/mod.rs: -------------------------------------------------------------------------------- 1 | //! This module contains extensions that are added to core types to aid in 2 | //! building expressions. These traits are not exported by default. The are also 3 | //! re-exported in `diesel::dsl` 4 | mod interval_dsl; 5 | 6 | pub use self::interval_dsl::IntervalDsl; 7 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_with_compound_foreign_keys/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE a (id SERIAL PRIMARY KEY, a INTEGER NOT NULL, b INTEGER NOT NULL); 2 | CREATE UNIQUE INDEX ON a (a, b); 3 | CREATE TABLE b (id SERIAL PRIMARY KEY, a INTEGER, b INTEGER, FOREIGN KEY (a, b) REFERENCES a (a, b)); 4 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/table_invalid_syntax_2.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | table! { 4 | some wrong syntax 5 | } 6 | // error-pattern: Invalid `table!` syntax. Please see the `table!` macro docs for more info. `https://docs.diesel.rs/diesel/macro.table.html` 7 | 8 | fn main() {} 9 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_1/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_demo_step_1_sqlite" 3 | version = "0.1.0" 4 | license = "MIT OR Apache-2.0" 5 | authors = ["Taryn Hill "] 6 | 7 | [dependencies] 8 | diesel = { version = "1.4.0", features = ["sqlite"] } 9 | dotenv = "0.10" 10 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_2/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_demo_step_2_sqlite" 3 | version = "0.1.0" 4 | license = "MIT OR Apache-2.0" 5 | authors = ["Taryn Hill "] 6 | 7 | [dependencies] 8 | diesel = { version = "1.4.0", features = ["sqlite"] } 9 | dotenv = "0.10" 10 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_demo_step_3_sqlite" 3 | version = "0.1.0" 4 | license = "MIT OR Apache-2.0" 5 | authors = ["Taryn Hill "] 6 | 7 | [dependencies] 8 | diesel = { version = "1.4.0", features = ["sqlite"] } 9 | dotenv = "0.10" 10 | -------------------------------------------------------------------------------- /migrations/sqlite/20170805195107_tables_which_make_infer_joinable_blow_up/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE self_referential_fk; 2 | DROP INDEX posts_title_is_unique; 3 | DROP TABLE fk_doesnt_reference_pk; 4 | DROP TABLE composite_fk; 5 | DROP TABLE multiple_fks_to_same_table; 6 | DROP TABLE cyclic_fk_2; 7 | DROP TABLE cyclic_fk_1; 8 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/queryable_by_name_requires_table_name_or_sql_type_annotation.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | #[derive(QueryableByName)] 4 | struct Foo { 5 | foo: i32, 6 | bar: String, 7 | } 8 | 9 | #[derive(QueryableByName)] 10 | struct Bar(i32, String); 11 | 12 | fn main() {} 13 | -------------------------------------------------------------------------------- /diesel_derives/tests/tests.rs: -------------------------------------------------------------------------------- 1 | #![deny(warnings)] 2 | 3 | #[macro_use] 4 | extern crate cfg_if; 5 | #[macro_use] 6 | extern crate diesel; 7 | 8 | mod helpers; 9 | mod schema; 10 | 11 | mod as_changeset; 12 | mod associations; 13 | mod identifiable; 14 | mod insertable; 15 | mod queryable; 16 | mod queryable_by_name; 17 | -------------------------------------------------------------------------------- /examples/sqlite/all_about_inserts/migrations/2017-12-16-173626_create_users/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users ( 2 | id INTEGER PRIMARY KEY AUTOINCREMENT, 3 | name TEXT NOT NULL, 4 | hair_color TEXT, 5 | created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 6 | updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP 7 | ); 8 | -------------------------------------------------------------------------------- /guide_drafts/README.md: -------------------------------------------------------------------------------- 1 | Guide Drafts 2 | ============ 3 | 4 | This directory contains guides that will eventually be added to the diesel.rs 5 | website, but are still being worked on. Once the guide content is deemed 6 | complete, and it has been styled and marked up for the website, they are removed 7 | from this directory. 8 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_on_non_struct.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | users { 6 | id -> Integer, 7 | name -> Text, 8 | hair_color -> Nullable, 9 | } 10 | } 11 | 12 | #[derive(AsChangeset)] 13 | #[table_name = "users"] 14 | enum User {} 15 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/belongs_to_second_parent.stderr: -------------------------------------------------------------------------------- 1 | warning: belongs_to takes a single parent. Change 2 | belongs_to(Bar, Baz) 3 | to 4 | belongs_to(Bar) 5 | belongs_to(Baz) 6 | --> $DIR/belongs_to_second_parent.rs:29:3 7 | | 8 | 29 | #[belongs_to(Bar, Baz)] 9 | | ^^^^^^^^^^^^^^^^^^^^ 10 | 11 | -------------------------------------------------------------------------------- /examples/postgres/all_about_inserts/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "all_about_inserts" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | diesel = { version = "1.4.0", features = ["postgres"] } 8 | serde = "1.0" 9 | serde_derive = "1.0" 10 | serde_json = "1.0" 11 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple_without_docs/postgres/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | users1 (id) { 3 | id -> Int4, 4 | } 5 | } 6 | 7 | table! { 8 | users2 (id) { 9 | id -> Int4, 10 | } 11 | } 12 | 13 | allow_tables_to_appear_in_same_query!( 14 | users1, 15 | users2, 16 | ); 17 | -------------------------------------------------------------------------------- /migrations/postgresql/20160116104628_create_special_posts_and_special_comments/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE special_posts ( 2 | id SERIAL PRIMARY KEY, 3 | user_id INTEGER NOT NULL, 4 | title VARCHAR NOT NULL 5 | ); 6 | 7 | CREATE TABLE special_comments ( 8 | id SERIAL PRIMARY KEY, 9 | special_post_id INTEGER NOT NULL 10 | ); 11 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple_without_docs/mysql/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | users1 (id) { 3 | id -> Integer, 4 | } 5 | } 6 | 7 | table! { 8 | users2 (id) { 9 | id -> Integer, 10 | } 11 | } 12 | 13 | allow_tables_to_appear_in_same_query!( 14 | users1, 15 | users2, 16 | ); 17 | -------------------------------------------------------------------------------- /examples/mysql/test_all: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | set -a 5 | if [ -f ../../.env ]; then . ../../.env; fi 6 | DATABASE_URL=${MYSQL_EXAMPLE_DATABASE_URL} 7 | set +a 8 | 9 | for dir in $(find . -maxdepth 1 -mindepth 1 -type d); do 10 | cd $dir 11 | ../../../bin/diesel database reset 12 | cargo build 13 | cd .. 14 | done 15 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/belongs_to_missing_parent_import.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | foos { 6 | id -> Integer, 7 | bar_id -> Integer, 8 | } 9 | } 10 | 11 | #[derive(Associations)] 12 | #[belongs_to(Bar)] 13 | struct Foo { 14 | bar_id: i32, 15 | } 16 | 17 | fn main() {} 18 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_bad_column_name_syntax.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | users { 6 | id -> Integer, 7 | name -> Text, 8 | } 9 | } 10 | 11 | #[derive(AsChangeset)] 12 | #[table_name = "users"] 13 | struct User { 14 | #[column_name] 15 | name: String, 16 | } 17 | -------------------------------------------------------------------------------- /examples/mysql/all_about_inserts/migrations/2017-12-16-173626_create_users/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users ( 2 | id INTEGER PRIMARY KEY AUTO_INCREMENT, 3 | name TEXT NOT NULL, 4 | hair_color TEXT, 5 | created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 6 | updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 7 | ); 8 | -------------------------------------------------------------------------------- /migrations/postgresql/20170805195107_tables_which_make_infer_joinable_blow_up/down.sql: -------------------------------------------------------------------------------- 1 | DROP TABLE self_referential_fk; 2 | DROP TABLE fk_doesnt_reference_pk; 3 | ALTER TABLE posts DROP CONSTRAINT title_is_unique; 4 | DROP TABLE composite_fk; 5 | DROP TABLE multiple_fks_to_same_table; 6 | DROP TABLE cyclic_fk_2 CASCADE; 7 | DROP TABLE cyclic_fk_1 CASCADE; 8 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple_without_docs/sqlite/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | users1 (id) { 3 | id -> Nullable, 4 | } 5 | } 6 | 7 | table! { 8 | users2 (id) { 9 | id -> Nullable, 10 | } 11 | } 12 | 13 | allow_tables_to_appear_in_same_query!( 14 | users1, 15 | users2, 16 | ); 17 | -------------------------------------------------------------------------------- /examples/mysql/all_about_inserts/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "all_about_inserts_mysql" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | diesel = { version = "1.4.0", features = ["mysql", "chrono"] } 8 | serde = "1.0" 9 | serde_derive = "1.0" 10 | serde_json = "1.0" 11 | chrono = "0.4" 12 | -------------------------------------------------------------------------------- /examples/sqlite/all_about_inserts/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "all_about_inserts_sqlite" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | diesel = { version = "1.4.0", features = ["sqlite", "chrono"] } 8 | serde = "1.0" 9 | serde_derive = "1.0" 10 | serde_json = "1.0" 11 | chrono = "0.4" 12 | -------------------------------------------------------------------------------- /migrations/sqlite/20160116104628_create_special_posts_and_special_comments/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE special_posts ( 2 | id INTEGER PRIMARY KEY AUTOINCREMENT, 3 | user_id INTEGER NOT NULL, 4 | title VARCHAR NOT NULL 5 | ); 6 | 7 | CREATE TABLE special_comments ( 8 | id INTEGER PRIMARY KEY AUTOINCREMENT, 9 | special_post_id INTEGER NOT NULL 10 | ); 11 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_patch_file/mysql/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | users1 (id) { 3 | id -> Integer, 4 | } 5 | } 6 | 7 | table! { 8 | users2 (myid) { 9 | #[sql_name = "id"] 10 | myid -> Integer, 11 | } 12 | } 13 | 14 | allow_tables_to_appear_in_same_query!( 15 | users1, 16 | users2, 17 | ); 18 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_patch_file/postgres/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | users1 (id) { 3 | id -> Int4, 4 | } 5 | } 6 | 7 | table! { 8 | users2 (myid) { 9 | #[sql_name = "id"] 10 | myid -> Int4, 11 | } 12 | } 13 | 14 | allow_tables_to_appear_in_same_query!( 15 | users1, 16 | users2, 17 | ); 18 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_patch_file/sqlite/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | users1 (id) { 3 | id -> Integer, 4 | } 5 | } 6 | 7 | table! { 8 | users2 (myid) { 9 | #[sql_name = "id"] 10 | myid -> Integer, 11 | } 12 | } 13 | 14 | allow_tables_to_appear_in_same_query!( 15 | users1, 16 | users2, 17 | ); 18 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_missing_table_import.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | #[derive(AsChangeset)] 5 | struct User { 6 | id: i32, 7 | name: String, 8 | } 9 | 10 | #[derive(AsChangeset)] 11 | #[table_name = "users"] 12 | struct UserForm { 13 | id: i32, 14 | name: String, 15 | } 16 | 17 | fn main() {} 18 | -------------------------------------------------------------------------------- /diesel/src/pg/expression/helper_types.rs: -------------------------------------------------------------------------------- 1 | use crate::dsl::AsExprOf; 2 | use crate::sql_types::VarChar; 3 | 4 | /// The return type of `lhs.ilike(rhs)` 5 | pub type ILike = super::operators::ILike>; 6 | 7 | /// The return type of `lhs.not_ilike(rhs)` 8 | pub type NotILike = super::operators::NotILike>; 9 | -------------------------------------------------------------------------------- /examples/postgres/all_about_inserts/migrations/2017-12-16-173626_create_users/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users ( 2 | id SERIAL PRIMARY KEY, 3 | name TEXT NOT NULL, 4 | hair_color TEXT, 5 | created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 6 | updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP 7 | ); 8 | 9 | SELECT diesel_manage_updated_at('users'); 10 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_bad_primary_key_syntax.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | users { 6 | id -> Integer, 7 | name -> Text, 8 | } 9 | } 10 | 11 | #[derive(AsChangeset)] 12 | #[primary_key(id, bar = "baz", qux(id))] 13 | struct UserForm { 14 | id: i32, 15 | name: String, 16 | } 17 | 18 | fn main() {} 19 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/insertable_empty_struct.stderr: -------------------------------------------------------------------------------- 1 | error: Cannot derive Insertable for unit structs 2 | --> $DIR/insertable_empty_struct.rs:10:10 3 | | 4 | 10 | #[derive(Insertable)] 5 | | ^^^^^^^^^^ 6 | | 7 | = help: Use `insert_into(users::table).default_values()` if you want `DEFAULT VALUES` 8 | 9 | error: aborting due to previous error 10 | 11 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/2017-12-20-144812_create_users/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users ( 2 | id SERIAL PRIMARY KEY, 3 | username TEXT NOT NULL, 4 | hashed_password TEXT NOT NULL, 5 | created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 6 | updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP 7 | ); 8 | 9 | SELECT diesel_manage_updated_at('users'); 10 | -------------------------------------------------------------------------------- /diesel/src/sqlite/connection/diesel_manage_updated_at.sql: -------------------------------------------------------------------------------- 1 | CREATE TRIGGER __diesel_manage_updated_at_{table_name} 2 | AFTER UPDATE ON {table_name} 3 | FOR EACH ROW WHEN 4 | old.updated_at IS NULL AND 5 | new.updated_at IS NULL OR 6 | old.updated_at == new.updated_at 7 | BEGIN 8 | UPDATE {table_name} 9 | SET updated_at = CURRENT_TIMESTAMP 10 | WHERE ROWID = new.ROWID; 11 | END 12 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_missing_column_name_tuple_struct.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | users { 6 | id -> Integer, 7 | name -> Text, 8 | hair_color -> Nullable, 9 | } 10 | } 11 | 12 | #[derive(AsChangeset)] 13 | #[table_name = "users"] 14 | struct User(i32, #[column_name = "name"] String, String); 15 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/belongs_to_missing_parent_import.stderr: -------------------------------------------------------------------------------- 1 | error[E0412]: cannot find type `Bar` in this scope 2 | --> $DIR/belongs_to_missing_parent_import.rs:12:14 3 | | 4 | 12 | #[belongs_to(Bar)] 5 | | ^^^ not found in this scope 6 | 7 | error: aborting due to previous error 8 | 9 | For more information about this error, try `rustc --explain E0412`. 10 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_2/src/models.rs: -------------------------------------------------------------------------------- 1 | use schema::posts; 2 | 3 | #[derive(Queryable)] 4 | pub struct Post { 5 | pub id: i32, 6 | pub title: String, 7 | pub body: String, 8 | pub published: bool, 9 | } 10 | 11 | #[derive(Insertable)] 12 | #[table_name = "posts"] 13 | pub struct NewPost<'a> { 14 | pub title: &'a str, 15 | pub body: &'a str, 16 | } 17 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/src/models.rs: -------------------------------------------------------------------------------- 1 | use schema::posts; 2 | 3 | #[derive(Queryable)] 4 | pub struct Post { 5 | pub id: i32, 6 | pub title: String, 7 | pub body: String, 8 | pub published: bool, 9 | } 10 | 11 | #[derive(Insertable)] 12 | #[table_name = "posts"] 13 | pub struct NewPost<'a> { 14 | pub title: &'a str, 15 | pub body: &'a str, 16 | } 17 | -------------------------------------------------------------------------------- /diesel/src/data_types.rs: -------------------------------------------------------------------------------- 1 | //! Structs to represent the primitive equivalent of SQL types where 2 | //! there is no existing Rust primitive, or where using it would be 3 | //! confusing (such as date and time types). This module will re-export 4 | //! all backend specific data structures when compiled against that 5 | //! backend. 6 | #[cfg(feature = "postgres")] 7 | pub use crate::pg::data_types::*; 8 | -------------------------------------------------------------------------------- /diesel_cli/src/setup_sql/postgres/initial_setup/down.sql: -------------------------------------------------------------------------------- 1 | -- This file was automatically created by Diesel to setup helper functions 2 | -- and other internal bookkeeping. This file is safe to edit, any future 3 | -- changes will be added to existing projects as new migrations. 4 | 5 | DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); 6 | DROP FUNCTION IF EXISTS diesel_set_updated_at(); 7 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_deprecated_column_name.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | users { 6 | id -> Integer, 7 | name -> Text, 8 | } 9 | } 10 | 11 | #[derive(AsChangeset)] 12 | #[table_name(users)] 13 | struct UserForm { 14 | id: i32, 15 | #[column_name(name)] 16 | name: String, 17 | } 18 | 19 | fn main() {} 20 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_2/src/models.rs: -------------------------------------------------------------------------------- 1 | use schema::posts; 2 | 3 | #[derive(Queryable)] 4 | pub struct Post { 5 | pub id: i32, 6 | pub title: String, 7 | pub body: String, 8 | pub published: bool, 9 | } 10 | 11 | #[derive(Insertable)] 12 | #[table_name = "posts"] 13 | pub struct NewPost<'a> { 14 | pub title: &'a str, 15 | pub body: &'a str, 16 | } 17 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/src/models.rs: -------------------------------------------------------------------------------- 1 | use schema::posts; 2 | 3 | #[derive(Queryable)] 4 | pub struct Post { 5 | pub id: i32, 6 | pub title: String, 7 | pub body: String, 8 | pub published: bool, 9 | } 10 | 11 | #[derive(Insertable)] 12 | #[table_name = "posts"] 13 | pub struct NewPost<'a> { 14 | pub title: &'a str, 15 | pub body: &'a str, 16 | } 17 | -------------------------------------------------------------------------------- /examples/postgres/test_all: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | set -a 5 | if [ -f ../../.env ]; then . ../../.env; fi 6 | DATABASE_URL=${PG_EXAMPLE_DATABASE_URL} 7 | set +a 8 | 9 | for dir in $(find . -maxdepth 1 -mindepth 1 -type d); do 10 | cd $dir 11 | 12 | if [ -d "migrations" ]; then 13 | ../../../bin/diesel database reset 14 | fi 15 | 16 | cargo test 17 | cd .. 18 | done 19 | -------------------------------------------------------------------------------- /diesel/src/type_impls/decimal.rs: -------------------------------------------------------------------------------- 1 | #![allow(dead_code)] 2 | 3 | #[cfg(feature = "bigdecimal")] 4 | mod bigdecimal { 5 | extern crate bigdecimal; 6 | use self::bigdecimal::BigDecimal; 7 | use crate::sql_types::Numeric; 8 | 9 | #[derive(FromSqlRow, AsExpression)] 10 | #[diesel(foreign_derive)] 11 | #[sql_type = "Numeric"] 12 | struct BigDecimalProxy(BigDecimal); 13 | } 14 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/cannot_pass_aggregate_to_where.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::dsl::count; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | } 11 | } 12 | 13 | fn main() { 14 | use self::users::dsl::*; 15 | 16 | let source = users.filter(count(id).gt(3)); 17 | //~^ ERROR NonAggregate 18 | } 19 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_2/src/models.rs: -------------------------------------------------------------------------------- 1 | use super::schema::posts; 2 | 3 | #[derive(Queryable)] 4 | pub struct Post { 5 | pub id: i32, 6 | pub title: String, 7 | pub body: String, 8 | pub published: bool, 9 | } 10 | 11 | #[derive(Insertable)] 12 | #[table_name = "posts"] 13 | pub struct NewPost<'a> { 14 | pub title: &'a str, 15 | pub body: &'a str, 16 | } 17 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/src/models.rs: -------------------------------------------------------------------------------- 1 | use super::schema::posts; 2 | 3 | #[derive(Queryable)] 4 | pub struct Post { 5 | pub id: i32, 6 | pub title: String, 7 | pub body: String, 8 | pub published: bool, 9 | } 10 | 11 | #[derive(Insertable)] 12 | #[table_name = "posts"] 13 | pub struct NewPost<'a> { 14 | pub title: &'a str, 15 | pub body: &'a str, 16 | } 17 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_patch_file/sqlite/schema.patch: -------------------------------------------------------------------------------- 1 | @@ -1,12 +1,13 @@ 2 | table! { 3 | users1 (id) { 4 | - id -> Nullable, 5 | + id -> Integer, 6 | } 7 | } 8 | 9 | table! { 10 | - users2 (id) { 11 | - id -> Nullable, 12 | + users2 (myid) { 13 | + #[sql_name = "id"] 14 | + myid -> Integer, 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/order_requires_column_from_same_table.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | } 10 | } 11 | 12 | table! { 13 | posts { 14 | id -> Integer, 15 | } 16 | } 17 | 18 | fn main() { 19 | let source = users::table.order(posts::id); 20 | //~^ ERROR E0277 21 | } 22 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_custom_types/postgres/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | use foo::*; 3 | use bar::*; 4 | 5 | users1 (id) { 6 | id -> Int4, 7 | } 8 | } 9 | 10 | table! { 11 | use foo::*; 12 | use bar::*; 13 | 14 | users2 (id) { 15 | id -> Int4, 16 | } 17 | } 18 | 19 | allow_tables_to_appear_in_same_query!( 20 | users1, 21 | users2, 22 | ); 23 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/cannot_mix_aggregate_and_non_aggregate_selects.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::dsl::count; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | } 11 | } 12 | 13 | fn main() { 14 | use self::users::dsl::*; 15 | 16 | let source = users.select((id, count(users.star()))); 17 | //~^ ERROR E0277 18 | } 19 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_custom_types/mysql/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | use foo::*; 3 | use bar::*; 4 | 5 | users1 (id) { 6 | id -> Integer, 7 | } 8 | } 9 | 10 | table! { 11 | use foo::*; 12 | use bar::*; 13 | 14 | users2 (id) { 15 | id -> Integer, 16 | } 17 | } 18 | 19 | allow_tables_to_appear_in_same_query!( 20 | users1, 21 | users2, 22 | ); 23 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/belongs_to_incorrect_lifetime_syntax.stderr: -------------------------------------------------------------------------------- 1 | error[E0261]: use of undeclared lifetime name `'a` 2 | --> $DIR/belongs_to_incorrect_lifetime_syntax.rs:25:23 3 | | 4 | 25 | #[belongs_to(parent = "Foo<'a>")] 5 | | ^^^^^^^^^ undeclared lifetime 6 | 7 | error: aborting due to previous error 8 | 9 | For more information about this error, try `rustc --explain E0261`. 10 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/2017-12-20-163829_create_posts/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE posts ( 2 | id SERIAL PRIMARY KEY, 3 | user_id INTEGER NOT NULL REFERENCES users (id), 4 | title TEXT NOT NULL, 5 | body TEXT NOT NULL, 6 | created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 7 | updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP 8 | ); 9 | 10 | SELECT diesel_manage_updated_at('posts'); 11 | -------------------------------------------------------------------------------- /migrations/mysql/20170804172328_add_foreign_keys/down.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE posts DROP FOREIGN KEY posts_user_id_fkey; 2 | ALTER TABLE comments DROP FOREIGN KEY comments_post_id_fkey; 3 | ALTER TABLE followings DROP FOREIGN KEY followings_user_id_fkey; 4 | ALTER TABLE followings DROP FOREIGN KEY followings_post_id_fkey; 5 | ALTER TABLE likes DROP FOREIGN KEY likes_comment_id_fkey; 6 | ALTER TABLE likes DROP FOREIGN KEY likes_user_id_fkey; 7 | -------------------------------------------------------------------------------- /migrations/postgresql/20170209180355_add_one_off_tables_from_integration_tests/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE numbers (n INTEGER PRIMARY KEY); 2 | CREATE TABLE precision_numbers (n DOUBLE PRECISION NOT NULL PRIMARY KEY); 3 | CREATE TABLE nullable_doubles (id SERIAL PRIMARY KEY, n DOUBLE PRECISION); 4 | CREATE TABLE users_with_name_pk (name VARCHAR PRIMARY KEY); 5 | CREATE TABLE points (x INTEGER NOT NULL, y INTEGER NOT NULL, PRIMARY KEY (x, y)); 6 | -------------------------------------------------------------------------------- /migrations/postgresql/20170804172328_add_foreign_keys/down.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE posts DROP CONSTRAINT posts_user_id_fkey; 2 | ALTER TABLE comments DROP CONSTRAINT comments_post_id_fkey; 3 | ALTER TABLE followings DROP CONSTRAINT followings_user_id_fkey; 4 | ALTER TABLE followings DROP CONSTRAINT followings_post_id_fkey; 5 | ALTER TABLE likes DROP CONSTRAINT likes_comment_id_fkey; 6 | ALTER TABLE likes DROP CONSTRAINT likes_user_id_fkey; 7 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_struct_with_only_primary_key.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | table! { 4 | foo { 5 | id -> Integer, 6 | bar -> Integer, 7 | } 8 | } 9 | 10 | #[derive(AsChangeset)] 11 | #[table_name="foo"] 12 | struct Foo1 { 13 | id: i32, 14 | bar: i32, 15 | } 16 | 17 | #[derive(AsChangeset)] 18 | #[table_name="foo"] 19 | struct Foo2 { 20 | id: i32, 21 | } 22 | -------------------------------------------------------------------------------- /migrations/mysql/20170209180355_add_one_off_tables_from_integration_tests/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE numbers (n INTEGER PRIMARY KEY); 2 | CREATE TABLE precision_numbers (n DOUBLE PRECISION NOT NULL PRIMARY KEY); 3 | CREATE TABLE nullable_doubles (id INT PRIMARY KEY AUTO_INCREMENT, n DOUBLE PRECISION); 4 | CREATE TABLE users_with_name_pk (name VARCHAR(50) PRIMARY KEY); 5 | CREATE TABLE points (x INTEGER NOT NULL, y INTEGER NOT NULL, PRIMARY KEY (x, y)); 6 | -------------------------------------------------------------------------------- /migrations/sqlite/20170209180355_add_one_off_tables_from_integration_tests/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE numbers (n INTEGER PRIMARY KEY); 2 | CREATE TABLE precision_numbers (n DOUBLE PRECISION NOT NULL PRIMARY KEY); 3 | CREATE TABLE nullable_doubles (id INTEGER PRIMARY KEY AUTOINCREMENT, n DOUBLE PRECISION); 4 | CREATE TABLE users_with_name_pk (name VARCHAR PRIMARY KEY); 5 | CREATE TABLE points (x INTEGER NOT NULL, y INTEGER NOT NULL, PRIMARY KEY (x, y)); 6 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_custom_types/sqlite/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | use foo::*; 3 | use bar::*; 4 | 5 | users1 (id) { 6 | id -> Nullable, 7 | } 8 | } 9 | 10 | table! { 11 | use foo::*; 12 | use bar::*; 13 | 14 | users2 (id) { 15 | id -> Nullable, 16 | } 17 | } 18 | 19 | allow_tables_to_appear_in_same_query!( 20 | users1, 21 | users2, 22 | ); 23 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_except_tables/postgres/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `users2` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | users2 (id) { 6 | /// The `id` column of the `users2` table. 7 | /// 8 | /// Its SQL type is `Int4`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Int4, 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_only_tables/mysql/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `users1` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | users1 (id) { 6 | /// The `id` column of the `users1` table. 7 | /// 8 | /// Its SQL type is `Integer`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Integer, 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_only_tables/postgres/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `users1` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | users1 (id) { 6 | /// The `id` column of the `users1` table. 7 | /// 8 | /// Its SQL type is `Int4`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Int4, 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_except_tables/mysql/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `users2` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | users2 (id) { 6 | /// The `id` column of the `users2` table. 7 | /// 8 | /// Its SQL type is `Integer`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Integer, 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/numeric_ops_require_numeric_column.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | fn main() { 14 | use self::users::dsl::*; 15 | 16 | let _ = users.select(name + name); 17 | //~^ ERROR binary operation `+` cannot be applied to type `users::columns::name` 18 | } 19 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/boxed_queries_must_be_used_with_proper_connection.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::pg::Pg; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | } 11 | } 12 | 13 | fn main() { 14 | let connection = SqliteConnection::establish("").unwrap(); 15 | users::table.into_boxed::().load::<(i32,)>(&connection); 16 | //~^ ERROR type mismatch 17 | } 18 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/belongs_to_missing_foreign_key_field.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | struct Bar; 5 | 6 | #[derive(Associations)] 7 | #[belongs_to(Bar)] 8 | #[belongs_to(Bar, foreign_key = "bar_id")] 9 | struct Foo {} 10 | 11 | #[derive(Associations)] 12 | #[belongs_to(Bar)] 13 | #[belongs_to(Bar, foreign_key = "bar_id")] 14 | struct Baz { 15 | #[column_name = "baz_id"] 16 | bar_id: i32, 17 | } 18 | 19 | fn main() {} 20 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/migrations/2017-12-20-183155_create_comments/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE comments ( 2 | id SERIAL PRIMARY KEY, 3 | user_id INTEGER NOT NULL REFERENCES users (id), 4 | post_id INTEGER NOT NULL REFERENCES posts (id), 5 | body TEXT NOT NULL, 6 | created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 7 | updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP 8 | ); 9 | 10 | SELECT diesel_manage_updated_at('comments'); 11 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_except_tables/sqlite/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `users2` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | users2 (id) { 6 | /// The `id` column of the `users2` table. 7 | /// 8 | /// Its SQL type is `Nullable`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Nullable, 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_only_tables/sqlite/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `users1` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | users1 (id) { 6 | /// The `id` column of the `users1` table. 7 | /// 8 | /// Its SQL type is `Nullable`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Nullable, 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /migrations/postgresql/20151219180527_create_users_and_posts_and_comments/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users ( 2 | id SERIAL PRIMARY KEY, 3 | name VARCHAR NOT NULL, 4 | hair_color VARCHAR 5 | ); 6 | 7 | CREATE TABLE posts ( 8 | id SERIAL PRIMARY KEY, 9 | user_id INTEGER NOT NULL, 10 | title VARCHAR NOT NULL, 11 | body TEXT 12 | ); 13 | 14 | CREATE TABLE comments ( 15 | id SERIAL PRIMARY KEY, 16 | post_id INTEGER NOT NULL, 17 | text TEXT NOT NULL 18 | ); 19 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/update_requires_left_side_of_eq_to_be_a_column.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | fn main() { 14 | use self::users::dsl::*; 15 | 16 | let foo = "foo".into_sql::(); 17 | let command = update(users).set(foo.eq(name)); 18 | //~^ ERROR Column 19 | } 20 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/update_requires_set.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | 5 | table! { 6 | users { 7 | id -> Integer, 8 | } 9 | } 10 | 11 | fn main() { 12 | let conn = SqliteConnection::establish(":memory:").unwrap(); 13 | update(users::table) 14 | .execute(&conn); 15 | //~^ ERROR diesel::query_builder::update_statement::SetNotCalled: diesel::query_builder::QueryFragment<_> 16 | } 17 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_bad_column_name.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | users { 6 | id -> Integer, 7 | } 8 | } 9 | 10 | #[derive(AsChangeset)] 11 | #[table_name = "users"] 12 | struct UserStruct { 13 | name: String, 14 | #[column_name = "hair_color"] 15 | color_de_pelo: String, 16 | } 17 | 18 | #[derive(AsChangeset)] 19 | #[table_name = "users"] 20 | struct UserTuple(#[column_name = "name"] String); 21 | -------------------------------------------------------------------------------- /diesel_compile_tests/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_compile_tests" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [workspace] 7 | 8 | [dependencies] 9 | diesel = { version = "1.4.0", default-features = false, features = ["extras", "sqlite", "postgres", "mysql", "unstable"] } 10 | compiletest_rs = "=0.3.22" 11 | 12 | [replace] 13 | "diesel:1.4.3" = { path = "../diesel" } 14 | "diesel_derives:1.4.1" = { path = "../diesel_derives" } 15 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/expressions_can_only_be_compared_for_equality_to_expressions_of_same_type.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | fn main() { 14 | use self::users::dsl::*; 15 | 16 | let pred = id.eq("string"); 17 | //~^ ERROR E0277 18 | let pred = id.eq(name); 19 | //~^ ERROR type mismatch 20 | } 21 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/select_requires_column_from_same_table.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | table! { 14 | posts { 15 | id -> Integer, 16 | title -> VarChar, 17 | } 18 | } 19 | 20 | fn main() { 21 | let select_id = users::table.select(posts::id); 22 | //~^ ERROR SelectableExpression 23 | } 24 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/table_column_names.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | table! { 4 | users { 5 | id -> Integer, 6 | users -> Integer, 7 | } 8 | } 9 | // error-pattern: Column `users` cannot be named the same as its table. 10 | // error-pattern: You may use `#[sql_name = "users"]` to reference the table's `users` column. 11 | // error-pattern: Docs available at: `https://docs.diesel.rs/diesel/macro.table.html` 12 | 13 | fn main() {} 14 | -------------------------------------------------------------------------------- /diesel_cli/src/infer_schema_internals/mod.rs: -------------------------------------------------------------------------------- 1 | mod data_structures; 2 | mod foreign_keys; 3 | mod inference; 4 | mod table_data; 5 | 6 | #[cfg(feature = "uses_information_schema")] 7 | mod information_schema; 8 | #[cfg(feature = "mysql")] 9 | mod mysql; 10 | #[cfg(feature = "postgres")] 11 | mod pg; 12 | #[cfg(feature = "sqlite")] 13 | mod sqlite; 14 | 15 | pub use self::data_structures::*; 16 | pub use self::foreign_keys::*; 17 | pub use self::inference::*; 18 | pub use self::table_data::*; 19 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/ordering_functions_require_ord.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::dsl::{max, min}; 6 | 7 | table! { 8 | stuff (b) { 9 | b -> Bool, 10 | } 11 | } 12 | 13 | fn main() { 14 | let source = stuff::table.select(max(stuff::b)); 15 | //~^ ERROR SqlOrd 16 | //~| ERROR E0277 17 | let source = stuff::table.select(min(stuff::b)); 18 | //~^ ERROR SqlOrd 19 | //~| ERROR E0277 20 | } 21 | -------------------------------------------------------------------------------- /diesel/src/pg/upsert/mod.rs: -------------------------------------------------------------------------------- 1 | //! Types and functions related to PG's `ON CONFLICT` clause 2 | //! 3 | //! See [the methods on `InsertStatement`](../../query_builder/struct.InsertStatement.html#impl-1) 4 | //! for usage examples. 5 | 6 | mod on_conflict_actions; 7 | mod on_conflict_clause; 8 | mod on_conflict_extension; 9 | mod on_conflict_target; 10 | 11 | pub use self::on_conflict_actions::excluded; 12 | pub use self::on_conflict_extension::*; 13 | pub use self::on_conflict_target::on_constraint; 14 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/select_for_update_cannot_be_used_on_sqlite.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::sqlite::SqliteConnection; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | } 11 | } 12 | 13 | fn main() { 14 | let conn = SqliteConnection::establish("").unwrap(); 15 | users::table 16 | .for_update() 17 | .load(&conn) 18 | //~^ ERROR: E0277 19 | //~| ERROR: E0277 20 | .unwrap(); 21 | } 22 | -------------------------------------------------------------------------------- /diesel_derives/src/resolved_at_shim.rs: -------------------------------------------------------------------------------- 1 | use proc_macro2::Span; 2 | 3 | pub trait ResolvedAtExt { 4 | fn resolved_at(self, span: Span) -> Span; 5 | } 6 | 7 | #[cfg(feature = "nightly")] 8 | impl ResolvedAtExt for Span { 9 | fn resolved_at(self, span: Span) -> Span { 10 | self.unstable().resolved_at(span.unstable()).into() 11 | } 12 | } 13 | 14 | #[cfg(not(feature = "nightly"))] 15 | impl ResolvedAtExt for Span { 16 | fn resolved_at(self, _: Span) -> Span { 17 | self 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_deprecated_column_name.stderr: -------------------------------------------------------------------------------- 1 | warning: The form `table_name(value)` is deprecated. Use `table_name = "value"` instead 2 | --> $DIR/as_changeset_deprecated_column_name.rs:12:3 3 | | 4 | 12 | #[table_name(users)] 5 | | ^^^^^^^^^^^^^^^^^ 6 | 7 | warning: The form `column_name(value)` is deprecated. Use `column_name = "value"` instead 8 | --> $DIR/as_changeset_deprecated_column_name.rs:15:7 9 | | 10 | 15 | #[column_name(name)] 11 | | ^^^^^^^^^^^^^^^^^ 12 | 13 | -------------------------------------------------------------------------------- /bin/bench: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | 4 | if [ "$1" = "sqlite" ]; then 5 | (cd diesel_tests && DATABASE_URL=/tmp/test.db cargo bench --no-default-features --features "unstable sqlite") 6 | elif [ "$1" = "postgres" ]; then 7 | (cd diesel_tests && cargo bench --no-default-features --features "unstable postgres") 8 | else 9 | (cd diesel_tests && DATABASE_URL=/tmp/test.db cargo bench --no-default-features --features "unstable sqlite") 10 | (cd diesel_tests && cargo bench --no-default-features --features "unstable postgres") 11 | fi 12 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/insert_requires_value_of_same_type_as_column.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::pg::PgConnection; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | name -> Text, 11 | hair_color -> Text, 12 | } 13 | } 14 | 15 | fn main() { 16 | use users::dsl::*; 17 | let conn = PgConnection::establish("").unwrap(); 18 | 19 | insert_into(users) 20 | .values(&name.eq(1)); 21 | //~^ ERROR E0277 22 | } 23 | -------------------------------------------------------------------------------- /migrations/sqlite/20151219180527_create_users_and_posts_and_comments/up.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE users ( 2 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 3 | name VARCHAR NOT NULL, 4 | hair_color VARCHAR 5 | ); 6 | 7 | CREATE TABLE posts ( 8 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 9 | user_id INTEGER NOT NULL, 10 | title VARCHAR NOT NULL, 11 | body TEXT 12 | ); 13 | 14 | CREATE TABLE comments ( 15 | id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 16 | post_id INTEGER NOT NULL, 17 | text TEXT NOT NULL 18 | ); 19 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | 8 | [*] 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | indent_style = space 14 | indent_size = 4 15 | 16 | [*.rs] 17 | indent_style = space 18 | indent_size = 4 19 | 20 | [*.toml] 21 | indent_style = space 22 | indent_size = 4 23 | 24 | [*.md] 25 | trim_trailing_whitespace = false 26 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/boxed_queries_require_selectable_expression_for_order.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::pg::Pg; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | name -> VarChar, 11 | } 12 | } 13 | 14 | table! { 15 | posts { 16 | id -> Integer, 17 | title -> VarChar, 18 | } 19 | } 20 | 21 | fn main() { 22 | users::table.into_boxed::().order(posts::title.desc()); 23 | //~^ ERROR AppearsInFromClause 24 | } 25 | -------------------------------------------------------------------------------- /diesel/README.md: -------------------------------------------------------------------------------- 1 | [![Diesel](https://diesel.rs/assets/images/diesel_logo_stacked_black.png)](https://diesel.rs) 2 | 3 | # Diesel - A safe, extensible ORM and Query Builder for Rust 4 | 5 | Diesel is the most productive way to interact with databases in Rust because of its safe and composable abstractions over queries. 6 | 7 | ## Getting Started 8 | 9 | This is the Readme of the main crate. You can find [an extensive Getting Started tutorial](https://diesel.rs/guides/getting-started) and more information on our [website](https://diesel.rs). 10 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_on_non_struct.stderr: -------------------------------------------------------------------------------- 1 | error: This derive can only be used on structs 2 | --> $DIR/as_changeset_on_non_struct.rs:12:10 3 | | 4 | 12 | #[derive(AsChangeset)] 5 | | ^^^^^^^^^^^ 6 | 7 | error[E0601]: `main` function not found in crate `as_changeset_on_non_struct` 8 | | 9 | = note: consider adding a `main` function to `$DIR/as_changeset_on_non_struct.rs` 10 | 11 | error: aborting due to 2 previous errors 12 | 13 | For more information about this error, try `rustc --explain E0601`. 14 | -------------------------------------------------------------------------------- /diesel/src/query_dsl/nullable_select_dsl.rs: -------------------------------------------------------------------------------- 1 | /// The `nullable` method 2 | /// 3 | /// This trait should not be relied on directly by most apps. Its behavior is 4 | /// provided by [`QueryDsl`]. However you may need a where clause on this trait 5 | /// to call `nullable` from generic code. 6 | /// 7 | /// [`QueryDsl`]: ../trait.QueryDsl.html#method.nullable 8 | pub trait SelectNullableDsl { 9 | /// The return type of `nullable` 10 | type Output; 11 | 12 | /// See the trait documentation 13 | fn nullable(self) -> Self::Output; 14 | } 15 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/boxed_queries_require_selectable_expression_for_filter.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::pg::Pg; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | name -> VarChar, 11 | } 12 | } 13 | 14 | table! { 15 | posts { 16 | id -> Integer, 17 | title -> VarChar, 18 | } 19 | } 20 | 21 | fn main() { 22 | users::table.into_boxed::().filter(posts::title.eq("Hello")); 23 | //~^ ERROR AppearsInFromClause 24 | } 25 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/select_for_update_no_wait_cannot_be_used_on_sqlite.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::sqlite::SqliteConnection; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | } 11 | } 12 | 13 | fn main() { 14 | let conn = SqliteConnection::establish("").unwrap(); 15 | users::table 16 | .for_update() 17 | .no_wait() 18 | .load(&conn) 19 | //~^ ERROR: E0277 20 | //~| ERROR: E0277 21 | .unwrap(); 22 | } 23 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/sqlite_upsert_cannot_be_used_on_pg.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | 5 | table! { 6 | users { 7 | id -> Integer, 8 | } 9 | } 10 | 11 | #[derive(Insertable)] 12 | #[table_name="users"] 13 | struct User { 14 | id: i32, 15 | } 16 | 17 | fn main() { 18 | let connection = PgConnection::establish("").unwrap(); 19 | replace_into(users::table) 20 | .values(&User { id: 1 }) 21 | .execute(&connection); 22 | //~^ ERROR E0277 23 | } 24 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_1/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | extern crate dotenv; 4 | 5 | pub mod models; 6 | pub mod schema; 7 | 8 | use diesel::prelude::*; 9 | use dotenv::dotenv; 10 | use std::env; 11 | 12 | pub fn establish_connection() -> MysqlConnection { 13 | dotenv().ok(); 14 | 15 | let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); 16 | MysqlConnection::establish(&database_url) 17 | .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) 18 | } 19 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/array_only_usable_with_pg.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::dsl::*; 6 | 7 | fn main() { 8 | let connection = SqliteConnection::establish("").unwrap(); 9 | select(array((1,))).get_result::>(&connection); 10 | //~^ ERROR E0271 11 | //~| ERROR E0277 12 | 13 | let connection = MysqlConnection::establish("").unwrap(); 14 | select(array((1,))).get_result::>(&connection); 15 | //~^ ERROR E0271 16 | //~| ERROR E0277 17 | } 18 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_1/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | extern crate dotenv; 4 | 5 | pub mod models; 6 | pub mod schema; 7 | 8 | use diesel::prelude::*; 9 | use dotenv::dotenv; 10 | use std::env; 11 | 12 | pub fn establish_connection() -> SqliteConnection { 13 | dotenv().ok(); 14 | 15 | let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); 16 | SqliteConnection::establish(&database_url) 17 | .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) 18 | } 19 | -------------------------------------------------------------------------------- /diesel/src/sqlite/mod.rs: -------------------------------------------------------------------------------- 1 | //! Provides types and functions related to working with SQLite 2 | //! 3 | //! Much of this module is re-exported from database agnostic locations. 4 | //! However, if you are writing code specifically to extend Diesel on 5 | //! SQLite, you may need to work with this module directly. 6 | 7 | mod backend; 8 | mod connection; 9 | mod types; 10 | 11 | pub mod query_builder; 12 | 13 | pub use self::backend::{Sqlite, SqliteType}; 14 | pub use self::connection::SqliteConnection; 15 | pub use self::query_builder::SqliteQueryBuilder; 16 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/select_for_update_skip_locked_cannot_be_used_on_sqlite.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::sqlite::SqliteConnection; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | } 11 | } 12 | 13 | fn main() { 14 | let conn = SqliteConnection::establish("").unwrap(); 15 | users::table 16 | .for_update() 17 | .skip_locked() 18 | .load(&conn) 19 | //~^ ERROR: E0277 20 | //~| ERROR: E0277 21 | .unwrap(); 22 | } 23 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/select_sql_still_ensures_result_type.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::dsl::sql; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | name -> VarChar, 11 | } 12 | } 13 | 14 | fn main() { 15 | let connection = PgConnection::establish("").unwrap(); 16 | let select_count = users::table.select(sql::("COUNT(*)")); 17 | let count = select_count.get_result::(&connection).unwrap(); 18 | //~^ ERROR E0277 19 | } 20 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "advanced-blog-cli" 3 | version = "0.1.0" 4 | authors = ["Sean Griffin "] 5 | 6 | [dependencies] 7 | bcrypt = "0.1.0" 8 | chrono = "0.4.0" 9 | diesel = { version = "1.4.0", features = ["postgres", "chrono"] } 10 | dotenv = "0.10.0" 11 | structopt = "0.1.6" 12 | structopt-derive = "0.1.6" 13 | tempfile = "2.2.0" 14 | 15 | [dev-dependencies] 16 | assert_matches = "1.1" 17 | diesel_migrations = { version = "1.4.0", features = ["postgres"] } 18 | lazy_static = "1.0" 19 | -------------------------------------------------------------------------------- /diesel_migrations/migrations_internals/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "migrations_internals" 3 | version = "1.4.0" 4 | authors = ["Sean Griffin "] 5 | license = "MIT OR Apache-2.0" 6 | description = "Internal implementation of diesels migration mechanism" 7 | homepage = "https://diesel.rs" 8 | 9 | [dependencies] 10 | diesel = { version = "~1.4.0", default-features = false } 11 | barrel = { version = ">= 0.5.0", optional = true, features = ["diesel"] } 12 | 13 | [dev-dependencies] 14 | tempfile = "3.0.0" 15 | 16 | [features] 17 | default = [] 18 | -------------------------------------------------------------------------------- /diesel/src/sqlite/types/numeric.rs: -------------------------------------------------------------------------------- 1 | #![cfg(feature = "bigdecimal")] 2 | 3 | extern crate bigdecimal; 4 | 5 | use self::bigdecimal::BigDecimal; 6 | 7 | use crate::deserialize::{self, FromSql}; 8 | use crate::sql_types::{Double, Numeric}; 9 | use crate::sqlite::connection::SqliteValue; 10 | use crate::sqlite::Sqlite; 11 | 12 | impl FromSql for BigDecimal { 13 | fn from_sql(bytes: Option<&SqliteValue>) -> deserialize::Result { 14 | let data = >::from_sql(bytes)?; 15 | Ok(data.into()) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_1/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | extern crate dotenv; 4 | 5 | pub mod models; 6 | pub mod schema; 7 | 8 | use diesel::pg::PgConnection; 9 | use diesel::prelude::*; 10 | use dotenv::dotenv; 11 | use std::env; 12 | 13 | pub fn establish_connection() -> PgConnection { 14 | dotenv().ok(); 15 | 16 | let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); 17 | PgConnection::establish(&database_url) 18 | .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) 19 | } 20 | -------------------------------------------------------------------------------- /diesel_cli/tests/tests.rs: -------------------------------------------------------------------------------- 1 | #![deny(warnings)] 2 | 3 | extern crate chrono; 4 | extern crate diesel; 5 | #[macro_use] 6 | extern crate difference; 7 | extern crate migrations_internals; 8 | extern crate regex; 9 | extern crate tempfile; 10 | 11 | mod completion_generation; 12 | mod database_drop; 13 | mod database_reset; 14 | mod database_setup; 15 | mod database_url_errors; 16 | mod exit_codes; 17 | mod migration_generate; 18 | mod migration_list; 19 | mod migration_redo; 20 | mod migration_revert; 21 | mod migration_run; 22 | mod print_schema; 23 | mod setup; 24 | mod support; 25 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_specifying_schema_name/postgres/expected.rs: -------------------------------------------------------------------------------- 1 | pub mod custom_schema { 2 | table! { 3 | /// Representation of the `custom_schema.in_schema` table. 4 | /// 5 | /// (Automatically generated by Diesel.) 6 | custom_schema.in_schema (id) { 7 | /// The `id` column of the `custom_schema.in_schema` table. 8 | /// 9 | /// Its SQL type is `Int4`. 10 | /// 11 | /// (Automatically generated by Diesel.) 12 | id -> Int4, 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_specifying_schema_name_with_foreign_keys/postgres/schema.sql: -------------------------------------------------------------------------------- 1 | CREATE SCHEMA custom_schema; 2 | CREATE TABLE custom_schema.a( 3 | id serial NOT NULL, 4 | CONSTRAINT a_pkey PRIMARY KEY (id) 5 | 6 | ); 7 | CREATE TABLE custom_schema.b( 8 | id serial NOT NULL, 9 | parent serial NOT NULL, 10 | CONSTRAINT b_pkey PRIMARY KEY (id) 11 | 12 | ); 13 | ALTER TABLE custom_schema.b ADD CONSTRAINT ab_fkey FOREIGN KEY (parent) 14 | REFERENCES custom_schema.a (id) MATCH FULL 15 | ON DELETE NO ACTION ON UPDATE NO ACTION; 16 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/sqlite_insert_or_ignore_cannot_be_used_on_pg.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | 5 | table! { 6 | users { 7 | id -> Integer, 8 | } 9 | } 10 | 11 | #[derive(Insertable)] 12 | #[table_name="users"] 13 | struct User { 14 | id: i32, 15 | } 16 | 17 | fn main() { 18 | let connection = PgConnection::establish("").unwrap(); 19 | insert_or_ignore_into(users::table) 20 | .values(users::id.eq(1)) 21 | .execute(&connection) 22 | //~^ ERROR E0277 23 | .unwrap(); 24 | } 25 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/columns_cannot_be_rhs_of_insert.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::pg::PgConnection; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | name -> Text, 11 | hair_color -> Text, 12 | } 13 | } 14 | 15 | fn main() { 16 | use users::dsl::*; 17 | let conn = PgConnection::establish("").unwrap(); 18 | 19 | insert_into(users) 20 | .values(&name.eq(hair_color)) 21 | .execute(&conn) 22 | //~^ ERROR ColumnInsertValue 23 | .unwrap(); 24 | } 25 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/filter_requires_bool_nonaggregate_expression.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | fn main() { 14 | use diesel::dsl::sum; 15 | 16 | let _ = users::table.filter(users::name); 17 | //~^ ERROR type mismatch resolving `::SqlType == diesel::sql_types::Bool` 18 | let _ = users::table.filter(sum(users::id).eq(1)); 19 | //~^ ERROR NonAggregate 20 | } 21 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/subselect_requires_correct_type.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | 5 | table!{ 6 | users{ 7 | id -> Integer, 8 | name -> Text, 9 | } 10 | } 11 | 12 | table! { 13 | posts { 14 | id -> Integer, 15 | user_id -> Integer, 16 | } 17 | } 18 | 19 | fn main() { 20 | let conn = PgConnection::establish("").unwrap(); 21 | let subquery = users::table.filter(users::id.eq(1)); 22 | let query = posts::table.filter(posts::user_id.eq_any(subquery)); 23 | //~^ ERROR E0277 24 | } 25 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/belongs_to_incorrect_lifetime_syntax.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | table! { 4 | foo { 5 | id -> Integer, 6 | } 7 | } 8 | 9 | table! { 10 | bars { 11 | id -> Integer, 12 | foo_id -> Integer, 13 | } 14 | } 15 | 16 | #[derive(Identifiable)] 17 | #[table_name = "foo"] 18 | struct Foo<'a> { 19 | id: i32, 20 | _marker: ::std::marker::PhantomData<&'a ()>, 21 | } 22 | 23 | 24 | #[derive(Associations)] 25 | #[belongs_to(parent = "Foo<'a>")] 26 | struct Bar { 27 | foo_id: i32, 28 | } 29 | 30 | fn main() {} 31 | -------------------------------------------------------------------------------- /diesel_tests/tests/backend_specifics.rs: -------------------------------------------------------------------------------- 1 | use super::*; 2 | 3 | #[derive(PartialEq, Eq, Debug, Clone, Queryable, Identifiable, Associations)] 4 | #[belongs_to(User)] 5 | pub struct Post { 6 | pub id: i32, 7 | pub user_id: i32, 8 | pub title: String, 9 | pub body: Option, 10 | } 11 | 12 | impl Post { 13 | pub fn new(id: i32, user_id: i32, title: &str, body: Option<&str>) -> Self { 14 | Post { 15 | id: id, 16 | user_id: user_id, 17 | title: title.to_string(), 18 | body: body.map(|s| s.to_string()), 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /diesel/src/pg/expression/operators.rs: -------------------------------------------------------------------------------- 1 | use crate::pg::Pg; 2 | 3 | infix_operator!(IsDistinctFrom, " IS DISTINCT FROM ", backend: Pg); 4 | infix_operator!(IsNotDistinctFrom, " IS NOT DISTINCT FROM ", backend: Pg); 5 | infix_operator!(OverlapsWith, " && ", backend: Pg); 6 | infix_operator!(Contains, " @> ", backend: Pg); 7 | infix_operator!(IsContainedBy, " <@ ", backend: Pg); 8 | infix_operator!(ILike, " ILIKE ", backend: Pg); 9 | infix_operator!(NotILike, " NOT ILIKE ", backend: Pg); 10 | postfix_operator!(NullsFirst, " NULLS FIRST", (), backend: Pg); 11 | postfix_operator!(NullsLast, " NULLS LAST", (), backend: Pg); 12 | -------------------------------------------------------------------------------- /diesel/src/query_builder/order_clause.rs: -------------------------------------------------------------------------------- 1 | simple_clause!(NoOrderClause, OrderClause, " ORDER BY "); 2 | 3 | impl<'a, DB, Expr> Into + 'a>>> for OrderClause 4 | where 5 | DB: Backend, 6 | Expr: QueryFragment + 'a, 7 | { 8 | fn into(self) -> Option + 'a>> { 9 | Some(Box::new(self.0)) 10 | } 11 | } 12 | 13 | impl<'a, DB> Into + 'a>>> for NoOrderClause 14 | where 15 | DB: Backend, 16 | { 17 | fn into(self) -> Option + 'a>> { 18 | None 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /diesel/src/mysql/mod.rs: -------------------------------------------------------------------------------- 1 | //! Provides types and functions related to working with MySQL 2 | //! 3 | //! Much of this module is re-exported from database agnostic locations. 4 | //! However, if you are writing code specifically to extend Diesel on 5 | //! MySQL, you may need to work with this module directly. 6 | 7 | mod backend; 8 | mod connection; 9 | mod value; 10 | 11 | mod query_builder; 12 | pub mod types; 13 | 14 | pub use self::backend::{Mysql, MysqlType, MysqlTypeMetadata}; 15 | pub use self::connection::MysqlConnection; 16 | pub use self::query_builder::MysqlQueryBuilder; 17 | pub use self::value::MysqlValue; 18 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/sql_type_bad_options.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | #[derive(SqlType)] 5 | #[postgres] 6 | struct Type1; 7 | 8 | #[derive(SqlType)] 9 | #[postgres(type_name = "foo", oid = "2", array_oid = "3")] 10 | struct Type2; 11 | 12 | #[derive(SqlType)] 13 | #[postgres(oid = "2")] 14 | struct Type3; 15 | 16 | #[derive(SqlType)] 17 | #[postgres(oid = "NaN", array_oid = "1")] 18 | struct Type4; 19 | 20 | #[derive(SqlType)] 21 | #[postgres(oid = "NaN", ary_oid = "1")] 22 | struct Type5; 23 | 24 | #[derive(SqlType)] 25 | #[postgres = "foo"] 26 | struct Type6; 27 | 28 | fn main() {} 29 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/update_requires_column_be_from_same_table.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | table! { 14 | posts { 15 | id -> Integer, 16 | title -> VarChar, 17 | } 18 | } 19 | 20 | fn main() { 21 | use self::users::dsl::*; 22 | 23 | let command = update(users).set(posts::title.eq("Hello")); 24 | //~^ ERROR type mismatch 25 | let command = update(users).set(name.eq(posts::title)); 26 | //~^ ERROR E0277 27 | } 28 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/belongs_to_second_parent.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | bar (id) { 6 | id -> Integer, 7 | } 8 | } 9 | 10 | table! { 11 | foo (bar_id) { 12 | bar_id -> Integer, 13 | } 14 | } 15 | 16 | #[derive(Identifiable)] 17 | #[table_name = "bar"] 18 | struct Bar { 19 | id: i32, 20 | } 21 | 22 | #[derive(Identifiable)] 23 | #[table_name = "bar"] 24 | struct Baz { 25 | id: i32, 26 | } 27 | 28 | #[derive(Associations)] 29 | #[belongs_to(Bar, Baz)] 30 | #[table_name = "foo"] 31 | struct Foo { 32 | bar_id: i32, 33 | } 34 | 35 | fn main() {} 36 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/insertable_missing_table_or_column.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | users { 6 | id -> Integer, 7 | } 8 | } 9 | 10 | #[derive(Insertable)] 11 | struct Post { 12 | id: i32, 13 | } 14 | 15 | #[derive(Insertable)] 16 | #[table_name = "posts"] 17 | struct Post2 { 18 | id: i32, 19 | } 20 | 21 | #[derive(Insertable)] 22 | #[table_name = "users"] 23 | struct User1 { 24 | name: String 25 | } 26 | 27 | #[derive(Insertable)] 28 | #[table_name = "users"] 29 | struct User2 { 30 | #[column_name = "name"] 31 | name: String 32 | } 33 | 34 | fn main() {} 35 | -------------------------------------------------------------------------------- /diesel_migrations/migrations_macros/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "migrations_macros" 3 | version = "1.4.1" 4 | authors = ["Sean Griffin "] 5 | license = "MIT OR Apache-2.0" 6 | description = "Codegeneration macros for diesels embedded migrations" 7 | documentation = "https://docs.diesel.rs" 8 | homepage = "https://diesel.rs" 9 | 10 | [dependencies] 11 | migrations_internals = "~1.4.0" 12 | syn = { version = "1", features = ["extra-traits"] } 13 | quote = "1" 14 | proc-macro2 = "1" 15 | 16 | [dev-dependencies] 17 | tempfile = "3.0.0" 18 | 19 | [lib] 20 | proc-macro = true 21 | 22 | [features] 23 | default = [] 24 | -------------------------------------------------------------------------------- /diesel/src/mysql/value.rs: -------------------------------------------------------------------------------- 1 | use super::Mysql; 2 | use crate::backend::BinaryRawValue; 3 | 4 | /// Raw mysql value as received from the database 5 | #[derive(Copy, Clone, Debug)] 6 | pub struct MysqlValue<'a> { 7 | raw: &'a [u8], 8 | } 9 | 10 | impl<'a> MysqlValue<'a> { 11 | pub(crate) fn new(raw: &'a [u8]) -> Self { 12 | Self { raw } 13 | } 14 | 15 | /// Get the underlying raw byte representation 16 | pub fn as_bytes(&self) -> &[u8] { 17 | self.raw 18 | } 19 | } 20 | 21 | impl<'a> BinaryRawValue<'a> for Mysql { 22 | fn as_bytes(value: Self::RawValue) -> &'a [u8] { 23 | value.raw 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_compound_primary_key/postgres/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `asd` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | asd (id, qsd) { 6 | /// The `id` column of the `asd` table. 7 | /// 8 | /// Its SQL type is `Int4`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Int4, 12 | /// The `qsd` column of the `asd` table. 13 | /// 14 | /// Its SQL type is `Int4`. 15 | /// 16 | /// (Automatically generated by Diesel.) 17 | qsd -> Int4, 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_type_renaming/postgres/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `users` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | users (id) { 6 | /// The `id` column of the `users` table. 7 | /// 8 | /// Its SQL type is `Int4`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Int4, 12 | /// The `job` column of the `users` table. 13 | /// 14 | /// Its SQL type is `UserJob`. 15 | /// 16 | /// (Automatically generated by Diesel.) 17 | job -> UserJob, 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_compound_primary_key/mysql/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `asd` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | asd (id, qsd) { 6 | /// The `id` column of the `asd` table. 7 | /// 8 | /// Its SQL type is `Integer`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Integer, 12 | /// The `qsd` column of the `asd` table. 13 | /// 14 | /// Its SQL type is `Integer`. 15 | /// 16 | /// (Automatically generated by Diesel.) 17 | qsd -> Integer, 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/cannot_update_target_with_methods_other_than_filter_called.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | fn main() { 14 | use self::users::dsl::*; 15 | 16 | let command = update(users.select(id)).set(id.eq(1)); 17 | //~^ ERROR E0277 18 | //~| NOTE 19 | //~| NOTE IntoUpdateTarget 20 | //~| NOTE 21 | let command = update(users.order(id)).set(id.eq(1)); 22 | //~^ ERROR E0277 23 | //~| NOTE 24 | //~| NOTE IntoUpdateTarget 25 | //~| NOTE 26 | } 27 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/README.md: -------------------------------------------------------------------------------- 1 | # `rs-diesel-sqlite` 2 | 3 | Diesel's `Getting Started` guide using SQLite instead of Postgresql 4 | 5 | ## Usage 6 | 7 | ``` 8 | $ echo "DATABASE_URL=file:test.db" > .env 9 | $ diesel migration run 10 | 11 | $ cargo run --bin show_posts 12 | 13 | $ cargo run --bin write_post 14 | # write your post 15 | 16 | $ cargo run --bin publish_post 1 17 | 18 | $ cargo run --bin show_posts 19 | # your post will be printed here 20 | 21 | # Delete post with given title 22 | $ cargo run --bin delete_post "title of post to delete" 23 | 24 | $ cargo run --bin show_posts 25 | # observe that no posts are shown 26 | ``` 27 | -------------------------------------------------------------------------------- /diesel_migrations/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_migrations" 3 | version = "1.4.0" 4 | authors = ["Sean Griffin "] 5 | license = "MIT OR Apache-2.0" 6 | description = "Migration management for diesel" 7 | documentation = "https://docs.rs/crate/diesel_migrations" 8 | homepage = "https://diesel.rs" 9 | 10 | 11 | [dependencies] 12 | migrations_internals = "~1.4.0" 13 | migrations_macros = "~1.4.0" 14 | 15 | [dev-dependencies] 16 | diesel = { version = "1.4.0", default-features = false } 17 | dotenv = ">=0.8, <0.11" 18 | cfg-if = "0.1.0" 19 | 20 | [features] 21 | default = [] 22 | sqlite = [] 23 | postgres = [] 24 | mysql = [] 25 | -------------------------------------------------------------------------------- /diesel_tests/tests/deserialization.rs: -------------------------------------------------------------------------------- 1 | use crate::schema::*; 2 | use diesel::*; 3 | use std::borrow::Cow; 4 | 5 | #[derive(Queryable, PartialEq, Debug)] 6 | struct CowUser<'a> { 7 | id: i32, 8 | name: Cow<'a, str>, 9 | } 10 | 11 | #[test] 12 | fn generated_queryable_allows_lifetimes() { 13 | use crate::schema::users::dsl::*; 14 | let connection = connection_with_sean_and_tess_in_users_table(); 15 | 16 | let expected_user = CowUser { 17 | id: 1, 18 | name: Cow::Owned("Sean".to_string()), 19 | }; 20 | assert_eq!( 21 | Ok(expected_user), 22 | users.select((id, name)).first(&connection) 23 | ); 24 | } 25 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_missing_table_import.stderr: -------------------------------------------------------------------------------- 1 | error[E0433]: failed to resolve: use of undeclared type or module `users` 2 | --> $DIR/as_changeset_missing_table_import.rs:5:8 3 | | 4 | 5 | struct User { 5 | | ^^^^ use of undeclared type or module `users` 6 | 7 | error[E0433]: failed to resolve: use of undeclared type or module `users` 8 | --> $DIR/as_changeset_missing_table_import.rs:11:16 9 | | 10 | 11 | #[table_name = "users"] 11 | | ^^^^^^^ use of undeclared type or module `users` 12 | 13 | error: aborting due to 2 previous errors 14 | 15 | For more information about this error, try `rustc --explain E0433`. 16 | -------------------------------------------------------------------------------- /migrations/mysql/20170804172328_add_foreign_keys/up.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE posts ADD CONSTRAINT posts_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id); 2 | ALTER TABLE comments ADD CONSTRAINT comments_post_id_fkey FOREIGN KEY (post_id) REFERENCES posts (id); 3 | ALTER TABLE followings ADD CONSTRAINT followings_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id); 4 | ALTER TABLE followings ADD CONSTRAINT followings_post_id_fkey FOREIGN KEY (post_id) REFERENCES posts (id); 5 | ALTER TABLE likes ADD CONSTRAINT likes_comment_id_fkey FOREIGN KEY (comment_id) REFERENCES comments (id); 6 | ALTER TABLE likes ADD CONSTRAINT likes_user_id_fkey FOREIGN KEY (user_id) REFERENCES users (id); 7 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/src/bin/delete_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_pg; 3 | 4 | use diesel::prelude::*; 5 | use diesel_demo_step_3_pg::*; 6 | use std::env::args; 7 | 8 | fn main() { 9 | use self::schema::posts::dsl::*; 10 | 11 | let target = args().nth(1).expect("Expected a target to match against"); 12 | let pattern = format!("%{}%", target); 13 | 14 | let connection = establish_connection(); 15 | let num_deleted = diesel::delete(posts.filter(title.like(pattern))) 16 | .execute(&connection) 17 | .expect("Error deleting posts"); 18 | 19 | println!("Deleted {} posts", num_deleted); 20 | } 21 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/cannot_use_expression_methods_on_tuples.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::prelude::*; 4 | 5 | table! { 6 | users { 7 | id -> Integer, 8 | name -> Text, 9 | } 10 | } 11 | 12 | fn main() { 13 | use self::users::dsl::*; 14 | // Sanity check that expression methods are in scope 15 | users.filter(id.is_not_null()); 16 | users.filter(id.eq_any(users.select(id))); 17 | 18 | users.filter((id, name).is_not_null()); 19 | //~^ ERROR no method named `is_not_null` found 20 | users.filter((id, name).eq_any(users.find(1))); 21 | //~^ ERROR no method named `eq_any` found 22 | } 23 | -------------------------------------------------------------------------------- /diesel_tests/tests/postgres_specific_schema.rs: -------------------------------------------------------------------------------- 1 | use super::{posts, User}; 2 | 3 | #[derive(PartialEq, Eq, Debug, Clone, Queryable, Identifiable, Associations)] 4 | #[belongs_to(User)] 5 | pub struct Post { 6 | pub id: i32, 7 | pub user_id: i32, 8 | pub title: String, 9 | pub body: Option, 10 | pub tags: Vec, 11 | } 12 | 13 | impl Post { 14 | pub fn new(id: i32, user_id: i32, title: &str, body: Option<&str>) -> Self { 15 | Post { 16 | id: id, 17 | user_id: user_id, 18 | title: title.to_string(), 19 | body: body.map(|s| s.to_string()), 20 | tags: Vec::new(), 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/src/bin/delete_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_mysql; 3 | 4 | use diesel::prelude::*; 5 | use diesel_demo_step_3_mysql::*; 6 | use std::env::args; 7 | 8 | fn main() { 9 | use self::schema::posts::dsl::*; 10 | 11 | let target = args().nth(1).expect("Expected a target to match against"); 12 | let pattern = format!("%{}%", target); 13 | 14 | let connection = establish_connection(); 15 | 16 | let num_deleted = diesel::delete(posts.filter(title.like(pattern))) 17 | .execute(&connection) 18 | .expect("Error deleting posts"); 19 | 20 | println!("Deleted {} posts", num_deleted); 21 | } 22 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_compound_primary_key/sqlite/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `asd` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | asd (id, qsd) { 6 | /// The `id` column of the `asd` table. 7 | /// 8 | /// Its SQL type is `Nullable`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Nullable, 12 | /// The `qsd` column of the `asd` table. 13 | /// 14 | /// Its SQL type is `Nullable`. 15 | /// 16 | /// (Automatically generated by Diesel.) 17 | qsd -> Nullable, 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/array_expressions_must_be_correct_type.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::dsl::*; 6 | 7 | fn main() { 8 | let connection = PgConnection::establish("").unwrap(); 9 | select(array((1, 3))).get_result::>(&connection); 10 | select(array((1f64, 3f64))).get_result::>(&connection); 11 | //~^ ERROR E0277 12 | //~| ERROR E0277 13 | //~| ERROR E0277 14 | //~| ERROR E0277 15 | //~| ERROR E0277 16 | //~| ERROR E0277 17 | //~| ERROR E0277 18 | //~| ERROR E0277 19 | //~| ERROR E0277 20 | select(array((1f64, 3f64))).get_result::>(&connection); 21 | } 22 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/exists_can_only_take_subselects.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | use diesel::dsl::exists; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | } 10 | } 11 | 12 | table! { 13 | posts { 14 | id -> Integer, 15 | } 16 | } 17 | 18 | fn main() { 19 | let conn = SqliteConnection::establish(":memory:").unwrap(); 20 | // Sanity check, no error 21 | users::table.filter(exists(posts::table.select(posts::id))).execute(&conn).unwrap(); 22 | 23 | users::table.filter(exists(true)); 24 | //~^ ERROR SelectQuery 25 | users::table.filter(exists(users::id)); 26 | //~^ ERROR SelectQuery 27 | } 28 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/delete_statement_does_not_support_returning_methods_on_sqlite.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | fn main() { 14 | use self::users::dsl::*; 15 | let connection = SqliteConnection::establish(":memory:").unwrap(); 16 | 17 | delete(users.filter(name.eq("Bill"))) 18 | .get_result(&connection); 19 | //~^ ERROR SupportsReturningClause 20 | 21 | delete(users.filter(name.eq("Bill"))) 22 | .returning(name) 23 | .get_result(&connection); 24 | //~^ ERROR SupportsReturningClause 25 | } 26 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | mysql: 4 | image: mysql 5 | container_name: diesel.mysql 6 | volumes: 7 | - "mysql-data:/var/lib/mysql/:delegated" 8 | - "./docker/mysql/init:/docker-entrypoint-initdb.d" 9 | ports: 10 | - "3306:3306" 11 | environment: 12 | - MYSQL_ALLOW_EMPTY_PASSWORD=true 13 | postgres: 14 | image: postgres 15 | container_name: diesel.postgres 16 | volumes: 17 | - "postgres-data:/var/lib/postgres/:delegated" 18 | - "./docker/postgres/init:/docker-entrypoint-initdb.d" 19 | ports: 20 | - "5432:5432" 21 | volumes: 22 | mysql-data: 23 | driver: local 24 | postgres-data: 25 | driver: local 26 | -------------------------------------------------------------------------------- /migrations/postgresql/20170804172328_add_foreign_keys/up.sql: -------------------------------------------------------------------------------- 1 | ALTER TABLE posts ADD CONSTRAINT posts_user_id_fkey FOREIGN KEY (user_id) REFERENCES users DEFERRABLE; 2 | ALTER TABLE comments ADD CONSTRAINT comments_post_id_fkey FOREIGN KEY (post_id) REFERENCES posts DEFERRABLE; 3 | ALTER TABLE followings ADD CONSTRAINT followings_user_id_fkey FOREIGN KEY (user_id) REFERENCES users DEFERRABLE; 4 | ALTER TABLE followings ADD CONSTRAINT followings_post_id_fkey FOREIGN KEY (post_id) REFERENCES posts DEFERRABLE; 5 | ALTER TABLE likes ADD CONSTRAINT likes_comment_id_fkey FOREIGN KEY (comment_id) REFERENCES comments DEFERRABLE; 6 | ALTER TABLE likes ADD CONSTRAINT likes_user_id_fkey FOREIGN KEY (user_id) REFERENCES users DEFERRABLE; 7 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/distinct_on_clause_only_supported_for_pg.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | fn main() { 14 | use self::users::dsl::*; 15 | let sqlite_connection = SqliteConnection::establish(":memory:").unwrap(); 16 | 17 | users.distinct_on(name).get_results(&sqlite_connection); 18 | //~^ ERROR Backend == diesel::pg::Pg 19 | 20 | let mysql_connection = MysqlConnection::establish("mysql://foo").unwrap(); 21 | 22 | users.distinct_on(name).get_results(&mysql_connection); 23 | //~^ ERROR Backend == diesel::pg::Pg 24 | } 25 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/src/bin/delete_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_sqlite; 3 | 4 | use self::diesel::prelude::*; 5 | use self::diesel_demo_step_3_sqlite::*; 6 | use std::env::args; 7 | 8 | fn main() { 9 | use diesel_demo_step_3_sqlite::schema::posts::dsl::*; 10 | 11 | let target = args().nth(1).expect("Expected a target to match against"); 12 | let pattern = format!("%{}%", target); 13 | 14 | let connection = establish_connection(); 15 | let num_deleted = diesel::delete(posts.filter(title.like(pattern))) 16 | .execute(&connection) 17 | .expect("Error deleting posts"); 18 | 19 | println!("Deleted {} posts", num_deleted); 20 | } 21 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/selecting_multiple_columns_requires_all_must_be_from_selectable_table.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | table! { 14 | posts { 15 | id -> Integer, 16 | title -> VarChar, 17 | user_id -> Integer, 18 | } 19 | } 20 | 21 | fn main() { 22 | let stuff = users::table.select((posts::id, posts::user_id)); 23 | //~^ ERROR Selectable 24 | //~| ERROR E0277 25 | //~| ERROR E0277 26 | let stuff = users::table.select((posts::id, users::name)); 27 | //~^ ERROR Selectable 28 | //~| ERROR E0277 29 | } 30 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/find_requires_correct_type.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | int_primary_key { 8 | id -> Integer, 9 | } 10 | } 11 | 12 | table! { 13 | string_primary_key { 14 | id -> VarChar, 15 | } 16 | } 17 | 18 | fn main() { 19 | let connection = PgConnection::establish("").unwrap(); 20 | // FIXME: It'd be nice if this mentioned `AsExpression` 21 | int_primary_key::table.find("1"); 22 | //~^ ERROR Expression 23 | //~| ERROR E0277 24 | // FIXME: It'd be nice if this mentioned `AsExpression` 25 | string_primary_key::table.find(1); 26 | //~^ ERROR Expression 27 | //~| ERROR E0277 28 | } 29 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/ilike_only_compiles_for_pg.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | 5 | table! { 6 | users { 7 | id -> Integer, 8 | name -> VarChar, 9 | } 10 | } 11 | 12 | #[derive(Insertable)] 13 | #[table_name="users"] 14 | struct User { 15 | id: i32, 16 | name: String, 17 | } 18 | 19 | fn main() { 20 | let connection = SqliteConnection::establish("").unwrap(); 21 | users::table.filter(users::name.ilike("%hey%")).execute(&connection); 22 | //~^ ERROR E0271 23 | 24 | let connection = MysqlConnection::establish("").unwrap(); 25 | users::table.filter(users::name.ilike("%hey%")).execute(&connection); 26 | //~^ ERROR E0271 27 | } 28 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_1/src/bin/show_posts.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_1_pg; 3 | 4 | use self::models::*; 5 | use diesel::prelude::*; 6 | use diesel_demo_step_1_pg::*; 7 | 8 | fn main() { 9 | use self::schema::posts::dsl::*; 10 | 11 | let connection = establish_connection(); 12 | let results = posts 13 | .filter(published.eq(true)) 14 | .limit(5) 15 | .load::(&connection) 16 | .expect("Error loading posts"); 17 | 18 | println!("Displaying {} posts", results.len()); 19 | for post in results { 20 | println!("{}", post.title); 21 | println!("-----------\n"); 22 | println!("{}", post.body); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_2/src/bin/show_posts.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_2_pg; 3 | 4 | use self::models::*; 5 | use diesel::prelude::*; 6 | use diesel_demo_step_2_pg::*; 7 | 8 | fn main() { 9 | use self::schema::posts::dsl::*; 10 | 11 | let connection = establish_connection(); 12 | let results = posts 13 | .filter(published.eq(true)) 14 | .limit(5) 15 | .load::(&connection) 16 | .expect("Error loading posts"); 17 | 18 | println!("Displaying {} posts", results.len()); 19 | for post in results { 20 | println!("{}", post.title); 21 | println!("-----------\n"); 22 | println!("{}", post.body); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/src/bin/show_posts.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_pg; 3 | 4 | use self::models::*; 5 | use diesel::prelude::*; 6 | use diesel_demo_step_3_pg::*; 7 | 8 | fn main() { 9 | use self::schema::posts::dsl::*; 10 | 11 | let connection = establish_connection(); 12 | let results = posts 13 | .filter(published.eq(true)) 14 | .limit(5) 15 | .load::(&connection) 16 | .expect("Error loading posts"); 17 | 18 | println!("Displaying {} posts", results.len()); 19 | for post in results { 20 | println!("{}", post.title); 21 | println!("-----------\n"); 22 | println!("{}", post.body); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/update_requires_valid_where_clause.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | 5 | table! { 6 | users { 7 | id -> Integer, 8 | } 9 | } 10 | 11 | table! { 12 | posts { 13 | id -> Integer, 14 | } 15 | } 16 | 17 | fn main() { 18 | // Sanity check: Valid update 19 | update(users::table).filter(users::id.eq(1)); 20 | 21 | update(users::table.filter(posts::id.eq(1))); 22 | //~^ ERROR E0277 23 | //~| ERROR E0277 24 | 25 | update(users::table).filter(posts::id.eq(1)); 26 | //~^ ERROR E0277 27 | 28 | update(users::table).set(users::id.eq(1)) 29 | .filter(posts::id.eq(1)); 30 | //~^ ERROR E0277 31 | } 32 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_1/src/bin/show_posts.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_1_mysql; 3 | 4 | use self::models::*; 5 | use diesel::prelude::*; 6 | use diesel_demo_step_1_mysql::*; 7 | 8 | fn main() { 9 | use self::schema::posts::dsl::*; 10 | 11 | let connection = establish_connection(); 12 | let results = posts 13 | .filter(published.eq(true)) 14 | .limit(5) 15 | .load::(&connection) 16 | .expect("Error loading posts"); 17 | 18 | println!("Displaying {} posts", results.len()); 19 | for post in results { 20 | println!("{}", post.title); 21 | println!("-----------\n"); 22 | println!("{}", post.body); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_2/src/bin/show_posts.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_2_mysql; 3 | 4 | use self::models::*; 5 | use diesel::prelude::*; 6 | use diesel_demo_step_2_mysql::*; 7 | 8 | fn main() { 9 | use self::schema::posts::dsl::*; 10 | 11 | let connection = establish_connection(); 12 | let results = posts 13 | .filter(published.eq(true)) 14 | .limit(5) 15 | .load::(&connection) 16 | .expect("Error loading posts"); 17 | 18 | println!("Displaying {} posts", results.len()); 19 | for post in results { 20 | println!("{}", post.title); 21 | println!("-----------\n"); 22 | println!("{}", post.body); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/src/bin/show_posts.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_mysql; 3 | 4 | use self::models::*; 5 | use diesel::prelude::*; 6 | use diesel_demo_step_3_mysql::*; 7 | 8 | fn main() { 9 | use self::schema::posts::dsl::*; 10 | 11 | let connection = establish_connection(); 12 | let results = posts 13 | .filter(published.eq(true)) 14 | .limit(5) 15 | .load::(&connection) 16 | .expect("Error loading posts"); 17 | 18 | println!("Displaying {} posts", results.len()); 19 | for post in results { 20 | println!("{}", post.title); 21 | println!("-----------\n"); 22 | println!("{}", post.body); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_1/src/bin/show_posts.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_1_sqlite; 3 | 4 | use self::models::*; 5 | use diesel::prelude::*; 6 | use diesel_demo_step_1_sqlite::*; 7 | 8 | fn main() { 9 | use self::schema::posts::dsl::*; 10 | 11 | let connection = establish_connection(); 12 | let results = posts 13 | .filter(published.eq(true)) 14 | .limit(5) 15 | .load::(&connection) 16 | .expect("Error loading posts"); 17 | 18 | println!("Displaying {} posts", results.len()); 19 | for post in results { 20 | println!("{}", post.title); 21 | println!("----------\n"); 22 | println!("{}", post.body); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_2/src/bin/show_posts.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_2_sqlite; 3 | 4 | use self::models::*; 5 | use diesel::prelude::*; 6 | use diesel_demo_step_2_sqlite::*; 7 | 8 | fn main() { 9 | use self::schema::posts::dsl::*; 10 | 11 | let connection = establish_connection(); 12 | let results = posts 13 | .filter(published.eq(true)) 14 | .limit(5) 15 | .load::(&connection) 16 | .expect("Error loading posts"); 17 | 18 | println!("Displaying {} posts", results.len()); 19 | for post in results { 20 | println!("{}", post.title); 21 | println!("----------\n"); 22 | println!("{}", post.body); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /diesel/src/expression/grouped.rs: -------------------------------------------------------------------------------- 1 | use crate::backend::Backend; 2 | use crate::expression::Expression; 3 | use crate::query_builder::*; 4 | use crate::result::QueryResult; 5 | 6 | #[derive(Debug, Copy, Clone, QueryId, Default, DieselNumericOps, NonAggregate)] 7 | pub struct Grouped(pub T); 8 | 9 | impl Expression for Grouped { 10 | type SqlType = T::SqlType; 11 | } 12 | 13 | impl, DB: Backend> QueryFragment for Grouped { 14 | fn walk_ast(&self, mut out: AstPass) -> QueryResult<()> { 15 | out.push_sql("("); 16 | self.0.walk_ast(out.reborrow())?; 17 | out.push_sql(")"); 18 | Ok(()) 19 | } 20 | } 21 | 22 | impl_selectable_expression!(Grouped); 23 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/any_is_only_selectable_if_inner_expr_is_selectable.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | use diesel::dsl::*; 5 | 6 | table! { 7 | stuff { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | table! { 14 | more_stuff (names) { 15 | names -> Array, 16 | } 17 | } 18 | 19 | #[derive(Queryable)] 20 | struct Stuff { 21 | id: i32, 22 | name: String, 23 | } 24 | 25 | fn main() { 26 | use self::stuff::dsl::*; 27 | 28 | let conn = PgConnection::establish("").unwrap(); 29 | 30 | let _ = stuff.filter(name.eq(any(more_stuff::names))) 31 | .load(&conn); 32 | //~^ ERROR AppearsInFromClause 33 | } 34 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_expression_bad_sql_type.stderr: -------------------------------------------------------------------------------- 1 | error: `sql_type` must be in the form `sql_type = "value"` 2 | --> $DIR/as_expression_bad_sql_type.rs:5:3 3 | | 4 | 5 | #[sql_type(Foo)] 5 | | ^^^^^^^^^^^^^ 6 | 7 | error: `sql_type` must be in the form `sql_type = "value"` 8 | --> $DIR/as_expression_bad_sql_type.rs:6:3 9 | | 10 | 6 | #[sql_type] 11 | | ^^^^^^^^ 12 | 13 | error: Invalid Rust type 14 | --> $DIR/as_expression_bad_sql_type.rs:7:14 15 | | 16 | 7 | #[sql_type = "@%&&*"] 17 | | ^^^^^^^ 18 | 19 | error: Invalid Rust type 20 | --> $DIR/as_expression_bad_sql_type.rs:8:14 21 | | 22 | 8 | #[sql_type = "1omg"] 23 | | ^^^^^^ 24 | 25 | error: aborting due to 4 previous errors 26 | 27 | -------------------------------------------------------------------------------- /diesel/src/query_builder/distinct_clause.rs: -------------------------------------------------------------------------------- 1 | use crate::backend::Backend; 2 | use crate::query_builder::*; 3 | use crate::result::QueryResult; 4 | 5 | #[derive(Debug, Clone, Copy, QueryId)] 6 | pub struct NoDistinctClause; 7 | #[derive(Debug, Clone, Copy, QueryId)] 8 | pub struct DistinctClause; 9 | 10 | impl QueryFragment for NoDistinctClause { 11 | fn walk_ast(&self, _: AstPass) -> QueryResult<()> { 12 | Ok(()) 13 | } 14 | } 15 | 16 | impl QueryFragment for DistinctClause { 17 | fn walk_ast(&self, mut out: AstPass) -> QueryResult<()> { 18 | out.push_sql("DISTINCT "); 19 | Ok(()) 20 | } 21 | } 22 | 23 | #[cfg(feature = "postgres")] 24 | pub use crate::pg::DistinctOnClause; 25 | -------------------------------------------------------------------------------- /_build/install-rust.yml: -------------------------------------------------------------------------------- 1 | steps: 2 | # Linux and Windows 3 | - bash: | 4 | rustup install $RUSTUP_TOOLCHAIN 5 | env: 6 | RUSTUP_TOOLCHAIN: ${{parameters.rust_version}} 7 | displayName: Install rust 8 | condition: not(eq(variables['Agent.OS'], 'Darwin')) 9 | # Macos 10 | - script: | 11 | curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain $RUSTUP_TOOLCHAIN 12 | echo "##vso[task.setvariable variable=PATH;]$PATH:$HOME/.cargo/bin" 13 | env: 14 | RUSTUP_TOOLCHAIN: ${{parameters.rust_version}} 15 | displayName: Install rust (MacOs) 16 | condition: eq(variables['Agent.OS'], 'Darwin') 17 | - bash: | 18 | rustc -Vv 19 | cargo -V 20 | displayName: Query rust and cargo versions 21 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/update_statement_does_not_support_returning_methods_on_sqlite.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | fn main() { 14 | use self::users::dsl::*; 15 | let connection = SqliteConnection::establish(":memory:").unwrap(); 16 | 17 | update(users.filter(id.eq(1))) 18 | .set(name.eq("Bill")) 19 | .get_result(&connection); 20 | //~^ ERROR SupportsReturningClause 21 | 22 | update(users.filter(id.eq(1))) 23 | .set(name.eq("Bill")) 24 | .returning(name) 25 | .get_result(&connection); 26 | //~^ ERROR SupportsReturningClause 27 | } 28 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/src/bin/publish_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_pg; 3 | 4 | use self::models::Post; 5 | use diesel::prelude::*; 6 | use diesel_demo_step_3_pg::*; 7 | use std::env::args; 8 | 9 | fn main() { 10 | use self::schema::posts::dsl::{posts, published}; 11 | 12 | let id = args() 13 | .nth(1) 14 | .expect("publish_post requires a post id") 15 | .parse::() 16 | .expect("Invalid ID"); 17 | let connection = establish_connection(); 18 | 19 | let post = diesel::update(posts.find(id)) 20 | .set(published.eq(true)) 21 | .get_result::(&connection) 22 | .unwrap(); 23 | println!("Published post {}", post.title); 24 | } 25 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/codegen_does_not_add_save_changes_method_on_structs_without_primary_key.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | 5 | table! { 6 | users { 7 | id -> Integer, 8 | name -> VarChar, 9 | hair_color -> VarChar, 10 | } 11 | } 12 | 13 | #[derive(Queryable, AsChangeset)] 14 | #[table_name = "users"] 15 | pub struct User { 16 | name: String, 17 | hair_color: String, 18 | } 19 | 20 | fn main() { 21 | let connection = PgConnection::establish("").unwrap(); 22 | let mut user = User { 23 | name: "Sean".to_string(), 24 | hair_color: "black".to_string(), 25 | }; 26 | user.save_changes(&connection); 27 | //~^ ERROR no method named `save_changes` found 28 | } 29 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/distinct_on_allows_only_fields_of_table.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | table! { 14 | posts { 15 | id -> Integer, 16 | name -> Text, 17 | content -> Text, 18 | } 19 | } 20 | 21 | fn main() { 22 | let connection = PgConnection::establish("postgres://foo").unwrap(); 23 | 24 | users::table.distinct_on(posts::id).get_results(&connection); 25 | //~^ ERROR SelectableExpression 26 | 27 | posts::table.distinct_on((posts::name, users::name)).get_result(&connection); 28 | //~^ ERROR SelectableExpression 29 | //~| ERROR AppearsInFromClause 30 | } 31 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/custom_returning_requires_nonaggregate.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | use diesel::dsl::count; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | #[derive(Insertable)] 14 | #[table_name="users"] 15 | pub struct NewUser { 16 | name: String, 17 | } 18 | 19 | fn main() { 20 | use self::users::dsl::*; 21 | 22 | let stmt = update(users.filter(id.eq(1))).set(name.eq("Bill")).returning(count(id)); 23 | //~^ ERROR NonAggregate 24 | 25 | let new_user = NewUser { 26 | name: "Foobar".to_string(), 27 | }; 28 | let stmt = insert_into(users).values(&new_user).returning((name, count(name))); 29 | //~^ ERROR NonAggregate 30 | } 31 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/src/test_helpers.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel_migrations; 2 | 3 | use self::diesel_migrations::run_pending_migrations; 4 | use diesel::prelude::*; 5 | use dotenv; 6 | use std::sync::{Mutex, MutexGuard}; 7 | 8 | pub fn connection() -> PgConnection { 9 | let url = dotenv::var("DATABASE_URL").expect("DATABASE_URL must be set"); 10 | let conn = PgConnection::establish(&url).unwrap(); 11 | run_pending_migrations(&conn).unwrap(); 12 | conn.begin_test_transaction().unwrap(); 13 | conn 14 | } 15 | 16 | pub fn this_test_modifies_env() -> MutexGuard<'static, ()> { 17 | let _ = dotenv::var("FORCING_DOTENV_LOAD"); 18 | lazy_static! { 19 | static ref ENV_LOCK: Mutex<()> = Mutex::new(()); 20 | } 21 | ENV_LOCK.lock().unwrap() 22 | } 23 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/src/bin/show_posts.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_sqlite; 3 | 4 | use self::diesel::prelude::*; 5 | use self::diesel_demo_step_3_sqlite::models::*; 6 | use self::diesel_demo_step_3_sqlite::*; 7 | 8 | fn main() { 9 | use diesel_demo_step_3_sqlite::schema::posts::dsl::*; 10 | 11 | let connection = establish_connection(); 12 | let results = posts 13 | .filter(published.eq(true)) 14 | .limit(5) 15 | .load::(&connection) 16 | .expect("Error loading posts"); 17 | 18 | println!("Displaying {} posts", results.len()); 19 | for post in results { 20 | println!("{}", post.title); 21 | println!("----------\n"); 22 | println!("{}", post.body); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /diesel_cli/tests/exit_codes.rs: -------------------------------------------------------------------------------- 1 | use support::project; 2 | 3 | #[test] 4 | fn errors_dont_cause_panic() { 5 | let p = project("errors_dont_panic").build(); 6 | 7 | let result = p.command_without_database_url("migration").arg("run").run(); 8 | 9 | assert!(!result.is_success()); 10 | assert!(!result.stdout().contains("thread '
' panicked at")) 11 | } 12 | 13 | #[test] 14 | fn errors_exit_code_is_1() { 15 | let p = project("errors_exit_1").build(); 16 | 17 | let result = p.command_without_database_url("migration").arg("run").run(); 18 | 19 | assert_eq!(1, result.code()) 20 | } 21 | 22 | #[test] 23 | fn successful_run_exits_0() { 24 | let p = project("successes_exit_0").build(); 25 | 26 | let result = p.command("setup").run(); 27 | 28 | assert_eq!(0, result.code()) 29 | } 30 | -------------------------------------------------------------------------------- /diesel/src/query_dsl/limit_dsl.rs: -------------------------------------------------------------------------------- 1 | use crate::query_source::Table; 2 | 3 | /// The `limit` method 4 | /// 5 | /// This trait should not be relied on directly by most apps. Its behavior is 6 | /// provided by [`QueryDsl`]. However, you may need a where clause on this trait 7 | /// to call `limit` from generic code. 8 | /// 9 | /// [`QueryDsl`]: ../trait.QueryDsl.html 10 | pub trait LimitDsl { 11 | /// The type returned by `.limit` 12 | type Output; 13 | 14 | /// See the trait documentation 15 | fn limit(self, limit: i64) -> Self::Output; 16 | } 17 | 18 | impl LimitDsl for T 19 | where 20 | T: Table, 21 | T::Query: LimitDsl, 22 | { 23 | type Output = ::Output; 24 | 25 | fn limit(self, limit: i64) -> Self::Output { 26 | self.as_query().limit(limit) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/select_carries_correct_result_type_info.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | fn main() { 14 | use self::users::dsl::*; 15 | 16 | let connection = PgConnection::establish("").unwrap(); 17 | let select_id = users.select(id); 18 | let select_name = users.select(name); 19 | 20 | let ids = select_name.load::(&connection); 21 | //~^ ERROR the trait bound `i32: diesel::deserialize::FromSql` is not satisfied 22 | let names = select_id.load::(&connection); 23 | //~^ ERROR the trait bound `*const str: diesel::deserialize::FromSql` is not satisfied 24 | } 25 | -------------------------------------------------------------------------------- /diesel/src/query_dsl/offset_dsl.rs: -------------------------------------------------------------------------------- 1 | use crate::query_source::Table; 2 | 3 | /// The `offset` method 4 | /// 5 | /// This trait should not be relied on directly by most apps. Its behavior is 6 | /// provided by [`QueryDsl`]. However, you may need a where clause on this trait 7 | /// to call `offset` from generic code. 8 | /// 9 | /// [`QueryDsl`]: ../trait.QueryDsl.html 10 | pub trait OffsetDsl { 11 | /// The type returned by `.offset`. 12 | type Output; 13 | 14 | /// See the trait documentation 15 | fn offset(self, offset: i64) -> Self::Output; 16 | } 17 | 18 | impl OffsetDsl for T 19 | where 20 | T: Table, 21 | T::Query: OffsetDsl, 22 | { 23 | type Output = ::Output; 24 | 25 | fn offset(self, offset: i64) -> Self::Output { 26 | self.as_query().offset(offset) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile_tests.rs: -------------------------------------------------------------------------------- 1 | #![cfg(not(windows))] 2 | extern crate compiletest_rs as compiletest; 3 | 4 | use std::env::var; 5 | use std::path::PathBuf; 6 | 7 | fn run_mode(mode: &'static str) { 8 | let mut config = compiletest::Config::default(); 9 | 10 | let cfg_mode = mode.parse().expect("Invalid mode"); 11 | 12 | if let Ok(name) = var::<&str>("TESTNAME") { 13 | let s: String = name.to_owned(); 14 | config.filter = Some(s) 15 | } 16 | config.mode = cfg_mode; 17 | config.src_base = PathBuf::from(format!("tests/{}", mode)); 18 | config.link_deps(); 19 | config.clean_rmeta(); 20 | 21 | compiletest::run_tests(&config); 22 | } 23 | 24 | #[test] 25 | fn compile_fail() { 26 | run_mode("compile-fail"); 27 | } 28 | 29 | #[test] 30 | fn ui() { 31 | run_mode("ui") 32 | } 33 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/belongs_to_invalid_option_syntax.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | bar (id) { 6 | id -> Integer, 7 | } 8 | } 9 | 10 | table! { 11 | foo (bar_id) { 12 | bar_id -> Integer, 13 | } 14 | } 15 | 16 | #[derive(Identifiable)] 17 | #[table_name = "bar"] 18 | struct Bar { 19 | id: i32, 20 | } 21 | 22 | #[derive(Identifiable)] 23 | #[table_name = "bar"] 24 | struct Baz { 25 | id: i32, 26 | } 27 | 28 | #[derive(Associations)] 29 | #[belongs_to] 30 | #[belongs_to = "Bar"] 31 | #[belongs_to()] 32 | #[belongs_to(foreign_key = "bar_id")] 33 | #[belongs_to(Bar, foreign_key)] 34 | #[belongs_to(Bar, foreign_key(bar_id))] 35 | #[belongs_to(Baz, foreign_key = "bar_id", random_option)] 36 | #[table_name = "foo"] 37 | struct Foo { 38 | bar_id: i32, 39 | } 40 | 41 | fn main() {} 42 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/aggregate_expression_requires_column_from_same_table.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | } 10 | } 11 | 12 | table! { 13 | posts { 14 | id -> Integer, 15 | } 16 | } 17 | 18 | fn main() { 19 | use diesel::dsl::*; 20 | let source = users::table.select(sum(posts::id)); 21 | //~^ ERROR E0277 22 | //~| ERROR AppearsInFromClause 23 | let source = users::table.select(avg(posts::id)); 24 | //~^ ERROR E0277 25 | //~| ERROR AppearsInFromClause 26 | let source = users::table.select(max(posts::id)); 27 | //~^ ERROR E0277 28 | //~| ERROR AppearsInFromClause 29 | let source = users::table.select(min(posts::id)); 30 | //~^ ERROR E0277 31 | //~| ERROR AppearsInFromClause 32 | } 33 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/as_changeset_bad_primary_key_syntax.stderr: -------------------------------------------------------------------------------- 1 | error: Expected `bar` found `bar = "baz"` 2 | --> $DIR/as_changeset_bad_primary_key_syntax.rs:12:19 3 | | 4 | 12 | #[primary_key(id, bar = "baz", qux(id))] 5 | | ^^^^^^^^^^^ 6 | 7 | error: Expected `qux` found `qux (id)` 8 | --> $DIR/as_changeset_bad_primary_key_syntax.rs:12:32 9 | | 10 | 12 | #[primary_key(id, bar = "baz", qux(id))] 11 | | ^^^^^^^ 12 | 13 | error[E0433]: failed to resolve: use of undeclared type or module `user_forms` 14 | --> $DIR/as_changeset_bad_primary_key_syntax.rs:13:8 15 | | 16 | 13 | struct UserForm { 17 | | ^^^^^^^^ use of undeclared type or module `user_forms` 18 | 19 | error: aborting due to 3 previous errors 20 | 21 | For more information about this error, try `rustc --explain E0433`. 22 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/belongs_to_missing_foreign_key_column.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | struct Bar; 5 | 6 | table! { 7 | foo { 8 | id -> Integer, 9 | } 10 | } 11 | 12 | #[derive(Associations)] 13 | #[belongs_to(Bar)] 14 | #[table_name = "foo"] 15 | struct Foo1 { 16 | bar_id: i32, 17 | } 18 | 19 | #[derive(Associations)] 20 | #[belongs_to(Bar, foreign_key = "bar_id")] 21 | #[table_name = "foo"] 22 | struct Foo2 { 23 | bar_id: i32, 24 | } 25 | 26 | #[derive(Associations)] 27 | #[belongs_to(Bar)] 28 | #[table_name = "foo"] 29 | struct Foo3 { 30 | #[column_name = "bar_id"] 31 | baz_id: i32, 32 | } 33 | 34 | #[derive(Associations)] 35 | #[belongs_to(Bar, foreign_key = "bar_id")] 36 | #[table_name = "foo"] 37 | struct Foo4 { 38 | #[column_name = "bar_id"] 39 | baz_id: i32, 40 | } 41 | 42 | fn main() {} 43 | -------------------------------------------------------------------------------- /.env.sample: -------------------------------------------------------------------------------- 1 | # The database to use when testing against Postgres. 2 | PG_DATABASE_URL=postgresql://postgres@localhost:5432/diesel_test 3 | # The database to use when running the Postgres examples during testing. 4 | PG_EXAMPLE_DATABASE_URL=postgresql://postgres@localhost:5432/diesel_example 5 | 6 | # The database to use when testing against MySQL. 7 | MYSQL_DATABASE_URL=mysql://root@127.0.0.1:3306/diesel_test 8 | # The database to use when running the MySQL examples during testing. 9 | MYSQL_EXAMPLE_DATABASE_URL=mysql://root@127.0.0.1:3306/diesel_example 10 | # A database different from the others above used for certain unit tests. 11 | # TODO: this is magical, explain what it's there for. 12 | MYSQL_UNIT_TEST_DATABASE_URL=mysql://root@127.0.0.1:3306/diesel_unit_test 13 | 14 | # The database to use when testing against SQLite. 15 | SQLITE_DATABASE_URL=/tmp/diesel_test.sqlite 16 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple/postgres/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `users1` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | users1 (id) { 6 | /// The `id` column of the `users1` table. 7 | /// 8 | /// Its SQL type is `Int4`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Int4, 12 | } 13 | } 14 | 15 | table! { 16 | /// Representation of the `users2` table. 17 | /// 18 | /// (Automatically generated by Diesel.) 19 | users2 (id) { 20 | /// The `id` column of the `users2` table. 21 | /// 22 | /// Its SQL type is `Int4`. 23 | /// 24 | /// (Automatically generated by Diesel.) 25 | id -> Int4, 26 | } 27 | } 28 | 29 | allow_tables_to_appear_in_same_query!( 30 | users1, 31 | users2, 32 | ); 33 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/belongs_to_missing_foreign_key_field.stderr: -------------------------------------------------------------------------------- 1 | error: No field with column name bar_id 2 | --> $DIR/belongs_to_missing_foreign_key_field.rs:7:14 3 | | 4 | 7 | #[belongs_to(Bar)] 5 | | ^^^ 6 | 7 | error: No field with column name bar_id 8 | --> $DIR/belongs_to_missing_foreign_key_field.rs:8:33 9 | | 10 | 8 | #[belongs_to(Bar, foreign_key = "bar_id")] 11 | | ^^^^^^^^ 12 | 13 | error: No field with column name bar_id 14 | --> $DIR/belongs_to_missing_foreign_key_field.rs:12:14 15 | | 16 | 12 | #[belongs_to(Bar)] 17 | | ^^^ 18 | 19 | error: No field with column name bar_id 20 | --> $DIR/belongs_to_missing_foreign_key_field.rs:13:33 21 | | 22 | 13 | #[belongs_to(Bar, foreign_key = "bar_id")] 23 | | ^^^^^^^^ 24 | 25 | error: aborting due to 4 previous errors 26 | 27 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple/mysql/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `users1` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | users1 (id) { 6 | /// The `id` column of the `users1` table. 7 | /// 8 | /// Its SQL type is `Integer`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Integer, 12 | } 13 | } 14 | 15 | table! { 16 | /// Representation of the `users2` table. 17 | /// 18 | /// (Automatically generated by Diesel.) 19 | users2 (id) { 20 | /// The `id` column of the `users2` table. 21 | /// 22 | /// Its SQL type is `Integer`. 23 | /// 24 | /// (Automatically generated by Diesel.) 25 | id -> Integer, 26 | } 27 | } 28 | 29 | allow_tables_to_appear_in_same_query!( 30 | users1, 31 | users2, 32 | ); 33 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/insert_cannot_reference_columns_from_other_table.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::pg::PgConnection; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | } 11 | } 12 | 13 | table! { 14 | posts { 15 | id -> Integer, 16 | } 17 | } 18 | 19 | fn main() { 20 | let conn = PgConnection::establish("").unwrap(); 21 | 22 | insert_into(users::table) 23 | .values(&posts::id.eq(1)); 24 | //~^ ERROR type mismatch resolving `::Table == users::table` 25 | 26 | insert_into(users::table) 27 | .values(&(posts::id.eq(1), users::id.eq(2))); 28 | //~^ ERROR type mismatch resolving `::Table == users::table` 29 | //~| ERROR E0271 30 | //FIXME: Bad error on the second one 31 | } 32 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/src/bin/publish_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_mysql; 3 | 4 | use self::models::Post; 5 | use diesel::prelude::*; 6 | use diesel_demo_step_3_mysql::*; 7 | use std::env::args; 8 | 9 | fn main() { 10 | use self::schema::posts::dsl::{posts, published}; 11 | 12 | let id = args() 13 | .nth(1) 14 | .expect("publish_post requires a post id") 15 | .parse::() 16 | .expect("Invalid ID"); 17 | let connection = establish_connection(); 18 | 19 | let post: Post = posts 20 | .find(id) 21 | .first(&connection) 22 | .unwrap_or_else(|_| panic!("Unable to find post {}", id)); 23 | 24 | diesel::update(posts.find(id)) 25 | .set(published.eq(true)) 26 | .execute(&connection) 27 | .unwrap(); 28 | 29 | println!("Published post {}", post.title); 30 | } 31 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/src/comment.rs: -------------------------------------------------------------------------------- 1 | use chrono::NaiveDateTime; 2 | 3 | use auth::User; 4 | use post::Post; 5 | use schema::comments; 6 | 7 | #[derive(Queryable, Identifiable, Associations)] 8 | #[belongs_to(User)] 9 | #[belongs_to(Post)] 10 | pub struct Comment { 11 | pub id: i32, 12 | pub user_id: i32, 13 | pub post_id: i32, 14 | pub body: String, 15 | pub created_at: NaiveDateTime, 16 | pub updated_at: NaiveDateTime, 17 | } 18 | 19 | pub fn render(comments_and_post_title: &[(Comment, String)]) { 20 | for &(ref comment, ref post_title) in comments_and_post_title { 21 | println!("On post {}", post_title); 22 | println!( 23 | "At {} (id: {})", 24 | comment.updated_at.format("%F %T"), 25 | comment.id 26 | ); 27 | println!("{}", comment.body); 28 | println!("===============\n"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_2/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | extern crate dotenv; 4 | 5 | pub mod models; 6 | pub mod schema; 7 | 8 | use diesel::prelude::*; 9 | use dotenv::dotenv; 10 | use std::env; 11 | 12 | use self::models::{NewPost, Post}; 13 | 14 | pub fn establish_connection() -> PgConnection { 15 | dotenv().ok(); 16 | 17 | let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); 18 | PgConnection::establish(&database_url) 19 | .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) 20 | } 21 | 22 | pub fn create_post(conn: &PgConnection, title: &str, body: &str) -> Post { 23 | use schema::posts; 24 | 25 | let new_post = NewPost { title, body }; 26 | 27 | diesel::insert_into(posts::table) 28 | .values(&new_post) 29 | .get_result(conn) 30 | .expect("Error saving new post") 31 | } 32 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | extern crate dotenv; 4 | 5 | pub mod models; 6 | pub mod schema; 7 | 8 | use diesel::prelude::*; 9 | use dotenv::dotenv; 10 | use std::env; 11 | 12 | use self::models::{NewPost, Post}; 13 | 14 | pub fn establish_connection() -> PgConnection { 15 | dotenv().ok(); 16 | 17 | let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); 18 | PgConnection::establish(&database_url) 19 | .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) 20 | } 21 | 22 | pub fn create_post(conn: &PgConnection, title: &str, body: &str) -> Post { 23 | use schema::posts; 24 | 25 | let new_post = NewPost { title, body }; 26 | 27 | diesel::insert_into(posts::table) 28 | .values(&new_post) 29 | .get_result(conn) 30 | .expect("Error saving new post") 31 | } 32 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_2/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | extern crate dotenv; 4 | 5 | pub mod models; 6 | pub mod schema; 7 | 8 | use diesel::prelude::*; 9 | use dotenv::dotenv; 10 | use std::env; 11 | 12 | use self::models::NewPost; 13 | 14 | pub fn establish_connection() -> SqliteConnection { 15 | dotenv().ok(); 16 | 17 | let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); 18 | SqliteConnection::establish(&database_url) 19 | .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) 20 | } 21 | 22 | pub fn create_post(conn: &SqliteConnection, title: &str, body: &str) -> usize { 23 | use schema::posts; 24 | 25 | let new_post = NewPost { title, body }; 26 | 27 | diesel::insert_into(posts::table) 28 | .values(&new_post) 29 | .execute(conn) 30 | .expect("Error saving new post") 31 | } 32 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | extern crate dotenv; 5 | 6 | pub mod models; 7 | pub mod schema; 8 | 9 | use diesel::prelude::*; 10 | use dotenv::dotenv; 11 | use std::env; 12 | 13 | use models::NewPost; 14 | 15 | pub fn establish_connection() -> SqliteConnection { 16 | dotenv().ok(); 17 | 18 | let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); 19 | SqliteConnection::establish(&database_url) 20 | .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) 21 | } 22 | 23 | pub fn create_post(conn: &SqliteConnection, title: &str, body: &str) -> usize { 24 | use schema::posts; 25 | 26 | let new_post = NewPost { title, body }; 27 | 28 | diesel::insert_into(posts::table) 29 | .values(&new_post) 30 | .execute(conn) 31 | .expect("Error saving new post") 32 | } 33 | -------------------------------------------------------------------------------- /diesel/src/query_builder/insert_statement/column_list.rs: -------------------------------------------------------------------------------- 1 | use crate::backend::Backend; 2 | use crate::query_builder::*; 3 | use crate::query_source::Column; 4 | use crate::result::QueryResult; 5 | 6 | /// Represents the column list for use in an insert statement. 7 | /// 8 | /// This trait is implemented by columns and tuples of columns. 9 | pub trait ColumnList { 10 | /// The table these columns belong to 11 | type Table; 12 | 13 | /// Generate the SQL for this column list. 14 | /// 15 | /// Column names must *not* be qualified. 16 | fn walk_ast(&self, out: AstPass) -> QueryResult<()>; 17 | } 18 | 19 | impl ColumnList for C 20 | where 21 | C: Column, 22 | { 23 | type Table = ::Table; 24 | 25 | fn walk_ast(&self, mut out: AstPass) -> QueryResult<()> { 26 | out.push_identifier(C::NAME)?; 27 | Ok(()) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/custom_returning_requires_selectable_expression.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | 5 | table! { 6 | users { 7 | id -> Integer, 8 | name -> VarChar, 9 | } 10 | } 11 | 12 | table! { 13 | bad { 14 | id -> Integer, 15 | age -> Integer, 16 | } 17 | } 18 | 19 | #[derive(Insertable)] 20 | #[table_name="users"] 21 | pub struct NewUser { 22 | name: String, 23 | } 24 | 25 | fn main() { 26 | use self::users::dsl::*; 27 | 28 | let stmt = update(users.filter(id.eq(1))).set(name.eq("Bill")).returning(bad::age); 29 | //~^ ERROR SelectableExpression 30 | 31 | let new_user = NewUser { 32 | name: "Foobar".to_string(), 33 | }; 34 | let stmt = insert_into(users).values(&new_user).returning((name, bad::age)); 35 | //~^ ERROR SelectableExpression 36 | //~| ERROR AppearsInFromClause 37 | } 38 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/src/bin/publish_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_sqlite; 3 | 4 | use self::diesel::prelude::*; 5 | use self::diesel_demo_step_3_sqlite::*; 6 | use std::env::args; 7 | 8 | fn main() { 9 | use diesel_demo_step_3_sqlite::schema::posts::dsl::{posts, published}; 10 | 11 | let id = args() 12 | .nth(1) 13 | .expect("publish_post requires a post id") 14 | .parse::() 15 | .expect("Invalid ID"); 16 | let connection = establish_connection(); 17 | 18 | let _ = diesel::update(posts.find(id)) 19 | .set(published.eq(true)) 20 | .execute(&connection) 21 | .unwrap(); 22 | 23 | let post: models::Post = posts 24 | .find(id) 25 | .first(&connection) 26 | .unwrap_or_else(|_| panic!("Unable to find post {}", id)); 27 | 28 | println!("Published post {}", post.title); 29 | } 30 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_simple/sqlite/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `users1` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | users1 (id) { 6 | /// The `id` column of the `users1` table. 7 | /// 8 | /// Its SQL type is `Nullable`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | id -> Nullable, 12 | } 13 | } 14 | 15 | table! { 16 | /// Representation of the `users2` table. 17 | /// 18 | /// (Automatically generated by Diesel.) 19 | users2 (id) { 20 | /// The `id` column of the `users2` table. 21 | /// 22 | /// Its SQL type is `Nullable`. 23 | /// 24 | /// (Automatically generated by Diesel.) 25 | id -> Nullable, 26 | } 27 | } 28 | 29 | allow_tables_to_appear_in_same_query!( 30 | users1, 31 | users2, 32 | ); 33 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_2/src/bin/write_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_2_sqlite; 3 | 4 | use self::diesel_demo_step_2_sqlite::*; 5 | use std::io::{stdin, Read}; 6 | 7 | fn main() { 8 | let connection = establish_connection(); 9 | 10 | println!("What would you like your title to be?"); 11 | let mut title = String::new(); 12 | stdin().read_line(&mut title).unwrap(); 13 | let title = &title[..(title.len() - 1)]; // Drop the newline character 14 | println!( 15 | "\nOk! Let's write {} (Press {} when finished)\n", 16 | title, EOF 17 | ); 18 | let mut body = String::new(); 19 | stdin().read_to_string(&mut body).unwrap(); 20 | 21 | let _ = create_post(&connection, title, &body); 22 | println!("\nSaved draft {}", title); 23 | } 24 | 25 | #[cfg(not(windows))] 26 | const EOF: &str = "CTRL+D"; 27 | 28 | #[cfg(windows)] 29 | const EOF: &str = "CTRL+Z"; 30 | -------------------------------------------------------------------------------- /examples/sqlite/getting_started_step_3/src/bin/write_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_sqlite; 3 | 4 | use self::diesel_demo_step_3_sqlite::*; 5 | use std::io::{stdin, Read}; 6 | 7 | fn main() { 8 | let connection = establish_connection(); 9 | 10 | println!("What would you like your title to be?"); 11 | let mut title = String::new(); 12 | stdin().read_line(&mut title).unwrap(); 13 | let title = &title[..(title.len() - 1)]; // Drop the newline character 14 | println!( 15 | "\nOk! Let's write {} (Press {} when finished)\n", 16 | title, EOF 17 | ); 18 | let mut body = String::new(); 19 | stdin().read_to_string(&mut body).unwrap(); 20 | 21 | let _ = create_post(&connection, title, &body); 22 | println!("\nSaved draft {}", title); 23 | } 24 | 25 | #[cfg(not(windows))] 26 | const EOF: &str = "CTRL+D"; 27 | 28 | #[cfg(windows)] 29 | const EOF: &str = "CTRL+Z"; 30 | -------------------------------------------------------------------------------- /diesel/src/query_dsl/boxed_dsl.rs: -------------------------------------------------------------------------------- 1 | use crate::query_builder::AsQuery; 2 | use crate::query_source::Table; 3 | 4 | /// The `into_boxed` method 5 | /// 6 | /// This trait should not be relied on directly by most apps. Its behavior is 7 | /// provided by [`QueryDsl`]. However, you may need a where clause on this trait 8 | /// to call `into_boxed` from generic code. 9 | /// 10 | /// [`QueryDsl`]: ../trait.QueryDsl.html 11 | pub trait BoxedDsl<'a, DB> { 12 | /// The return type of `internal_into_boxed` 13 | type Output; 14 | 15 | /// See the trait documentation. 16 | fn internal_into_boxed(self) -> Self::Output; 17 | } 18 | 19 | impl<'a, T, DB> BoxedDsl<'a, DB> for T 20 | where 21 | T: Table + AsQuery, 22 | T::Query: BoxedDsl<'a, DB>, 23 | { 24 | type Output = >::Output; 25 | 26 | fn internal_into_boxed(self) -> Self::Output { 27 | self.as_query().internal_into_boxed() 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /diesel_derives/src/non_aggregate.rs: -------------------------------------------------------------------------------- 1 | use proc_macro2::*; 2 | use syn; 3 | 4 | use util::*; 5 | 6 | pub fn derive(mut item: syn::DeriveInput) -> Result { 7 | let type_params = item 8 | .generics 9 | .type_params() 10 | .map(|param| param.ident.clone()) 11 | .collect::>(); 12 | for type_param in type_params { 13 | let where_clause = item.generics.make_where_clause(); 14 | where_clause 15 | .predicates 16 | .push(parse_quote!(#type_param: NonAggregate)); 17 | } 18 | 19 | let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl(); 20 | let struct_name = &item.ident; 21 | 22 | Ok(wrap_in_dummy_mod(quote! { 23 | use diesel::expression::NonAggregate; 24 | 25 | impl #impl_generics NonAggregate for #struct_name #ty_generics 26 | #where_clause 27 | { 28 | } 29 | })) 30 | } 31 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_2/src/bin/write_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_2_mysql; 3 | 4 | use diesel_demo_step_2_mysql::*; 5 | use std::io::{stdin, Read}; 6 | 7 | fn main() { 8 | let connection = establish_connection(); 9 | 10 | let mut title = String::new(); 11 | let mut body = String::new(); 12 | 13 | println!("What would you like your title to be?"); 14 | stdin().read_line(&mut title).unwrap(); 15 | let title = title.trim_end(); // Remove the trailing newline 16 | 17 | println!( 18 | "\nOk! Let's write {} (Press {} when finished)\n", 19 | title, EOF 20 | ); 21 | stdin().read_to_string(&mut body).unwrap(); 22 | 23 | let post = create_post(&connection, title, &body); 24 | println!("\nSaved draft {} with id {}", title, post.id); 25 | } 26 | 27 | #[cfg(not(windows))] 28 | const EOF: &str = "CTRL+D"; 29 | 30 | #[cfg(windows)] 31 | const EOF: &str = "CTRL+Z"; 32 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/src/bin/write_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_mysql; 3 | 4 | use diesel_demo_step_3_mysql::*; 5 | use std::io::{stdin, Read}; 6 | 7 | fn main() { 8 | let connection = establish_connection(); 9 | 10 | let mut title = String::new(); 11 | let mut body = String::new(); 12 | 13 | println!("What would you like your title to be?"); 14 | stdin().read_line(&mut title).unwrap(); 15 | let title = title.trim_end(); // Remove the trailing newline 16 | 17 | println!( 18 | "\nOk! Let's write {} (Press {} when finished)\n", 19 | title, EOF 20 | ); 21 | stdin().read_to_string(&mut body).unwrap(); 22 | 23 | let post = create_post(&connection, title, &body); 24 | println!("\nSaved draft {} with id {}", title, post.id); 25 | } 26 | 27 | #[cfg(not(windows))] 28 | const EOF: &str = "CTRL+D"; 29 | 30 | #[cfg(windows)] 31 | const EOF: &str = "CTRL+Z"; 32 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_2/src/bin/write_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_2_pg; 3 | 4 | use diesel_demo_step_2_pg::*; 5 | use std::io::{stdin, Read}; 6 | 7 | fn main() { 8 | let connection = establish_connection(); 9 | 10 | let mut title = String::new(); 11 | let mut body = String::new(); 12 | 13 | println!("What would you like your title to be?"); 14 | stdin().read_line(&mut title).unwrap(); 15 | let title = title.trim_end(); // Remove the trailing newline 16 | 17 | println!( 18 | "\nOk! Let's write {} (Press {} when finished)\n", 19 | title, EOF 20 | ); 21 | stdin().read_to_string(&mut body).unwrap(); 22 | 23 | let post = create_post(&connection, title, &body); 24 | println!("\nSaved draft {} with id {}", title, post.id); 25 | } 26 | 27 | #[cfg(not(windows))] 28 | const EOF: &str = "CTRL+D"; 29 | 30 | #[cfg(windows)] 31 | const EOF: &str = "CTRL+Z"; 32 | -------------------------------------------------------------------------------- /examples/postgres/getting_started_step_3/src/bin/write_post.rs: -------------------------------------------------------------------------------- 1 | extern crate diesel; 2 | extern crate diesel_demo_step_3_pg; 3 | 4 | use diesel_demo_step_3_pg::*; 5 | use std::io::{stdin, Read}; 6 | 7 | fn main() { 8 | let connection = establish_connection(); 9 | 10 | let mut title = String::new(); 11 | let mut body = String::new(); 12 | 13 | println!("What would you like your title to be?"); 14 | stdin().read_line(&mut title).unwrap(); 15 | let title = title.trim_end(); // Remove the trailing newline 16 | 17 | println!( 18 | "\nOk! Let's write {} (Press {} when finished)\n", 19 | title, EOF 20 | ); 21 | stdin().read_to_string(&mut body).unwrap(); 22 | 23 | let post = create_post(&connection, title, &body); 24 | println!("\nSaved draft {} with id {}", title, post.id); 25 | } 26 | 27 | #[cfg(not(windows))] 28 | const EOF: &str = "CTRL+D"; 29 | 30 | #[cfg(windows)] 31 | const EOF: &str = "CTRL+Z"; 32 | -------------------------------------------------------------------------------- /_build/failable_step.yml: -------------------------------------------------------------------------------- 1 | parameters: 2 | rust_version: '' 3 | 4 | steps: 5 | - bash: | 6 | if [[ "$RUST_VERSION" == nightly* ]]; then 7 | export RUSTFLAGS='--cap-lints=warn' 8 | (${{ parameters.bash }}) 2>&1 | tee log 9 | export EXIT_CODE=${PIPESTATUS[0]} 10 | echo "The exit code is $EXIT_CODE" 11 | if grep -q "warning:" log; then 12 | echo -e "\043#vso[task.logissue type=warning;]${{ parameters.displayName}} contains new warnings" 13 | fi 14 | if [[ $EXIT_CODE -eq 0 ]]; then 15 | true 16 | else 17 | false 18 | fi 19 | else 20 | ${{ parameters.bash }} 21 | fi 22 | displayName: ${{ parameters.displayName }} 23 | env: 24 | RUST_VERSION: ${{parameters.rust_version}} 25 | condition: | 26 | or( 27 | startsWith(variables['parameters.rust_version'], 'nightly'), 28 | succeeded() 29 | ) 30 | -------------------------------------------------------------------------------- /diesel/src/expression/not.rs: -------------------------------------------------------------------------------- 1 | use crate::expression::grouped::Grouped; 2 | use crate::expression::AsExpression; 3 | use crate::helper_types::not; 4 | use crate::sql_types::Bool; 5 | 6 | /// Creates a SQL `NOT` expression 7 | /// 8 | /// # Example 9 | /// 10 | /// ```rust 11 | /// # #[macro_use] extern crate diesel; 12 | /// # include!("../doctest_setup.rs"); 13 | /// # 14 | /// # fn main() { 15 | /// # use schema::users::dsl::*; 16 | /// # let connection = establish_connection(); 17 | /// use diesel::dsl::not; 18 | /// 19 | /// let users_with_name = users.select(id).filter(name.eq("Sean")); 20 | /// let users_not_with_name = users.select(id).filter( 21 | /// not(name.eq("Sean"))); 22 | /// 23 | /// assert_eq!(Ok(1), users_with_name.first(&connection)); 24 | /// assert_eq!(Ok(2), users_not_with_name.first(&connection)); 25 | /// # } 26 | /// ``` 27 | pub fn not>(expr: T) -> not { 28 | super::operators::Not::new(Grouped(expr.as_expression())) 29 | } 30 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/array_expressions_must_be_same_type.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::dsl::*; 6 | 7 | fn main() { 8 | let connection = PgConnection::establish("").unwrap(); 9 | select(array((1, 3))).get_result::>(&connection).unwrap(); 10 | select(array((1f64, 3f64))).get_result::>(&connection).unwrap(); 11 | 12 | select(array((1, 3f64))).get_result::>(&connection).unwrap(); 13 | //~^ ERROR E0277 14 | //~| ERROR E0277 15 | //~| ERROR E0277 16 | //~| ERROR E0277 17 | //~| ERROR E0277 18 | //~| ERROR E0277 19 | //~| ERROR E0277 20 | //~| ERROR E0277 21 | 22 | select(array((1, 3f64))).get_result::>(&connection).unwrap(); 23 | //~^ ERROR E0277 24 | //~| ERROR E0277 25 | //~| ERROR E0277 26 | //~| ERROR E0277 27 | //~| ERROR E0277 28 | //~| ERROR E0277 29 | //~| ERROR E0277 30 | //~| ERROR E0277 31 | } 32 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_3/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | extern crate dotenv; 4 | 5 | pub mod models; 6 | pub mod schema; 7 | 8 | use diesel::prelude::*; 9 | use dotenv::dotenv; 10 | use std::env; 11 | 12 | use self::models::{NewPost, Post}; 13 | 14 | pub fn establish_connection() -> MysqlConnection { 15 | dotenv().ok(); 16 | 17 | let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); 18 | MysqlConnection::establish(&database_url) 19 | .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) 20 | } 21 | 22 | pub fn create_post(conn: &MysqlConnection, title: &str, body: &str) -> Post { 23 | use schema::posts::dsl::{id, posts}; 24 | 25 | let new_post = NewPost { title, body }; 26 | 27 | diesel::insert_into(posts) 28 | .values(&new_post) 29 | .execute(conn) 30 | .expect("Error saving new post"); 31 | 32 | posts.order(id.desc()).first(conn).unwrap() 33 | } 34 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/insert_from_select_cant_be_used_with_tuples_or_arrays.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | use diesel::pg::PgConnection; 6 | 7 | table! { 8 | users { 9 | id -> Integer, 10 | name -> Text, 11 | hair_color -> Nullable, 12 | } 13 | } 14 | 15 | table! { 16 | posts (user_id) { 17 | user_id -> Integer, 18 | title -> Text, 19 | body -> Nullable, 20 | } 21 | } 22 | 23 | fn main() { 24 | use users::dsl::*; 25 | use posts::dsl::*; 26 | let conn = PgConnection::establish("").unwrap(); 27 | 28 | // Sanity check, valid query 29 | insert_into(posts) 30 | .values(users) 31 | .execute(&conn) 32 | .unwrap(); 33 | 34 | insert_into(posts) 35 | .values(vec![users, users]); 36 | //~^ ERROR E0277 37 | 38 | insert_into(posts) 39 | .values((users, users)); 40 | //~^ ERROR E0271 41 | } 42 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/user_defined_functions_follow_same_selection_rules.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] extern crate diesel; 2 | 3 | use diesel::*; 4 | use diesel::sql_types::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | table! { 14 | posts { 15 | id -> Integer, 16 | title -> VarChar, 17 | } 18 | } 19 | 20 | #[derive(Queryable)] 21 | struct User { 22 | id: i32, 23 | name: String, 24 | } 25 | 26 | sql_function!(fn foo(x: Integer) -> Integer); 27 | sql_function!(fn bar(x: VarChar) -> VarChar); 28 | 29 | fn main() { 30 | use self::users::name; 31 | use self::posts::title; 32 | 33 | let conn = PgConnection::establish("").unwrap(); 34 | 35 | let _ = users::table.filter(name.eq(foo(1))); 36 | //~^ ERROR type mismatch 37 | 38 | let _ = users::table.filter(name.eq(bar(title))) 39 | .load::(&conn); 40 | //~^ ERROR AppearsInFromClause 41 | } 42 | -------------------------------------------------------------------------------- /examples/mysql/getting_started_step_2/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | extern crate dotenv; 4 | 5 | pub mod models; 6 | pub mod schema; 7 | 8 | use diesel::prelude::*; 9 | use dotenv::dotenv; 10 | use std::env; 11 | 12 | use self::models::{NewPost, Post}; 13 | 14 | pub fn establish_connection() -> MysqlConnection { 15 | dotenv().ok(); 16 | 17 | let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); 18 | MysqlConnection::establish(&database_url) 19 | .unwrap_or_else(|_| panic!("Error connecting to {}", database_url)) 20 | } 21 | 22 | pub fn create_post(conn: &MysqlConnection, title: &str, body: &str) -> Post { 23 | use schema::posts; 24 | 25 | let new_post = NewPost { title, body }; 26 | 27 | diesel::insert_into(posts::table) 28 | .values(&new_post) 29 | .execute(conn) 30 | .expect("Error saving new post"); 31 | 32 | posts::table.order(posts::id.desc()).first(conn).unwrap() 33 | } 34 | -------------------------------------------------------------------------------- /diesel_derives/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "diesel_derives" 3 | version = "1.4.1" 4 | authors = ["Sean Griffin "] 5 | license = "MIT OR Apache-2.0" 6 | description = "You should not use this crate directly, it is internal to Diesel." 7 | documentation = "https://diesel.rs/guides/" 8 | homepage = "https://diesel.rs" 9 | repository = "https://github.com/diesel-rs/diesel/tree/master/diesel_derives" 10 | autotests = false 11 | 12 | [dependencies] 13 | syn = { version = "1", features = ["full", "fold"] } 14 | quote = "1" 15 | proc-macro2 = "1" 16 | 17 | [dev-dependencies] 18 | cfg-if = "0.1.0" 19 | diesel = "1.4.0" 20 | dotenv = "0.10.0" 21 | 22 | [lib] 23 | proc-macro = true 24 | 25 | [[test]] 26 | name = "tests" 27 | 28 | [features] 29 | default = [] 30 | nightly = ["proc-macro2/nightly"] 31 | postgres = [] 32 | sqlite = [] 33 | mysql = [] 34 | 35 | [badges] 36 | travis-ci = { repository = "diesel-rs/diesel" } 37 | appveyor = { repository = "diesel-rs/diesel" } 38 | -------------------------------------------------------------------------------- /diesel/src/pg/expression/mod.rs: -------------------------------------------------------------------------------- 1 | //! PostgreSQL related query builder extensions 2 | //! 3 | //! Everything in this module is re-exported from database agnostic locations. 4 | //! You should rely on the re-exports rather than this module directly. It is 5 | //! kept separate purely for documentation purposes. 6 | 7 | pub(crate) mod array; 8 | #[doc(hidden)] 9 | pub mod array_comparison; 10 | pub(crate) mod expression_methods; 11 | pub mod extensions; 12 | #[doc(hidden)] 13 | pub mod helper_types; 14 | #[doc(hidden)] 15 | pub mod operators; 16 | 17 | mod date_and_time; 18 | 19 | /// PostgreSQL specific expression DSL methods. 20 | /// 21 | /// This module will be glob imported by 22 | /// [`diesel::dsl`](../../../dsl/index.html) when compiled with the `feature = 23 | /// "postgres"` flag. 24 | pub mod dsl { 25 | #[doc(inline)] 26 | pub use super::array_comparison::{all, any}; 27 | 28 | #[doc(inline)] 29 | pub use super::array::array; 30 | 31 | pub use super::extensions::*; 32 | } 33 | -------------------------------------------------------------------------------- /diesel_cli/tests/print_schema/print_schema_column_renaming/postgres/expected.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | /// Representation of the `with_keywords` table. 3 | /// 4 | /// (Automatically generated by Diesel.) 5 | with_keywords (fn_) { 6 | /// The `fn` column of the `with_keywords` table. 7 | /// 8 | /// Its SQL type is `Int4`. 9 | /// 10 | /// (Automatically generated by Diesel.) 11 | #[sql_name = "fn"] 12 | fn_ -> Int4, 13 | /// The `let` column of the `with_keywords` table. 14 | /// 15 | /// Its SQL type is `Int4`. 16 | /// 17 | /// (Automatically generated by Diesel.) 18 | #[sql_name = "let"] 19 | let_ -> Int4, 20 | /// The `extern` column of the `with_keywords` table. 21 | /// 22 | /// Its SQL type is `Int4`. 23 | /// 24 | /// (Automatically generated by Diesel.) 25 | #[sql_name = "extern"] 26 | extern_ -> Int4, 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /diesel/src/expression/functions/helper_types.rs: -------------------------------------------------------------------------------- 1 | #![allow(non_camel_case_types)] 2 | 3 | use crate::dsl::{AsExprOf, SqlTypeOf}; 4 | use crate::expression::grouped::Grouped; 5 | use crate::expression::operators; 6 | use crate::sql_types::Bool; 7 | 8 | /// The return type of [`not(expr)`](../dsl/fn.not.html) 9 | pub type not = operators::Not>>; 10 | 11 | /// The return type of [`max(expr)`](../dsl/fn.max.html) 12 | pub type max = super::aggregate_ordering::max::HelperType, Expr>; 13 | 14 | /// The return type of [`min(expr)`](../dsl/fn.min.html) 15 | pub type min = super::aggregate_ordering::min::HelperType, Expr>; 16 | 17 | /// The return type of [`sum(expr)`](../dsl/fn.sum.html) 18 | pub type sum = super::aggregate_folding::sum::HelperType, Expr>; 19 | 20 | /// The return type of [`avg(expr)`](../dsl/fn.avg.html) 21 | pub type avg = super::aggregate_folding::avg::HelperType, Expr>; 22 | -------------------------------------------------------------------------------- /examples/postgres/advanced-blog-cli/src/schema.rs: -------------------------------------------------------------------------------- 1 | table! { 2 | comments (id) { 3 | id -> Int4, 4 | user_id -> Int4, 5 | post_id -> Int4, 6 | body -> Text, 7 | created_at -> Timestamp, 8 | updated_at -> Timestamp, 9 | } 10 | } 11 | 12 | table! { 13 | posts (id) { 14 | id -> Int4, 15 | user_id -> Int4, 16 | title -> Text, 17 | body -> Text, 18 | created_at -> Timestamp, 19 | updated_at -> Timestamp, 20 | published_at -> Nullable, 21 | } 22 | } 23 | 24 | table! { 25 | users (id) { 26 | id -> Int4, 27 | username -> Text, 28 | hashed_password -> Text, 29 | created_at -> Timestamp, 30 | updated_at -> Timestamp, 31 | } 32 | } 33 | 34 | joinable!(comments -> posts (post_id)); 35 | joinable!(comments -> users (user_id)); 36 | joinable!(posts -> users (user_id)); 37 | 38 | allow_tables_to_appear_in_same_query!(comments, posts, users,); 39 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/compile-fail/cannot_join_to_non_joinable_table.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | use diesel::*; 5 | 6 | table! { 7 | users { 8 | id -> Integer, 9 | name -> VarChar, 10 | } 11 | } 12 | 13 | table! { 14 | posts { 15 | id -> Integer, 16 | title -> VarChar, 17 | } 18 | } 19 | 20 | table! { 21 | comments { 22 | id -> Integer, 23 | post_id -> Integer, 24 | } 25 | } 26 | 27 | joinable!(comments -> posts (post_id)); 28 | allow_tables_to_appear_in_same_query!(comments, posts, users); 29 | 30 | fn main() { 31 | let _ = users::table.inner_join(posts::table); 32 | //~^ ERROR 0277 33 | let _ = users::table.left_outer_join(posts::table); 34 | //~^ ERROR 0277 35 | 36 | // Sanity check to make sure the error is when users 37 | // become involved 38 | let join = posts::table.inner_join(comments::table); 39 | let _ = users::table.inner_join(join); 40 | //~^ ERROR 0277 41 | } 42 | -------------------------------------------------------------------------------- /diesel_compile_tests/tests/ui/identifiable_missing_pk_field.rs: -------------------------------------------------------------------------------- 1 | #[macro_use] 2 | extern crate diesel; 3 | 4 | table! { 5 | foo { 6 | id -> Integer, 7 | } 8 | } 9 | 10 | #[derive(Identifiable)] 11 | #[table_name = "foo"] 12 | struct Foo1 { 13 | } 14 | 15 | #[derive(Identifiable)] 16 | #[table_name = "foo"] 17 | struct Foo2 { 18 | #[column_name = "foo"] 19 | id: i32, 20 | } 21 | 22 | #[derive(Identifiable)] 23 | #[primary_key(bar)] 24 | #[table_name = "foo"] 25 | struct Foo3 { 26 | } 27 | 28 | #[derive(Identifiable)] 29 | #[primary_key(baz)] 30 | #[table_name = "foo"] 31 | struct Foo4 { 32 | #[column_name = "bar"] 33 | baz: i32, 34 | } 35 | 36 | #[derive(Identifiable)] 37 | #[primary_key(foo, bar)] 38 | #[table_name = "foo"] 39 | struct Foo5 { 40 | foo: i32, 41 | } 42 | 43 | #[derive(Identifiable)] 44 | #[primary_key(foo, bar)] 45 | #[table_name = "foo"] 46 | struct Foo6 { 47 | foo: i32, 48 | #[column_name = "baz"] 49 | bar: i32, 50 | } 51 | 52 | fn main() {} 53 | --------------------------------------------------------------------------------