/*    ==Scripting Parameters==

    Source Server Version : SQL Server 2017 (14.0.1000)
    Source Database Engine Edition : Microsoft SQL Server Enterprise Edition
    Source Database Engine Type : Standalone SQL Server

    Target Server Version : SQL Server 2017
    Target Database Engine Edition : Microsoft SQL Server Standard Edition
    Target Database Engine Type : Standalone SQL Server
*/
USE [master]
GO
/****** Object:  Database [database4]    Script Date: 23.4.2018 22:44:44 ******/
CREATE DATABASE [database4]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'database4', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\database4.mdf' , SIZE = 204800KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
 LOG ON 
( NAME = N'database4_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\database4_log.ldf' , SIZE = 139264KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
GO
ALTER DATABASE [database4] SET COMPATIBILITY_LEVEL = 140
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [database4].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [database4] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [database4] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [database4] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [database4] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [database4] SET ARITHABORT OFF 
GO
ALTER DATABASE [database4] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [database4] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [database4] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [database4] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [database4] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [database4] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [database4] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [database4] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [database4] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [database4] SET  DISABLE_BROKER 
GO
ALTER DATABASE [database4] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [database4] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [database4] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [database4] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [database4] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [database4] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [database4] SET HONOR_BROKER_PRIORITY OFF 
GO
ALTER DATABASE [database4] SET RECOVERY FULL 
GO
ALTER DATABASE [database4] SET  MULTI_USER 
GO
ALTER DATABASE [database4] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [database4] SET DB_CHAINING OFF 
GO
ALTER DATABASE [database4] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO
ALTER DATABASE [database4] SET TARGET_RECOVERY_TIME = 60 SECONDS 
GO
ALTER DATABASE [database4] SET DELAYED_DURABILITY = DISABLED 
GO
EXEC sys.sp_db_vardecimal_storage_format N'database4', N'ON'
GO
ALTER DATABASE [database4] SET QUERY_STORE = OFF
GO
USE [database4]
GO
ALTER DATABASE SCOPED CONFIGURATION SET IDENTITY_CACHE = ON;
GO
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = PRIMARY;
GO
USE [database4]
GO
/****** Object:  User [LabVIEWConveyorControl]    Script Date: 23.4.2018 22:44:44 ******/
CREATE USER [LabVIEWConveyorControl] FOR LOGIN [LabVIEWConveyorControl] WITH DEFAULT_SCHEMA=[application_schema]
GO
/****** Object:  Schema [application_schema]    Script Date: 23.4.2018 22:44:44 ******/
CREATE SCHEMA [application_schema]
GO
/****** Object:  Table [dbo].[lineStatus]    Script Date: 23.4.2018 22:44:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[lineStatus](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[linePart] [nchar](10) NOT NULL,
	[status] [nchar](30) NOT NULL,
	[time] [datetime] NOT NULL,
 CONSTRAINT [PK_lineStatus] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[objects]    Script Date: 23.4.2018 22:44:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[objects](
	[objectID] [int] IDENTITY(1,1) NOT NULL,
	[objectType] [int] NOT NULL,
	[captureTime] [datetime] NOT NULL,
 CONSTRAINT [PK_objects] PRIMARY KEY CLUSTERED 
(
	[objectID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[objectTypes]    Script Date: 23.4.2018 22:44:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[objectTypes](
	[objectTypeID] [int] IDENTITY(1,1) NOT NULL,
	[objectTypeName] [nchar](20) NOT NULL,
	[geometryType] [nchar](20) NOT NULL,
	[image] [varchar](max) NOT NULL,
 CONSTRAINT [PK_objectTypes] PRIMARY KEY CLUSTERED 
(
	[objectTypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[objects]  WITH CHECK ADD  CONSTRAINT [FK_objects_objectTypes] FOREIGN KEY([objectType])
REFERENCES [dbo].[objectTypes] ([objectTypeID])
GO
ALTER TABLE [dbo].[objects] CHECK CONSTRAINT [FK_objects_objectTypes]
GO
/****** Object:  StoredProcedure [dbo].[insertObjectTypeProcedure]    Script Date: 23.4.2018 22:44:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[insertObjectTypeProcedure]   
      @objectTypeName varchar(10),	  
	  @geometryType varchar(10), 
	  @image varchar(MAX)  
AS   

    SET NOCOUNT ON;  
    INSERT into objectTypes (objectTypeName, geometryType,image ) values (@objectTypeName,@geometryType,@image)  
    ;  
GO
/****** Object:  StoredProcedure [dbo].[loadObjectTypesProcedure]    Script Date: 23.4.2018 22:44:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[loadObjectTypesProcedure]        
AS   
    SET NOCOUNT ON;  
    SELECT * 
    FROM objectTypes  
    ;  
GO
/****** Object:  StoredProcedure [dbo].[readObjectsProcedure]    Script Date: 23.4.2018 22:44:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[readObjectsProcedure]   
      @timePeriod int   
AS   

    SET NOCOUNT ON;  
    SELECT * 
    FROM objects  
    WHERE DATEDIFF(hour,objects.captureTime,CURRENT_TIMESTAMP)< @timePeriod AND DATEDIFF(hour,objects.captureTime,CURRENT_TIMESTAMP)>=0;  
GO
/****** Object:  StoredProcedure [dbo].[readStatusProcedure]    Script Date: 23.4.2018 22:44:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[readStatusProcedure]  
    @timePeriod int     
AS   

    SET NOCOUNT ON;  
    SELECT *
    FROM lineStatus  
    WHERE DATEDIFF(hour,lineStatus.time,CURRENT_TIMESTAMP)<@timePeriod AND DATEDIFF(hour, lineStatus.time,CURRENT_TIMESTAMP)>=0;  
GO
/****** Object:  StoredProcedure [dbo].[writeObjectProcedure]    Script Date: 23.4.2018 22:44:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[writeObjectProcedure]   
      @objectType varchar(10),	  
	  @captureTime datetime 
	    
AS   

    SET NOCOUNT ON;  
    INSERT into objects (objectType,captureTime) values (@objectType,@captureTime)  
    ;  
GO
/****** Object:  StoredProcedure [dbo].[writeStatusProcedure]    Script Date: 23.4.2018 22:44:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[writeStatusProcedure]   
      @linePart varchar(10),
	  @status varchar(30),
	  @time datetime 
	    
AS   

    SET NOCOUNT ON;  
    INSERT into lineStatus (lineStatus.linePart,lineStatus.status,lineStatus.time) values (@linePart,@status,@time)  
    ;  
GO
USE [master]
GO
ALTER DATABASE [database4] SET  READ_WRITE 
GO
