-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeocode for older versions.sql
326 lines (300 loc) · 10.8 KB
/
Geocode for older versions.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/****** Object: Table [Profile.Import].[PRNSWebservice.Log] Script Date: 11/30/2023 9:31:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Profile.Import].[PRNSWebservice.Log](
[LogID] [int] IDENTITY(1,1) NOT NULL,
[Job] [varchar](100) NOT NULL,
[BatchID] [varchar](100) NOT NULL,
[RowID] [int] NOT NULL,
[HttpMethod] [varchar](10) NULL,
[URL] [varchar](500) NULL,
[PostData] [varchar](max) NULL,
[ServiceCallStart] [datetime] NULL,
[ServiceCallEnd] [datetime] NULL,
[ProcessEnd] [datetime] NULL,
[Success] [bit] NULL,
[HttpResponseCode] [int] NULL,
[HttpResponse] [varchar](max) NULL,
[ResultCount] [int] NULL,
[ErrorText] [varchar](max) NULL,
PRIMARY KEY CLUSTERED
(
[LogID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: Table [Profile.Import].[PRNSWebservice.Log.Summary] Script Date: 11/30/2023 9:31:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Profile.Import].[PRNSWebservice.Log.Summary](
[LogID] [int] IDENTITY(1,1) NOT NULL,
[Job] [varchar](100) NOT NULL,
[BatchID] [varchar](100) NOT NULL,
[RecordsCount] [int] NULL,
[RowsCount] [int] NULL,
[JobStart] [datetime] NULL,
[JobEnd] [datetime] NULL,
[ErrorCount] [int] NULL
) ON [PRIMARY]
GO
/****** Object: Table [Profile.Import].[PRNSWebservice.Options] Script Date: 11/30/2023 9:31:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Profile.Import].[PRNSWebservice.Options](
[job] [varchar](100) NOT NULL,
[url] [varchar](500) NULL,
[options] [varchar](100) NULL,
[apiKey] [varchar](100) NULL,
[logLevel] [int] NULL,
[batchSize] [int] NULL,
[GetPostDataProc] [varchar](1000) NULL,
[ImportDataProc] [varchar](1000) NULL,
PRIMARY KEY CLUSTERED
(
[job] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [Profile.Import].[PRNSWebservice.Options] ([job], [url], [options], [apiKey], [logLevel], [batchSize], [GetPostDataProc], [ImportDataProc]) VALUES (N'geocode', N'https://maps.googleapis.com/maps/api/geocode/xml?address=', NULL, NULL, 2, NULL, N'[Profile.Import].[GoogleWebservice.GetGeocodeAPIData]', N'[Profile.Import].[GoogleWebservice.ParseGeocodeResults]')
GO
/****** Object: StoredProcedure [Profile.Import].[GoogleWebservice.GetGeocodeAPIData] Script Date: 11/30/2023 9:31:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [Profile.Import].[GoogleWebservice.GetGeocodeAPIData]
@Job varchar(55),
@BatchID varchar(100)
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #tmp (LogID INT, BatchID VARCHAR(100), RowID INT IDENTITY, HttpMethod VARCHAR(10), URL VARCHAR(500), PostData VARCHAR(MAX))
INSERT INTO #tmp(URL)
SELECT DISTINCT addressstring
FROM [Profile.Data].Person
WHERE (ISNULL(latitude ,0)=0
OR geoscore = 0)
and addressstring<>''
and IsActive = 1
UPDATE t SET
t.LogID = -1,
t.BatchID = @BatchID,
t.HttpMethod = 'GET',
t.URL = o.url + REPLACE(REPLACE(t.URL, '#', '' ), ' ', '+') + '&sensor=false' + isnull('&key=' + apikey, '')
FROM #tmp t
JOIN [Profile.Import].[PRNSWebservice.Options] o ON o.job = 'geocode'
IF EXISTS (SELECT 1 FROM [Profile.Import].[PRNSWebservice.Options] WHERE job = 'geocode' AND logLevel > 0)
BEGIN
DECLARE @LogIDTable TABLE (LogID int, RowID int)
INSERT INTO [Profile.Import].[PRNSWebservice.Log] (Job, BatchID, RowID, HttpMethod, URL)
OUTPUT inserted.LogID, Inserted.RowID into @LogIDTable
SELECT 'Geocode', BatchID, RowID, HttpMethod, URL FROM #tmp
UPDATE t SET t.LogID = l.LogID FROM #tmp t JOIN @LogIDTable l ON t.RowID = l.RowID
END
SELECT * FROM #tmp
END
GO
/****** Object: StoredProcedure [Profile.Import].[GoogleWebservice.ParseGeocodeResults] Script Date: 11/30/2023 9:31:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [Profile.Import].[GoogleWebservice.ParseGeocodeResults]
@BatchID varchar(100) = '',
@RowID int = -1,
@LogID int = -1,
@URL varchar (500) = '',
@Data varchar(max)
AS
BEGIN
SET NOCOUNT ON;
declare @x xml, @status varchar(100), @errorText varchar(max), @lat varchar(20), @lng varchar(20), @location_type varchar(100)
begin try
set @x = cast(@data as xml)
end try
begin catch
set @status = 'XML Parsing Error'
set @errorText = ERROR_MESSAGE()
end catch
if @x is not null
BEGIN
select @status = nref.value('status[1]','varchar(100)'),
@errorText = nref.value('error_message[1]','varchar(max)'),
@lat = nref.value('result[1]/geometry[1]/location[1]/lat[1]','varchar(20)'),
@lng = nref.value('result[1]/geometry[1]/location[1]/lng[1]','varchar(20)'),
@location_type = nref.value('result[1]/geometry[1]/location_type[1]','varchar(100)')
from @x.nodes('//GeocodeResponse[1]') as R(nref)
END
IF @status = 'OK'
BEGIN
UPDATE t SET t.Latitude = @lat, t.Longitude = @lng, t.GeoScore = case when @location_type = 'ROOFTOP' then 9 when @location_type = 'RANGE_INTERPOLATED' then 6 when @location_type = 'GEOMETRIC_CENTER' then 4 else 3 end
FROM [Profile.Data].Person t
JOIN [Profile.Import].[PRNSWebservice.Options] o ON o.job = 'geocode'
AND @URL = o.url + REPLACE(REPLACE(t.AddressString, '#', '' ), ' ', '+') + '&sensor=false' + isnull('&key=' + options, '')
AND isnull(t.GeoScore, 0) < 10
update [Profile.Import].[PRNSWebservice.Log] set ResultCount = @@ROWCOUNT where LogID = @LogID
END
ELSE
BEGIN
if @LogID > 0
begin
select @LogID = isnull(@LogID, -1) from [Profile.Import].[PRNSWebservice.Log] where BatchID = @BatchID and RowID = @RowID
end
if @LogID > 0
update [Profile.Import].[PRNSWebservice.Log] set Success = 0, HttpResponse = @Data, ErrorText = isnull(@status, '') + ' : ' + isnull(@errorText, '') where LogID = @LogID
else
insert into [Profile.Import].[PRNSWebservice.Log] (Job, BatchID, RowID, URL, HttpResponse, Success, ErrorText) Values ('Geocode', @BatchID, @RowID, @URL, @Data, 0, isnull(@status, '') + ' : ' + isnull(@errorText, ''))
END
END
GO
/****** Object: StoredProcedure [Profile.Import].[PRNSWebservice.AddLog] Script Date: 11/30/2023 9:31:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [Profile.Import].[PRNSWebservice.AddLog]
@logID BIGINT = -1,
@batchID varchar(100) = null,
@rowID int = -1,
@Job varchar(55),
@action VARCHAR(200),
@actionText varchar(max) = null,
@newLogID BIGINT OUTPUT
AS
BEGIN
DECLARE @LogLevel INT
SELECT @LogLevel = LogLevel FROM [Profile.Import].[PRNSWebservice.Options] WHERE Job=@Job
IF @LogLevel > 0 OR @action = 'Error'
BEGIN
IF @logID < 0
BEGIN
SELECT @logID = ISNULL(LogID, -1) FROM [Profile.Import].[PRNSWebservice.Log] WHERE BatchID = @batchID AND RowID = @rowID
if @logID < 0
BEGIN
DECLARE @LogIDTable TABLE (logID BIGINT)
INSERT INTO [Profile.Import].[PRNSWebservice.Log] (Job, BatchID, RowID)
OUTPUT Inserted.LogID INTO @LogIDTable
VALUES (@job, @batchID, @rowID)
SELECT @logID = LogID from @LogIDTable
END
END
IF @action='StartService'
BEGIN
UPDATE [Profile.Import].[PRNSWebservice.Log]
SET ServiceCallStart = GETDATE()
WHERE LogID = @logID
END
IF @action='EndService'
BEGIN
UPDATE [Profile.Import].[PRNSWebservice.Log]
SET ServiceCallEnd = GETDATE()
WHERE LogID = @logID
END
IF @action='RowComplete'
BEGIN
UPDATE [Profile.Import].[PRNSWebservice.Log]
SET ProcessEnd =GETDATE(),
Success= isnull(Success, 1)
WHERE LogID = @logID
END
IF @action='Error'
BEGIN
UPDATE [Profile.Import].[PRNSWebservice.Log]
SET ErrorText = isnull(ErrorText + ' ', '') + @actionText,
ProcessEnd =GETDATE(),
Success=0
WHERE LogID = @logID
END
END
Select @newLogID = @logID
END
GO
/****** Object: StoredProcedure [Profile.Import].[PRNSWebservice.CheckForErrors] Script Date: 11/30/2023 9:31:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [Profile.Import].[PRNSWebservice.CheckForErrors]
@BatchID varchar(100)
AS
BEGIN
DECLARE @ErrorCount int
select @ErrorCount = count(*) from [Profile.Import].[PRNSWebservice.Log] WHERE BatchID = @BatchID AND Success = 0
UPDATE [Profile.Import].[PRNSWebservice.Log.Summary] set JobEnd = GETDATE(), ErrorCount = @ErrorCount WHERE BatchID = @BatchID
IF @ErrorCount > 0
RAISERROR('%i Errors recorded in [Profile.Import].[PRNSWebservice.Log] for BatchID %s',16,1, @ErrorCount, @BatchID);
END
GO
/****** Object: StoredProcedure [Profile.Import].[PRNSWebservice.GetPostData] Script Date: 11/30/2023 9:31:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [Profile.Import].[PRNSWebservice.GetPostData]
@Job varchar(55)
AS
BEGIN
DECLARE @batchID UNIQUEIDENTIFIER, @logLevel int, @proc varchar(100)
select @batchID = NEWID()
select @proc = GetPostDataProc, @logLevel = logLevel from [Profile.Import].[PRNSWebservice.Options] where job = @job
IF @logLevel >= 0
BEGIN
INSERT INTO [Profile.Import].[PRNSWebservice.Log.Summary] (Job, BatchID, JobStart)
SELECT @Job, @BatchID, getdate()
END
if @proc is null
BEGIN
RAISERROR('Job doesn''t exist', 16, -1)
return
END
exec @proc @Job=@Job, @BatchID=@BatchID
END
GO
/****** Object: StoredProcedure [Profile.Import].[PRNSWebservice.ImportData] Script Date: 11/30/2023 9:31:45 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [Profile.Import].[PRNSWebservice.ImportData]
@Job varchar(55),
@BatchID varchar(100) = '',
@RowID int = -1,
@HttpResponseCode int = -1,
@LogID int = -1,
@URL varchar (500) = '',
@Data varchar(max)
AS
BEGIN
if EXISTS (SELECT 1 FROM [Profile.Import].[PRNSWebservice.Options] WHERE job = @Job AND logLevel = 2) OR @HttpResponseCode <> 200
begin
if @LogID > 0
begin
select @LogID = isnull(@LogID, -1) from [Profile.Import].[PRNSWebservice.Log] where BatchID = @BatchID and RowID = @RowID
end
if @LogID > 0
update [Profile.Import].[PRNSWebservice.Log] set HttpResponseCode = @HttpResponseCode,
HttpResponse = @Data,
Success = Case when @HttpResponseCode = 200 then null else 0 end
where LogID = @LogID
else
insert into [Profile.Import].[PRNSWebservice.Log] (Job, BatchID, RowID, URL, HttpResponseCode, HttpResponse, Success) Values (@Job, @BatchID, @RowID, @URL, @HttpResponseCode, @Data, Case when @HttpResponseCode = 200 then null else 0 end)
end
if @HttpResponseCode = 200
begin
declare @proc varchar(100), @sql nvarchar(2000)
select @proc = ImportDataProc from [Profile.Import].[PRNSWebservice.Options] where job = @job
if @proc is null
BEGIN
RAISERROR('Job doesn''t exist', 16, -1)
return
END
exec @proc @data=@data, @URL=@URL, @BatchID=@BatchID, @RowID=@RowID, @LogID=@LogID, @Job=@Job
END
END
GO