File: BaseNode.cs

package info (click to toggle)
mysql-connector-net 6.4.3-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,160 kB
  • ctags: 8,552
  • sloc: cs: 63,689; xml: 7,505; sql: 345; makefile: 50; ansic: 40
file content (419 lines) | stat: -rw-r--r-- 14,772 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright  2008, 2010, Oracle and/or its affiliates. All rights reserved.
//
// MySQL Connector/NET is licensed under the terms of the GPLv2
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most 
// MySQL Connectors. There are special exceptions to the terms and 
// conditions of the GPLv2 as it is applied to this software, see the 
// FLOSS License Exception
// <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
//
// This program is free software; you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published 
// by the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
//
// You should have received a copy of the GNU General Public License along 
// with this program; if not, write to the Free Software Foundation, Inc., 
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using MySql.Data.VisualStudio.Properties;
using Microsoft.VisualStudio.Shell.Interop;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Data;
using System.Diagnostics;
using Microsoft.VisualStudio;
using System.Data.Common;
using System.Resources;
using System.Globalization;
using System.Data;
using Microsoft.VisualStudio.Data.AdoDotNet;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using MySql.Data.VisualStudio.Editors;
using Microsoft.VisualStudio.Shell;

namespace MySql.Data.VisualStudio
{
	abstract class BaseNode
	{
		protected Guid editorGuid;
		protected Guid commandGroupGuid;
        protected string name;
        private static string defaultStorageEngine;
        public DataViewHierarchyAccessor HierarchyAccessor;
        public bool IsNew;

		public BaseNode(DataViewHierarchyAccessor hierarchyAccessor, int id)
		{
			HierarchyAccessor = hierarchyAccessor;
			ItemId = id;
            IsNew = ItemId == 0;

            // set the server and database from our active connection
            DbConnection conn = (DbConnection)HierarchyAccessor.Connection.GetLockedProviderObject();
            Server = conn.DataSource;
            Database = conn.Database;
            HierarchyAccessor.Connection.UnlockProviderObject();

            if (!IsNew)
                Name = hierarchyAccessor.GetNodeName(id);
            else
                ItemId = hierarchyAccessor.CreateObjectNode();

            NameIndex = 2;
            commandGroupGuid = VSConstants.GUID_TextEditorFactory;
			editorGuid = Guid.Empty;
		}

		#region Properties

        public string Name 
        {
            get
            {
                if (name == null)
                    Name = GenerateUniqueName();
                return name;
            }
            protected set 
            {
                name = value;
            }
        }

        private int _itemId;
        public int ItemId 
        {
            get { return _itemId; }
            protected set { _itemId = value; }
        }

        private string _nodeId;
        public string NodeId 
        {
            get { return _nodeId; }
            protected set { _nodeId = value; }
        }

        private string _server;
        public string Server 
        {
            get { return _server; }
            private set { _server = value; }
        }

        private string _database;
        public string Database 
        {
            get { return _database; }
            private set { _database = value; }
        }

        private bool _dirty;
        public virtual bool Dirty 
        {
            get { return _dirty; }
            protected set { _dirty = value; }
        }

        protected string Moniker 
        { 
            get { return String.Format("mysql://{0}/{1}/{2}", Server, Database, Name); }
        }

        private int _nameIndex;
        public int NameIndex 
        {
            get { return _nameIndex; }
            protected set { _nameIndex = value; }
        }

        public virtual string SchemaCollection
        {
            get { return NodeId + "s"; }
        }

        public virtual string LocalizedTypeString
        {
            get
            {
                return Resources.ResourceManager.GetString("Type_" + NodeId);
            }
        }

        public string DefaultStorageEngine
        {
            get
            {
                if (defaultStorageEngine == null)
                {
                    DataTable dt = GetDataTable("SHOW VARIABLES LIKE 'storage_engine'");
                    defaultStorageEngine = "MyISAM";
                    if (dt != null && dt.Rows.Count == 1)
                        defaultStorageEngine = (string)dt.Rows[0][1];
                }
                return defaultStorageEngine;
            }
        }

