* Makefile.am:
[monodevelop.git] / extras / MonoDevelop.Database / MonoDevelop.Database.Sql / QueryService.cs
blob5b8e62c4cf4ccc0406cbdd349aa37c2959618635
1 //
2 // Authors:
3 // Christian Hergert <chris@mosaix.net>
4 // Ben Motmans <ben.motmans@gmail.com>
5 //
6 // Copyright (C) 2005 Christian Hergert
7 // Copyright (c) 2007 Ben Motmans
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
28 using System;
29 using System.Data;
30 using System.Threading;
31 using System.Collections.Generic;
32 using Mono.Addins;
33 using MonoDevelop.Database.Sql;
34 using MonoDevelop.Core;
35 using MonoDevelop.Core.Gui;
37 namespace MonoDevelop.Database.Sql
39 public delegate void DatabaseConnectionContextCallback (DatabaseConnectionContext context, bool connected, object state);
41 public static class QueryService
43 public static void RaiseException (Exception exception)
45 MessageService.ShowException (exception);
46 LoggingService.LogError ("Database Exception", exception);
49 public static void RaiseException (string message, Exception exception)
51 MessageService.ShowException (exception, message);
52 LoggingService.LogError ("Database Exception", exception);
55 //TODO: show errors
56 public static void EnsureConnection (DatabaseConnectionContext context, DatabaseConnectionContextCallback callback, object state)
58 if (context == null)
59 throw new ArgumentNullException ("context");
60 if (callback == null)
61 throw new ArgumentNullException ("callback");
63 IConnectionPool pool = context.ConnectionPool;
64 if (pool.IsInitialized) {
65 callback (context, true, state);
66 return;
69 IDbFactory fac = DbFactoryService.GetDbFactory (context.ConnectionSettings);
70 //FIXME: connection settings dialog
71 //bool requiresPassword = fac.GetCapabilities ("ConnectionSettings", SchemaActions.Schema) == (int)ConnectionSettingsCapabilities.Password;
72 bool requiresPassword = true;
74 if (!context.ConnectionSettings.SavePassword && String.IsNullOrEmpty (context.ConnectionSettings.Password) && requiresPassword) {
75 string password = MessageService.GetPassword (
76 AddinCatalog.GetString ("Please enter the password for connection '{0}'",
77 context.ConnectionSettings.Name),
78 AddinCatalog.GetString ("Enter Password")
81 if (password == null) {
82 callback (context, false, state);
83 return;
84 } else {
85 context.ConnectionSettings.Password = password;
89 EnsureConnectionState internalState = new EnsureConnectionState (context, callback, state);
90 ThreadPool.QueueUserWorkItem (new WaitCallback (EnsureConnectionThreaded), internalState);
93 private static void EnsureConnectionThreaded (object obj)
95 EnsureConnectionState internalState = obj as EnsureConnectionState;
96 IConnectionPool pool = internalState.ConnectionContext.ConnectionPool;
98 try {
99 bool connected = pool.Initialize ();
100 DispatchService.GuiDispatch (delegate () {
101 internalState.Callback (internalState.ConnectionContext, connected, internalState.State);
103 } catch (Exception e) {
104 LoggingService.LogDebug (e.ToString ());
106 DispatchService.GuiDispatch (delegate () {
107 internalState.Callback (internalState.ConnectionContext, false, internalState.State);
113 internal class EnsureConnectionState
115 public object State;
116 public DatabaseConnectionContext ConnectionContext;
117 public DatabaseConnectionContextCallback Callback;
119 public EnsureConnectionState (DatabaseConnectionContext context, DatabaseConnectionContextCallback callback, object state)
121 ConnectionContext = context;
122 Callback = callback;
123 State = state;