Datei: NDO.Mapping/NDO.Mapping/Connection.cs

Last Commit (e34b4f0)
1 //
2 // Copyright (c) 2002-2024 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;
24 using System.Collections.Generic;
25 using System.ComponentModel;
26 using System.Linq;
27 using System.Text;
28 using System.Text.RegularExpressions;
29 using System.Xml;
30
31 namespace NDO.Mapping
32 {
33 ····/// <summary>
34 ····/// This class encapsulates a connection string and makes it available with a short ID.
35 ····/// </summary>
36 ····/// <remarks>This class is equivalent to the Connection element in the mapping file schema.</remarks>
37 ····public class Connection : MappingNode
38 ····{
39 ········#region Standard Connection Stuff
40 ········/// <summary>
41 ········///
42 ········/// </summary>
43 ········public const string DummyConnectionString = "Substitute this string with your connection string";
44
45 ········static Connection standardConnection;
46 ········
47 ········/// <summary>
48 ········/// Creates a new, empty Connection object.
49 ········/// </summary>
50 ········public static Connection StandardConnection
51 ········{
52 ············get { return standardConnection; }
53 ········}
54
55 ········static Connection()
56 ········{
57 ············standardConnection = new Connection(null);
58 ············standardConnection.name = DummyConnectionString;
59 ············standardConnection.type = "";
60 ········}
61
62 ········#endregion
63
64 ········# region State variables and accessor properties
65 ········private string iD = "";
66 ········/// <summary>
67 ········/// Gets or sets the ID, with which the connection is referenced in the Class objects.
68 ········/// </summary>
69 ········[Description("The ID, with which the connection is referenced in the Class objects.")]
70 ········public string ID
71 ········{
72 ············get { return iD; }
73 ············set { iD = value; this.Changed = true; }
74 ········}
75 ········private string name = "";
76 ········/// <summary>
77 ········/// Gets or sets the connection string.
78 ········/// </summary>
79 ········[Description("The connection string.")]
80 ········public string Name
81 ········{
82 ············get { return name; }
83 ············set { name = value; this.Changed = true; }
84 ········}
85
86 ········/// <summary>
87 ········/// This is used to log the connection without password.
88 ········/// </summary>
89 ········[Browsable( false )]
90 ········public string DisplayName
91 ········{
92 ············get
93 ············{
94 ················Regex regex = new Regex( "password=[^;]*" );
95 ················return regex.Replace( this.name, "password=***" );················
96 ············}
97 ········}
98
99 ········private string type = "Sql";
100 ········/// <summary>
101 ········/// Gets or sets the provider type string.
102 ········/// </summary>
103 ········/// <remarks>
104 ········/// This string sould match the Name property of a
105 ········/// provider registered in the NDOProviderFactory.
106 ········/// <seealso cref="NDOInterfaces.INDOProviderFactory"/>
107 ········/// </remarks>
108 ········[Description("The provider type string.")]
109 ········public string Type
110 ········{
111 ············get { return type; }
112 ············set { type = value; this.Changed = true; }
113 ········}
 
 
 
 
 
 
114
115 ········/// <summary>
116 ········/// Gets the parent of the Connection object
117 ········/// </summary>
118 ········[Browsable(false)]
119 ········public NDOMapping Parent
120 ········{
121 ············get { return NodeParent as NDOMapping; }
122 ············//············set { parent = value; }
123 ········}
124
125 ········/// <summary>
126 ········/// Removes the Connection object from the Connection list of the parent object.
127 ········/// </summary>
128 ········public override void Remove()
129 ········{
130 ············Parent.RemoveConnection(this);
131 ········}
132
133 ········//private const string sqlString = "Provider=SQLOLEDB.1;";
134 ········//private const string oracleString1 = "Provider=MSDAORA.1;";
135 ········//private const string oracleString2 = "Provider=OraOLEDB.Oracle.1;";
136 ········//private const string jetString = "Provider=Microsoft.Jet.OLEDB";
137
138 ········#endregion
139
140
141 ········/// <summary>
142 ········/// Constructs a new Connection object
143 ········/// </summary>
144 ········/// <param name="parent">The NDOMapping object, which is the parent of the connection.</param>
145 ········public Connection(NDOMapping parent)
146 ············: base(parent)
147 ········{
148 ········}
149
150 ········/// <summary>
151 ········/// Returns a connection type according to an OleDb connection string.
152 ········/// </summary>
153 ········/// <param name="connStr">An OleDb connection string.</param>
154 ········/// <returns>A type name, if the connection string could be recognized, else null.</returns>
155 ········public string GetDefaultTypeString(string connStr)
156 ········{
157 ············return null;
158 ········}
159
160 ········/// <summary>
161 ········/// This function used by the enhancer only. Don't call it.
162 ········/// </summary>
163 ········public void FromStandardConnection()
164 ········{
165 ············name = standardConnection.name;
166 ············type = standardConnection.type;
167
168 ············if (null == name || string.Empty == name)
169 ············{
170 ················name = DummyConnectionString;
171 ············}
172
173 ········}
174
175 ········/// <summary>
176 ········/// Construct a Connection object based on a Connection node of the mapping file.
177 ········/// </summary>
178 ········/// <param name="connNode">Connection node</param>
179 ········/// <param name="parent">An object of type NDOMapping, which is the parent of the connection object.</param>
180 ········internal Connection(XmlNode connNode, NDOMapping parent)
181 ············: base(connNode, parent)
182 ········{
183 ············name = connNode.Attributes["Name"].Value;
184 ············iD = connNode.Attributes["ID"].Value;
185 ············type = connNode.Attributes["Type"].Value;
186 ············if (name == null || name == string.Empty)
187 ············{
188 ················FromStandardConnection();
189 ············}
190 ········}
191
192 ········/// <summary>
193 ········/// Saves the Connection object as a Connection node. Don't use that funktion directly,
194 ········/// it will be called by the Save function of the NDOMapping class.
195 ········/// </summary>
196 ········/// <param name="parentNode">The parent node</param>········
197 ········internal void Save(XmlNode parentNode)
198 ········{
199 ············XmlElement newNode = parentNode.OwnerDocument.CreateElement("Connection");
200 ············newNode.SetAttribute("ID", iD.ToString());
201 ············newNode.SetAttribute("Name", name);
202 ············newNode.SetAttribute("Type", type);
203 ············parentNode.AppendChild(newNode);
204 ············base.SaveProperties(newNode);
205 ········}
206
207 ········/// <summary>
208 ········/// String representation of the Connection object
209 ········/// </summary>
210 ········/// <returns>The connection string</returns>
211 ········public override string ToString()
212 ········{
213 ············return Name;
214 ········}
215 ····}
216
217 }
218
New Commit (14cacf2)
1 //
2 // Copyright (c) 2002-2024 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 NDOInterfaces;
24 using System;
25 using System.Collections.Generic;
26 using System.ComponentModel;
27 using System.Linq;
28 using System.Text;
29 using System.Text.RegularExpressions;
30 using System.Xml;
31
32 namespace NDO.Mapping
33 {
34 ····/// <summary>
35 ····/// This class encapsulates a connection string and makes it available with a short ID.
36 ····/// </summary>
37 ····/// <remarks>This class is equivalent to the Connection element in the mapping file schema.</remarks>
38 ····public class Connection : MappingNode
39 ····{
40 ········#region Standard Connection Stuff
41 ········/// <summary>
42 ········///
43 ········/// </summary>
44 ········public const string DummyConnectionString = "Substitute this string with your connection string";
45
46 ········static Connection standardConnection;
47 ········
48 ········/// <summary>
49 ········/// Creates a new, empty Connection object.
50 ········/// </summary>
51 ········public static Connection StandardConnection
52 ········{
53 ············get { return standardConnection; }
54 ········}
55
56 ········static Connection()
57 ········{
58 ············standardConnection = new Connection(null);
59 ············standardConnection.name = DummyConnectionString;
60 ············standardConnection.type = "";
61 ········}
62
63 ········#endregion
64
65 ········# region State variables and accessor properties
66 ········private string iD = "";
67 ········/// <summary>
68 ········/// Gets or sets the ID, with which the connection is referenced in the Class objects.
69 ········/// </summary>
70 ········[Description("The ID, with which the connection is referenced in the Class objects.")]
71 ········public string ID
72 ········{
73 ············get { return iD; }
74 ············set { iD = value; this.Changed = true; }
75 ········}
76 ········private string name = "";
77 ········/// <summary>
78 ········/// Gets or sets the connection string.
79 ········/// </summary>
80 ········[Description("The connection string.")]
81 ········public string Name
82 ········{
83 ············get { return name; }
84 ············set { name = value; this.Changed = true; }
85 ········}
86
87 ········/// <summary>
88 ········/// This is used to log the connection without password.
89 ········/// </summary>
90 ········[Browsable( false )]
91 ········public string DisplayName
92 ········{
93 ············get
94 ············{
95 ················Regex regex = new Regex( "password=[^;]*" );
96 ················return regex.Replace( this.name, "password=***" );················
97 ············}
98 ········}
99
100 ········private string type = "Sql";
101 ········/// <summary>
102 ········/// Gets or sets the provider type string.
103 ········/// </summary>
104 ········/// <remarks>
105 ········/// This string sould match the Name property of a
106 ········/// provider registered in the NDOProviderFactory.
107 ········/// <seealso cref="NDOInterfaces.INDOProviderFactory"/>
108 ········/// </remarks>
109 ········[Description("The provider type string.")]
110 ········public string Type
111 ········{
112 ············get { return type; }
113 ············set { type = value; this.Changed = true; }
114 ········}
115
116 ········/// <summary>
117 ········/// Gets the NDO Provider for this connection type
118 ········/// </summary>
119 ········[Browsable( false )]
120 ········public IProvider Provider => Parent.GetProvider( Type );
121
122 ········/// <summary>
123 ········/// Gets the parent of the Connection object
124 ········/// </summary>
125 ········[Browsable(false)]
126 ········public NDOMapping Parent
127 ········{
128 ············get { return NodeParent as NDOMapping; }
129 ············//············set { parent = value; }
130 ········}
131
132 ········/// <summary>
133 ········/// Removes the Connection object from the Connection list of the parent object.
134 ········/// </summary>
135 ········public override void Remove()
136 ········{
137 ············Parent.RemoveConnection(this);
138 ········}
 
 
 
 
 
139
140 ········#endregion
141
142
143 ········/// <summary>
144 ········/// Constructs a new Connection object
145 ········/// </summary>
146 ········/// <param name="parent">The NDOMapping object, which is the parent of the connection.</param>
147 ········public Connection(NDOMapping parent)
148 ············: base(parent)
149 ········{
150 ········}
151
152 ········/// <summary>
153 ········/// Returns a connection type according to an OleDb connection string.
154 ········/// </summary>
155 ········/// <param name="connStr">An OleDb connection string.</param>
156 ········/// <returns>A type name, if the connection string could be recognized, else null.</returns>
157 ········public string GetDefaultTypeString(string connStr)
158 ········{
159 ············return null;
160 ········}
161
162 ········/// <summary>
163 ········/// This function used by the enhancer only. Don't call it.
164 ········/// </summary>
165 ········public void FromStandardConnection()
166 ········{
167 ············name = standardConnection.name;
168 ············type = standardConnection.type;
169
170 ············if (null == name || string.Empty == name)
171 ············{
172 ················name = DummyConnectionString;
173 ············}
174
175 ········}
176
177 ········/// <summary>
178 ········/// Construct a Connection object based on a Connection node of the mapping file.
179 ········/// </summary>
180 ········/// <param name="connNode">Connection node</param>
181 ········/// <param name="parent">An object of type NDOMapping, which is the parent of the connection object.</param>
182 ········internal Connection(XmlNode connNode, NDOMapping parent)
183 ············: base(connNode, parent)
184 ········{
185 ············name = connNode.Attributes["Name"].Value;
186 ············iD = connNode.Attributes["ID"].Value;
187 ············type = connNode.Attributes["Type"].Value;
188 ············if (name == null || name == string.Empty)
189 ············{
190 ················FromStandardConnection();
191 ············}
192 ········}
193
194 ········/// <summary>
195 ········/// Saves the Connection object as a Connection node. Don't use that funktion directly,
196 ········/// it will be called by the Save function of the NDOMapping class.
197 ········/// </summary>
198 ········/// <param name="parentNode">The parent node</param>········
199 ········internal void Save(XmlNode parentNode)
200 ········{
201 ············XmlElement newNode = parentNode.OwnerDocument.CreateElement("Connection");
202 ············newNode.SetAttribute("ID", iD.ToString());
203 ············newNode.SetAttribute("Name", name);
204 ············newNode.SetAttribute("Type", type);
205 ············parentNode.AppendChild(newNode);
206 ············base.SaveProperties(newNode);
207 ········}
208
209 ········/// <summary>
210 ········/// String representation of the Connection object
211 ········/// </summary>
212 ········/// <returns>The connection string</returns>
213 ········public override string ToString()
214 ········{
215 ············return Name;
216 ········}
217 ····}
218
219 }
220