Skip to content

Commit

Permalink
Add set_vector_related and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroripper committed Dec 6, 2023
1 parent c3209f9 commit be854e3
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/OpenSQL/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,58 @@ function number_of_rows(db::SQLite.DB, table::String, column::String)
df = DBInterface.execute(db, query) |> DataFrame
return df[!, 1][1]
end

function read_vector_related(
db::SQLite.DB,
table::String,
id::String,
relation_type::String,
)
sanity_check(db, table, "id")
table_as_source = table * "_relation_"
table_as_target = "_relation_" * table

tables = table_names(db)

table_relations_source = tables[findall(x -> startswith(x, table_as_source), tables)]

table_relations_target = tables[findall(x -> endswith(x, table_as_target), tables)]

related = []

for relation_table in table_relations_source
query = "SELECT target_id FROM $relation_table WHERE source_id = '$id' AND relation_type = '$relation_type'"
df = DBInterface.execute(db, query) |> DataFrame
if !isempty(df)
push!(related, df[!, 1][1])
end
end

for relation_table in table_relations_target
query = "SELECT source_id FROM $relation_table WHERE target_id = '$id' AND relation_type = '$relation_type'"
df = DBInterface.execute(db, query) |> DataFrame
if !isempty(df)
push!(related, df[!, 1][1])

Check warning on line 111 in src/OpenSQL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/OpenSQL/read.jl#L108-L111

Added lines #L108 - L111 were not covered by tests
end
end

Check warning on line 113 in src/OpenSQL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/OpenSQL/read.jl#L113

Added line #L113 was not covered by tests

return related
end

function read_related(
db::SQLite.DB,
table_1::String,
table_2::String,
table_1_id::String,
relation_type::String,
)
id_parameter_on_table_1 = lowercase(table_2) * "_" * relation_type

query = "SELECT $id_parameter_on_table_1 FROM $table_1 WHERE id = '$table_1_id'"
df = DBInterface.execute(db, query) |> DataFrame
if isempty(df)
error("id \"$table_1_id\" does not exist in table \"$table_1\".")

Check warning on line 130 in src/OpenSQL/read.jl

View check run for this annotation

Codecov / codecov/patch

src/OpenSQL/read.jl#L130

Added line #L130 was not covered by tests
end
result = df[!, 1][1]
return result
end
19 changes: 18 additions & 1 deletion src/OpenSQL/update.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,32 @@ function set_related!(
table2::String,
id_1::String,
id_2::String,
relation_type::String,
)
id_parameter_on_table_1 = lowercase(table2) * "_id"
id_parameter_on_table_1 = lowercase(table2) * "_" * relation_type
SQLite.execute(
db,
"UPDATE $table1 SET $id_parameter_on_table_1 = '$id_2' WHERE id = '$id_1'",
)
return nothing
end

function set_vector_related!(
db::DBInterface.Connection,
table1::String,
table2::String,
id_1::String,
id_2::String,
relation_type::String,
)
relation_table = _relation_table_name(table1, table2)
SQLite.execute(
db,
"INSERT INTO $relation_table (source_id, target_id, relation_type) VALUES ('$id_1', '$id_2', '$relation_type')",
)
return nothing
end

function set_related_time_series!(
db::DBInterface.Connection,
table::String;
Expand Down
27 changes: 26 additions & 1 deletion src/sql_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ end

get_collections(db::OpenSQL.DB) = return OpenSQL.table_names(db)

Check warning on line 53 in src/sql_interface.jl

View check run for this annotation

Codecov / codecov/patch

src/sql_interface.jl#L53

Added line #L53 was not covered by tests

get_related(
db::OpenSQL.DB,
source::String,
target::String,
source_id::String,
relation_type::String,
) = OpenSQL.read_related(db, source, target, source_id, relation_type)

get_vector_related(
db::OpenSQL.DB,
source::String,
source_id::String,
relation_type::String,
) = OpenSQL.read_vector_related(db, source, source_id, relation_type)

# Modification
create_element!(db::OpenSQL.DB, collection::String; kwargs...) =
OpenSQL.create_element!(db, collection; kwargs...)
Expand Down Expand Up @@ -81,7 +96,17 @@ set_related!(
target::String,
source_id::String,
target_id::String,
) = OpenSQL.set_related!(db, source, target, source_id, target_id)
relation_type::String,
) = OpenSQL.set_related!(db, source, target, source_id, target_id, relation_type)

set_vector_related!(
db::OpenSQL.DB,
source::String,
target::String,
source_id::String,
target_id::String,
relation_type::String,
) = OpenSQL.set_vector_related!(db, source, target, source_id, target_id, relation_type)

delete_relation!(
db::OpenSQL.DB,
Expand Down
106 changes: 106 additions & 0 deletions test/OpenSQL/create_case.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,25 @@ function create_case_1()
"Resource",
"Plant 1",
"R1",
"id",
)
PSRI.set_related!(
db,
"Plant",
"Resource",
"Plant 2",
"R2",
"id",
)

@test PSRI.get_related(
db,
"Plant",
"Resource",
"Plant 1",
"id",
) == "R1"

@test PSRI.get_parm(db, "Plant", "resource_id", "Plant 1") == "R1"

PSRI.delete_relation!(
Expand Down Expand Up @@ -150,4 +160,100 @@ function create_case_1()
return rm(joinpath(case_path, "psrclasses.sqlite"))
end

function create_case_relations()
case_path = joinpath(@__DIR__, "data", "case_1")
if isfile(joinpath(case_path, "psrclasses.sqlite"))
rm(joinpath(case_path, "psrclasses.sqlite"))
end

db = PSRI.create_study(
PSRI.SQLInterface();
data_path = case_path,
schema = "toy_schema",
study_collection = "Configuration",
id = "Toy Case",
value1 = 1.0,
)

PSRI.create_element!(
db,
"Plant";
id = "Plant 1",
capacity = 50.0,
)

PSRI.create_element!(
db,
"Plant";
id = "Plant 2",
)

PSRI.create_element!(
db,
"Cost";
id = "Cost 1",
value = 30.0,
)

PSRI.create_element!(
db,
"Cost";
id = "Cost 2",
value = 40.0,
)

PSRI.set_vector_related!(
db,
"Plant",
"Cost",
"Plant 1",
"Cost 1",
"sometype",
)

PSRI.set_vector_related!(
db,
"Plant",
"Cost",
"Plant 1",
"Cost 2",
"sometype2",
)

PSRI.set_vector_related!(
db,
"Plant",
"Cost",
"Plant 2",
"Cost 1",
"sometype",
)

@test PSRI.get_vector_related(
db,
"Plant",
"Plant 1",
"sometype",
) == ["Cost 1"]

@test PSRI.get_vector_related(
db,
"Plant",
"Plant 1",
"sometype2",
) == ["Cost 2"]

# @test PSRI.get_vector_related(
# db,
# "Plant",
# "Plant 2",
# "sometype"
# ) == ["Cost 1"]

PSRI.OpenSQL.close(db)

return rm(joinpath(case_path, "psrclasses.sqlite"))
end

create_case_1()
create_case_relations()

0 comments on commit be854e3

Please sign in to comment.