/// <summary> /// Request a cancel connect /// </summary> public async Task <ListDatabasesResponse> ListDatabases(string ownerUri) { var listParams = new ListDatabasesParams(); listParams.OwnerUri = ownerUri; return(await Driver.SendRequest(ListDatabasesRequest.Type, listParams)); }
public void TestListDatabasesInvalidParams() { var service = TestObjects.GetTestConnectionService(); var listParams = new ListDatabasesParams(); Assert.Throws <ArgumentException>(() => service.ListDatabases(listParams)); listParams.OwnerUri = "file://notmyfile.sql"; Assert.Throws <Exception>(() => service.ListDatabases(listParams)); }
public async void ListDatabasesOnServerForCurrentConnectionReturnsDatabaseNames() { // Result set for the query of database names Dictionary <string, string>[] data = { new Dictionary <string, string> { { "name", "master" } }, new Dictionary <string, string> { { "name", "model" } }, new Dictionary <string, string> { { "name", "msdb" } }, new Dictionary <string, string> { { "name", "tempdb" } }, new Dictionary <string, string> { { "name", "mydatabase" } }, }; // Setup mock connection factory to inject query results var mockFactory = new Mock <ISqlConnectionFactory>(); mockFactory.Setup(factory => factory.CreateSqlConnection(It.IsAny <string>())) .Returns(CreateMockDbConnection(new[] { data })); var connectionService = new ConnectionService(mockFactory.Object); // connect to a database instance string ownerUri = "file://my/sample/file.sql"; var connectionResult = await connectionService .Connect(new ConnectParams() { OwnerUri = ownerUri, Connection = TestObjects.GetTestConnectionDetails() }); // verify that a valid connection id was returned Assert.NotEmpty(connectionResult.ConnectionId); // list databases for the connection ListDatabasesParams parameters = new ListDatabasesParams(); parameters.OwnerUri = ownerUri; var listDatabasesResult = connectionService.ListDatabases(parameters); string[] databaseNames = listDatabasesResult.DatabaseNames; Assert.Equal(databaseNames.Length, 5); Assert.Equal(databaseNames[0], "master"); Assert.Equal(databaseNames[1], "model"); Assert.Equal(databaseNames[2], "msdb"); Assert.Equal(databaseNames[3], "tempdb"); Assert.Equal(databaseNames[4], "mydatabase"); }
/// <summary> /// List all databases on the server specified /// </summary> public ListDatabasesResponse ListDatabases(ListDatabasesParams listDatabasesParams) { // Verify parameters var owner = listDatabasesParams.OwnerUri; if (string.IsNullOrEmpty(owner)) { throw new ArgumentException(SR.ConnectionServiceListDbErrorNullOwnerUri); } // Use the existing connection as a base for the search ConnectionInfo info; if (!TryFindConnection(owner, out info)) { throw new Exception(SR.ConnectionServiceListDbErrorNotConnected(owner)); } ConnectionDetails connectionDetails = info.ConnectionDetails.Clone(); // Connect to master and query sys.databases connectionDetails.DatabaseName = "master"; var connection = this.ConnectionFactory.CreateSqlConnection(BuildConnectionString(connectionDetails)); connection.Open(); List <string> results = new List <string>(); var systemDatabases = new[] { "master", "model", "msdb", "tempdb" }; using (DbCommand command = connection.CreateCommand()) { command.CommandText = "SELECT name FROM sys.databases ORDER BY name ASC"; command.CommandTimeout = 15; command.CommandType = CommandType.Text; using (var reader = command.ExecuteReader()) { while (reader.Read()) { results.Add(reader[0].ToString()); } } } // Put system databases at the top of the list results = results.Where(s => systemDatabases.Any(s.Equals)).Concat( results.Where(s => systemDatabases.All(x => !s.Equals(x)))).ToList(); connection.Close(); ListDatabasesResponse response = new ListDatabasesResponse(); response.DatabaseNames = results.ToArray(); return(response); }
/// <summary> /// Handle requests to list databases on the current server /// </summary> protected async Task HandleListDatabasesRequest( ListDatabasesParams listDatabasesParams, RequestContext <ListDatabasesResponse> requestContext) { Logger.Write(LogLevel.Verbose, "ListDatabasesRequest"); try { ListDatabasesResponse result = Instance.ListDatabases(listDatabasesParams); await requestContext.SendResult(result); } catch (Exception ex) { await requestContext.SendError(ex.ToString()); } }