Skip to content

Commit 7ae116e

Browse files
first commit
0 parents  commit 7ae116e

20 files changed

+538
-0
lines changed

.formatter.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

.gitignore

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
sql_ecto-*.tar
24+
25+
# Temporary files, for example, from tests.
26+
/tmp/

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SqlEcto
2+
3+
**TODO: Add description**
4+
5+
## Installation
6+
7+
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
8+
by adding `sql_ecto` to your list of dependencies in `mix.exs`:
9+
10+
```elixir
11+
def deps do
12+
[
13+
{:sql_ecto, "~> 0.1.0"}
14+
]
15+
end
16+
```
17+
18+
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
19+
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
20+
be found at <https://hexdocs.pm/sql_ecto>.
21+

config/config.exs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Config
2+
3+
config :sql_ecto, SqlEcto.Repo,
4+
database: "sql_ecto_repo",
5+
username: "sangeetha",
6+
password: "postgres",
7+
hostname: "localhost"
8+
9+
10+
config :sql_ecto,
11+
ecto_repos: [SqlEcto.Repo]

lib/sql_ecto.ex

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule SqlEcto do
2+
@moduledoc """
3+
Documentation for `SqlEcto`.
4+
"""
5+
6+
@doc """
7+
Hello world.
8+
9+
## Examples
10+
11+
iex> SqlEcto.hello()
12+
:world
13+
14+
"""
15+
def hello do
16+
:world
17+
end
18+
end

lib/sql_ecto/application.ex

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule SqlEcto.Application do
2+
# See https://hexdocs.pm/elixir/Application.html
3+
# for more information on OTP Applications
4+
@moduledoc false
5+
6+
use Application
7+
8+
@impl true
9+
def start(_type, _args) do
10+
children = [
11+
SqlEcto.Repo,
12+
]
13+
14+
# See https://hexdocs.pm/elixir/Supervisor.html
15+
# for other strategies and supported options
16+
opts = [strategy: :one_for_one, name: SqlEcto.Supervisor]
17+
Supervisor.start_link(children, opts)
18+
end
19+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
CREATE TABLE regions (
2+
region_id SERIAL PRIMARY KEY,
3+
region_name CHARACTER VARYING (25)
4+
);
5+
6+
CREATE TABLE countries (
7+
country_id CHARACTER (2) PRIMARY KEY,
8+
country_name CHARACTER VARYING (40),
9+
region_id INTEGER NOT NULL,
10+
FOREIGN KEY (region_id) REFERENCES regions (region_id) ON UPDATE CASCADE ON DELETE CASCADE
11+
);
12+
13+
CREATE TABLE locations (
14+
location_id SERIAL PRIMARY KEY,
15+
street_address CHARACTER VARYING (40),
16+
postal_code CHARACTER VARYING (12),
17+
city CHARACTER VARYING (30) NOT NULL,
18+
state_province CHARACTER VARYING (25),
19+
country_id CHARACTER (2) NOT NULL,
20+
FOREIGN KEY (country_id) REFERENCES countries (country_id) ON UPDATE CASCADE ON DELETE CASCADE
21+
);
22+
23+
CREATE TABLE departments (
24+
department_id SERIAL PRIMARY KEY,
25+
department_name CHARACTER VARYING (30) NOT NULL,
26+
location_id INTEGER,
27+
FOREIGN KEY (location_id) REFERENCES locations (location_id) ON UPDATE CASCADE ON DELETE CASCADE
28+
);
29+
30+
CREATE TABLE jobs (
31+
job_id SERIAL PRIMARY KEY,
32+
job_title CHARACTER VARYING (35) NOT NULL,
33+
min_salary NUMERIC (8, 2),
34+
max_salary NUMERIC (8, 2)
35+
);
36+
37+
CREATE TABLE employees (
38+
employee_id SERIAL PRIMARY KEY,
39+
first_name CHARACTER VARYING (20),
40+
last_name CHARACTER VARYING (25) NOT NULL,
41+
email CHARACTER VARYING (100) NOT NULL,
42+
phone_number CHARACTER VARYING (20),
43+
hire_date DATE NOT NULL,
44+
job_id INTEGER NOT NULL,
45+
salary NUMERIC (8, 2) NOT NULL,
46+
manager_id INTEGER,
47+
department_id INTEGER,
48+
FOREIGN KEY (job_id) REFERENCES jobs (job_id) ON UPDATE CASCADE ON DELETE CASCADE,
49+
FOREIGN KEY (department_id) REFERENCES departments (department_id) ON UPDATE CASCADE ON DELETE CASCADE,
50+
FOREIGN KEY (manager_id) REFERENCES employees (employee_id) ON UPDATE CASCADE ON DELETE CASCADE
51+
);
52+
53+
CREATE TABLE dependents (
54+
dependent_id SERIAL PRIMARY KEY,
55+
first_name CHARACTER VARYING (50) NOT NULL,
56+
last_name CHARACTER VARYING (50) NOT NULL,
57+
relationship CHARACTER VARYING (25) NOT NULL,
58+
employee_id INTEGER NOT NULL,
59+
FOREIGN KEY (employee_id) REFERENCES employees (employee_id) ON DELETE CASCADE ON UPDATE CASCADE
60+
);

