-
-
Notifications
You must be signed in to change notification settings - Fork 20
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
14 changed files
with
910 additions
and
21 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
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
Binary file not shown.
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,31 @@ | ||
DROP DATABASE IF EXISTS njord_db; | ||
|
||
CREATE DATABASE njord_db; | ||
|
||
USE njord_db; | ||
|
||
-- Table: users | ||
CREATE TABLE users ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, -- Auto incrementing primary key for the user ID | ||
username VARCHAR(255) NOT NULL, -- Username field | ||
email VARCHAR(255) NOT NULL, -- Email field | ||
address VARCHAR(255) -- Address field | ||
); | ||
|
||
-- Table: categories | ||
CREATE TABLE categories ( | ||
id INT PRIMARY KEY, -- Primary key for categories | ||
name VARCHAR(255) NOT NULL -- Name of the category | ||
); | ||
|
||
-- Table: products | ||
CREATE TABLE products ( | ||
id INT PRIMARY KEY, -- Primary key for products | ||
name VARCHAR(255) NOT NULL, -- Product name | ||
description TEXT, -- Product description | ||
price DECIMAL(10, 2) NOT NULL, -- Price with up to two decimal places | ||
stock_quantity INT NOT NULL, -- Stock quantity | ||
category_id INT NOT NULL, -- Foreign key to categories (one-to-one relationship) | ||
discount DECIMAL(5, 2) DEFAULT 0.00, -- Discount field with default value | ||
FOREIGN KEY (category_id) REFERENCES categories(id) -- Foreign key constraint to categories table | ||
); |
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
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
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,52 @@ | ||
use super::User; | ||
use njord::condition::{Condition, Value}; | ||
use njord::keys::AutoIncrementPrimaryKey; | ||
use njord::mysql; | ||
use std::vec; | ||
|
||
#[test] | ||
fn delete_row() { | ||
insert_row(); | ||
|
||
let url = "mysql://njord_user:njord_password@localhost:3306/njord_db"; | ||
let mut conn = mysql::open(url); | ||
|
||
match conn { | ||
Ok(ref mut c) => { | ||
let result = mysql::delete(c) | ||
.from(User::default()) | ||
.where_clause(Condition::Eq( | ||
"username".to_string(), | ||
Value::Literal("chasewillden2".to_string()), | ||
)) | ||
.build(); | ||
assert!(result.is_ok()); | ||
} | ||
Err(e) => { | ||
panic!("Failed to DELETE: {:?}", e); | ||
} | ||
} | ||
} | ||
|
||
/// Helper function to insert a row to be deleted | ||
fn insert_row() { | ||
let url = "mysql://njord_user:njord_password@localhost:3306/njord_db"; | ||
let mut conn = mysql::open(url); | ||
|
||
let table_row: User = User { | ||
id: AutoIncrementPrimaryKey::default(), | ||
username: "chasewillden2".to_string(), | ||
email: "[email protected]".to_string(), | ||
address: "Some Random Address 1".to_string(), | ||
}; | ||
|
||
match conn { | ||
Ok(ref mut c) => { | ||
let result = mysql::insert(c, vec![table_row]); | ||
assert!(result.is_ok()); | ||
} | ||
Err(e) => { | ||
panic!("Failed to INSERT: {:?}", e); | ||
} | ||
} | ||
} |
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,27 @@ | ||
use super::User; | ||
use njord::keys::AutoIncrementPrimaryKey; | ||
use njord::mysql; | ||
use std::vec; | ||
|
||
#[test] | ||
fn insert_row() { | ||
let url = "mysql://njord_user:njord_password@localhost:3306/njord_db"; | ||
let mut conn = mysql::open(url); | ||
|
||
let table_row: User = User { | ||
id: AutoIncrementPrimaryKey::default(), | ||
username: "chasewillden".to_string(), | ||
email: "[email protected]".to_string(), | ||
address: "Some Random Address 1".to_string(), | ||
}; | ||
|
||
match conn { | ||
Ok(ref mut c) => { | ||
let result = mysql::insert(c, vec![table_row]); | ||
assert!(result.is_ok()); | ||
} | ||
Err(e) => { | ||
panic!("Failed to INSERT: {:?}", e); | ||
} | ||
} | ||
} |
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,56 @@ | ||
mod delete_test; | ||
mod insert_test; | ||
mod open_test; | ||
mod select_joins_test; | ||
mod select_test; | ||
mod update_test; | ||
|
||
use njord::keys::{AutoIncrementPrimaryKey, PrimaryKey}; | ||
use njord::table::Table; | ||
use njord_derive::Table; | ||
|
||
#[derive(Table, Clone)] | ||
#[table_name = "users"] | ||
pub struct User { | ||
pub id: AutoIncrementPrimaryKey<usize>, | ||
pub username: String, | ||
pub email: String, | ||
pub address: String, | ||
} | ||
|
||
#[derive(Table)] | ||
#[table_name = "users"] | ||
pub struct UserWithSubQuery { | ||
pub id: AutoIncrementPrimaryKey<usize>, | ||
pub username: String, | ||
pub email: String, | ||
pub address: String, | ||
pub additional_address: String, | ||
} | ||
|
||
#[derive(Table)] | ||
#[table_name = "categories"] | ||
pub struct Category { | ||
pub id: PrimaryKey<usize>, | ||
pub name: String, | ||
} | ||
|
||
#[derive(Table)] | ||
#[table_name = "products"] | ||
pub struct Product { | ||
pub id: PrimaryKey<usize>, | ||
pub name: String, | ||
pub description: String, | ||
pub price: f64, | ||
pub stock_quantity: usize, | ||
pub category: Category, // one-to-one relationship | ||
pub discount: f64, | ||
} | ||
|
||
#[derive(Table)] | ||
#[table_name = "users"] | ||
pub struct UsersWithJoin { | ||
username: String, | ||
price: f64, | ||
name: String, | ||
} |
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,8 @@ | ||
use njord::mysql; | ||
|
||
#[test] | ||
fn open_db() { | ||
let url = "mysql://njord_user:njord_password@localhost:3306/njord_db"; | ||
let result = mysql::open(url); | ||
assert!(result.is_ok()); | ||
} |
Oops, something went wrong.