SQL SMO PowerShell

Some simple PS that has potential.


Just some shortcuts on top of SMO to add powershell flavor.
Basically you pick from a collection, and choose an item, rinse and repeat.
Easier done than said.
Note: Be careful with this, you are on top of SMO and that can kill things.
Note: Piping any SMO object at all to Export-Clixml looks interesting.
TODO: Needs more documentation.
TODO: Add script options.
TODO: Add depends function with the dependency walker stuff.
TODO: Consolidate pick and choose function, make it smarter.
e.g.
(express).Databases | pick Northwind | choose Tables | list
(express).Databases | pick Northwind | choose Tables | pick Suppliers | script
(express).Databases | pick Northwind | choose Tables | pick Suppliers | Select-Object -Property Columns
(express).Databases | pick Northwind | choose Views | owner dbo | list


[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | Out-Null;
function express() {
server ".\sqlexpress"
}
function server($name) {
New-Object Microsoft.SqlServer.Management.Smo.Server $name
}
function list() {
$input | Select-Object -Property Name
}
function pick($name) {
$input | Where-Object {$_.Name -eq $name}
}
function choose($name) {
$input | Select-Object -ExpandProperty $name
}
function owner($owner) {
$input | Where-Object {$_.Owner -eq $owner}
}
function script() {
$input | ForEach-Object {$_.Script()}
}

view raw

sql.smo.ps1

hosted with ❤ by GitHub


Sample dependency injection for suburbanites

Programmers write a whole lot of code to use to “spin up” other objects in order to use that objects services and all of the required dependencies to use the object services are otherwise of absolutely no concern to the consumer of the service. It is pernicious. See Separation of concerns for more info.

The factory may seem like a lot of work and a lot of code but most times you don’t code it and it is generated, either at compile time, perhaps using attributes for some aspect oriented programming, or at runtime using reflection. In the end whatever work you put into configuring it is supposed to be recouped by all the supposedly many consumers who no longer have to open their garage door and rummage about for two stroke oil.


// All I want is my lawn mowed.
// Yet I have to gather up the tools which are of no direct use to me in this case.
var myLawn = {}, myWalk = {};
var myTools = MyGarage.Tools(["mower", "gas", "oil", "broom"]);
var gardener = new Gardener(myTools);
gardener.mow(myLawn);
//Given the factory code below now all I have to do is this.
var worker = workerFactory().getWorker("gardener")
worker.mow(myLawn);
worker.sweep(myWalk);
var workerFactory = function() {
map = {
gardener : {
tools : ["mower", "gas", "oil", "broom"],
skills : {mow: function(lawn) {return "yes boss";}, sweep: function(walk) {return "yes boss";}}
}
}
function getWorker(type) {
var worker = {type: type};
// Give the worker tools
var tools = map[type].tools;
worker.tools = [];
for (var item in tools) {
worker.tools[item] = tools[item];
}
// Give the worker skills
var skills = map[type].skills;
for (var item in skills) {
worker[item] = skills[item];
}
return worker;
}
return {
getWorker: getWorker
}
}


Breeze and knockout quick start.

An attempt at a bare minimum to get breeze and knockout going.


Create a new MVC4 web application web api project. This will bring in jQuery and knockout.
Nuget install-package Breeze.WebApi.
Add a dbcontext class in Models and a breeze api controller in Controllers.
Add an html page with a view model and a view.


using System.Linq;
using System.Web.Http;
using Breeze.WebApi;
using MvcApplication1.Models;
using Newtonsoft.Json.Linq;
namespace MvcApplication1.Controllers
{
[BreezeController]
public class FooController : ApiController
{
private readonly EFContextProvider<FooData> _ctx = new EFContextProvider<FooData>();
[HttpGet]
public string Metadata()
{
return _ctx.Metadata();
}
[HttpGet]
public IQueryable<Foo> Foos()
{
return _ctx.Context.Foo;
}
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
return _ctx.SaveChanges(saveBundle);
}
}
}


using System;
using System.Data.Entity;
namespace MvcApplication1.Models
{
public class Foo
{
public int Id { get; set; }
public String Name { get; set; }
}
public class FooData : DbContext
{
public DbSet<Foo> Foo { get; set; }
}
}

view raw

FooData.cs

hosted with ❤ by GitHub


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title></title>
<script src="Scripts/jquery-1.8.2.min.js"></script>
<script src="Scripts/knockout-2.2.0.js"></script>
<script src="Scripts/q.min.js"></script>
<script src="Scripts/breeze.min.js"></script>
<script type="text/javascript">
var mgr = new breeze.EntityManager("breeze/Foo");
var Vm = function () {
var items = ko.observableArray();
var item = ko.observable();
mgr.executeQuery(breeze.EntityQuery.from("Foos"))
.then(function (val) {
val.results.forEach(function (result) {
items.push(result);
});
});
function add() {
items.push(mgr.createEntity("Foo", { Name: item() }));
mgr.saveChanges();
item("");
}
function remove(data) {
data.entityAspect.setDeleted();
mgr.saveChanges();
items.remove(data);
}
return {
items: items,
item: item,
add: add,
remove: remove
};
};
$(function () {
ko.applyBindings(new Vm());
});
</script>
</head>
<body>
<form data-bind="submit: add">
<input type="text" data-bind="value: item" />
<input type="submit" value="add" />
</form>
<div data-bind="foreach: items">
<span data-bind="text: Name"></span>
<input type="button" value="delete" data-bind="click: $parent.remove" />
<br />
</div>
</body>
</html>

view raw

HtmlPage1.html

hosted with ❤ by GitHub