| | 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 | | */ |
| | 18 | | using CBAM.SQL.PostgreSQL; |
| | 19 | | using System; |
| | 20 | | using System.Collections.Generic; |
| | 21 | | using System.Linq; |
| | 22 | | using System.Text; |
| | 23 | | using UtilPack; |
| | 24 | |
|
| | 25 | | namespace 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 ) |
| 0 | 38 | | : base( error?.ToString() ) |
| | 39 | | { |
| 0 | 40 | | this.Errors = error == null ? Empty<PgSQLError>.Array : new PgSQLError[] { error }; |
| 0 | 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 ) |
| 0 | 48 | | : base( errors == null || errors.Count == 0 ? null : errors[0].ToString() ) |
| | 49 | | { |
| 0 | 50 | | this.Errors = errors?.ToArray() ?? Empty<PgSQLError>.Array; |
| 0 | 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 ) |
| 0 | 59 | | : base( msg, cause ) |
| | 60 | | { |
| 0 | 61 | | this.Errors = Empty<PgSQLError>.Array; |
| 0 | 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"/> |
| 0 | 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> |
| | 99 | | public PgSQLError( |
| | 100 | | String severity, |
| | 101 | | String code, |
| | 102 | | String message, |
| | 103 | | String detail, |
| | 104 | | String hint, |
| | 105 | | String position, |
| | 106 | | String internalPosition, |
| | 107 | | String internalQuery, |
| | 108 | | String where, |
| | 109 | | String file, |
| | 110 | | String line, |
| | 111 | | String routine, |
| | 112 | | String schemaName, |
| | 113 | | String tableName, |
| | 114 | | String columnName, |
| | 115 | | String datatypeName, |
| | 116 | | String constraintName |
| | 117 | | ) |
| | 118 | | { |
| | 119 | | this.Severity = severity; |
| | 120 | | this.Code = code; |
| | 121 | | this.Message = message; |
| | 122 | | this.Detail = detail; |
| | 123 | | this.Hint = hint; |
| | 124 | | this.Position = position; |
| | 125 | | this.InternalPosition = internalPosition; |
| | 126 | | this.InternalQuery = internalQuery; |
| | 127 | | this.Where = where; |
| | 128 | | this.File = file; |
| | 129 | | this.Line = line; |
| | 130 | | this.Routine = routine; |
| | 131 | | this.SchemaName = schemaName; |
| | 132 | | this.TableName = tableName; |
| | 133 | | this.ColumnName = columnName; |
| | 134 | | this.DatatypeName = datatypeName; |
| | 135 | | this.ConstraintName = constraintName; |
| | 136 | | } |
| | 137 | |
|
| | 138 | | /// <summary> |
| | 139 | | /// Gets the error severity. |
| | 140 | | /// </summary> |
| | 141 | | /// <value>The error severity.</value> |
| | 142 | | public String Severity { get; } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// Gets the error code. |
| | 146 | | /// </summary> |
| | 147 | | /// <value>The error code.</value> |
| | 148 | | public String Code { get; } |
| | 149 | |
|
| | 150 | | /// <summary> |
| | 151 | | /// Gets the informative error message. |
| | 152 | | /// </summary> |
| | 153 | | /// <value>The informative error message.</value> |
| | 154 | | 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> |
| | 160 | | 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> |
| | 166 | | 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> |
| | 172 | | 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> |
| | 178 | | public String InternalPosition { get; } |
| | 179 | |
|
| | 180 | | /// <summary> |
| | 181 | | /// Gets the internal query. |
| | 182 | | /// </summary> |
| | 183 | | /// <value>The internal query.</value> |
| | 184 | | 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> |
| | 190 | | 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> |
| | 196 | | 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> |
| | 202 | | public String Line { get; } |
| | 203 | |
|
| | 204 | | /// <summary> |
| | 205 | | /// Gets the routine name, if applicable. |
| | 206 | | /// </summary> |
| | 207 | | /// <value>The routine name, if applicable.</value> |
| | 208 | | public String Routine { get; } |
| | 209 | |
|
| | 210 | | /// <summary> |
| | 211 | | /// Gets the schema name, if applicable. |
| | 212 | | /// </summary> |
| | 213 | | /// <value>The schema name, if applicable.</value> |
| | 214 | | public String SchemaName { get; } |
| | 215 | |
|
| | 216 | | /// <summary> |
| | 217 | | /// Gets the table name, if applicable. |
| | 218 | | /// </summary> |
| | 219 | | /// <value>The table name, if applicable.</value> |
| | 220 | | public String TableName { get; } |
| | 221 | |
|
| | 222 | | /// <summary> |
| | 223 | | /// Gets the column name, if applicable. |
| | 224 | | /// </summary> |
| | 225 | | /// <value>The column name, if applicable.</value> |
| | 226 | | 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> |
| | 232 | | public String DatatypeName { get; } |
| | 233 | |
|
| | 234 | | /// <summary> |
| | 235 | | /// Gets the constraint name, if applicable. |
| | 236 | | /// </summary> |
| | 237 | | /// <value>The constraint name, if applicable.</value> |
| | 238 | | 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 | | { |
| | 246 | | var sb = new StringBuilder(); |
| | 247 | |
|
| | 248 | | sb.Append( this.Message ); |
| | 249 | |
|
| | 250 | | if ( !String.IsNullOrEmpty( this.Severity ) ) |
| | 251 | | { |
| | 252 | | sb.Append( ", Severity: " ).Append( this.Severity ); |
| | 253 | | } |
| | 254 | | if ( !String.IsNullOrEmpty( this.Code ) ) |
| | 255 | | { |
| | 256 | | sb.Append( ", Code: " ).Append( this.Code ); |
| | 257 | | } |
| | 258 | | if ( !String.IsNullOrEmpty( this.Hint ) ) |
| | 259 | | { |
| | 260 | | sb.Append( ", Hint: " ).Append( this.Hint ); |
| | 261 | | } |
| | 262 | |
|
| | 263 | | return sb.ToString(); |
| | 264 | | } |
| | 265 | | } |
| | 266 | | } |
| | 267 | |
|
| | 268 | | /// <summary> |
| | 269 | | /// This class contains extension methods for types defined in this assembly. |
| | 270 | | /// </summary> |
| | 271 | | public 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 | | } |