All posts by Rikard Ottosson

About Rikard Ottosson

.NET developer. Father. Couch potato. Old, cynical, willing to learn, but quick to judge. Likes old hard rock. Hope to buy more guitars.

Finding dependent columns in SQL Azure

When fiddling with database relations in an Azure database you quickly notice you are on your own in terms of tooling in a way that you’re not used to from Microsoft. Metadata however is available so you can work around these things relatively easily.

So: when you are dropping and recreating tables as you design them – here’s a quick snippet to see which columns are depending on the particular table you are editing now:

create procedure show_referencing_relations 
@schemaname sysname, 
@tablename sysname 
as 
select 
 [DropSQL] = 'ALTER TABLE [' + @schemaname + '].[' 
 + ptbl.name + '] DROP CONSTRAINT [' + fks.name + '];',
  [AddSQL] = 'ALTER TABLE [' + @schemaname + '].[' 
 + ptbl.name + '] ADD CONSTRAINT [' + fks.name + 
  '] FOREIGN KEY (' + pcol.name + ') REFERENCES ['
 + @schemaname + '].[' + @tablename + '] (' + col.name + ')',        [ConstraintName] = fks.name,
[Source Column Name] = col.name,
[Parent Table Name] = ptbl.name,
[Parent Column Name] = pcol.name 
from sys.foreign_keys fks 
 inner join sys.all_objects ao on fks.referenced_object_id = ao.object_id 
 inner join sys.foreign_key_columns fkc on 
fks.object_id = fkc.constraint_object_id
 inner join sys.all_columns col on 
 col.object_id = ao.object_id and 
 fkc.referenced_column_id = col.column_id
 inner join sys.all_objects ptbl on fkc.parent_object_id = ptbl.object_id
 inner join sys.all_columns pcol on 
 fkc.parent_column_id = pcol.column_id and 
 pcol.object_id = fkc.parent_object_id
 --inner join sys.all_objects pcol on fkc.constraint_column_id = pcol.object_id
 where ao.name = @tablename

Run it with schema and table names, and you get some SQL to use before and after your planned change. First one to remove the constraints and the second one to restore them. The results of the stored procedure show the name of the constraint, the column in your table that is being referenced plus the parent table and column involved in the relationship.

Then you can quickly write code to drop and recreate these relations.

F# for fun and profit

