forked from geral2/SQL-APIConsumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstall.sql
227 lines (168 loc) · 3.74 KB
/
Install.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
TfGMSQLAccess installation script
*/
sp_configure 'clr enabled',1
RECONFIGURE
go
ALTER DATABASE TFGM SET TRUSTWORTHY ON
--drop the assembly dependant functions first
drop function if exists [dbo].ufnTFGM_Get
drop function if exists [dbo].ufnTFGM_GetID
go
--drop and create the assembly
if exists ( select *
from sys.assemblies a
where [name] = 'TfGMSQLAccess')
drop ASSEMBLY TfGMSQLAccess
create ASSEMBLY TfGMSQLAccess
AUTHORIZATION dbo
FROM N'C:\clr\TfGMSQLAccess.dll'
WITH PERMISSION_SET = UNSAFE
GO
/*
Function to access the API which returns entire response as a nvarchar(max)
*/
create function [dbo].[ufnTFGM_Get]
(
@URL NVARCHAR (500) NULL
, @OcpApimSubscriptionKey NVARCHAR (100) NULL
, @expand NVARCHAR (1000) null
, @select NVARCHAR (1000) null
, @filter NVARCHAR (1000) null
, @orderby NVARCHAR (100) null
, @top NVARCHAR (10) null
, @skip NVARCHAR (10) null
, @count NVARCHAR (10) null
)
returns nvarchar(max)
AS EXTERNAL NAME [TfGMSQLAccess].[Functions].[UfnTFGM_Get]
go
/*
Function to access the API which returns entire response as a nvarchar(max)
*/
create function [dbo].[ufnTFGM_GetID]
(
@URL NVARCHAR (500) NULL
, @OcpApimSubscriptionKey NVARCHAR (100) NULL
, @id int null
, @select NVARCHAR (1000) null
)
returns nvarchar(max)
AS EXTERNAL NAME [TfGMSQLAccess].[Functions].[UfnTFGM_GetID]
go
/*
Store the key here
*/
drop table if exists dbo.tfgmKey
go
create table dbo.tfgmKey
(
APIkey varchar(100)
)
go
/*
Scalar function which returns the key
*/
drop function if exists dbo.ufnGetKey
go
create function dbo.ufnGetKey()
returns varchar(100)
as
begin
declare @key as varchar(100)
select @key = APIkey
from dbo.tfgmKey
return @key
end
go
/*
First stage wrapper which breaks the returned JSON in key / value pair
*/
drop function if exists [dbo].ufnGetAPIContent
go
create function dbo.ufnGetAPIContent(
@url varchar(max)
, @expand NVARCHAR (1000)
, @select NVARCHAR (1000)
, @filter NVARCHAR (1000)
, @orderby NVARCHAR (100)
, @top NVARCHAR (10)
, @skip NVARCHAR (10)
, @count NVARCHAR (10)
)
returns @ret table
(
[key] nvarchar(4000)
, [value] nvarchar(max)
, [type] tinyint
)
as
begin
declare @json as varchar(max) = [dbo].[ufnTFGM_Get] (@URL, dbo.ufnGetKey(),@expand,@select,@filter,@orderby,@top,@skip,@count)
if ISJSON(@json) = 1
begin
insert into @ret
(
[key]
, [value]
, [type]
)
SELECT [key]
, [value]
, [type]
FROM OPENJSON(@json)
end
else
begin
insert into @ret
(
[key]
, [value]
, [type]
)
select 'error'
, @json
, 1 --string value
end
return
end
go
/*
dbo.ufnGetLocationCoords
*/
drop function if exists [dbo].ufnGetLocationCoords
go
create function [dbo].ufnGetLocationCoords(@WellKnownText varchar(100))
/*
Split the contents of the entry "WellKnownText" into long and lat
This is derived from a location entry
e.g.
https://api.tfgm.com/odata/Accidents?$expand=Location&$top=2
returns an array of trafficevents items, each with the child
"Location": {
"Id": 38831250,
"LocationSpatial": {
"Geography": {
"CoordinateSystemId": 4326,
"WellKnownText": "POINT (-2.16602264021093 53.5857669129475)",
"WellKnownBinary": null
}
}
*/
returns @ret table
(
Longitude float
, Latitude float
)
as
begin
--declare @sub varchar(100) = 'POINT (-2.16602264021093 53.5857669129475)' --long,lat
declare @strip as varchar(100) = substring (@WellKnownText, CHARINDEX('(', @WellKnownText)+1,CHARINDEX(')', @WellKnownText)- CHARINDEX('(', @WellKnownText)-1)
insert into @ret (Longitude, Latitude)
values
(
substring(@strip, 0, charindex(' ', @strip))
, substring(@strip, charindex(' ', @strip),100)
)
return
end