Skip to content

Commit

Permalink
Add SecurutyType db table; Update reference data.
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed Dec 11, 2012
1 parent fdd19b2 commit 8c773d2
Show file tree
Hide file tree
Showing 10 changed files with 637 additions and 554 deletions.
3 changes: 3 additions & 0 deletions src/Database/Database.refactorlog
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<Operations Version="1.0" xmlns="http://schemas.microsoft.com/sqlserver/dac/Serialization/2012/02">
</Operations>
24 changes: 22 additions & 2 deletions src/Database/Database.sqlproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,32 @@
<Build Include="dbo\Tables\Quote.sql" />
<Build Include="dbo\Tables\Exchange.sql" />
<Build Include="dbo\Tables\Version.sql" />
<Build Include="dbo\Tables\SecurityType.sql" />
</ItemGroup>
<ItemGroup>
<PostDeploy Include="Scripts\PostDeployment.sql" />
</ItemGroup>
<ItemGroup>
<None Include="Scripts\ReferenceData.Exchange.sql" />
<None Include="Scripts\ReferenceData.Security.sql" />
<None Include="Scripts\ReferenceData.Exchange.csv">
<CopyToOutputDirectory>DoNotCopy</CopyToOutputDirectory>
</None>
<None Include="Scripts\ReferenceData.Security.csv">
<CopyToOutputDirectory>DoNotCopy</CopyToOutputDirectory>
</None>
<None Include="Scripts\ReferenceData.SecurityType.csv">
<CopyToOutputDirectory>DoNotCopy</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<SqlCmdVariable Include="ScriptsDir">
<DefaultValue>$(ProjectDir)Scripts\</DefaultValue>
</SqlCmdVariable>
</ItemGroup>
<ItemGroup>
<RefactorLog Include="Database.refactorlog" />
</ItemGroup>
<PropertyGroup>
<PreBuildEvent>
</PreBuildEvent>
</PropertyGroup>
</Project>
90 changes: 88 additions & 2 deletions src/Database/Scripts/PostDeployment.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,88 @@
:r .\ReferenceData.Exchange.sql
:r .\ReferenceData.Security.sql
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL DROP TABLE #Temp;

SELECT TOP(0) CAST([ExchangeID] AS SMALLINT) AS [ExchangeID], [Code], [Name], [Economy], [Headquarters], [MarketCap], [TradeValue] INTO #Temp FROM [dbo].[Exchange];

BULK INSERT #Temp FROM '$(ScriptsDir)ReferenceData.Exchange.csv'
WITH (FIRSTROW = 2, FIELDTERMINATOR = ' | ', ROWTERMINATOR = '\n', KEEPNULLS);
GO

SET IDENTITY_INSERT [dbo].[Exchange] ON;
GO

MERGE INTO [dbo].[Exchange] AS Target
USING (SELECT [ExchangeID], [Code], [Name], [Economy], [Headquarters], [MarketCap], [TradeValue] FROM #Temp) AS Source
ON Target.[ExchangeID] = Source.[ExchangeID]
-- update matched rows
WHEN MATCHED THEN
UPDATE SET [Code] = Source.[Code], [Name] = Source.[Name], [Economy] = Source.[Economy], [Headquarters] = Source.[Headquarters], [MarketCap] = Source.[MarketCap], [TradeValue] = Source.[TradeValue]
-- insert new rows
WHEN NOT MATCHED BY TARGET THEN
INSERT ([ExchangeID], [Code], [Name], [Economy], [Headquarters], [MarketCap], [TradeValue])
VALUES ([ExchangeID], [Code], [Name], [Economy], [Headquarters], [MarketCap], [TradeValue]);
-- delete rows that are in the target but not the source
-- WHEN NOT MATCHED BY SOURCE THEN
-- DELETE;
GO

SET IDENTITY_INSERT [dbo].[Exchange] OFF;
GO

IF OBJECT_ID('tempdb..#Temp') IS NOT NULL DROP TABLE #Temp;
GO

SELECT TOP(0) [TypeID], [Name] INTO #Temp FROM [dbo].[SecurityType];

BULK INSERT #Temp FROM '$(ScriptsDir)ReferenceData.SecurityType.csv'
WITH (FIRSTROW = 2, FIELDTERMINATOR = ' | ', ROWTERMINATOR = '\n', KEEPNULLS);
GO

MERGE INTO [dbo].[SecurityType] AS Target
USING (SELECT [TypeID], [Name] FROM #Temp) AS Source
ON Target.[TypeID] = Source.[TypeID]
-- update matched rows
WHEN MATCHED THEN
UPDATE SET [Name] = Source.[Name]
-- insert new rows
WHEN NOT MATCHED BY TARGET THEN
INSERT ([TypeID], [Name])
VALUES ([TypeID], [Name]);
-- delete rows that are in the target but not the source
-- WHEN NOT MATCHED BY SOURCE THEN
-- DELETE;
GO

SET IDENTITY_INSERT [dbo].[Security] OFF;
GO

IF OBJECT_ID('tempdb..#Temp') IS NOT NULL DROP TABLE #Temp;
GO

SELECT TOP(0) CAST([SecurityID] AS SMALLINT) AS [SecurityID], [ExchangeID], [TypeID], [Ticker], [Name] INTO #Temp FROM [dbo].[Security];

SET IDENTITY_INSERT [dbo].[Security] ON;
GO

BULK INSERT #Temp FROM '$(ScriptsDir)ReferenceData.Security.csv'
WITH (FIRSTROW = 2, FIELDTERMINATOR = ' | ', ROWTERMINATOR = '\n', KEEPNULLS);
GO

MERGE INTO [dbo].[Security] AS Target
USING (SELECT [SecurityID], [ExchangeID], [TypeID], [Ticker], [Name] FROM #Temp) AS Source
ON Target.[SecurityID] = Source.[SecurityID]
-- update matched rows
WHEN MATCHED THEN
UPDATE SET [ExchangeID] = Source.[ExchangeID], [TypeID] = Source.[TypeID], [Ticker] = Source.[Ticker], [Name] = Source.[Name]
-- insert new rows
WHEN NOT MATCHED BY TARGET THEN
INSERT ([SecurityID], [ExchangeID], [TypeID], [Ticker], [Name])
VALUES ([SecurityID], [ExchangeID], [TypeID], [Ticker], [Name]);
-- delete rows that are in the target but not the source
-- WHEN NOT MATCHED BY SOURCE THEN
-- DELETE;
GO

SET IDENTITY_INSERT [dbo].[Security] OFF;
GO

IF OBJECT_ID('tempdb..#Temp') IS NOT NULL DROP TABLE #Temp;
GO
5 changes: 5 additions & 0 deletions src/Database/Scripts/ReferenceData.Exchange.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ExchangeID | Code | Name | Economy | Headquarters | MarketCap | TradeValue
1 | NYSE | NYSE Euronext | US,EU | New York City | 14242 | 20161
2 | NASDAQ | NASDAQ OMX | US,EU | New York City | 4687 | 13552
3 | TSE | Tokyo Stock Exchange | JP | Tokyo | 3325 | 3972
17 | MICEX | Moscow Exchange | RU | Moscow | 800 | 514
26 changes: 0 additions & 26 deletions src/Database/Scripts/ReferenceData.Exchange.sql

This file was deleted.

Loading

0 comments on commit 8c773d2

Please sign in to comment.