| | 1 | | using System.ComponentModel.DataAnnotations; |
| | 2 | |
|
| | 3 | | [assembly: System.Runtime.CompilerServices.InternalsVisibleTo( "Core.Tests" )] |
| | 4 | | namespace Common.Core.Classes; |
| | 5 | |
|
| | 6 | | /// <summary>Base class for models with an integer as the primary key.</summary> |
| | 7 | | public abstract class ModelData : ModelBase |
| | 8 | | { |
| | 9 | | /// <summary>Gets or sets the primary key.</summary> |
| | 10 | | [Key] |
| | 11 | | [Range( 0, 2147483647 )] |
| | 12 | | public abstract int Id { get; set; } |
| | 13 | |
|
| | 14 | | #region Internal Methods |
| | 15 | |
|
| | 16 | | internal static void SetSQLColumn( string colName, object? newValue, IList<string> sql ) |
| 84 | 17 | | { |
| | 18 | | const char cCharQuote = '\''; |
| | 19 | | const string cStrQuote = "'"; |
| | 20 | | const string cDblQuote = "''"; |
| | 21 | |
|
| 84 | 22 | | string quote = string.Empty; |
| 84 | 23 | | bool hasNewValue = newValue is not null; |
| | 24 | |
|
| 84 | 25 | | if( hasNewValue ) |
| 79 | 26 | | { |
| 79 | 27 | | if( newValue is string str ) |
| 75 | 28 | | { |
| 75 | 29 | | quote = cStrQuote; |
| 93 | 30 | | if( str.Contains( cCharQuote ) ) { newValue = str.Replace( cStrQuote, cDblQuote ); } |
| 75 | 31 | | } |
| 4 | 32 | | else if( newValue is char chr ) |
| 3 | 33 | | { |
| 3 | 34 | | quote = cStrQuote; |
| 6 | 35 | | if( chr == cCharQuote ) { newValue = cDblQuote; } |
| 3 | 36 | | } |
| 79 | 37 | | } |
| | 38 | |
|
| 84 | 39 | | newValue = hasNewValue ? $"{quote}{newValue}{quote}" : "NULL"; |
| 84 | 40 | | sql.Add( $"[{colName}]={newValue}" ); |
| 84 | 41 | | } |
| | 42 | |
|
| | 43 | | #endregion |
| | 44 | | } |