Datei: NDODLL/SqlDumper.cs

Last Commit (7e08240)
1 //
2 // Copyright (c) 2002-2023 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System.IO;
24 using System.Data;
25 using NDOInterfaces;
26 using Microsoft.Extensions.Logging;
27
28 namespace NDO
29 {
30 ····/// <summary>
31 ····/// Zusammenfassung für SqlDumper.
32 ····/// </summary>
33 ····internal class SqlDumper
34 ····{
35 ········ILogger logger;
36 ········IDbCommand insertCommand;
37 ········IDbCommand selectCommand;
38 ········IDbCommand updateCommand;
39 ········IDbCommand deleteCommand;
40 ········IProvider provider;
41
42 ········public SqlDumper(ILoggerFactory loggerFactopry, IProvider provider, IDbCommand insertCommand, IDbCommand selectCommand, IDbCommand updateCommand, IDbCommand deleteCommand)
43 ········{
44 ············this.logger = loggerFactopry.CreateLogger<SqlDumper>();
45 ············this.provider = provider;
46 ············this.updateCommand = updateCommand;
47 ············this.insertCommand = insertCommand;
48 ············this.selectCommand = selectCommand;
49 ············this.deleteCommand = deleteCommand;
50 ········}
51
52 ········internal void Dump(DataRow[] rows)
53 ········{
54 ············if (logger == null)
55 ················return;
56
57 ············bool hasSelect = false;
58 ············bool hasInsert = false;
59 ············bool hasDelete = false;
60 ············bool hasUpdate = false;
61
62 ············if (rows == null || rows.Length == 0)
63 ············{
64 ················hasSelect = true;
65 ············}
66 ············else
67 ············{
68 ················for (int i = 0; i < rows.Length; i++)
69 ················{
70 ····················DataRow row = rows[i];
71 ····················if (row.RowState == System.Data.DataRowState.Added)
72 ························hasInsert = true;
73 ····················if (row.RowState == System.Data.DataRowState.Modified)
74 ························hasUpdate = true;
75 ····················if (row.RowState == System.Data.DataRowState.Deleted)
76 ························hasDelete = true;
77 ················}
78 ············}
79
80 ············StringWriter sw = null;
81
82 ············try
83 ············{
84 ················sw = new StringWriter();
85 ················if (hasSelect)
86 ················{
87 ····················sw.WriteLine("Select Command:");
88 ····················sw.WriteLine(this.selectCommand.CommandText);
89 ····················DumpParameters(selectCommand, sw);
90 ················}
91
92 ················if (hasInsert)
93 ················{
94 ····················sw.WriteLine("Insert Command:");
95 ····················sw.WriteLine(this.insertCommand.CommandText);
96 ····················DumpParameters(insertCommand, sw);············
97 ················}
98 ················if (hasDelete)
99 ················{
100 ····················sw.WriteLine("Delete Command:");
101 ····················sw.WriteLine(this.deleteCommand.CommandText);
102 ····················DumpParameters(deleteCommand, sw);
103 ····················sw.WriteLine(rows.Length.ToString() + " Zeilen zu löschen");
104 ················}
105 ················if (hasUpdate)
106 ················{
107 ····················if (updateCommand != null)
108 ····················{
109 ························sw.WriteLine("Update Command:");
110 ························sw.WriteLine(this.updateCommand.CommandText);
111 ························DumpParameters(updateCommand, sw);
112 ····················}
113 ····················else
114 ························sw.WriteLine("No Update Command");
115 ················}
116 ············
117 ················if (rows != null)
118 ················{
119 ····················for (int i = 0; i < rows.Length; i++)
120 ····················{
121 ························DataRow r = rows[i];
122 ························sw.WriteLine("----");
123 ························sw.WriteLine("Row: " + r.RowState.ToString());
124 ························DataRowVersion drv = r.RowState == DataRowState.Deleted ? DataRowVersion.Original : DataRowVersion.Current;
125 ························foreach (DataColumn c in r.Table.Columns)
126 ························{
127 ····························sw.WriteLine(c.ColumnName + " = " + r[c, drv].ToString());
128 ························}
129 ····················}
130 ················
131 ················}
132 ············}
133 ············finally
134 ············{
135 ················if (sw != null)
136 ················{····················
137 ····················sw.Close();
 
138 ····················this.logger.LogDebug( sw.ToString() );
139 ················}
140 ············}
141 ········}
142
143 ········private void DumpParameters(IDbCommand cmd, TextWriter sw)
144 ········{
145 ············int j = 1;
146 ············foreach(IDbDataParameter dp in cmd.Parameters)
147 ············{
148 ················string parType = provider.GetDbTypeString(dp);
149 ················sw.Write($"Parameter {j}: {dp.ToString()}({parType}) Source Column: {dp.SourceColumn} ");
150 ················sw.Write($"Prec: {dp.Precision} Scale: {dp.Scale} Size: {dp.Size}");
151 ················if (dp.Value != null)
152 ····················sw.WriteLine(" Value: " + dp.Value);
153 ················else
154 ····················sw.WriteLine();
155
156 ················j++;
157 ············}
158 ········}
159 ····}
160 }
161
New Commit (0a71280)
1 //
2 // Copyright (c) 2002-2023 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System.IO;
24 using System.Data;
25 using NDOInterfaces;
26 using Microsoft.Extensions.Logging;
27
28 namespace NDO
29 {
30 ····/// <summary>
31 ····/// Zusammenfassung für SqlDumper.
32 ····/// </summary>
33 ····internal class SqlDumper
34 ····{
35 ········ILogger logger;
36 ········IDbCommand insertCommand;
37 ········IDbCommand selectCommand;
38 ········IDbCommand updateCommand;
39 ········IDbCommand deleteCommand;
40 ········IProvider provider;
41
42 ········public SqlDumper(ILoggerFactory loggerFactopry, IProvider provider, IDbCommand insertCommand, IDbCommand selectCommand, IDbCommand updateCommand, IDbCommand deleteCommand)
43 ········{
44 ············this.logger = loggerFactopry.CreateLogger<SqlDumper>();
45 ············this.provider = provider;
46 ············this.updateCommand = updateCommand;
47 ············this.insertCommand = insertCommand;
48 ············this.selectCommand = selectCommand;
49 ············this.deleteCommand = deleteCommand;
50 ········}
51
52 ········internal void Dump(DataRow[] rows)
53 ········{
54 ············if (logger == null)
55 ················return;
56
57 ············bool hasSelect = false;
58 ············bool hasInsert = false;
59 ············bool hasDelete = false;
60 ············bool hasUpdate = false;
61
62 ············if (rows == null || rows.Length == 0)
63 ············{
64 ················hasSelect = true;
65 ············}
66 ············else
67 ············{
68 ················for (int i = 0; i < rows.Length; i++)
69 ················{
70 ····················DataRow row = rows[i];
71 ····················if (row.RowState == System.Data.DataRowState.Added)
72 ························hasInsert = true;
73 ····················if (row.RowState == System.Data.DataRowState.Modified)
74 ························hasUpdate = true;
75 ····················if (row.RowState == System.Data.DataRowState.Deleted)
76 ························hasDelete = true;
77 ················}
78 ············}
79
80 ············StringWriter sw = null;
81
82 ············try
83 ············{
84 ················sw = new StringWriter();
85 ················if (hasSelect)
86 ················{
87 ····················sw.WriteLine("Select Command:");
88 ····················sw.WriteLine(this.selectCommand.CommandText);
89 ····················DumpParameters(selectCommand, sw);
90 ················}
91
92 ················if (hasInsert)
93 ················{
94 ····················sw.WriteLine("Insert Command:");
95 ····················sw.WriteLine(this.insertCommand.CommandText);
96 ····················DumpParameters(insertCommand, sw);············
97 ················}
98 ················if (hasDelete)
99 ················{
100 ····················sw.WriteLine("Delete Command:");
101 ····················sw.WriteLine(this.deleteCommand.CommandText);
102 ····················DumpParameters(deleteCommand, sw);
103 ····················sw.WriteLine(rows.Length.ToString() + " Zeilen zu löschen");
104 ················}
105 ················if (hasUpdate)
106 ················{
107 ····················if (updateCommand != null)
108 ····················{
109 ························sw.WriteLine("Update Command:");
110 ························sw.WriteLine(this.updateCommand.CommandText);
111 ························DumpParameters(updateCommand, sw);
112 ····················}
113 ····················else
114 ························sw.WriteLine("No Update Command");
115 ················}
116 ············
117 ················if (rows != null)
118 ················{
119 ····················for (int i = 0; i < rows.Length; i++)
120 ····················{
121 ························DataRow r = rows[i];
122 ························sw.WriteLine("----");
123 ························sw.WriteLine("Row: " + r.RowState.ToString());
124 ························DataRowVersion drv = r.RowState == DataRowState.Deleted ? DataRowVersion.Original : DataRowVersion.Current;
125 ························foreach (DataColumn c in r.Table.Columns)
126 ························{
127 ····························sw.WriteLine(c.ColumnName + " = " + r[c, drv].ToString());
128 ························}
129 ····················}
130 ················
131 ················}
132 ············}
133 ············finally
134 ············{
135 ················if (sw != null)
136 ················{····················
137 ····················sw.Close();
138 ····················if (this.logger != null && this.logger.IsEnabled( LogLevel.Debug ))
139 ························this.logger.LogDebug( sw.ToString() );
140 ················}
141 ············}
142 ········}
143
144 ········private void DumpParameters(IDbCommand cmd, TextWriter sw)
145 ········{
146 ············int j = 1;
147 ············foreach(IDbDataParameter dp in cmd.Parameters)
148 ············{
149 ················string parType = provider.GetDbTypeString(dp);
150 ················sw.Write($"Parameter {j}: {dp.ToString()}({parType}) Source Column: {dp.SourceColumn} ");
151 ················sw.Write($"Prec: {dp.Precision} Scale: {dp.Scale} Size: {dp.Size}");
152 ················if (dp.Value != null)
153 ····················sw.WriteLine(" Value: " + dp.Value);
154 ················else
155 ····················sw.WriteLine();
156
157 ················j++;
158 ············}
159 ········}
160 ····}
161 }
162