Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. USE PROD
  2. GO
  3.  
  4.  
  5. CREATE FUNCTION [dbo].[CompareXml]
  6. (
  7. @xml1 XML,
  8. @xml2 XML
  9. )
  10. RETURNS INT
  11. AS
  12. BEGIN
  13. DECLARE @ret INT
  14. SELECT @ret = 0
  15.  
  16.  
  17. -- -------------------------------------------------------------
  18. -- If one of the arguments is NULL then we assume that they are
  19. -- not equal.
  20. -- -------------------------------------------------------------
  21. IF @xml1 IS NULL OR @xml2 IS NULL
  22. BEGIN
  23. RETURN 1
  24. END
  25.  
  26. -- -------------------------------------------------------------
  27. -- Match the name of the elements
  28. -- -------------------------------------------------------------
  29. IF (SELECT @xml1.value('(local-name((/*)[1]))','VARCHAR(MAX)'))
  30. <>
  31. (SELECT @xml2.value('(local-name((/*)[1]))','VARCHAR(MAX)'))
  32. BEGIN
  33. RETURN 1
  34. END
  35.  
  36. ---------------------------------------------------------------
  37. --Match the value of the elements
  38. ---------------------------------------------------------------
  39. IF((@xml1.query('count(/*)').value('.','INT') = 1) AND (@xml2.query('count(/*)').value('.','INT') = 1))
  40. BEGIN
  41. DECLARE @elValue1 VARCHAR(MAX), @elValue2 VARCHAR(MAX)
  42.  
  43. SELECT
  44. @elValue1 = @xml1.value('((/*)[1])','VARCHAR(MAX)'),
  45. @elValue2 = @xml2.value('((/*)[1])','VARCHAR(MAX)')
  46.  
  47. IF @elValue1 <> @elValue2
  48. BEGIN
  49. RETURN 1
  50. END
  51. END
  52.  
  53. -- -------------------------------------------------------------
  54. -- Match the number of attributes
  55. -- -------------------------------------------------------------
  56. DECLARE @attCnt1 INT, @attCnt2 INT
  57. SELECT
  58. @attCnt1 = @xml1.query('count(/*/@*)').value('.','INT'),
  59. @attCnt2 = @xml2.query('count(/*/@*)').value('.','INT')
  60.  
  61. IF @attCnt1 <> @attCnt2 BEGIN
  62. RETURN 1
  63. END
  64.  
  65.  
  66. -- -------------------------------------------------------------
  67. -- Match the attributes of attributes
  68. -- Here we need to run a loop over each attribute in the
  69. -- first XML element and see if the same attribut exists
  70. -- in the second element. If the attribute exists, we
  71. -- need to check if the value is the same.
  72. -- -------------------------------------------------------------
  73. DECLARE @cnt INT, @cnt2 INT
  74. DECLARE @attName VARCHAR(MAX)
  75. DECLARE @attValue VARCHAR(MAX)
  76.  
  77. SELECT @cnt = 1
  78.  
  79. WHILE @cnt <= @attCnt1
  80. BEGIN
  81. SELECT @attName = NULL, @attValue = NULL
  82. SELECT
  83. @attName = @xml1.value(
  84. 'local-name((/*/@*[sql:variable("@cnt")])[1])',
  85. 'varchar(MAX)'),
  86. @attValue = @xml1.value(
  87. '(/*/@*[sql:variable("@cnt")])[1]',
  88. 'varchar(MAX)')
  89.  
  90. -- check if the attribute exists in the other XML document
  91. IF @xml2.exist(
  92. '(/*/@*[local-name()=sql:variable("@attName")])[1]'
  93. ) = 0
  94. BEGIN
  95. RETURN 1
  96. END
  97.  
  98. IF @xml2.value(
  99. '(/*/@*[local-name()=sql:variable("@attName")])[1]',
  100. 'varchar(MAX)')
  101. <>
  102. @attValue
  103. BEGIN
  104. RETURN 1
  105. END
  106.  
  107. SELECT @cnt = @cnt + 1
  108. END
  109.  
  110. -- -------------------------------------------------------------
  111. -- Match the number of child elements
  112. -- -------------------------------------------------------------
  113. DECLARE @elCnt1 INT, @elCnt2 INT
  114. SELECT
  115. @elCnt1 = @xml1.query('count(/*/*)').value('.','INT'),
  116. @elCnt2 = @xml2.query('count(/*/*)').value('.','INT')
  117.  
  118.  
  119. IF @elCnt1 <> @elCnt2
  120. BEGIN
  121. RETURN 1
  122. END
  123.  
  124.  
  125. -- -------------------------------------------------------------
  126. -- Start recursion for each child element
  127. -- -------------------------------------------------------------
  128. SELECT @cnt = 1
  129. SELECT @cnt2 = 1
  130. DECLARE @x1 XML, @x2 XML
  131. DECLARE @noMatch INT
  132.  
  133. WHILE @cnt <= @elCnt1
  134. BEGIN
  135.  
  136. SELECT @x1 = @xml1.query('/*/*[sql:variable("@cnt")]')
  137. --RETURN CONVERT(VARCHAR(MAX),@x1)
  138. WHILE @cnt2 <= @elCnt2
  139. BEGIN
  140. SELECT @x2 = @xml2.query('/*/*[sql:variable("@cnt2")]')
  141. SELECT @noMatch = dbo.CompareXml( @x1, @x2 )
  142. IF @noMatch = 0 BREAK
  143. SELECT @cnt2 = @cnt2 + 1
  144. END
  145.  
  146. SELECT @cnt2 = 1
  147.  
  148. IF @noMatch = 1
  149. BEGIN
  150. RETURN 1
  151. END
  152.  
  153. SELECT @cnt = @cnt + 1
  154. END
  155.  
  156. RETURN @ret
  157. END
  158.  
  159. GO
  160.  
  161.  
  162. DECLARE @ExcludeMerchantsList TABLE(MerchantCodeId CHAR(4));
  163.  
  164. INSERT INTO @ExcludeMerchantsList
  165. VALUES
  166. ('1066'),
  167. ('1501'),
  168. ('1500'),
  169. ('1502'),
  170. ('1503'),
  171. ('1504'),
  172. ('1505'),
  173. ('1506'),
  174. ('1507'),
  175. ('1508'),
  176. ('1509'),
  177. ('1510'),
  178. ('1511'),
  179. ('1512'),
  180. ('1513'),
  181. ('1514'),
  182. ('1515'),
  183. ('1516'),
  184. ('1517'),
  185. ('1518'),
  186. ('1519'),
  187. ('1520'),
  188. ('1521'),
  189. ('1522'),
  190. ('1523'),
  191. ('1524'),
  192. ('1525'),
  193. ('1526'),
  194. ('1527'),
  195. ('1528'),
  196. ('2011'),
  197. ('4001'),
  198. ('4438'),
  199. ('4449'),
  200. ('4550'),
  201. ('4552'),
  202. ('4557'),
  203. ('4580'),
  204. ('5108'),
  205. ('5158'),
  206. ('5172'),
  207. ('5173'),
  208. ('5188'),
  209. ('5647'),
  210. ('5736'),
  211. ('6015'),
  212. ('6073');
  213.  
  214.  
  215.  
  216. DECLARE @FullAccessExpressFilters NVARCHAR(MAX) = '<Filters><FilterCountries><Add ID="*"></Add></FilterCountries><FilterBanks><Add ID="*"></Add></FilterBanks><FilterCurrencies><Add ID="*"></Add></FilterCurrencies><FilterChannels><Add ID="*"></Add></FilterChannels></Filters>';
  217.  
  218.  
  219. SELECT [MerchantId], [m].[MERCHANT_ID], [XMLConfiguration]
  220. FROM [dbo].[MerchantSetup] [ms]
  221. JOIN [dbo].[Merchants] [m] ON [m].[Id] = [ms].[MerchantId]
  222. WHERE CAST(CONVERT(NVARCHAR(MAX), REPLACE([XMLConfiguration], 'UTF-8', 'UTF-16')) AS XML).value('(/Configuration/DirectConfiguration/@enabled)[1]', 'Nvarchar(max)') = 'true'
  223. AND [ActiveConfiguration] = 1
  224. AND datalength(CAST(CONVERT(NVARCHAR(MAX), REPLACE(REPLACE(REPLACE([XMLConfiguration], 'UTF-8', 'UTF-16'), '<Parameters/>', ''), '<Parameters />', '')) AS XML).query('/Configuration/DirectConfiguration/Filters')) = 5 -- Empty
  225. AND dbo.CompareXml(@FullAccessExpressFilters, CAST(CAST(CONVERT(NVARCHAR(MAX), REPLACE(REPLACE(REPLACE([XMLConfiguration], 'UTF-8', 'UTF-16'), '<Parameters/>', ''), '<Parameters />', '')) AS XML).query('/Configuration/Filters') AS NVARCHAR(MAX))) > 0
  226. AND [m].MERCHANT_ALLOWED = 1
  227. AND [m].MERCHANT_ACTIVE = 1
  228. AND [XMLConfiguration] IS NOT NULL
  229. AND [m].[MERCHANT_ID] NOT IN (SELECT MerchantCodeId FROM @ExcludeMerchantsList)
  230. AND [XMLConfiguration] LIKE '%DirectConfiguration%enabled="true"%'
  231.  
  232. UNION
  233.  
  234. SELECT [MS].[MerchantId], [m].[MERCHANT_ID], [XMLConfiguration]
  235.  
  236. FROM [dbo].[MerchantSetup] [ms]
  237. JOIN [dbo].[Merchants] [m] ON [m].[Id] = [ms].[MerchantId]
  238. WHERE CAST(CONVERT(NVARCHAR(MAX), REPLACE([XMLConfiguration], 'UTF-8', 'UTF-16')) AS XML).value('(/Configuration/DirectConfiguration/@enabled)[1]', 'NVARCHAR(MAX)') = 'true'
  239. AND [ActiveConfiguration] = 1
  240. AND datalength(CAST(CONVERT(NVARCHAR(MAX), REPLACE([XMLConfiguration], 'UTF-8', 'UTF-16')) AS XML).query('/Configuration/DirectConfiguration/Filters')) <> 5 -- not empty
  241. AND dbo.CompareXml(
  242. CAST(CAST(CONVERT(NVARCHAR(MAX), REPLACE(REPLACE(REPLACE([XMLConfiguration], 'UTF-8', 'UTF-16'), '<Parameters/>', ''), '<Parameters />', '')) AS XML).query('/Configuration/Filters') AS NVARCHAR(MAX)),
  243. CAST(CAST(CONVERT(NVARCHAR(MAX), REPLACE(REPLACE(REPLACE([XMLConfiguration], 'UTF-8', 'UTF-16'), '<Parameters/>', ''), '<Parameters />', '')) AS XML).query('/Configuration/DirectConfiguration/Filters') AS NVARCHAR(MAX))) > 0
  244. AND [m].MERCHANT_ALLOWED = 1
  245. AND [m].MERCHANT_ACTIVE = 1
  246. AND [XMLConfiguration] IS NOT NULL
  247. AND [m].[MERCHANT_ID] NOT IN (SELECT MerchantCodeId FROM @ExcludeMerchantsList)
  248. AND [XMLConfiguration] LIKE '%DirectConfiguration%enabled="true"%'
  249.  
  250. ORDER BY [MS].[MerchantId];
  251.  
  252. GO
  253.  
  254. DROP FUNCTION [dbo].[CompareXml];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement