Advertisement
MagnusArias

SBO | Funkcje 3 C#

Nov 30th, 2019
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Data.SqlTypes;
  5. using Microsoft.SqlServer.Server;
  6. using System.Collections.Generic;
  7. using System.IO;
  8.  
  9. [Serializable]
  10. [SqlUserDefinedAggregate(Format.Native)]
  11. public struct GEOAVE
  12. {
  13.     private int licznik;  // field to count the not null rows
  14.     private double iloczyn;
  15.     private double temp;
  16.     public void Init()
  17.     {
  18.         licznik = 0;
  19.         iloczyn = 1;// initialization
  20.     }
  21.     public void Accumulate(double? Value)
  22.     {
  23.         if (Value != null)  // count just the rows that are not equal to NULL
  24.         {
  25.             licznik++;
  26.             iloczyn = iloczyn * (double)Value;
  27.         }
  28.     }
  29.     public void Merge(GEOAVE Group)
  30.     {
  31.         this.licznik += Group.licznik;
  32.         this.iloczyn *= Group.iloczyn; // when merge is needed the counter of other groups should be added
  33.     }
  34.  
  35.     public double? Terminate() //SqlString
  36.     {
  37.         //return new SqlString(Counter.ToString());
  38.         temp = 1.0 / licznik;
  39.         iloczyn = Math.Pow(iloczyn, temp);
  40.         return (double?)this.iloczyn;//returning the results
  41.     }
  42. }
  43.  
  44. [Serializable]
  45. [SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize = 8000)]
  46. public struct LiczZnaki : IBinarySerialize
  47. {
  48.     private int? licznik;  // field to count the not null rows
  49.     private int licz;
  50.     public void Init()
  51.     {
  52.         licz = 0;
  53.         licznik = 0;
  54.     }
  55.     public void Accumulate(SqlString Value)
  56.     {
  57.         if (!Value.IsNull)  // count just the rows that are not equal to NULL
  58.         {
  59.             licz++;
  60.             licznik = licznik + Value.ToString().Length;
  61.         }
  62.     }
  63.     public void Merge(LiczZnaki Group)
  64.     {
  65.         this.licznik += Group.licznik;
  66.         this.licz += Group.licz;
  67.     }
  68.  
  69.     public int? Terminate() //SqlString
  70.     {
  71.         if (licz != 0) return this.licznik;//returning the results
  72.         else return null;
  73.     }
  74.  
  75.     public void Read(BinaryReader r)
  76.     {
  77.         licz = r.ReadInt32();
  78.         licznik = r.ReadInt32();
  79.     }
  80.  
  81.     public void Write(BinaryWriter w)
  82.     {
  83.         w.Write(licz);
  84.         w.Write((int)licznik);
  85.        
  86.     }
  87. }
  88.  
  89.  
  90. [Serializable]
  91. [SqlUserDefinedAggregate(Format.UserDefined, MaxByteSize = -1)]
  92. public struct LaczZnaki : IBinarySerialize
  93. {
  94.     private string slowo;  // field to count the not null rows
  95.     private int licz;
  96.     public void Init()
  97.     {
  98.         licz = 0;
  99.         slowo = "";
  100.     }
  101.     public void Accumulate(SqlString Value)
  102.     {
  103.         if (!Value.IsNull)  // count just the rows that are not equal to NULL
  104.         {
  105.             licz++;
  106.             slowo = slowo + Value.ToString() + ", ";
  107.         }
  108.     }
  109.     public void Merge(LaczZnaki Group)
  110.     {
  111.         this.slowo += Group.slowo;
  112.         this.licz += Group.licz;
  113.     }
  114.  
  115.     public string Terminate() //SqlString
  116.     {
  117.         if (licz != 0) return this.slowo.Substring(0, slowo.Length-2) ;//returning the results
  118.         else return null;
  119.     }
  120.  
  121.     public void Read(BinaryReader r)
  122.     {
  123.         licz = r.ReadInt32();
  124.         slowo = r.ReadString();
  125.     }
  126.  
  127.     public void Write(BinaryWriter w)
  128.     {
  129.         w.Write(licz);
  130.         w.Write(slowo);
  131.  
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement