Summary

Class:CBAM.SQL.PostgreSQL.PgSQLError
Assembly:CBAM.SQL.PostgreSQL
File(s):/repo-dir/contents/Source/Code/CBAM.SQL.PostgreSQL/PgSQLException.cs
Covered lines:0
Uncovered lines:63
Coverable lines:63
Total lines:286
Line coverage:0%
Branch coverage:0%

Coverage History

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
.ctor(...)100%0%
ToString()600%0%

File(s)

/repo-dir/contents/Source/Code/CBAM.SQL.PostgreSQL/PgSQLException.cs

#LineLine coverage
 1/*
 2 * Copyright 2017 Stanislav Muhametsin. All rights Reserved.
 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
 13 * implied.
 14 *
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18using CBAM.SQL.PostgreSQL;
 19using System;
 20using System.Collections.Generic;
 21using System.Linq;
 22using System.Text;
 23using UtilPack;
 24
 25namespace CBAM.SQL.PostgreSQL
 26{
 27   /// <summary>
 28   /// This class extends <see cref="SQLException"/> to provide PostgreSQL-specific <see cref="PgSQLError"/> objects des
 29   /// </summary>
 30   /// <seealso cref="PgSQLError"/>
 31   public sealed class PgSQLException : SQLException
 32   {
 33      /// <summary>
 34      /// Creates a new instance of <see cref="PgSQLException"/> with one instance of backend <see cref="PgSQLError"/>.
 35      /// </summary>
 36      /// <param name="error">The information about backend error.</param>
 37      public PgSQLException( PgSQLError error )
 38         : base( error?.ToString() )
 39      {
 40         this.Errors = error == null ? Empty<PgSQLError>.Array : new PgSQLError[] { error };
 41      }
 42
 43      /// <summary>
 44      /// Creates a new instance of <see cref="PgSQLException"/> with many instances of backend <see cref="PgSQLError"/>
 45      /// </summary>
 46      /// <param name="errors">Information about backend errors.</param>
 47      public PgSQLException( IList<PgSQLError> errors )
 48         : base( errors == null || errors.Count == 0 ? null : errors[0].ToString() )
 49      {
 50         this.Errors = errors?.ToArray() ?? Empty<PgSQLError>.Array;
 51      }
 52
 53      /// <summary>
 54      /// Creates a new instance of <see cref="PgSQLException"/> with given message and optional inner exception.
 55      /// </summary>
 56      /// <param name="msg">The textual message describing an error.</param>
 57      /// <param name="cause">The optional inner exception.</param>
 58      public PgSQLException( String msg, Exception cause = null )
 59         : base( msg, cause )
 60      {
 61         this.Errors = Empty<PgSQLError>.Array;
 62      }
 63
 64      /// <summary>
 65      /// Gets the array of all backend errors this <see cref="PgSQLException"/> holds.
 66      /// </summary>
 67      /// <value>The array of all backend errors this <see cref="PgSQLException"/> holds.</value>
 68      /// <seealso cref="PgSQLError"/>
 69      public PgSQLError[] Errors { get; }
 70
 71   }
 72
 73   /// <summary>
 74   /// This class encapsulates all information about an error occurred at PostgreSQL backend process.
 75   /// </summary>
 76   public sealed class PgSQLError
 77   {
 78      /// <summary>
 79      /// Creates a new instance of <see cref="PgSQLError"/> with given parameters.
 80      /// Any and all of the parameters may be <c>null</c>.
 81      /// </summary>
 82      /// <param name="severity">The error severity.</param>
 83      /// <param name="code">The error code.</param>
 84      /// <param name="message">The informative error message.</param>
 85      /// <param name="detail">Information about the details of error.</param>
 86      /// <param name="hint">Any possible hint about detecting or avoiding an error.</param>
 87      /// <param name="position">The position information in SQL code.</param>
 88      /// <param name="internalPosition">Internal position information in source code.</param>
 89      /// <param name="internalQuery">Internal query.</param>
 90      /// <param name="where">Function information in source code where error occurred.</param>
 91      /// <param name="file">The file name of source code where error occurred.</param>
 92      /// <param name="line">The line number of source code file where error occurred.</param>
 93      /// <param name="routine">Routine name, if applicable.</param>
 94      /// <param name="schemaName">Schema name, if applicable.</param>
 95      /// <param name="tableName">Table name, if applicable.</param>
 96      /// <param name="columnName">Column name, if applicable.</param>
 97      /// <param name="datatypeName">The type name of data, if applicable.</param>
 98      /// <param name="constraintName">The constraint name, if applicable.</param>
 099      public PgSQLError(
 0100         String severity,
 0101         String code,
 0102         String message,
 0103         String detail,
 0104         String hint,
 0105         String position,
 0106         String internalPosition,
 0107         String internalQuery,
 0108         String where,
 0109         String file,
 0110         String line,
 0111         String routine,
 0112         String schemaName,
 0113         String tableName,
 0114         String columnName,
 0115         String datatypeName,
 0116         String constraintName
 0117         )
 118      {
 0119         this.Severity = severity;
 0120         this.Code = code;
 0121         this.Message = message;
 0122         this.Detail = detail;
 0123         this.Hint = hint;
 0124         this.Position = position;
 0125         this.InternalPosition = internalPosition;
 0126         this.InternalQuery = internalQuery;
 0127         this.Where = where;
 0128         this.File = file;
 0129         this.Line = line;
 0130         this.Routine = routine;
 0131         this.SchemaName = schemaName;
 0132         this.TableName = tableName;
 0133         this.ColumnName = columnName;
 0134         this.DatatypeName = datatypeName;
 0135         this.ConstraintName = constraintName;
 0136      }
 137
 138      /// <summary>
 139      /// Gets the error severity.
 140      /// </summary>
 141      /// <value>The error severity.</value>
 0142      public String Severity { get; }
 143
 144      /// <summary>
 145      /// Gets the error code.
 146      /// </summary>
 147      /// <value>The error code.</value>
 0148      public String Code { get; }
 149
 150      /// <summary>
 151      /// Gets the informative error message.
 152      /// </summary>
 153      /// <value>The informative error message.</value>
 0154      public String Message { get; }
 155
 156      /// <summary>
 157      /// Gets the information about the details of error.
 158      /// </summary>
 159      /// <value>The information about the details of error.</value>
 0160      public String Detail { get; }
 161
 162      /// <summary>
 163      /// Gets any possible hint about detecting or avoiding an error.
 164      /// </summary>
 165      /// <value>Any possible hint about detecting or avoiding an error.</value>
 0166      public String Hint { get; }
 167
 168      /// <summary>
 169      /// Gets the position information in SQL code.
 170      /// </summary>
 171      /// <value>The position information in SQL code.</value>
 0172      public String Position { get; }
 173
 174      /// <summary>
 175      /// Gets the internal position information in source code.
 176      /// </summary>
 177      /// <value>The internal position information in source code.</value>
 0178      public String InternalPosition { get; }
 179
 180      /// <summary>
 181      /// Gets the internal query.
 182      /// </summary>
 183      /// <value>The internal query.</value>
 0184      public String InternalQuery { get; }
 185
 186      /// <summary>
 187      /// Gets the function information in source code where error occurred.
 188      /// </summary>
 189      /// <value>The function information in source code where error occurred.</value>
 0190      public String Where { get; }
 191
 192      /// <summary>
 193      /// Gets the file name of source code where error occurred.
 194      /// </summary>
 195      /// <value>The file name of source code where error occurred.</value>
 0196      public String File { get; }
 197
 198      /// <summary>
 199      /// Gets the line number of source code file where error occurred.
 200      /// </summary>
 201      /// <value>The line number of source code file where error occurred.</value>
 0202      public String Line { get; }
 203
 204      /// <summary>
 205      /// Gets the routine name, if applicable.
 206      /// </summary>
 207      /// <value>The routine name, if applicable.</value>
 0208      public String Routine { get; }
 209
 210      /// <summary>
 211      /// Gets the schema name, if applicable.
 212      /// </summary>
 213      /// <value>The schema name, if applicable.</value>
 0214      public String SchemaName { get; }
 215
 216      /// <summary>
 217      /// Gets the table name, if applicable.
 218      /// </summary>
 219      /// <value>The table name, if applicable.</value>
 0220      public String TableName { get; }
 221
 222      /// <summary>
 223      /// Gets the column name, if applicable.
 224      /// </summary>
 225      /// <value>The column name, if applicable.</value>
 0226      public String ColumnName { get; }
 227
 228      /// <summary>
 229      /// Gets the type name of data, if applicable.
 230      /// </summary>
 231      /// <value>The type name of data, if applicable.</value>
 0232      public String DatatypeName { get; }
 233
 234      /// <summary>
 235      /// Gets the constraint name, if applicable.
 236      /// </summary>
 237      /// <value>The constraint name, if applicable.</value>
 0238      public String ConstraintName { get; }
 239
 240      /// <summary>
 241      /// Overrides <see cref="Object.ToString"/> to provide simple textual description of the object.
 242      /// </summary>
 243      /// <returns>The <see cref="Message"/>, followed by <see cref="Severity"/> if it is not <c>null</c> and not empty,
 244      public override String ToString()
 245      {
 0246         var sb = new StringBuilder();
 247
 0248         sb.Append( this.Message );
 249
 0250         if ( !String.IsNullOrEmpty( this.Severity ) )
 251         {
 0252            sb.Append( ", Severity: " ).Append( this.Severity );
 253         }
 0254         if ( !String.IsNullOrEmpty( this.Code ) )
 255         {
 0256            sb.Append( ", Code: " ).Append( this.Code );
 257         }
 0258         if ( !String.IsNullOrEmpty( this.Hint ) )
 259         {
 0260            sb.Append( ", Hint: " ).Append( this.Hint );
 261         }
 262
 0263         return sb.ToString();
 264      }
 265   }
 266}
 267
 268/// <summary>
 269/// This class contains extension methods for types defined in this assembly.
 270/// </summary>
 271public static partial class E_CBAM
 272{
 273   /// <summary>
 274   /// Helper method to check whether this <see cref="PgSQLException"/> has any given error codes.
 275   /// </summary>
 276   /// <param name="exception">This <see cref="PgSQLException"/>.</param>
 277   /// <param name="codes">The codes to check.</param>
 278   /// <returns><c>true</c> if this <see cref="PgSQLException"/> is not <c>null</c>, and has at least one <see cref="PgS
 279   public static Boolean HasErrorCodes( this PgSQLException exception, params String[] codes )
 280   {
 281      return exception != null
 282         && !codes.IsNullOrEmpty()
 283         && exception.Errors.Length > 0
 284         && exception.Errors.Any( e => codes.Any( c => String.Equals( e.Code, c, StringComparison.OrdinalIgnoreCase ) ) 
 285   }
 286}