(I am in no way affiliated with the brilliant blog of the name http://fsharpforfunandprofit.com/. I wish I was, but no, I just remembered the phrase and used it as a headline, sorry.)
It is no secret that Øredev 2013 will be rich in terms of F# content. Recently there has been a surge in interest for this Functional First .NET-language with lucrative opportunities in many fields, finance especially. The conciseness and correctness of F# code makes it attractive for work that features clever algorithms and as a .NET language it of course benefits from the general richness of the platform.
You should, if you haven’t already, check out the following sites and the links from there.
http://tryfsharp.org
http://fsharpforfunandprofit.com/

Øredev 2013 – it is drawing near

Since I last wrote about our most excellent conference Øredev, there have been a few announcements. Go check out the program for all details as it is now time for you to register if you have not already done so.

After the Build conference, the key takeaways, in my most humble opinion, were vastly improved performance and features in multi-monitor, multi-DPI  support  in XAML in Windows 8.1 as well as the enormous paradigm shift in ASP.NET 4.5.1 which unifies all view engines and paradigms into one ASP.NET, which will help users cherry pick the things they need to create apps that leverage all things that comprise ASP.NET. WebForms, MVC, SignalR and WebApi.

To demonstrate those things from Build, Mads Kristensen, Sr. Program Manager on the Web Platforms & Tools team, will grace us with his presence and do a talk entitled What’s new in Visual Studio 2013 for Web Developers, while one of our old favourites, Tess Ferrandez will return to do a session called What is new in XAML for Windows 8.1.

We will also, as you may have read on the Internets, feature keynotes from Anna Beatrice Scott, Randall Munroe and Jens Bergensten have been added to the lineup.

In other words, we are pretty excited about our conference, and believe you would be too, so get started with your reservations.

Tip: Clearing QueryString when using RadControls and ASP.NET WebForms

I had to google myself silly and the page I ended up finding was so hard to find I couldn’t actually find it again to link to it properly. The below code is not my idea, so if you feel wronged, submit your link and I shall attribute properly.

The problem is: I want to be able to navigate, or find my way back if you will, to a certain MultiPage and TabStrip that is nested inside a Telerik RadGrid. As it is a view and not a state change, I would like the querystring to handle this. However, to be nice, or obnoxious, depending on how you see it, Telerik will resubmit the same querystring when you navigate the tabs yourself, which will make it so that after changing tabs via querystring once, you cannot navigate to a different page by clicking anymore.

Anyway: People on the Internet, especially Telerik forums tell you to do Request.QueryString.Clear(), but that just doesn’t work because it is a readonly Collection and although it compiles, you will get a runtime error. So: What to do?

The post that I found simply used violence and Reflection to force the collection into being Read/Write, and then when Telerik reads from Request.Querystring to needlessly resubmit the exact previous query, it actually, accidentally, posts the correct, cleaned querystring. This enables my expected scenario where you by using GET can change tabs in a TabStrip/MultiPage.

        private void ClearQueryStringParam(string paramName)
        { Continue reading Tip: Clearing QueryString when using RadControls and ASP.NET WebForms 

Early Bird campaign ends in two weeks, Aug 15

Early Bird campaign ends in two weeks, Aug 15

We are proud of the program we have put together for Øredev 2013 and we are quite confident you will find the content interesting enough to at least consider coming to Malmö in November.

The thing is, our very lucrative early bird campaign is ending in two weeks, so if you are on the fence, this is the time to pull the trigger and make that reservation. It is easier to beg accounting for forgiveness than to ask for permission. It can’t hurt to at least ask.

Castle Windsor + NHibernate + MVC revisited

I have noticed some people come to this blog for reference on using Castle Windsor together with NHibernate and ASP.NET MVC, so I have to mention a few things that have happened since I last posted about it several years ago. First of all ,there is an excellent sample application that shows the simplicity with which you can hook these things up.

The sample shows you how to register Castle in an MVC application. how to add logging and how to hook up NHibernate 3.2 . This is far, far easier than what I described years ago thanks to improvements in all involved frameworks, really.

The important things to remember are:

  1. Use NuGet to install your libs
  2. Use MVC:s way of installing an IoC container (as demonstrated)
  3. Use logging, you might as well.
  4. Hook up the NHibernate Session Factory as a singleton and hook up ISession to request a new transient session from your singleton SessionFactory. There you go, the magic is taken care of.
  5. This is not mentioned in the demo, but if you are going building production code consider buying a subscription for NHProf if you don’t have one already. It wlll tell you where you mess up and need to do better.
  6. As above, look into hooking up Glimpse, see Scott Hanselman’s blog about it below.

Castle Windsor + NHibernate + log4net + ASP.NET MVC Sample

Windsor tutorial ASP.NET MVC 3-application To-be-Seen

Scott Hanselman on Glimpse:
If you’re not using Glimpse…

F# puzzle

One of my colleagues at Jayway launched a functional puzzle and solicited responses via Github gists. The challenge was as follows:

  1. Write a function that, given a sequence of either positive or negative integers, returns true if any subset of the list sums up to zero. Eg:
    find-solutions ([-7 4 3 8 -5]) => true
    find-solutions ([-6 2 3]) => false
  2. Make it work with any number, not just zero. Eg:
    find-solutions (0, [-7 4 3 8 -5]) => true
    find-solutions (13, [-7 4 3 8 -5]) => false
  3. Make it return all matching sequences, instead of a boolean. Eg:
    find-solutions (0, [-7 4 3 8 -5]) => [ [-7 4 3] [-7 4 8 -5] ]
    find-solutions (13, [-7 4 3 8 -5]) => []
  4. Make it take any predicate for the sum, not just equal to a number. Eg:
    find-solutions (isZero, [-7 4 3 8 -5]) => [ [-7 4 3] [-7 4 8 -5] ]
    find-solutions (isOdd, [-7 4 3]) => [ [-7] [3] [-7 4] [4 3] ]

Of course, me being an F# n00b I ended up at StackOverflow within hours of trying my hand at this, and I also asked among code monkeys on Facebook,  so I will not submit an answer is it wouldn’t be my own. My colleagues went ahead and figured out solutions on their own rather than use Google engineering like I did.

Anyway, as you can imagine, the first bit is the hard one. With higher order functions, delegating the actual filtering is done easily thereafter by supplying  the comparer function as a parameter.

So what do you do? I looked at many solutions, but couldn’t manage to use the comme il faut solution with a permutation tree, but went with a list of permutations instead which I then could trivially recurse through to see if I ever find a match.

If you are using Google engineering to solve F# problems, be aware that the old function Seq.map_concat has been renamed Seq.collect, which is a clear improvement.

In my case I found my permutation function on StackOverflow, provided by the excellent Tomáš Petříček, see blogroll, and just added my c0dez to find matches. This is from a console app, so hence the EntryPoint business.

let rec permutations list taken = 
  seq { if Set.count taken = List.length list then yield [] else
        for l in list do
          if not (Set.contains l taken) then 
            for perm in permutations list (Set.add l taken)  do
              yield l::perm }

let rec isMakingZero acc lst =
    match lst with
     | [] -> false
     | a :: tail -> (acc + a = 0) || isMakingZero (acc + a) tail 
    

let rec testpaths pathlist =
    if Seq.length pathlist = 0 then
        false
    else
        isMakingZero 0 (Seq.head pathlist) || testpaths (Seq.skip 1 pathlist)

[<EntryPoint>]
let main argv = 
    printfn "First %A"  (testpaths (permutations [1; -2; 3; -5] Set.empty))
    printfn "Second %A" (testpaths (permutations [1; -3; 2; -5] Set.empty))
    0 // return an integer exit code

    

As you can imagine, you just supply a function to testpaths that then gets passed down to isMakingZero, which should be renamed, in order to solve the remaining tasks.

F# Actor: An Actor Library

F# Actor: An Actor Library.

This is a follow-up on my post on the Actor Model, as I was very generous in my definition of F# Agents as a full-fledged Actor framework. F#:er s have been wanting something like the Akka framework for Scala. Colin Bull from whom I also reblogged the F# and Raven post below, posted about a framework on his blog that will provide this, and it also explains what the difference is and what the requirements are for an Actor framework.