if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8
go
use NucelarMonitor

if object_id(N'dbo.get_procedure', N'P') is not null drop procedure dbo.get_procedure
go
create procedure dbo.get_procedure 
    @database varchar(64), 
    @procedure varchar(64),
    @id integer output
as begin

    declare @database_id integer = (
        select top 1 id from dbo.Databases where 
            date_out is null and 
            [name] = @database
    )

    set nocount on

    if @database_id is null begin
        insert into dbo.Databases([name]) values (@database)
        set @database_id = scope_identity()
    end

    set @id = (
        select top 1 id from dbo.Procedures where 
            date_out is null and 
            [database] = @database_id and 
            [name] = @procedure
    )

    if @id is null begin
        insert into dbo.Procedures([database], [name]) values (@database_id, @procedure)
        set @id = scope_identity()
    end

end
go

if object_id(N'dbo.get_big_data', N'P') is not null drop procedure dbo.get_big_data
go
create procedure dbo.get_big_data 
    @value varchar(max),
    @id integer output
as begin

    declare @hash binary(64) = dbo.hash_big_data(@value)

    set nocount on

    set @id = (
        select top 1 id from dbo.BigData where 
            date_out is null and 
            [hash] = @hash
    )

    if @id is null begin
        insert into dbo.BigData([hash], [value]) values (@hash, @value)
        set @id = scope_identity()
    end

end
go

if object_id(N'dbo.get_message', N'P') is not null drop procedure dbo.get_message
go
create procedure dbo.get_message 
    @message varchar(128),
    @id integer output
as begin

    set nocount on

    set @id = (
        select top 1 id from dbo.Messages where 
            date_out is null and 
            [key] = @message
    )

    if @id is null begin
        insert into dbo.Messages([key]) values (@message)
        set @id = scope_identity()
    end

end
go

if object_id(N'dbo.get_type', N'P') is not null drop procedure dbo.get_type
go
create procedure dbo.get_type 
    @name varchar(32),
    @id integer output 
as begin set nocount on 

    set @id = (
        select top 1 id from dbo.Types where 
            date_out is null and 
            [name] = @name
    )

    if @id is null begin
        insert into dbo.Types([name]) values (@name)
        set @id = scope_identity()
    end

end
go

if object_id(N'dbo.get_machine', N'P') is not null drop procedure dbo.get_machine
go
create procedure dbo.get_machine 
    @key varchar(32),
    @id integer output
as begin set nocount on

    set @id = (
        select top 1 id from dbo.Machines where 
            date_out is null and 
            [key] = @key
    )

    if @id is null begin
        insert into dbo.Machines([key]) values (@key)
        set @id = scope_identity()
    end

end

if object_id(N'dbo.get_basic_data_ids', N'P') is not null drop procedure dbo.get_basic_data_ids
go
create procedure dbo.get_basic_data_ids 
    @key varchar(32),
    @candle_start datetime, 
    @candle_end datetime,
    @candle_interations integer, 
    @machine_id integer output,
    @candle_time_id integer output
as begin set nocount on

    execute dbo.get_machine @key, @machine_id output

    set @candle_time_id = (
        select top 1 id from dbo.CandlesTimes where 
            date_out is null and 
            [from] = @candle_start and 
            [to] = @candle_end and 
            iterations = @candle_interations
    )
    if @candle_time_id is null begin
        insert into dbo.CandlesTimes([from], [to], iterations) values (@candle_start, @candle_end, @candle_interations)
        set @candle_time_id = scope_identity()
    end
    set @candle_time_id = @candle_time_id

end
go

if object_id(N'dbo.get_interface', N'P') is not null drop procedure dbo.get_interface
go
create procedure dbo.get_interface 
    @name varchar(32),
    @id integer output
as begin set nocount on

    set @id = (
        select top 1 id from dbo.Interfaces where 
            date_out is null and 
            [name] = @name
    )

    if @id is null begin
        insert into dbo.Interfaces([name]) values (@name)
        set @id = scope_identity()
    end

end
go

if object_id(N'dbo.get_machine_interface', N'P') is not null drop procedure dbo.get_machine_interface
go
create procedure dbo.get_machine_interface 
    @key varchar(32),
    @name varchar(32), 
    @candle_start datetime, 
    @candle_end datetime,
    @candle_interations integer, 
    @machine_id integer output,
    @candle_time_id integer output, 
    @interface_id integer output
as begin set nocount on

    execute dbo.get_basic_data_ids 
        @key, 
        @candle_start, 
        @candle_end, 
        @candle_interations, 
        @machine_id output, 
        @candle_time_id output

    execute dbo.get_interface @name, @interface_id output

end
go