1+ /*
2+ Copyright 2016 James Craig
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+
17+ using BigBook ;
18+ using BigBook . Comparison ;
19+ using SQLHelperDBTests . HelperClasses . Interfaces ;
20+ using System ;
21+ using System . Collections . Generic ;
22+ using System . Data ;
23+ using System . Data . Common ;
24+
25+ namespace SQLHelperDBTests . HelperClasses . BaseClasses
26+ {
27+ /// <summary>
28+ /// Parameter base class
29+ /// </summary>
30+ /// <typeparam name="TDataType">Data type of the parameter</typeparam>
31+ public abstract class ParameterBase < TDataType > : IParameter < TDataType >
32+ {
33+ /// <summary>
34+ /// Constructor
35+ /// </summary>
36+ /// <param name="id">ID of the parameter</param>
37+ /// <param name="value">Value of the parameter</param>
38+ /// <param name="direction">Direction of the parameter</param>
39+ /// <param name="parameterStarter">
40+ /// What the database expects as the parameter starting string ("@" for SQL Server, ":" for
41+ /// Oracle, etc.)
42+ /// </param>
43+ protected ParameterBase ( string id , TDataType value , ParameterDirection direction = ParameterDirection . Input , string parameterStarter = "@" )
44+ : this ( id , value is null ? typeof ( TDataType ) . To ( DbType . Int32 ) : value . GetType ( ) . To ( DbType . Int32 ) , value , direction , parameterStarter )
45+ {
46+ }
47+
48+ /// <summary>
49+ /// Initializes a new instance of the <see cref="ParameterBase{TDataType}"/> class.
50+ /// </summary>
51+ /// <param name="parameter">The parameter.</param>
52+ protected ParameterBase ( ParameterBase < TDataType > parameter )
53+ : this ( parameter . ID , parameter . DatabaseType , parameter . Value , parameter . Direction , parameter . ParameterStarter )
54+ {
55+ }
56+
57+ /// <summary>
58+ /// Constructor
59+ /// </summary>
60+ /// <param name="id">ID of the parameter</param>
61+ /// <param name="type">Database type</param>
62+ /// <param name="value">Value of the parameter</param>
63+ /// <param name="direction">Direction of the parameter</param>
64+ /// <param name="parameterStarter">
65+ /// What the database expects as the parameter starting string ("@" for SQL Server, ":" for
66+ /// Oracle, etc.)
67+ /// </param>
68+ protected ParameterBase ( string id , SqlDbType type , object ? value = null , ParameterDirection direction = ParameterDirection . Input , string parameterStarter = "@" )
69+ : this ( id , type . To ( DbType . Int32 ) , value , direction , parameterStarter )
70+ {
71+ }
72+
73+ /// <summary>
74+ /// Constructor
75+ /// </summary>
76+ /// <param name="id">ID of the parameter</param>
77+ /// <param name="type">Database type</param>
78+ /// <param name="value">Value of the parameter</param>
79+ /// <param name="direction">Direction of the parameter</param>
80+ /// <param name="parameterStarter">
81+ /// What the database expects as the parameter starting string ("@" for SQL Server, ":" for
82+ /// Oracle, etc.)
83+ /// </param>
84+ protected ParameterBase ( string id , DbType type , object ? value = null , ParameterDirection direction = ParameterDirection . Input , string parameterStarter = "@" )
85+ {
86+ ID = id ;
87+ Value = ( TDataType ) value ! ;
88+ DatabaseType = type ;
89+ Direction = direction ;
90+ BatchID = id ;
91+ ParameterStarter = parameterStarter ;
92+ }
93+
94+ /// <summary>
95+ /// Database type
96+ /// </summary>
97+ public DbType DatabaseType { get ; set ; }
98+
99+ /// <summary>
100+ /// Direction of the parameter
101+ /// </summary>
102+ public ParameterDirection Direction { get ; set ; }
103+
104+ /// <summary>
105+ /// The Name that the parameter goes by
106+ /// </summary>
107+ public string ID { get ; set ; }
108+
109+ /// <summary>
110+ /// Gets the internal value.
111+ /// </summary>
112+ /// <value>The internal value.</value>
113+ public object ? InternalValue { get { return Value ; } }
114+
115+ /// <summary>
116+ /// Starting string of the parameter
117+ /// </summary>
118+ public string ParameterStarter { get ; set ; }
119+
120+ /// <summary>
121+ /// Parameter value
122+ /// </summary>
123+ public TDataType Value { get ; set ; }
124+
125+ /// <summary>
126+ /// Batch ID
127+ /// </summary>
128+ protected string BatchID { get ; set ; }
129+
130+ /// <summary>
131+ /// != operator
132+ /// </summary>
133+ /// <param name="first">First item</param>
134+ /// <param name="second">Second item</param>
135+ /// <returns>returns true if they are not equal, false otherwise</returns>
136+ public static bool operator != ( ParameterBase < TDataType > first , ParameterBase < TDataType > second )
137+ {
138+ return ! ( first == second ) ;
139+ }
140+
141+ /// <summary>
142+ /// The == operator
143+ /// </summary>
144+ /// <param name="first">First item</param>
145+ /// <param name="second">Second item</param>
146+ /// <returns>true if the first and second item are the same, false otherwise</returns>
147+ public static bool operator == ( ParameterBase < TDataType > first , ParameterBase < TDataType > second )
148+ {
149+ return ReferenceEquals ( first , second )
150+ || ( ! ( first is null )
151+ && ! ( second is null )
152+ && first . GetHashCode ( ) == second . GetHashCode ( ) ) ;
153+ }
154+
155+ /// <summary>
156+ /// Adds this parameter to the SQLHelper
157+ /// </summary>
158+ /// <param name="helper">SQLHelper</param>
159+ public abstract void AddParameter ( DbCommand helper ) ;
160+
161+ /// <summary>
162+ /// Finds itself in the string command and adds the value
163+ /// </summary>
164+ /// <param name="command">Command to add to</param>
165+ /// <returns>The resulting string</returns>
166+ public string AddParameter ( string command )
167+ {
168+ if ( string . IsNullOrEmpty ( command ) )
169+ return "" ;
170+ string StringValue = Value ? . ToString ( ) ?? "NULL" ;
171+ return command . Replace ( ParameterStarter + ID , typeof ( TDataType ) == typeof ( string ) ? "'" + StringValue + "'" : StringValue , StringComparison . OrdinalIgnoreCase ) ;
172+ }
173+
174+ /// <summary>
175+ /// Creates a copy of the parameter
176+ /// </summary>
177+ /// <param name="suffix">Suffix to add to the parameter (for batching purposes)</param>
178+ /// <returns>A copy of the parameter</returns>
179+ public abstract IParameter CreateCopy ( string suffix ) ;
180+
181+ /// <summary>
182+ /// Determines if the objects are equal
183+ /// </summary>
184+ /// <param name="obj">Object to compare to</param>
185+ /// <returns>True if they are equal, false otherwise</returns>
186+ public override bool Equals ( object ? obj )
187+ {
188+ return ( obj is ParameterBase < TDataType > OtherParameter )
189+ && OtherParameter . DatabaseType == DatabaseType
190+ && OtherParameter . Direction == Direction
191+ && OtherParameter . ID == ID
192+ && ( Canister . Builder . Bootstrapper ? . Resolve < GenericEqualityComparer < TDataType > > ( ) . Equals ( OtherParameter . Value , Value ) ?? false ) ;
193+ }
194+
195+ /// <summary>
196+ /// Returns a hash code for this instance.
197+ /// </summary>
198+ /// <returns>
199+ /// A hash code for this instance, suitable for use in hashing algorithms and data
200+ /// structures like a hash table.
201+ /// </returns>
202+ public override int GetHashCode ( )
203+ {
204+ var hashCode = 2030399226 ;
205+ hashCode = ( hashCode * - 1521134295 ) + DatabaseType . GetHashCode ( ) ;
206+ hashCode = ( hashCode * - 1521134295 ) + EqualityComparer < string > . Default . GetHashCode ( ID ) ;
207+ return ( hashCode * - 1521134295 ) + EqualityComparer < TDataType > . Default . GetHashCode ( Value ) ;
208+ }
209+
210+ /// <summary>
211+ /// Returns the string version of the parameter
212+ /// </summary>
213+ /// <returns>The string representation of the parameter</returns>
214+ public override string ToString ( )
215+ {
216+ return ParameterStarter + ID ;
217+ }
218+ }
219+ }
0 commit comments