lib/sql_ecto/database/insert_into_hr_database.sql

+160
Large diffs are not rendered by default.

lib/sql_ecto/hr/country.ex

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule SqlEcto.Hr.Country do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
@primary_key {:country_id, :string, []}
6+
7+
schema "countries" do
8+
9+
field :country_name, :string
10+
field :region_id, :integer
11+
12+
end
13+
14+
def changeset(countries, _params) do
15+
countries
16+
|> validate_required(:region_id)
17+
|> validate_length(:country_id, max: 2)
18+
|> validate_length(:country_name, max: 40)
19+
20+
end
21+
end

lib/sql_ecto/hr/department.ex

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule SqlEcto.Hr.Department do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
@primary_key {:department_id, :integer, []}
6+
7+
schema "departments" do
8+
9+
field :department_name, :string
10+
field :location_id, :integer
11+
12+
end
13+
14+
def changeset(departments, _params) do
15+
departments
16+
|> validate_required(:department_name)
17+
|> validate_length(:department_name, max: 30)
18+
end
19+
end

lib/sql_ecto/hr/dependent.ex

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule SqlEcto.Hr.Dependent do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
@primary_key {:dependent_id, :integer, []}
6+
7+
schema "dependents" do
8+
9+
field :first_name, :string
10+
field :last_name, :string
11+
field :relationship, :string
12+
field :employee_id, :integer
13+
14+
end
15+
16+
def changeset(dependents, _params) do
17+
dependents
18+
|> validate_required(:first_name)
19+
|> validate_required(:last_name)
20+
|> validate_required(:relationship)
21+
|> validate_required(:employee_id)
22+
|> validate_length(:first_name, max: 50)
23+
|> validate_length(:last_name, max: 50)
24+
|> validate_length(:relationship, max: 25)
25+
end
26+
end

lib/sql_ecto/hr/employee.ex

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
defmodule SqlEcto.Hr.Employee do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
@primary_key {:employee_id, :integer, []}
6+
7+
schema "employees" do
8+
9+
field :first_name, :string
10+
field :last_name, :string
11+
field :email, :string
12+
field :phone_number, :string
13+
field :hire_date, :date
14+
field :salary, :decimal
15+
field :manager_id, :integer
16+
field :job_id, :integer
17+
field :department_id, :integer
18+
19+
end
20+
21+
def changeset(employees, _params) do
22+
employees
23+
|> validate_required(:last_name)
24+
|> validate_required(:email)
25+
|> validate_required(:phone_number)
26+
|> validate_required(:hire_date)
27+
|> validate_required(:job_id)
28+
|> validate_required(:salary)
29+
|> validate_inclusion(:salary, [8, 2])
30+
|> validate_length(:first_name, max: 20)
31+
|> validate_length(:last_name, max: 25)
32+
|> validate_length(:email, max: 100)
33+
|> validate_length(:phone_number, max: 20)
34+
|> validate_length(:job_title, max: 35)
35+
|> validate_length(:job_title, max: 35)
36+
end
37+
end

