Last active
August 29, 2015 14:18
-
-
Save osya/cb3d97aa2034278e3385 to your computer and use it in GitHub Desktop.
Working with zipped parameters in .NET & SSRS #BI #MIS #regexp #SSRS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| '''============================================================ | |
| ''' Функции для сжатия и распаковки параметров | |
| '''____________________________________________________________ | |
| ''' Function #1/4: ver. 1.0 | |
| ''' <summary> | |
| ''' Сжимает строку | |
| ''' </summary> | |
| ''' <param name="text">Строка которую необходимо сжать</param> | |
| ''' <returns>Сжатая строка</returns> | |
| ''' <remarks></remarks> | |
| Public Function Zip(text As String) As String | |
| Dim buffer As Byte() = System.Text.Encoding.Unicode.GetBytes(text) | |
| Dim ms As New System.IO.MemoryStream() | |
| Using zipStream As New System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, True) | |
| zipStream.Write(buffer, 0, buffer.Length) | |
| End Using | |
| ms.Position = 0 | |
| Dim outStream As New System.IO.MemoryStream() | |
| Dim compressed As Byte() = New Byte(ms.Length - 1) {} | |
| ms.Read(compressed, 0, compressed.Length) | |
| Dim gzBuffer As Byte() = New Byte(compressed.Length + 3) {} | |
| System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length) | |
| System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4) | |
| Return Convert.ToBase64String(gzBuffer) | |
| End Function | |
| '''____________________________________________________________ | |
| ''' Function #2/4: ver. 1.0 | |
| ''' <summary> | |
| ''' Распаковывает ранее сжатую строку | |
| ''' </summary> | |
| ''' <param name="compressedText">Сжатая строка</param> | |
| ''' <returns>Распакованная строка</returns> | |
| ''' <remarks></remarks> | |
| Public Function UnZip(compressedText As String) As String | |
| Try | |
| Dim gzBuffer As Byte() = Convert.FromBase64String(compressedText) | |
| Using ms As New System.IO.MemoryStream() | |
| Dim msgLength As Integer = BitConverter.ToInt32(gzBuffer, 0) | |
| ms.Write(gzBuffer, 4, gzBuffer.Length - 4) | |
| Dim buffer As Byte() = New Byte(msgLength - 1) {} | |
| ms.Position = 0 | |
| Using zipStream As New System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress) | |
| zipStream.Read(buffer, 0, buffer.Length) | |
| End Using | |
| Return System.Text.Encoding.Unicode.GetString(buffer, 0, buffer.Length) | |
| End Using | |
| Catch | |
| Return "" | |
| End Try | |
| End Function | |
| '''____________________________________________________________ | |
| ''' Function #3/4: ver. 1.0 | |
| ''' <summary> | |
| ''' Извлекает значения параметров из сжатой строки | |
| ''' </summary> | |
| ''' <param name="ParamName">Название параметра</param> | |
| ''' <param name="ZippedParameters">Сжатая строка с параметрами</param> | |
| ''' <returns>Строковый массив параметров</returns> | |
| ''' <remarks></remarks> | |
| Public Function SplitParameters(ByVal ParamName As String, ByVal ZippedParameters As String) As String() | |
| Dim result As String() = {""} | |
| Dim unzippedString As String | |
| unzippedString = UnZip(ZippedParameters) | |
| Dim pattern As String | |
| pattern = String.Format(">>{0}=(?<values>.*?)#<<", ParamName) | |
| Dim rgx As System.Text.RegularExpressions.Regex | |
| rgx = New System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase) | |
| Dim m As System.Text.RegularExpressions.Match | |
| m = rgx.Match(unzippedString) | |
| If m.Success Then | |
| result = m.Groups("values").ToString().Split("#") | |
| End If | |
| Return result | |
| End Function | |
| '''____________________________________________________________ | |
| ''' Function #4/4: ver. 1.0 | |
| ''' <summary> | |
| ''' Подготавливает значения параметров для сжатия собирая все в одну строку | |
| ''' </summary> | |
| ''' <param name="ParamParameters">Параметры SSRS</param> | |
| ''' <param name="ParamNames">Список параметров через запятую</param> | |
| ''' <returns>Строка содержащая значения параметров переданных в функцию</returns> | |
| ''' <remarks></remarks> | |
| Public Function JoinParameters( _ | |
| ByVal ParamParameters As Parameters, _ | |
| ByVal ParamArray ParamNames() As String _ | |
| ) As String | |
| Dim result As New System.Text.StringBuilder() | |
| If ParamNames.Length > 0 Then | |
| For i As Integer = 0 To UBound(ParamNames, 1) | |
| Dim tempSB = New System.Text.StringBuilder() | |
| If ParamParameters(ParamNames(i)).IsMultiValue Then | |
| For j As Integer = 0 To ParamParameters(ParamNames(i)).Count - 1 | |
| tempSB.Append(String.Format("{0}#", ParamParameters(ParamNames(i)).Value(j).ToString())) | |
| Next | |
| Else | |
| tempSB.Append(String.Format("{0}#", ParamParameters(ParamNames(i)).Value.ToString())) | |
| End If | |
| result.Append(String.Format(">>{0}={1}<<", ParamNames(i), tempSB.ToString())) | |
| Next i | |
| End If | |
| Return result.ToString() | |
| End Function | |
| '''^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Это из отчета DemandSupplyZU_request4_db03n.rdl на DB03N