-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DATABASE_URL=postgres://leetcode:leetcode-password@localhost/leetcode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "lc-0607-sales-person" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# | ||
|
||
[问题描述](../problems/) |
Empty file.
6 changes: 6 additions & 0 deletions
6
src/leetcode/0607.sales-person/migrations/00000000000000_diesel_initial_setup/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
36 changes: 36 additions & 0 deletions
36
src/leetcode/0607.sales-person/migrations/00000000000000_diesel_initial_setup/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
2 changes: 2 additions & 0 deletions
2
src/leetcode/0607.sales-person/migrations/2024-06-01-032003_SalesPerson/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE SalesPerson; |
15 changes: 15 additions & 0 deletions
15
src/leetcode/0607.sales-person/migrations/2024-06-01-032003_SalesPerson/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
2 changes: 2 additions & 0 deletions
2
src/leetcode/0607.sales-person/migrations/2024-06-01-032301_Company/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE Company; |
12 changes: 12 additions & 0 deletions
12
src/leetcode/0607.sales-person/migrations/2024-06-01-032301_Company/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
2 changes: 2 additions & 0 deletions
2
src/leetcode/0607.sales-person/migrations/2024-06-01-032426_Orders/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE Orders; |
14 changes: 14 additions & 0 deletions
14
src/leetcode/0607.sales-person/migrations/2024-06-01-032426_Orders/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2024 Xu Shaohua <[email protected]>. 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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Varchar>, | ||
} | ||
} | ||
|
||
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<Int4>, | ||
hire_date -> Nullable<Date>, | ||
} | ||
} | ||
|
||
diesel::allow_tables_to_appear_in_same_query!( | ||
company, | ||
orders, | ||
salesperson, | ||
); |