lib/sql_ecto/hr/job.ex

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
defmodule SqlEcto.Hr.Job do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
@primary_key {:job_id, :integer, []}
6+
7+
schema "jobs" do
8+
9+
field :job_title, :string
10+
field :min_salary, :decimal
11+
field :max_salary, :decimal
12+
13+
end
14+
15+
def changeset(jobs, _params) do
16+
jobs
17+
|> validate_required(:job_title)
18+
|> validate_inclusion(:min_salary, [8, 2])
19+
|> validate_length(:job_title, max: 35)
20+
end
21+
end

lib/sql_ecto/hr/location.ex

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule SqlEcto.Hr.Location do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
@primary_key {:location_id, :integer, []}
6+
7+
schema "locations" do
8+
9+
field :street_address, :string
10+
field :postal_code, :string
11+
field :city, :string
12+
field :state_province, :string
13+
field :country_id, :string
14+
15+
end
16+
17+
def changeset(locations, _params) do
18+
locations
19+
|> validate_required(:city)
20+
|> validate_required(:country_id)
21+
|> validate_length(:street_address, max: 40)
22+
|> validate_length(:postal_code, max: 12)
23+
|> validate_length(:city, max: 30)
24+
|> validate_length(:state_province, max: 25)
25+
|> validate_length(:country_id, max: 2)
26+
end
27+
end

lib/sql_ecto/hr/region.ex

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule SqlEcto.Hr.Region do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
@primary_key {:region_id, :integer, []}
6+
7+
schema "regions" do
8+
9+
field :region_name, :string
10+
11+
end
12+
13+
def changeset(regions, _params) do
14+
regions
15+
|> validate_length(:region_name, max: 25)
16+
end
17+
end

lib/sql_ecto/repo.ex

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule SqlEcto.Repo do
2+
use Ecto.Repo,
3+
otp_app: :sql_ecto,
4+
adapter: Ecto.Adapters.Postgres
5+
end

mix.exs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
defmodule SqlEcto.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :sql_ecto,
7+
version: "0.1.0",
8+
elixir: "~> 1.14",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger],
18+
mod: {SqlEcto.Application, []}
19+
]
20+
end
21+
22+
# Run "mix help deps" to learn about dependencies.
23+
defp deps do
24+
[
25+
{:ecto_sql, "~> 3.0"},
26+
{:postgrex, ">= 0.0.0"}
27+
]
28+
end
29+
end

mix.lock

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
%{
2+
"db_connection": {:hex, :db_connection, "2.6.0", "77d835c472b5b67fc4f29556dee74bf511bbafecdcaf98c27d27fa5918152086", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c2f992d15725e721ec7fbc1189d4ecdb8afef76648c746a8e1cad35e3b8a35f3"},
3+
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
4+
"ecto": {:hex, :ecto, "3.10.3", "eb2ae2eecd210b4eb8bece1217b297ad4ff824b4384c0e3fdd28aaf96edd6135", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "44bec74e2364d491d70f7e42cd0d690922659d329f6465e89feb8a34e8cd3433"},
5+
"ecto_sql": {:hex, :ecto_sql, "3.10.2", "6b98b46534b5c2f8b8b5f03f126e75e2a73c64f3c071149d32987a5378b0fdbd", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.10.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.16.0 or ~> 0.17.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "68c018debca57cb9235e3889affdaec7a10616a4e3a80c99fa1d01fdafaa9007"},
6+
"postgrex": {:hex, :postgrex, "0.17.3", "c92cda8de2033a7585dae8c61b1d420a1a1322421df84da9a82a6764580c503d", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "946cf46935a4fdca7a81448be76ba3503cff082df42c6ec1ff16a4bdfbfb098d"},
7+
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
8+
}

test/sql_ecto_test.exs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule SqlEctoTest do
2+
use ExUnit.Case
3+
doctest SqlEcto
4+
5+
test "greets the world" do
6+
assert SqlEcto.hello() == :world
7+
end
8+
end

test/test_helper.exs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ExUnit.start()

0 commit comments

Comments
 (0)