Skip to content

Commit

Permalink
add create_table function
Browse files Browse the repository at this point in the history
  • Loading branch information
justus-springer committed Nov 14, 2023
1 parent 8d5b2ce commit fddab5d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 177 deletions.
2 changes: 2 additions & 0 deletions docs/src/database_functionality.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ A connection to the online
[ldp-database](https://www.math.uni-tuebingen.de/forschung/algebra/ldp-database/)
is planned.


```@docs
DatabaseAdapter
SQLiteAdapter
SQLiteAdapterSurfaces
create_table
import_from_database
find_in_database
default_column_functions
Expand Down
16 changes: 8 additions & 8 deletions src/Database/SQLiteAdapter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
SQLiteAdapter{T} <: DatabaseAdapter{T}
An adapter to an SQLite database holding objects of type `T` where `T <:
MoriDreamSpace`. The type `T` should at least implement the following methods
(see their docstrings for more information):
`default_column_functions`, `find_in_database`, `sqlite_import_row`.
MoriDreamSpace`. The type `T` should at least implement the following methods:
[`create_table`](@ref), [`default_column_functions`](@ref),
[`find_in_database`](@ref), [`sqlite_import_row`](@ref).
"""
struct SQLiteAdapter{T} <: DatabaseAdapter{T}
Expand All @@ -21,10 +20,10 @@ end
@doc raw"""
default_column_functions(::Type{T}) where {T <: MoriDreamSpace}
Returns a `Dict{Symbol, Function}` that serves as a default for the
names of the columns to export and how to export them for a given
subtype of `MoriDreamSpace`. Should be implemented by all subtypes
of `MoriDreamSpace` where database functionality is desired.
Returns a `Dict{Symbol, Function}` that serves as a default for the names of
the columns to export and how to export them for a given subtype of
`MoriDreamSpace`. This should be implemented by all subtypes of
`MoriDreamSpace` where database functionality is desired.
The fallback definition for a general `T` returns an empty dictionary.
Expand Down Expand Up @@ -166,6 +165,7 @@ import_from_database(db :: SQLiteAdapter{T}, id :: Int) where {T <: MoriDreamSpa
_argsym_to_arg(T :: Type{<:MoriDreamSpace}, row :: Union{SQLite.Row, NamedTuple}, argsym :: Symbol) =
argsym == :variety ? sqlite_import_row(T, row) : row[argsym]


@doc raw"""
update_in_database(db :: SQLiteAdapter{T}, column_functions :: Dict{Symbol, <:Function}; sql :: String, column_function_args :: AbstractVector{Symbol}) where {T <: MoriDreamSpace}
Expand Down
147 changes: 117 additions & 30 deletions src/Database/SQLiteAdapterSurfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,120 @@ An adapter to an SQLite database holding objects of type
"""
const SQLiteAdapterSurfaces = SQLiteAdapter{SurfaceWithTorusAction}



# The list of column names together with their SQLite definition. The last
# entry in each tuple says if the column is explicitly exported. It is false
# for columns that are automatically computed from other columns, such as
# `log_canonicity`.
const _db_column_defs = [
(:is_toric, "INTEGER NOT NULL", true),
(:gen_matrix, "TEXT NOT NULL", true),
(:rays, "TEXT", true),
(:nrays, "INTEGER", true),
(:lss, "TEXT", true),
(:dss, "TEXT", true),
(:case_, "TEXT", true),
(:block_sizes, "TEXT", true),
(:nblocks, "INTEGER", true),
(:number_of_parabolic_fixed_point_curves, "INTEGER", true),
(:orientation, "INTEGER", true),
(:is_intrinsic_quadric, "INTEGER", true),
(:class_group_rank, "INTEGER", true),
(:class_group_torsion, "TEXT", true),
(:class_group_torsion_order, "INTEGER", true),
(:degree_matrix, "TEXT", true),
(:canonical_divisor_class, "TEXT", true),
(:gorenstein_index, "INTEGER", true),
(:picard_index, "INTEGER", true),
(:log_canonicity_numerator, "INTEGER", true),
(:log_canonicity_denominator, "INTEGER", true),
(:log_canonicity, "REAL AS (CAST(log_canonicity_numerator AS FLOAT) / CAST(log_canonicity_denominator AS FLOAT))", false),
(:anticanonical_self_intersection_numerator, "INTEGER", true),
(:anticanonical_self_intersection_denominator, "INTEGER", true),
(:anticanonical_self_intersection, "REAL AS (CAST(anticanonical_self_intersection_numerator AS FLOAT) / CAST(anticanonical_self_intersection_denominator AS FLOAT))", false),
(:admits_kaehler_ricci_soliton, "INTEGER", false),
(:admits_kaehler_einstein_metric, "INTEGER", true),
(:admits_sasaki_einstein_metric, "INTEGER", false),
(:is_quasismooth, "INTEGER", true),
(:is_factorial, "INTEGER", true),
(:is_smooth, "INTEGER", true),
(:number_of_singularities, "INTEGER", true),
]

@doc raw"""
create_table(db :: SQLiteAdapterSurfaces; temp = false, ifnotexists = true)
Create a new SQLite table with default column definitions holding
`SurfaceWithTorusAction`s.
# Example
Create a new SQLite database for holding `SurfaceWithTorusAction`s. The
resulting table can be inspected with
[`SQLite.jl`](https://juliadatabases.org/SQLite.jl/stable/):
```jldoctest
julia> db = SQLiteAdapterSurfaces("my_database.db", "surfaces", "surface_id")
SQLiteAdapterSurfaces(SQLite.DB("my_database.db"), "my_database.db", "surfaces", "surface_id")
julia> create_table(db);
julia> using SQLite
julia> SQLite.tables(db.db)
1-element Vector{SQLite.DBTable}:
SQLite.DBTable("surfaces", Tables.Schema:
:surface_id Union{Missing, Int64}
:is_toric Union{Missing, Int64}
:gen_matrix Union{Missing, String}
:rays Union{Missing, String}
:nrays Union{Missing, Int64}
:lss Union{Missing, String}
:dss Union{Missing, String}
:case_ Union{Missing, String}
:block_sizes Union{Missing, String}
:nblocks Union{Missing, Int64}
:number_of_parabolic_fixed_point_curves Union{Missing, Int64}
:orientation Union{Missing, Int64}
:is_intrinsic_quadric Union{Missing, Int64}
:class_group_rank Union{Missing, Int64}
:class_group_torsion Union{Missing, String}
:class_group_torsion_order Union{Missing, Int64}
:degree_matrix Union{Missing, String}
:canonical_divisor_class Union{Missing, String}
:gorenstein_index Union{Missing, Int64}
:picard_index Union{Missing, Int64}
:log_canonicity_numerator Union{Missing, Int64}
:log_canonicity_denominator Union{Missing, Int64}
:log_canonicity Union{Missing, Float64}
:anticanonical_self_intersection_numerator Union{Missing, Int64}
:anticanonical_self_intersection_denominator Union{Missing, Int64}
:anticanonical_self_intersection Union{Missing, Float64}
:admits_kaehler_ricci_soliton Union{Missing, Int64}
:admits_kaehler_einstein_metric Union{Missing, Int64}
:admits_sasaki_einstein_metric Union{Missing, Int64}
:is_quasismooth Union{Missing, Int64}
:is_factorial Union{Missing, Int64}
:is_smooth Union{Missing, Int64}
:number_of_singularities Union{Missing, Int64})
```
"""
function create_table(db :: SQLiteAdapterSurfaces; temp = false, ifnotexists = true)
temp = temp ? "TEMP" : ""
ifnotexists = ifnotexists ? "IF NOT EXISTS" : ""
columns = [string(db.primary_key, ' ', "INTEGER PRIMARY KEY ASC")]
for (column_name, column_def, _) in _db_column_defs
push!(columns, string(column_name, ' ', column_def))
end
sql = "CREATE $temp TABLE $ifnotexists $(db.table_name) ($(join(columns, ',')))"
DBInterface.execute(db.db, sql)
end



##################################################
# Default functions to compute the columns in the
# SQLite database
Expand Down Expand Up @@ -105,36 +219,9 @@ result as a language-agnostic string for database storage instead of a Julia
type.
"""
default_column_functions(::Type{<:SurfaceWithTorusAction}) = Dict([
:is_toric => _db_is_toric,
:gen_matrix => _db_gen_matrix,
:rays => _db_rays,
:nrays => _db_nrays,
:lss => _db_lss,
:dss => _db_dss,
:case_ => _db_case_,
:block_sizes => _db_block_sizes,
:nblocks => _db_nblocks,
:number_of_parabolic_fixed_point_curves => _db_number_of_parabolic_fixed_point_curves,
:orientation => _db_orientation,
:is_intrinsic_quadric => _db_is_intrinsic_quadric,
:class_group_rank => _db_class_group_rank,
:class_group_torsion => _db_class_group_torsion,
:class_group_torsion_order => _db_class_group_torsion_order,
:degree_matrix => _db_degree_matrix,
:canonical_divisor_class => _db_canonical_divisor_class,
:gorenstein_index => _db_gorenstein_index,
:picard_index => _db_picard_index,
:log_canonicity_numerator => _db_log_canonicity_numerator,
:log_canonicity_denominator => _db_log_canonicity_denominator,
:anticanonical_self_intersection_numerator => _db_anticanonical_self_intersection_numerator,
:anticanonical_self_intersection_denominator => _db_anticanonical_self_intersection_denominator,
:admits_kaehler_einstein_metric => _db_admits_kaehler_einstein_metric,
:is_quasismooth => _db_is_quasismooth,
:is_factorial => _db_is_factorial,
:is_smooth => _db_is_smooth,
:number_of_singularities => _db_number_of_singularities
])
default_column_functions(::Type{<:SurfaceWithTorusAction}) =
Dict([column_name => eval(Symbol(:_db_, column_name))
for (column_name, _, exported) in _db_column_defs if exported])


@doc raw"""
Expand Down
1 change: 1 addition & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export create_table
export AdmissibleOperation
export AdmissibleRowOperation
export admits_kaehler_einstein_metric
Expand Down
136 changes: 0 additions & 136 deletions test/Database/create.sql

This file was deleted.

5 changes: 2 additions & 3 deletions test/Database/sqlite_database.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

DB_FILE_PATH = joinpath(dirname(@__FILE__), "test.db")
# DB_FILE_PATH = "test/Database/test.db"
CREATE_TABLE_FILE_PATH = joinpath(dirname(@__FILE__), "create.sql")
# CREATE_TABLE_FILE_PATH = "test/Database/create.sql"

TABLE_NAME = "surfaces"
PRIMARY_KEY = "surface_id"

@testset "Insert surfaces into test database" begin

rm(DB_FILE_PATH, force=true)
db = SQLiteAdapter{SurfaceWithTorusAction}(DB_FILE_PATH, TABLE_NAME, PRIMARY_KEY)
DBInterface.execute(db.db, read(CREATE_TABLE_FILE_PATH, String))
create_table(db)

@testset "Insert C-star surface" begin

Expand Down

0 comments on commit fddab5d

Please sign in to comment.