3 // Christian Hergert <chris@mosaix.net>
4 // Ben Motmans <ben.motmans@gmail.com>
6 // Copyright (C) 2005 Christian Hergert
7 // Copyright (c) 2007 Ben Motmans
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
30 using System
.Threading
;
31 using System
.Collections
.Generic
;
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
);
56 public static void EnsureConnection (DatabaseConnectionContext context
, DatabaseConnectionContextCallback callback
, object state
)
59 throw new ArgumentNullException ("context");
61 throw new ArgumentNullException ("callback");
63 IConnectionPool pool
= context
.ConnectionPool
;
64 if (pool
.IsInitialized
) {
65 callback (context
, true, state
);
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
);
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
;
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
116 public DatabaseConnectionContext ConnectionContext
;
117 public DatabaseConnectionContextCallback Callback
;
119 public EnsureConnectionState (DatabaseConnectionContext context
, DatabaseConnectionContextCallback callback
, object state
)
121 ConnectionContext
= context
;