From e2184e8d69c9d6629e7f0077bc1915026245cc28 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Sat, 1 Jun 2024 13:35:53 +0800 Subject: [PATCH] leetcode: Add 0607 --- src/leetcode/0607.sales-person/.env | 1 + src/leetcode/0607.sales-person/.gitignore | 1 + src/leetcode/0607.sales-person/Cargo.toml | 7 ++++ src/leetcode/0607.sales-person/diesel.toml | 9 +++++ .../0607.sales-person/docker-compose.yml | 13 +++++++ src/leetcode/0607.sales-person/index.md | 4 ++ .../0607.sales-person/migrations/.keep | 0 .../down.sql | 6 +++ .../up.sql | 36 ++++++++++++++++++ .../2024-06-01-032003_SalesPerson/down.sql | 2 + .../2024-06-01-032003_SalesPerson/up.sql | 15 ++++++++ .../2024-06-01-032301_Company/down.sql | 2 + .../2024-06-01-032301_Company/up.sql | 12 ++++++ .../2024-06-01-032426_Orders/down.sql | 2 + .../2024-06-01-032426_Orders/up.sql | 14 +++++++ src/leetcode/0607.sales-person/query.sql | 28 ++++++++++++++ src/leetcode/0607.sales-person/src/main.rs | 5 +++ src/leetcode/0607.sales-person/src/schema.rs | 38 +++++++++++++++++++ 18 files changed, 195 insertions(+) create mode 100644 src/leetcode/0607.sales-person/.env create mode 100644 src/leetcode/0607.sales-person/.gitignore create mode 100644 src/leetcode/0607.sales-person/Cargo.toml create mode 100644 src/leetcode/0607.sales-person/diesel.toml create mode 100644 src/leetcode/0607.sales-person/docker-compose.yml create mode 100644 src/leetcode/0607.sales-person/index.md create mode 100644 src/leetcode/0607.sales-person/migrations/.keep create mode 100644 src/leetcode/0607.sales-person/migrations/00000000000000_diesel_initial_setup/down.sql create mode 100644 src/leetcode/0607.sales-person/migrations/00000000000000_diesel_initial_setup/up.sql create mode 100644 src/leetcode/0607.sales-person/migrations/2024-06-01-032003_SalesPerson/down.sql create mode 100644 src/leetcode/0607.sales-person/migrations/2024-06-01-032003_SalesPerson/up.sql create mode 100644 src/leetcode/0607.sales-person/migrations/2024-06-01-032301_Company/down.sql create mode 100644 src/leetcode/0607.sales-person/migrations/2024-06-01-032301_Company/up.sql create mode 100644 src/leetcode/0607.sales-person/migrations/2024-06-01-032426_Orders/down.sql create mode 100644 src/leetcode/0607.sales-person/migrations/2024-06-01-032426_Orders/up.sql create mode 100644 src/leetcode/0607.sales-person/query.sql create mode 100644 src/leetcode/0607.sales-person/src/main.rs create mode 100644 src/leetcode/0607.sales-person/src/schema.rs diff --git a/src/leetcode/0607.sales-person/.env b/src/leetcode/0607.sales-person/.env new file mode 100644 index 000000000..453c39f85 --- /dev/null +++ b/src/leetcode/0607.sales-person/.env @@ -0,0 +1 @@ +DATABASE_URL=postgres://leetcode:leetcode-password@localhost/leetcode diff --git a/src/leetcode/0607.sales-person/.gitignore b/src/leetcode/0607.sales-person/.gitignore new file mode 100644 index 000000000..c2de89b27 --- /dev/null +++ b/src/leetcode/0607.sales-person/.gitignore @@ -0,0 +1 @@ +/db diff --git a/src/leetcode/0607.sales-person/Cargo.toml b/src/leetcode/0607.sales-person/Cargo.toml new file mode 100644 index 000000000..f2bf379b0 --- /dev/null +++ b/src/leetcode/0607.sales-person/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lc-0607-sales-person" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] diff --git a/src/leetcode/0607.sales-person/diesel.toml b/src/leetcode/0607.sales-person/diesel.toml new file mode 100644 index 000000000..5255561b1 --- /dev/null +++ b/src/leetcode/0607.sales-person/diesel.toml @@ -0,0 +1,9 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" +custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] + +[migrations_directory] +dir = "/home/shaohua/dev/rust/TheAlgorithms/src/leetcode/0607.sales-person/migrations" diff --git a/src/leetcode/0607.sales-person/docker-compose.yml b/src/leetcode/0607.sales-person/docker-compose.yml new file mode 100644 index 000000000..afd296b19 --- /dev/null +++ b/src/leetcode/0607.sales-person/docker-compose.yml @@ -0,0 +1,13 @@ +version: "3.0" +services: + leetcode_db: + image: postgres:15.3 + restart: always + ports: + - 127.0.0.1:5432:5432 + environment: + POSTGRES_PASSWORD: leetcode-password + POSTGRES_USER: leetcode + POSTGRES_DB: leetcode + volumes: + - ./db:/var/lib/postgresql diff --git a/src/leetcode/0607.sales-person/index.md b/src/leetcode/0607.sales-person/index.md new file mode 100644 index 000000000..77a618ba8 --- /dev/null +++ b/src/leetcode/0607.sales-person/index.md @@ -0,0 +1,4 @@ + +# + +[问题描述](../problems/) diff --git a/src/leetcode/0607.sales-person/migrations/.keep b/src/leetcode/0607.sales-person/migrations/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/src/leetcode/0607.sales-person/migrations/00000000000000_diesel_initial_setup/down.sql b/src/leetcode/0607.sales-person/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 000000000..a9f526091 --- /dev/null +++ b/src/leetcode/0607.sales-person/migrations/00000000000000_diesel_initial_setup/down.sql @@ -0,0 +1,6 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + +DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); +DROP FUNCTION IF EXISTS diesel_set_updated_at(); diff --git a/src/leetcode/0607.sales-person/migrations/00000000000000_diesel_initial_setup/up.sql b/src/leetcode/0607.sales-person/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 000000000..d68895b1a --- /dev/null +++ b/src/leetcode/0607.sales-person/migrations/00000000000000_diesel_initial_setup/up.sql @@ -0,0 +1,36 @@ +-- This file was automatically created by Diesel to setup helper functions +-- and other internal bookkeeping. This file is safe to edit, any future +-- changes will be added to existing projects as new migrations. + + + + +-- Sets up a trigger for the given table to automatically set a column called +-- `updated_at` whenever the row is modified (unless `updated_at` was included +-- in the modified columns) +-- +-- # Example +-- +-- ```sql +-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); +-- +-- SELECT diesel_manage_updated_at('users'); +-- ``` +CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ +BEGIN + EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s + FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); +END; +$$ LANGUAGE plpgsql; + +CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ +BEGIN + IF ( + NEW IS DISTINCT FROM OLD AND + NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at + ) THEN + NEW.updated_at := current_timestamp; + END IF; + RETURN NEW; +END; +$$ LANGUAGE plpgsql; diff --git a/src/leetcode/0607.sales-person/migrations/2024-06-01-032003_SalesPerson/down.sql b/src/leetcode/0607.sales-person/migrations/2024-06-01-032003_SalesPerson/down.sql new file mode 100644 index 000000000..12e48bbd1 --- /dev/null +++ b/src/leetcode/0607.sales-person/migrations/2024-06-01-032003_SalesPerson/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE SalesPerson; diff --git a/src/leetcode/0607.sales-person/migrations/2024-06-01-032003_SalesPerson/up.sql b/src/leetcode/0607.sales-person/migrations/2024-06-01-032003_SalesPerson/up.sql new file mode 100644 index 000000000..6870cd73c --- /dev/null +++ b/src/leetcode/0607.sales-person/migrations/2024-06-01-032003_SalesPerson/up.sql @@ -0,0 +1,15 @@ +-- Your SQL goes here +CREATE TABLE IF NOT EXISTS SalesPerson +( + sales_id INTEGER PRIMARY KEY, + name VARCHAR(255) NOT NULL, + salary INTEGER NOT NULL, + commission_rate INTEGER, + hire_date DATE +); + +INSERT INTO SalesPerson (sales_id, name, salary, commission_rate, hire_date) VALUES ('1', 'John', '100000', '6', '4/1/2006'); +INSERT INTO SalesPerson (sales_id, name, salary, commission_rate, hire_date) VALUES ('2', 'Amy', '12000', '5', '5/1/2010'); +INSERT INTO SalesPerson (sales_id, name, salary, commission_rate, hire_date) VALUES ('3', 'Mark', '65000', '12', '12/25/2008'); +INSERT INTO SalesPerson (sales_id, name, salary, commission_rate, hire_date) VALUES ('4', 'Pam', '25000', '25', '1/1/2005'); +INSERT INTO SalesPerson (sales_id, name, salary, commission_rate, hire_date) VALUES ('5', 'Alex', '5000', '10', '2/3/2007'); diff --git a/src/leetcode/0607.sales-person/migrations/2024-06-01-032301_Company/down.sql b/src/leetcode/0607.sales-person/migrations/2024-06-01-032301_Company/down.sql new file mode 100644 index 000000000..cdcfb58df --- /dev/null +++ b/src/leetcode/0607.sales-person/migrations/2024-06-01-032301_Company/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE Company; diff --git a/src/leetcode/0607.sales-person/migrations/2024-06-01-032301_Company/up.sql b/src/leetcode/0607.sales-person/migrations/2024-06-01-032301_Company/up.sql new file mode 100644 index 000000000..e96333b35 --- /dev/null +++ b/src/leetcode/0607.sales-person/migrations/2024-06-01-032301_Company/up.sql @@ -0,0 +1,12 @@ +-- Your SQL goes here +CREATE TABLE IF NOT EXISTS Company +( + com_id INTEGER PRIMARY KEY, + name VARCHAR(255) NOT NULL, + city VARCHAR(255) +); + +INSERT INTO Company (com_id, name, city) VALUES ('1', 'RED', 'Boston'); +INSERT INTO Company (com_id, name, city) VALUES ('2', 'ORANGE', 'New York'); +INSERT INTO Company (com_id, name, city) VALUES ('3', 'YELLOW', 'Boston'); +INSERT INTO Company (com_id, name, city) VALUES ('4', 'GREEN', 'Austin'); diff --git a/src/leetcode/0607.sales-person/migrations/2024-06-01-032426_Orders/down.sql b/src/leetcode/0607.sales-person/migrations/2024-06-01-032426_Orders/down.sql new file mode 100644 index 000000000..0fdc2c1ce --- /dev/null +++ b/src/leetcode/0607.sales-person/migrations/2024-06-01-032426_Orders/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE Orders; diff --git a/src/leetcode/0607.sales-person/migrations/2024-06-01-032426_Orders/up.sql b/src/leetcode/0607.sales-person/migrations/2024-06-01-032426_Orders/up.sql new file mode 100644 index 000000000..5ca6c93f7 --- /dev/null +++ b/src/leetcode/0607.sales-person/migrations/2024-06-01-032426_Orders/up.sql @@ -0,0 +1,14 @@ +-- Your SQL goes here +CREATE TABLE IF NOT EXISTS Orders +( + order_id INTEGER PRIMARY KEY, + order_date DATE NOT NULL DEFAULT CURRENT_DATE, + com_id INTEGER NOT NULL, + sales_id INTEGER NOT NULL, + amount INTEGER NOT NULL +); + +INSERT INTO Orders (order_id, order_date, com_id, sales_id, amount) VALUES ('1', '1/1/2014', '3', '4', '10000'); +INSERT INTO Orders (order_id, order_date, com_id, sales_id, amount) VALUES ('2', '2/1/2014', '4', '5', '5000'); +INSERT INTO Orders (order_id, order_date, com_id, sales_id, amount) VALUES ('3', '3/1/2014', '1', '1', '50000'); +INSERT INTO Orders (order_id, order_date, com_id, sales_id, amount) VALUES ('4', '4/1/2014', '1', '4', '25000'); diff --git a/src/leetcode/0607.sales-person/query.sql b/src/leetcode/0607.sales-person/query.sql new file mode 100644 index 000000000..ac753a7df --- /dev/null +++ b/src/leetcode/0607.sales-person/query.sql @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 Xu Shaohua . All rights reserved. + * Use of this source is governed by General Public License that can be found + * in the LICENSE file. + */ + +SELECT name +FROM salesperson; + +SELECT name +FROM salesperson +WHERE sales_id NOT IN + (SELECT orders.sales_id + FROM orders + INNER JOIN company + ON company.com_id = orders.com_id + WHERE company.name = 'RED'); + + +EXPLAIN +SELECT name +FROM salesperson +WHERE sales_id NOT IN + (SELECT orders.sales_id + FROM orders + LEFT JOIN company + ON company.com_id = orders.com_id + WHERE company.name = 'RED'); \ No newline at end of file diff --git a/src/leetcode/0607.sales-person/src/main.rs b/src/leetcode/0607.sales-person/src/main.rs new file mode 100644 index 000000000..23d089cc7 --- /dev/null +++ b/src/leetcode/0607.sales-person/src/main.rs @@ -0,0 +1,5 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +fn main() {} diff --git a/src/leetcode/0607.sales-person/src/schema.rs b/src/leetcode/0607.sales-person/src/schema.rs new file mode 100644 index 000000000..e304a635c --- /dev/null +++ b/src/leetcode/0607.sales-person/src/schema.rs @@ -0,0 +1,38 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + company (com_id) { + com_id -> Int4, + #[max_length = 255] + name -> Varchar, + #[max_length = 255] + city -> Nullable, + } +} + +diesel::table! { + orders (order_id) { + order_id -> Int4, + order_date -> Date, + com_id -> Int4, + sales_id -> Int4, + amount -> Int4, + } +} + +diesel::table! { + salesperson (sales_id) { + sales_id -> Int4, + #[max_length = 255] + name -> Varchar, + salary -> Int4, + commission_rate -> Nullable, + hire_date -> Nullable, + } +} + +diesel::allow_tables_to_appear_in_same_query!( + company, + orders, + salesperson, +);