1
- using CsvHelper . Configuration . Attributes ;
2
- using KustoCopyConsole . Entity . State ;
1
+ using CsvHelper . Configuration ;
2
+ using CsvHelper ;
3
+ using CsvHelper . Configuration . Attributes ;
4
+ using CsvHelper . TypeConversion ;
5
+ using KustoCopyConsole . Entity ;
3
6
using System ;
4
7
using System . Collections . Generic ;
5
8
using System . Linq ;
6
9
using System . Text ;
7
10
using System . Threading . Tasks ;
11
+ using System . Text . Json . Serialization ;
12
+ using System . Text . Json ;
8
13
9
14
namespace KustoCopyConsole . Entity
10
15
{
11
16
internal class RowItem
12
17
{
18
+ #region Inner types
19
+ private class RowTypeConverter : DefaultTypeConverter
20
+ {
21
+ public override object ? ConvertFromString (
22
+ string ? text ,
23
+ IReaderRow row ,
24
+ MemberMapData memberMapData )
25
+ {
26
+ if ( string . IsNullOrWhiteSpace ( text ) )
27
+ {
28
+ return RowType . Unspecified ;
29
+ }
30
+ else
31
+ {
32
+ try
33
+ {
34
+ var state = RowType . Parse < RowType > ( text ) ;
35
+
36
+ return state ;
37
+ }
38
+ catch ( Exception ex )
39
+ {
40
+ throw new CopyException ( $ "Can't deserialize row type: '{ text } '", false , ex ) ;
41
+ }
42
+ }
43
+ }
44
+
45
+ public override string ? ConvertToString (
46
+ object ? value ,
47
+ IWriterRow row ,
48
+ MemberMapData memberMapData )
49
+ {
50
+ if ( value != null )
51
+ {
52
+ var text = value . ToString ( ) ;
53
+
54
+ return text ;
55
+ }
56
+ else
57
+ {
58
+ return string . Empty ;
59
+ }
60
+ }
61
+ }
62
+ #endregion
63
+
64
+ #region Data
13
65
[ Index ( 0 ) ]
14
66
public string FileVersion { get ; set ; } = string . Empty ;
15
67
16
68
[ Index ( 1 ) ]
17
- public DateTime ? Created { get ; set ; }
69
+ public DateTime ? Created { get ; set ; } = DateTime . Now ;
18
70
19
71
[ Index ( 2 ) ]
20
- public DateTime ? Updated { get ; set ; }
72
+ public DateTime ? Updated { get ; set ; } = DateTime . Now ;
21
73
22
74
[ Index ( 3 ) ]
23
- public string State { get ; set ; } = string . Empty ;
75
+ [ TypeConverter ( typeof ( RowTypeConverter ) ) ]
76
+ public RowType RowType { get ; set ; } = RowType . Unspecified ;
24
77
25
78
[ Index ( 4 ) ]
26
- public string SourceClusterUri { get ; set ; } = string . Empty ;
79
+ public string State { get ; set ; } = string . Empty ;
27
80
28
81
[ Index ( 5 ) ]
29
- public string SourceDatabaseName { get ; set ; } = string . Empty ;
82
+ public string SourceClusterUri { get ; set ; } = string . Empty ;
30
83
31
84
[ Index ( 6 ) ]
32
- public long ? IterationId { get ; set ; }
85
+ public string SourceDatabaseName { get ; set ; } = string . Empty ;
33
86
34
87
[ Index ( 7 ) ]
35
- public string CursorStart { get ; set ; } = string . Empty ;
88
+ public string DestinationClusterUri { get ; set ; } = string . Empty ;
36
89
37
90
[ Index ( 8 ) ]
38
- public string CursorEnd { get ; set ; } = string . Empty ;
91
+ public string DestinationDatabaseName { get ; set ; } = string . Empty ;
39
92
40
93
[ Index ( 9 ) ]
41
- public string SourceTableName { get ; set ; } = string . Empty ;
94
+ public string TableName { get ; set ; } = string . Empty ;
95
+
96
+ [ Index ( 10 ) ]
97
+ public long ? IterationId { get ; set ; }
98
+
99
+ [ Index ( 11 ) ]
100
+ public string CursorStart { get ; set ; } = string . Empty ;
101
+
102
+ [ Index ( 12 ) ]
103
+ public string CursorEnd { get ; set ; } = string . Empty ;
104
+
105
+ [ Index ( 13 ) ]
106
+ public long ? BlockId { get ; set ; }
107
+ #endregion
108
+
109
+ public T ParseState < T > ( ) where T : struct
110
+ {
111
+ try
112
+ {
113
+ return Enum . Parse < T > ( State ) ;
114
+ }
115
+ catch ( Exception ex )
116
+ {
117
+ throw new CopyException ( $ "Invalid state: '{ State } '", false , ex ) ;
118
+ }
119
+ }
120
+
121
+ public void Validate ( )
122
+ {
123
+ if ( RowType == RowType . Unspecified )
124
+ {
125
+ throw new CopyException ( "Invalid row type" , false ) ;
126
+ }
127
+ ValidateProperty (
128
+ nameof ( FileVersion ) ,
129
+ string . IsNullOrWhiteSpace ( FileVersion ) ,
130
+ RowType != RowType . FileVersion ) ;
131
+ ValidateProperty (
132
+ nameof ( Created ) ,
133
+ Created == null ,
134
+ false ) ;
135
+ ValidateProperty (
136
+ nameof ( Updated ) ,
137
+ Created == null ,
138
+ false ) ;
139
+ ValidateProperty (
140
+ nameof ( State ) ,
141
+ string . IsNullOrWhiteSpace ( State ) ,
142
+ RowType == RowType . FileVersion ) ;
143
+ }
144
+
145
+ public RowItem Clone ( )
146
+ {
147
+ var clone = ( RowItem ) MemberwiseClone ( ) ;
148
+
149
+ clone . Updated = DateTime . Now ;
150
+
151
+ return clone ;
152
+ }
153
+
154
+ private void ValidateProperty (
155
+ string propertyName ,
156
+ bool isNull ,
157
+ bool shouldBeNull )
158
+ {
159
+ if ( isNull && ! shouldBeNull )
160
+ {
161
+ throw new CopyException (
162
+ $ "{ propertyName } doesn't have value but should",
163
+ false ) ;
164
+ }
165
+ if ( ! isNull && shouldBeNull )
166
+ {
167
+ throw new CopyException (
168
+ $ "{ propertyName } has value but should not",
169
+ false ) ;
170
+ }
171
+ }
42
172
}
43
173
}
0 commit comments