Skip to content

Commit

Permalink
leetcode: Add 0607
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jun 1, 2024
1 parent 1bab9bb commit e2184e8
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/leetcode/0607.sales-person/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=postgres://leetcode:leetcode-password@localhost/leetcode
1 change: 1 addition & 0 deletions src/leetcode/0607.sales-person/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/db
7 changes: 7 additions & 0 deletions src/leetcode/0607.sales-person/Cargo.toml
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]
9 changes: 9 additions & 0 deletions src/leetcode/0607.sales-person/diesel.toml
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"
13 changes: 13 additions & 0 deletions src/leetcode/0607.sales-person/docker-compose.yml
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
4 changes: 4 additions & 0 deletions src/leetcode/0607.sales-person/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
Empty file.
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();
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;
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;
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');
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;
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');
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;
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');
28 changes: 28 additions & 0 deletions src/leetcode/0607.sales-person/query.sql
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');
5 changes: 5 additions & 0 deletions src/leetcode/0607.sales-person/src/main.rs
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() {}
38 changes: 38 additions & 0 deletions src/leetcode/0607.sales-person/src/schema.rs
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,
);

0 comments on commit e2184e8

Please sign in to comment.