diff --git a/Controllers/ColumnsController.cs b/Controllers/ColumnsController.cs index f5f0072..c0cdaeb 100644 --- a/Controllers/ColumnsController.cs +++ b/Controllers/ColumnsController.cs @@ -1,13 +1,12 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Serilog; +using Serilog.Events; +using System.Collections.Specialized; using SMO = Microsoft.SqlServer.Management.Smo; using SMOCommon = Microsoft.SqlServer.Management.Common; using MSSqlRestApi.Models; -using Serilog; -using Serilog.Events; namespace MSSqlRestApi.Controllers { diff --git a/Controllers/DatabasesController.cs b/Controllers/DatabasesController.cs index 8f9e89b..ba06c6a 100644 --- a/Controllers/DatabasesController.cs +++ b/Controllers/DatabasesController.cs @@ -1,13 +1,12 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Serilog; +using Serilog.Events; +using System.Collections.Specialized; using SMO = Microsoft.SqlServer.Management.Smo; using SMOCommon = Microsoft.SqlServer.Management.Common; using MSSqlRestApi.Models; -using Serilog; -using Serilog.Events; namespace MSSqlRestApi.Controllers { diff --git a/Controllers/TablesController.cs b/Controllers/TablesController.cs index afe1947..3b32619 100644 --- a/Controllers/TablesController.cs +++ b/Controllers/TablesController.cs @@ -3,11 +3,12 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Serilog; +using Serilog.Events; +using System.Collections.Specialized; using SMO = Microsoft.SqlServer.Management.Smo; using SMOCommon = Microsoft.SqlServer.Management.Common; using MSSqlRestApi.Models; -using Serilog; -using Serilog.Events; namespace MSSqlRestApi.Controllers { @@ -36,7 +37,6 @@ public IActionResult GetTables(string dbName) // Get all tables in all schemas in this database smoDb.Tables.Refresh(); var query = from table in smoDb.Tables.Cast() -// where !table.IsSystemObject orderby table.Schema select table; diff --git a/Dockerfile b/Dockerfile index 33bd2c8..173eca1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,7 @@ LABEL version=1.0 ENV HOMEDIR=/mssql-restapi \ MSSQL_HOST=127.0.0.1 \ MSSQL_PORT=1433 \ + MSSQL_DATABASe=master \ MSSQL_USERNAME=sa \ MSSQL_PASSWORD=Yukon900 diff --git a/Models/ColumnResource.cs b/Models/ColumnResource.cs index e975a18..d3d0c44 100644 --- a/Models/ColumnResource.cs +++ b/Models/ColumnResource.cs @@ -1,6 +1,9 @@ using System; -using System.Collections.Generic; +//using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; +using Serilog; +using Serilog.Events; +using System.Collections.Specialized; using SMO = Microsoft.SqlServer.Management.Smo; using SMOCommon = Microsoft.SqlServer.Management.Common; diff --git a/Models/Constants.cs b/Models/Constants.cs index d41710c..756de97 100644 --- a/Models/Constants.cs +++ b/Models/Constants.cs @@ -7,9 +7,11 @@ public static class Constants public const string LinkNameParent = "parent"; public const string MSSQLEnvVarHost = "MSSQL_HOST"; public const string MSSQLEnvVarPort = "MSSQL_PORT"; + public const string MSSQLEnvVarDatabase = "MSSQL_DATABASE"; public const string MSSQLEnvVarUsername = "MSSQL_USERNAME"; public const string MSSQLEnvVarPassword = "MSSQL_PASSWORD"; public const string MSSQLDefaultHost = "127.0.0.1"; + public const string MSSQLDefaultDatabase = "master"; public const string MSSQLDefaultPort = "1433"; public const string MSSQLDefaultUsername = "sa"; public const string MSSQLDefaultPassword = "Yukon900"; diff --git a/Models/DatabaseResource.cs b/Models/DatabaseResource.cs index f35b957..ca50935 100644 --- a/Models/DatabaseResource.cs +++ b/Models/DatabaseResource.cs @@ -1,8 +1,11 @@ using System; -using System.Collections.Generic; -using System.Data.SqlClient; -using System.Data.Common; +//using System.Collections.Generic; +//using System.Data.SqlClient; +//using System.Data.Common; using Microsoft.AspNetCore.Mvc; +using Serilog; +using Serilog.Events; +using System.Collections.Specialized; using SMO = Microsoft.SqlServer.Management.Smo; using SMOCommon = Microsoft.SqlServer.Management.Common; diff --git a/Models/DatabaseScriptResource.cs b/Models/DatabaseScriptResource.cs index 1f2bd40..61151ff 100644 --- a/Models/DatabaseScriptResource.cs +++ b/Models/DatabaseScriptResource.cs @@ -1,10 +1,10 @@ using System; using Microsoft.AspNetCore.Mvc; -using SMO = Microsoft.SqlServer.Management.Smo; -using SMOCommon = Microsoft.SqlServer.Management.Common; using Serilog; using Serilog.Events; using System.Collections.Specialized; +using SMO = Microsoft.SqlServer.Management.Smo; +using SMOCommon = Microsoft.SqlServer.Management.Common; namespace MSSqlRestApi.Models { diff --git a/Models/ServerContext.cs b/Models/ServerContext.cs index 436660c..ab1b0dd 100644 --- a/Models/ServerContext.cs +++ b/Models/ServerContext.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +//using System.Collections.Generic; using System.Data.SqlClient; using Serilog; using Serilog.Events; @@ -10,14 +10,11 @@ namespace MSSqlRestApi.Models { public class ServerContext { - private string _host; - public string Host { get {return this._host;} } - private string _port; - public string Port { get {return this._port;} } - private string _username; - public string Username { get {return this._username;} } - private string _password; - public string Password { get {return this._password;} } + public string Host { get; } + public string Port { get; } + public string Database { get; } + public string Username { get; } + public string Password { get; } private SMO.Server _smoServer; public SMO.Server SmoServer @@ -31,37 +28,24 @@ public SMO.Server SmoServer return _smoServer; } } - public ServerContext() { Log.Information("Initializing ServerContext with Environment variables:"); - this._host = Environment.GetEnvironmentVariable(Constants.MSSQLEnvVarHost) ?? Constants.MSSQLDefaultHost; - this._port = Environment.GetEnvironmentVariable(Constants.MSSQLEnvVarPort) ?? Constants.MSSQLDefaultPort; - this._username = Environment.GetEnvironmentVariable(Constants.MSSQLEnvVarUsername) ?? Constants.MSSQLDefaultUsername; - this._password = Environment.GetEnvironmentVariable(Constants.MSSQLEnvVarPassword) ?? Constants.MSSQLDefaultPassword; + this.Host = Environment.GetEnvironmentVariable(Constants.MSSQLEnvVarHost) ?? Constants.MSSQLDefaultHost; + this.Port = Environment.GetEnvironmentVariable(Constants.MSSQLEnvVarPort) ?? Constants.MSSQLDefaultPort; + this.Database = Environment.GetEnvironmentVariable(Constants.MSSQLEnvVarDatabase) ?? Constants.MSSQLDefaultDatabase; + this.Username = Environment.GetEnvironmentVariable(Constants.MSSQLEnvVarUsername) ?? Constants.MSSQLDefaultUsername; + this.Password = Environment.GetEnvironmentVariable(Constants.MSSQLEnvVarPassword) ?? Constants.MSSQLDefaultPassword; - Log.Information("{0}: {1}, {2}: {3}, {4}: {5}, {6}: {7}", - Constants.MSSQLEnvVarHost, this._host, - Constants.MSSQLEnvVarPort, this._port, - Constants.MSSQLEnvVarUsername, this._username, - Constants.MSSQLEnvVarPassword, this._password); + Log.Information("{0}: {1}, {2}: {3}, {4}: {5}, {6}: {7}, {8}: {9}", + Constants.MSSQLEnvVarHost, this.Host, + Constants.MSSQLEnvVarPort, this.Port, + Constants.MSSQLEnvVarDatabase, this.Database, + Constants.MSSQLEnvVarUsername, this.Username, + Constants.MSSQLEnvVarPassword, this.Password); this.Initialize(); } - public ServerContext(string host, string port, string username, string password) - { - Log.Information("Initializing ServerContext"); - this._host = host; - this._port = port; - this._username = username; - this._password = password; - - Log.Information("Host: {0}, Port: {1}, Username: {2}, Password: {3}", - this._host, this._port, this._username, this._password); - - this.Initialize(); - } - private void Initialize() { if(_smoServer == null) @@ -71,13 +55,17 @@ private void Initialize() // Build connection string SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.ApplicationName = "mssql-restapi"; - builder.DataSource = this._host + "," + this._port; - builder.UserID = this._username; - builder.Password = this._password; + builder.DataSource = this.Host + "," + this.Port; + builder.InitialCatalog = this.Database; + builder.UserID = this.Username; + builder.Password = this.Password; + builder.MultipleActiveResultSets = true; // required for SQL Azure builder.ConnectTimeout = 30; + builder.ConnectRetryCount = 3; + builder.ConnectRetryInterval = 15; builder.IntegratedSecurity = false; - builder.InitialCatalog = "master"; + // Create a SMO connection SqlConnection sqlConnection = new SqlConnection(builder.ConnectionString); SMOCommon.ServerConnection serverConnection = new SMOCommon.ServerConnection(sqlConnection); _smoServer = new SMO.Server(serverConnection); diff --git a/Models/ServerResource.cs b/Models/ServerResource.cs index 8dfc076..f9e854d 100644 --- a/Models/ServerResource.cs +++ b/Models/ServerResource.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; +using Serilog; +using Serilog.Events; using SMO = Microsoft.SqlServer.Management.Smo; using SMOCommon = Microsoft.SqlServer.Management.Common; @@ -8,12 +10,31 @@ namespace MSSqlRestApi.Models { public class ServerResource : Resource { - public string Name { get { return this.SmoServer.Name; } } - public string Product { get { return this.SmoServer.Product; } } + public string ServerName { get { return this.SmoServer.Name; } } + + public string Product + { + // Azure SQL DB and Azure SQL DW don't support the 'Product' property + get + { + string retVal = "Unknown"; + if(this.SmoServer.ServerType == SMOCommon.DatabaseEngineType.Standalone) + { + retVal = this.SmoServer.Product; + } + return retVal; + } + } public string HostPlatform { get{ return this.SmoServer.HostPlatform; } } + public string ServerType { get { return this.SmoServer.ServerType.ToString(); } } public string Edition { get { return this.SmoServer.Edition; } } + public string EngineEdition { get { return this.SmoServer.EngineEdition.ToString(); } } + public string ProductLevel { get { return this.SmoServer.ProductLevel; } } + public string Collation { get { return this.SmoServer.Collation; } } public string VersionString { get { return this.SmoServer.VersionString; } } - + public string ResourceVersionString { get { return this.SmoServer.ResourceVersionString; } } + + public Uri Databases { get; set; } private ServerContext _context; private SMO.Server SmoServer { get { return this._context.SmoServer; } } diff --git a/Models/TableResource.cs b/Models/TableResource.cs index 011fc11..0a73faf 100644 --- a/Models/TableResource.cs +++ b/Models/TableResource.cs @@ -1,10 +1,10 @@ using System; -using System.Collections.Generic; +//using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; -using SMO = Microsoft.SqlServer.Management.Smo; -using SMOCommon = Microsoft.SqlServer.Management.Common; using Serilog; using Serilog.Events; +using SMO = Microsoft.SqlServer.Management.Smo; +using SMOCommon = Microsoft.SqlServer.Management.Common; namespace MSSqlRestApi.Models { diff --git a/Models/TableScriptResource.cs b/Models/TableScriptResource.cs index 5853d6e..3836981 100644 --- a/Models/TableScriptResource.cs +++ b/Models/TableScriptResource.cs @@ -1,10 +1,10 @@ using System; using Microsoft.AspNetCore.Mvc; -using SMO = Microsoft.SqlServer.Management.Smo; -using SMOCommon = Microsoft.SqlServer.Management.Common; +using System.Collections.Specialized; using Serilog; using Serilog.Events; -using System.Collections.Specialized; +using SMO = Microsoft.SqlServer.Management.Smo; +using SMOCommon = Microsoft.SqlServer.Management.Common; namespace MSSqlRestApi.Models { diff --git a/Models/TablesInSchemaResource.cs b/Models/TablesInSchemaResource.cs index 6f9a315..6d1709a 100644 --- a/Models/TablesInSchemaResource.cs +++ b/Models/TablesInSchemaResource.cs @@ -2,10 +2,10 @@ using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Mvc; -using SMO = Microsoft.SqlServer.Management.Smo; -using SMOCommon = Microsoft.SqlServer.Management.Common; using Serilog; using Serilog.Events; +using SMO = Microsoft.SqlServer.Management.Smo; +using SMOCommon = Microsoft.SqlServer.Management.Common; namespace MSSqlRestApi.Models { diff --git a/Models/Top100RowsResource.cs b/Models/Top100RowsResource.cs index 0ebd7dc..9d1b876 100644 --- a/Models/Top100RowsResource.cs +++ b/Models/Top100RowsResource.cs @@ -1,11 +1,11 @@ using System; +using System.Data; using System.Collections.Generic; using Microsoft.AspNetCore.Mvc; -using SMO = Microsoft.SqlServer.Management.Smo; -using SMOCommon = Microsoft.SqlServer.Management.Common; using Serilog; using Serilog.Events; -using System.Data; +using SMO = Microsoft.SqlServer.Management.Smo; +using SMOCommon = Microsoft.SqlServer.Management.Common; namespace MSSqlRestApi.Models { diff --git a/README.md b/README.md index b2aa981..a397285 100644 --- a/README.md +++ b/README.md @@ -73,12 +73,13 @@ dotnet run You can use environment variables to to make the prototype connect to a local or remote SQL Server instance, Azure SQL Database and Azure SQL Data Warehouse. -Environment variable | Default Value | Description ---------------- | ------ | ------------ -**MSSQL_HOST** | *127.0.0.1* | The fully qualified server name -**MSSQL_PORT** | *1433* | SQL Server port -**MSSQL_USERNAME** | *sa* | Username for SQL Server authentication -**MSSQL_PASSWORD** | *Yukon900* | Password for SQL Server authentication +Environment variable | Description +--------------- | ------------ +**MSSQL_HOST** | Fully qualified server name. Defaults to *127.0.0.1* if not specified. +**MSSQL_PORT** | SQL Server port. Defaults to *1433* if not specified. +**MSSQL_DATABASE** | Initial catalog for the connection. Defaults to *master* if not specified. +**MSSQL_USERNAME** | Username for SQL Server authentication. Defaults to *sa* if not specified. +**MSSQL_PASSWORD** | Password for SQL Server authentication. Defaults to *Yukon900* if not specified. ## Connect to Azure SQL Database or Azure SQL Data Warehouse