Skip to content

Commit

Permalink
Removed unused usings, fixed bugs with Azure SQL DB support, support …
Browse files Browse the repository at this point in the history
…for Initial Catalog
  • Loading branch information
sanagama committed Oct 21, 2017
1 parent db164dc commit 24a8050
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 74 deletions.
7 changes: 3 additions & 4 deletions Controllers/ColumnsController.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
7 changes: 3 additions & 4 deletions Controllers/DatabasesController.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
6 changes: 3 additions & 3 deletions Controllers/TablesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<SMO.Table>()
// where !table.IsSystemObject
orderby table.Schema
select table;

Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion Models/ColumnResource.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 2 additions & 0 deletions Models/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
9 changes: 6 additions & 3 deletions Models/DatabaseResource.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 2 additions & 2 deletions Models/DatabaseScriptResource.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
62 changes: 25 additions & 37 deletions Models/ServerContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
//using System.Collections.Generic;
using System.Data.SqlClient;
using Serilog;
using Serilog.Events;
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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);
Expand Down
27 changes: 24 additions & 3 deletions Models/ServerResource.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
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;

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; } }
Expand Down
6 changes: 3 additions & 3 deletions Models/TableResource.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
6 changes: 3 additions & 3 deletions Models/TableScriptResource.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions Models/TablesInSchemaResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
6 changes: 3 additions & 3 deletions Models/Top100RowsResource.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 24a8050

Please sign in to comment.