    	#endregion

        #region Virtuals

        public virtual void ExecuteCommand(int command)
		{
			switch ((uint)command)
			{
                case 12291:  // design
				case PkgCmdIDList.cmdAlterTable:
				case PkgCmdIDList.cmdAlterTrigger:
				case PkgCmdIDList.cmdAlterProcedure:
				case PkgCmdIDList.cmdAlterView:
					Alter();
					break;

                case (uint)VSConstants.VSStd97CmdID.Delete:
                    Drop();
                    break;
            }
		}


        public virtual void Edit()
        {
            if (!HierarchyAccessor.ActivateDocumentIfOpen(Moniker))
                OpenEditor();
        }

        public virtual object GetEditor()
        {
            throw new NotImplementedException();
        }

        public virtual void Alter()
        {
            Edit();
        }

        public virtual string GetDropSQL()
        {
            return String.Format("DROP {0} `{1}`.`{2}`", NodeId, Database, Name);
        }

        private void Drop()
        {
            string typeString = LocalizedTypeString.ToLower(CultureInfo.CurrentCulture);

            DialogResult result = MessageBox.Show(String.Format(
                Resources.DropConfirmation, typeString, Name),
                String.Format(Resources.DropConfirmationCaption, typeString),
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);
            if (result == DialogResult.No)
                throw new OperationCanceledException();

            string sql = GetDropSQL();
            try
            {
                ExecuteSQL(sql);

                // now we drop the node from the hierarchy
                HierarchyAccessor.DropObjectNode(ItemId);
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    String.Format(Resources.ErrorAttemptingToDrop,
                    LocalizedTypeString, Name, ex.Message), Resources.ErrorTitle,
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        protected virtual string GenerateUniqueName()
        {
            DbConnection conn = (DbConnection)HierarchyAccessor.Connection.GetLockedProviderObject();
            string[] restrictions = new string[2] { null, Database };
            try
            {
                DataTable dt = conn.GetSchema(SchemaCollection, restrictions);

                int uniqueIndex = 1;
                string objectName = String.Empty;

                foreach (DataRow row in dt.Rows)
                {
                    objectName = String.Format("{0}{1}", NodeId, uniqueIndex).Replace(" ", "");
                    if (row[NameIndex].ToString().ToLowerInvariant() == objectName.ToLowerInvariant())
                        uniqueIndex++;
                }
                Name = String.Format("{0}{1}", NodeId, uniqueIndex).Replace(" ", "");
                HierarchyAccessor.SetProperty(ItemId, (int)__VSHPROPID.VSHPROPID_Name, Name);
                return Name;
            }
            finally
            {
                HierarchyAccessor.Connection.UnlockProviderObject();
            }
        }

        #endregion

        #region Private Methods

		private void OpenEditor()
		{
			IVsUIShellOpenDocument shell =
				MySqlDataProviderPackage.GetGlobalService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;

			IVsWindowFrame winFrame = null;

			object editor = GetEditor();
            object coreEditor = editor;
            editorGuid = editor.GetType().GUID;
            if (editor is VSCodeEditor)
                coreEditor = (editor as VSCodeEditor).CodeWindow;

			IntPtr viewPunk = Marshal.GetIUnknownForObject(coreEditor);
			IntPtr dataPunk = Marshal.GetIUnknownForObject(this);
            Guid viewGuid = VSConstants.LOGVIEWID_TextView;

			// Initialize IDE editor infrastracture
			int result = shell.InitializeEditorInstance(
				(uint)0,                // Initialization flags. We need default behavior here
				viewPunk,               // View object reference (should implement IVsWindowPane)
				dataPunk,               // Docuemnt object reference (should implement IVsPersistDocData)
				Moniker,                // Document moniker
				ref editorGuid,         // GUID of the editor type
				null,                   // Name of the physical view. We use default
				ref viewGuid,           // GUID identifying the logical view.
				null,                   // Initial caption defined by the document owner. Will be initialized by the editor later
				null,                   // Initial caption defined by the document editor. Will be initialized by the editor later
				// Pointer to the IVsUIHierarchy interface of the project that contains the document
				HierarchyAccessor.Hierarchy,
				(uint)ItemId,           // UI hierarchy item identifier of the document in the project system
				IntPtr.Zero,            // Pointer to the IUnknown interface of the document data object if the document data object already exists in the running document table
				// Project-specific service provider.
				HierarchyAccessor.ServiceProvider as Microsoft.VisualStudio.OLE.Interop.IServiceProvider,
				ref commandGroupGuid,   // Command UI GUID of the commands to display for this editor.
				out winFrame            // The window frame that contains the editor
				);

			Debug.Assert(winFrame != null &&
						 ErrorHandler.Succeeded(result), "Failed to initialize editor");

            // if our editor is a text buffer then hook up our language service
            //if (editor is TextBufferEditor)
            //{
            //    // now we tell our text buffer what language service to use
            //    Guid langSvcGuid = typeof(MySqlLanguageService).GUID;
            //    (editor as TextBufferEditor).TextBuffer.SetLanguageServiceID(ref langSvcGuid);
            //}

			winFrame.Show();
		}

        protected void SaveNode()
        {
            string nodePath = String.Format("/Connection/{0}s/{1}", NodeId, Name);
            HierarchyAccessor.SetNodePath(ItemId, nodePath);
        }

		protected void ExecuteSQL(string sql)
		{
			DbConnection conn = (DbConnection)HierarchyAccessor.Connection.GetLockedProviderObject();
			try
			{
				DbCommand cmd = conn.CreateCommand();
				cmd.CommandText = sql;
				cmd.ExecuteNonQuery();
			}
			finally
			{
				HierarchyAccessor.Connection.UnlockProviderObject();
			}
		}

        public DataTable GetSchema(string schemaName, string[] restrictions)
        {
            DbConnection conn = (DbConnection)HierarchyAccessor.Connection.GetLockedProviderObject();
            try
            {
                return conn.GetSchema(schemaName, restrictions);
            }
            finally
            {
                HierarchyAccessor.Connection.UnlockProviderObject();
            }
        }

        public DataTable GetDataTable(string sql)
        {
            DbConnection conn = (DbConnection)HierarchyAccessor.Connection.GetLockedProviderObject();
            try
            {
                DbDataAdapter da = MySqlProviderObjectFactory.Factory.CreateDataAdapter();
                DbCommand cmd = MySqlProviderObjectFactory.Factory.CreateCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            finally
            {
                HierarchyAccessor.Connection.UnlockProviderObject();
            }
        }

        /// <summary>
        /// Selects connection node in the Server Explorer window.
        /// </summary>
        private void SelectConnectionNode()
        {
            //MySqlDataProviderPackage package = MySqlDataProviderPackage.Instance;
            // Extracts connection mamanger global service
            DataExplorerConnectionManager manager = Package.GetGlobalService(typeof(DataExplorerConnectionManager))
                as DataExplorerConnectionManager;
            if (manager == null)
            {
                Debug.Fail("Failed to get connection manager!");
                return;
            }
            
            // Searches for connection using connection string for current connection
            DataExplorerConnection connection = manager.FindConnection(
                Guids.Provider, HierarchyAccessor.Connection.EncryptedConnectionString, true);
            if (connection == null)
            {
                Debug.Fail("Failed to find proper connection node!");
                return;
            }

            // Select connection node
            manager.SelectConnection(connection);
        }

        #endregion

        /// <summary>
        /// Refresh database node in server explorer
        /// </summary>
        public void Refresh()
        {
            SelectConnectionNode();
            IVsUIHierarchy hier = HierarchyAccessor.Hierarchy as IVsUIHierarchy;
            Guid g = VSConstants.GUID_VSStandardCommandSet97;
            hier.ExecCommand(VSConstants.VSITEMID_ROOT, ref g, (uint)VSConstants.VSStd97CmdID.Refresh, 
                (uint)OleCommandExecutionOption.DoDefault, IntPtr.Zero, IntPtr.Zero);
        }
	}
}