Viktar Karpach Web Developer Blog about ASP.NET,C#,T-SQL,Javascript,CSS,SilverlightViktar Karpach Web Developer Blog about ASP.NET,C#,T-SQL,Javascript,CSS,Silverlight. New technics and tehnlogies reviews.en-usKarpach Web Developer Bloghttp://www.karpach.com/save-for-android-photoshop-javascript.htmhttp://www.karpach.com/save-for-android-photoshop-javascript.htmJavascriptSave for android photoshop javascript<p>Due to different pixel density of different android devices developer / designer need to create different versions of graphic artifacts.</p> <p>Recently I've been trying to create my first Android application and got tired of creating image artifacts for different pixel density. As a result I came up with a following photoshop javascript that automates with tedious task.</p> <div class="javascript"><pre> <span class="keyword">var</span> docRef = activeDocument; <span class="keyword">function</span> saveAs(path,filename,width,height,dpi) { savedState = docRef.activeHistoryState docRef.resizeImage (width, height, dpi); <span class="keyword">var</span> folder = <span class="keyword">new</span> Folder (path); <span class="keyword">if</span> (!folder.exists) { folder.create(); } pngFile = <span class="keyword">new</span> File( path + filename) pngSaveOptions = <span class="keyword">new</span> PNGSaveOptions() app.activeDocument.saveAs(pngFile, pngSaveOptions, <span class="keyword">true</span>, Extension.LOWERCASE) docRef.activeHistoryState = savedState } <span class="keyword">function</span> main(isIcon) { <span class="keyword">if</span> (documents.length == 0) { alert(<span class="string">&quot;There are no documents open.&quot;</span>); <span class="keyword">return</span>; } <span class="keyword">try</span> { Path = app.activeDocument.path; } <span class="keyword">catch</span> (exception) { docRef.saveAs(File.saveDialog (<span class="string">&quot;Save image&quot;</span>,[<span class="string">&quot;*.psd&quot;</span>,<span class="string">&quot;*.png&quot;</span>,<span class="string">&quot;*.jpg&quot;</span>])); } Path = app.activeDocument.path; <span class="keyword">var</span> Name = app.activeDocument.name.replace(/\.[^\.]+$/, <span class="string">''</span>); startRulerUnits = app.preferences.rulerUnits; app.preferences.rulerUnits = Units.PIXELS; globSavedState = docRef.activeHistoryState; <span class="keyword">try</span> { docRef.mergeVisibleLayers(); } <span class="keyword">catch</span> (exception) { alert(<span class="string">&quot;Please select visible layer.&quot;</span>); <span class="keyword">return</span>; } <span class="keyword">if</span> (isIcon) { saveAs(Path + <span class="string">&quot;/drawable-xxhdpi/&quot;</span> , Name + <span class="string">&quot;.png&quot;</span>,144,144,480); saveAs(Path + <span class="string">&quot;/drawable-xhdpi/&quot;</span> , Name + <span class="string">&quot;.png&quot;</span>,96,96,320); saveAs(Path + <span class="string">&quot;/drawable-hdpi/&quot;</span> ,Name + <span class="string">&quot;.png&quot;</span>,72,72,240); saveAs(Path + <span class="string">&quot;/drawable-mdpi/&quot;</span> ,Name + <span class="string">&quot;.png&quot;</span>,48,48,160); saveAs(Path + <span class="string">&quot;/drawable-ldpi/&quot;</span> ,Name + <span class="string">&quot;.png&quot;</span>,36,36,120); } <span class="keyword">else</span> { <span class="keyword">var</span> dpi = 480; saveAs(Path + <span class="string">&quot;/drawable-xxhdpi/&quot;</span> , Name + <span class="string">&quot;.png&quot;</span>, docRef.width * dpi / docRef.resolution, docRef.height * dpi / docRef.resolution, dpi); dpi = 320; saveAs(Path + <span class="string">&quot;/drawable-xhdpi/&quot;</span> , Name + <span class="string">&quot;.png&quot;</span>, docRef.width * dpi / docRef.resolution,docRef.height * dpi / docRef.resolution, dpi); dpi = 240; saveAs(Path + <span class="string">&quot;/drawable-hdpi/&quot;</span> , Name + <span class="string">&quot;.png&quot;</span>, docRef.width * dpi / docRef.resolution ,docRef.height * dpi / docRef.resolution, dpi); dpi = 160; saveAs(Path + <span class="string">&quot;/drawable-mdpi/&quot;</span> , Name + <span class="string">&quot;.png&quot;</span>, docRef.width * dpi / docRef.resolution,docRef.height * dpi / docRef.resolution, dpi); dpi = 120; saveAs(Path + <span class="string">&quot;/drawable-ldpi/&quot;</span> , Name + <span class="string">&quot;.png&quot;</span>, docRef.width * dpi / docRef.resolution, docRef.height * dpi / docRef.resolution, dpi); } docRef.activeHistoryState = globSavedState; app.preferences.rulerUnits = startRulerUnits; docRef.save(); } </pre></div> <br/> <p>main(true) generates following:<br/><br/> 480dpi 144x144 - drawable-xxhdpi<br/> 320dpi 96x96 - drawable-xhdpi<br/> 240dpi 72x72 - drawable-hdpi<br/> 160dpi 48x48 - drawable-mdpi<br/> 120dpi 36x36 - drawable-ldpi<br/> </p> <p>main(false) generates images for android in respective folders, where dimensions are proportional to selected resolution / dpi.</p> <p> For example:<br/><br/> Resolution: 480dpi<br/> Width:150<br/> Height:80<br/> </p> <p> Becomes:<br/><br/> 480dpi 150x80 - drawable-xxhdpi<br/> 320dpi 100x53 - drawable-xhdpi<br/> 240dpi 75x40 - drawable-hdpi<br/> 160dpi 50x27 - drawable-mdpi<br/> 120dpi 37x20- drawable-ldpi<br/> </p> <p>See <a href="https://github.com/karpach/Save4Android">Save4Android github repository</a> for more details.</p>Sun, 17 Mar 2013 00:00:00 -07002013-03-17T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/t-sql-print-with-immediate-output.htmhttp://www.karpach.com/t-sql-print-with-immediate-output.htmT-SQLHow to log a message from t-sql script?<p>Recently I've been working on a long running script that will be executed directly in PROD enviroment. I needed an easy way to see a progress of t-sql script execution. </p> <p>I tried to use PRINT, but it doesn’t output anything until script execution is done.</p> <p>You can use some log table and insert progress status messages there, but there is an easier way:</p> <div class="sql"><pre> <span class="keyword">RAISERROR</span> (<span class="string">'Message'</span>, 10, 1) <span class="keyword">WITH</span> NOWAIT </pre></div> <br/> <p>RAISERROR with severity of 10 or less doesn't trigger try catch block and can be used for log purposes. Here is a little longer example:</p> <br/> <div class="sql"><pre> <span class="keyword">DECLARE</span> @i <span class="keyword">INT</span> = 0 <span class="keyword">WHILE</span> 1=1 <span class="keyword">BEGIN</span> <span class="keyword">IF</span> @i &gt;= 10 <span class="keyword">BREAK</span>; <span class="keyword">WAITFOR</span> DELAY <span class="string">'00:00:01'</span>; <span class="keyword">SET</span> @i = @i + 1 <span class="keyword">RAISERROR</span> (<span class="string">'%d sec running'</span>, 10, 1, @i) <span class="keyword">WITH</span> NOWAIT <span class="keyword">END</span> </pre></div> <br/> <p>Results:</p> <img src="http://www.karpach.com/images/uploaded/raiserror.gif" alt="Raiseerror execution example"/>Sat, 26 Jan 2013 00:00:00 -08002013-01-26T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/inserted-and-source-id-using-merge.htmhttp://www.karpach.com/inserted-and-source-id-using-merge.htmT-SQLHow to get both inserted source table id and target table id?<p> Lets say we have two tables source and target. We want to copy records from the source to the target table and save mappings between old and new ids in a third table. </p> <div class="sql"><pre> <span class="keyword">DECLARE</span> @source <span class="keyword">TABLE</span> ( id <span class="keyword">INT</span> <span class="keyword">IDENTITY</span>(5,1) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">PRIMARY</span> <span class="keyword">KEY</span>, <span class="keyword">name</span> <span class="keyword">VARCHAR</span>(<span class="sqlSystemFunction">max</span>) ) <span class="keyword">DECLARE</span> @target <span class="keyword">TABLE</span> ( id <span class="keyword">INT</span> <span class="keyword">IDENTITY</span>(1,1) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">PRIMARY</span> <span class="keyword">KEY</span>, <span class="keyword">name</span> <span class="keyword">VARCHAR</span>(<span class="sqlSystemFunction">max</span>) ) <span class="keyword">DECLARE</span> @inserted <span class="keyword">TABLE</span> ( id <span class="keyword">INT</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>, clone_id <span class="keyword">INT</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span> ) </pre></div> <br/> <p> Lets populate the source table: </p> <div class="sql"><pre> <span class="keyword">INSERT</span> <span class="keyword">INTO</span> @subscription (name) <span class="keyword">VALUES</span> (<span class="string">'Test 1'</span>), (<span class="string">'Test 2'</span>), (<span class="string">'Test 3'</span>) </pre></div> <br/> <p> Now we can populate the target table with the following code: </p> <div class="sql"><pre> <span class="keyword">INSERT</span> <span class="keyword">INTO</span> @target (<span class="keyword">name</span> ) <span class="keyword">SELECT</span> <span class="keyword">name</span> + <span class="string">' - Cloned'</span> <span class="keyword">FROM</span> @source </pre></div> <br/> <p> You can try to populate inserted table using the following code: </p> <div class="sql"><pre> <span class="keyword">INSERT</span> <span class="keyword">INTO</span> @target (<span class="keyword">name</span> ) <span class="keyword">OUTPUT</span> s.id, INSERTED.id <span class="keyword">INTO</span> @inserted <span class="keyword">SELECT</span> <span class="keyword">name</span> + <span class="string">' - Cloned'</span> <span class="keyword">FROM</span> @source s </pre></div> <br/> <p>But you will get a syntax error:</p> <p style="color:red">The multi-part identifier "s.id" could not be bound.</p> <p>This is due to a nature of the INSERT statement. The SELECT statement considered to be apart from the INSERT statement, so you can't reference its tables in the OUTPUT clause. However you can achieve desired outcome using <b>MERGE</b> sql statement, which is a part of SQL standard from 2003, but rarely used by anybody.</p> <div class="sql"><pre> MERGE @target t USING @source s <span class="keyword">ON</span> 0=1 <span class="keyword">WHEN</span> <span class="keyword">NOT</span> MATCHED <span class="keyword">THEN</span> <span class="keyword">INSERT</span> (<span class="keyword">name</span>) <span class="keyword">VALUES</span> (s.name + <span class="string">' - Cloned'</span>) <span class="keyword">OUTPUT</span> s.id, INSERTED.id <span class="keyword">INTO</span> @inserted; <span class="keyword">SELECT</span> * <span class="keyword">FROM</span> @source <span class="keyword">SELECT</span> * <span class="keyword">FROM</span> @target <span class="keyword">SELECT</span> * <span class="keyword">FROM</span> @inserted </pre></div> <br/> <p>Results:</p> <table class="tableDefinition"> <tr><th>id</th><th>name</th></tr> <tr><td>5</td><td>Test 1</td></tr> <tr><td>6</td><td>Test 2</td></tr> <tr><td>7</td><td>Test 3</td></tr> </table> <br/> <table class="tableDefinition"> <tr><th>id</th><th>name</th></tr> <tr><td>1</td><td>Test 1 - Cloned</td></tr> <tr><td>2</td><td>Test 2 - Cloned</td></tr> <tr><td>3</td><td>Test 3 - Cloned</td></tr> </table> <br/> <table class="tableDefinition"> <tr><th>id</th><th>clone_id</th></tr> <tr><td>5</td><td>1</td></tr> <tr><td>6</td><td>2</td></tr> <tr><td>7</td><td>3</td></tr> </table> Sat, 05 Jan 2013 00:00:00 -08002013-01-05T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/firebug-vs-google-chrome-developer-tools.htmhttp://www.karpach.com/firebug-vs-google-chrome-developer-tools.htmASP.NETFirebug vs Google Chrome Developer Tools<p>Recently I&rsquo;ve been trying to use more and more Chrome Developer Tools instead of Firebug. Here are couple of things that I thought are show stoppers, but appeared to be a lack of knowledge on my side.</p> <p>There are no AJAX requests in console window. There is a setting for this:</p> <p><img src="http://www.karpach.com/images/uploaded/console-ajax-google-chrome-setting.jpg" alt="Console AJAX Google Chrome Setting" /> <img src="http://www.karpach.com/images/uploaded/console-ajax-google-chrome.jpg" alt="Console AJAX Google Chrome" /></p> <p>Secondly I can&rsquo;t search all javascript files:</p> <p><img src="http://www.karpach.com/images/uploaded/google-chrome-scripts-search.jpg" alt="Google Chrome scripts search" /></p> <p>Actually you can search all javascript files and more. Just press Ctrl+Shift+F:</p> <p><img src="http://www.karpach.com/images/uploaded/google-chrome-scripts-search2.jpg" alt="Google Chrome scripts search" /></p> <p>Do you miss any FireBug functionality in Chrome Developer Tools?</p>Mon, 06 Aug 2012 00:00:00 -07002012-08-06T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/linq-deferred-loading-yield-return.htmhttp://www.karpach.com/linq-deferred-loading-yield-return.htmC# .NETLINQ deferred loading using C# yield-return<p>Yield keyword exists in C# language since version 2 along with Visual Studio 2005, however it is rarely used by developers. Recently I read <a href="http://visualstudiomagazine.com/articles/2012/02/01/demystifying-the-c-yield-return-mechanism.aspx" target="_blank">Demystifying the C# Yield-Return Mechanism</a> article about yield-return keyword usage. This article gave three most common usage scenarios, but author (James McCaffrey) didn't mention LINQ. I think LINQ is where yield-return becomes most useful since it provides mechanism for deferred loading. Here is a quick example.</p> <p>Lets say we have bunch of unit tests to run. It takes some time to run a single test, lets say 1 sec. For simplicity lets assume that even tests pass and odd fail:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } .cb4 { color: #3cb371; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">Test</span></pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">int</span> Id { <span class="cb1">get</span>; <span class="cb1">set</span>; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">bool</span> Assert()</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.Threading.<span class="cb2">Thread</span>.Sleep(1000);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.WriteLine(<span class="cb1">string</span>.Format(<span class="cb3">&quot;Test </span><span class="cb4">{0}</span><span class="cb3"> was processed.&quot;</span>, Id));&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> Id % 2 == 0;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> }</pre> </div> <p>&nbsp;</p> <p>Lets write couple LINQ like extension methods for Test collections. Process1 doesn't use yield return. Process2 uses yield return and PrintPass output &quot;Test # is passed&quot; message.</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } .cb4 { color: #3cb371; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">static</span> <span class="cb1">class</span> <span class="cb2">Extensions</span></pre> <pre class="cl"> {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb2">IEnumerable</span>&lt;<span class="cb2">Test</span>&gt; Process1(<span class="cb1">this</span> <span class="cb2">IEnumerable</span>&lt;<span class="cb2">Test</span>&gt; tests)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> result = <span class="cb1">new</span> <span class="cb2">List</span>&lt;<span class="cb2">Test</span>&gt;();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> test <span class="cb1">in</span> tests)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (test.Assert())</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; result.Add(test);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> result;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb2">IEnumerable</span>&lt;<span class="cb2">Test</span>&gt; Process2(<span class="cb1">this</span> <span class="cb2">IEnumerable</span>&lt;<span class="cb2">Test</span>&gt; tests)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> test <span class="cb1">in</span> tests)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (test.Assert())</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">yield</span> <span class="cb1">return</span> test;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> <span class="cb1">void</span> PrintPass(<span class="cb1">this</span> <span class="cb2">IEnumerable</span>&lt;<span class="cb2">Test</span>&gt; tests)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">foreach</span> (<span class="cb1">var</span> t <span class="cb1">in</span> tests)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.WriteLine(<span class="cb1">string</span>.Format(<span class="cb3">&quot;Test </span><span class="cb4">{0}</span><span class="cb3"> is passed.&quot;</span>, t.Id));&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; } </pre> <pre class="cl"> }</pre> </div> <p>&nbsp;</p> <p>Lets fill up test collection and compare output for Process1 and Process2 method:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">class</span> <span class="cb2">Program</span></pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">static</span> <span class="cb1">void</span> Main()</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> tests = <span class="cb1">new</span> <span class="cb2">List</span>&lt;<span class="cb2">Test</span>&gt;(<span class="cb1">new</span> []</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">Test</span> {Id = 1},&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">Test</span> {Id = 2},</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">Test</span> {Id = 3},</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">Test</span> {Id = 4},</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">new</span> <span class="cb2">Test</span> {Id = 5},</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; });</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.WriteLine(<span class="cb3">&quot;**** No yield return *******&quot;</span>);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; tests.Process1().PrintPass();&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.WriteLine(<span class="cb3">&quot;**** Using yield return *******&quot;</span>);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; tests.Process2().PrintPass();&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb2">Console</span>.ReadKey();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> }</pre> </div> <p>&nbsp;</p> <p>Output:</p> <p>**** No yield return *******<br /> Test 1 was processed.<br /> Test 2 was processed.<br /> Test 3 was processed.<br /> Test 4 was processed.<br /> Test 5 was processed.<br /> Test 2 is passed.<br /> Test 4 is passed.<br /> <br /> **** Using yield return *******<br /> Test 1 was processed.<br /> Test 2 was processed.<br /> Test 2 is passed.<br /> Test 3 was processed.<br /> Test 4 was processed.<br /> Test 4 is passed.<br /> Test 5 was processed.</p> <p>As you can see, in first case whole collection was built before PrintPass method output anything. In second case you can see PrintPass prints &quot;Test 2 is passed.&quot; right after Assert method for this test have done its processing.</p>Sat, 31 Mar 2012 00:00:00 -07002012-03-31T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/pivot-tsql.htmhttp://www.karpach.com/pivot-tsql.htmT-SQLHow to use PIVOT in T-SQL queries?<p>Web has a lot of PIVOT examples, however all of them are based on fact tables. Regular relational database rarely has facts tables. Lets see how you can use PIVOT statement with regular database. First of all lets create sample Grocery database:</p> <p><img alt="Grocery Database Diagram" src="http://www.karpach.com/images/uploaded/Pivot-Store-Diagram.png" /></p> <p>Here is a script that can create this database:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">CREATE DATABASE </span>Grocery</pre> <pre class="cl"> GO</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">USE </span>Grocery</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">CREATE TABLE </span>Products</pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp; ProductId <span class="cb1">INT IDENTITY</span>(1,1) <span class="cb1">PRIMARY KEY</span>,</pre> <pre class="cl"> &nbsp; ProductName <span class="cb1">VARCHAR</span>(50),</pre> <pre class="cl"> &nbsp; Price <span class="cb1">MONEY</span></pre> <pre class="cl"> )</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">CREATE TABLE </span>Stores</pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; StoreId <span class="cb1">INT IDENTITY</span>(1,1) <span class="cb1">PRIMARY KEY</span>,</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; StoreName <span class="cb1">VARCHAR</span>(50)</pre> <pre class="cl"> )</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">CREATE TABLE </span>Orders</pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; OrderId <span class="cb1">INT IDENTITY</span>(1,1) <span class="cb1">PRIMARY KEY</span>,</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; StoreId <span class="cb1">INT</span>,</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; OrderDate <span class="cb1">DATE</span>,</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">CONSTRAINT </span>FK_Orders_Store <span class="cb1">FOREIGN KEY </span>(StoreId) <span class="cb1">REFERENCES </span>Stores(StoreId)</pre> <pre class="cl"> )</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">CREATE TABLE </span>OrderProducts</pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; OrderId <span class="cb1">INT</span>,</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; ProductId <span class="cb1">INT</span>,</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Qty <span class="cb1">INT</span>,</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">CONSTRAINT </span>PK_OrderId_ProductId <span class="cb1">PRIMARY KEY </span>(OrderId,ProductId),</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">CONSTRAINT </span>FK_OrderProducts_Products <span class="cb1">FOREIGN KEY </span>(ProductId) <span class="cb1">REFERENCES </span>Products(ProductId),</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">CONSTRAINT </span>FK_OrderProducts_Orders <span class="cb1">FOREIGN KEY </span>(OrderId) <span class="cb1">REFERENCES </span>Orders(OrderId)</pre> <pre class="cl"> )</pre> </div> <p>&nbsp;</p> <p>Lets populate those tables:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">INSERT INTO </span>Products</pre> <pre class="cl"> (ProductName, Price)</pre> <pre class="cl"><span class="cb1">VALUES </span> </pre> <pre class="cl"> (<span class="cb2">'Milk'</span>, 2.99),</pre> <pre class="cl"> (<span class="cb2">'Bread'</span>,1.99),</pre> <pre class="cl"> (<span class="cb2">'Tomato'</span>,0.99),</pre> <pre class="cl"> (<span class="cb2">'Grape'</span>,1.99)</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">INSERT INTO </span>Stores</pre> <pre class="cl"> (StoreName )</pre> <pre class="cl"><span class="cb1">VALUES </span> </pre> <pre class="cl"> (<span class="cb2">'Jewel'</span>),</pre> <pre class="cl"> (<span class="cb2">'Dominicks'</span>),</pre> <pre class="cl"> (<span class="cb2">'Walmart'</span>)</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">INSERT INTO </span>Orders</pre> <pre class="cl"> (StoreId, OrderDate )</pre> <pre class="cl"><span class="cb1">VALUES </span> </pre> <pre class="cl"> (1,<span class="cb2">'1/11/2011'</span>),</pre> <pre class="cl"> (1,<span class="cb2">'1/14/2011'</span>),</pre> <pre class="cl"> (2,<span class="cb2">'2/05/2011'</span>),</pre> <pre class="cl"> (3,<span class="cb2">'3/17/2011'</span>),</pre> <pre class="cl"> (3,<span class="cb2">'3/29/2011'</span>),</pre> <pre class="cl"> (3,<span class="cb2">'4/02/2011'</span>)</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"><span class="cb1">INSERT INTO </span>OrderProducts</pre> <pre class="cl"> ( OrderId, ProductId, Qty )</pre> <pre class="cl"><span class="cb1">VALUES </span></pre> <pre class="cl"> (1,2,2),</pre> <pre class="cl"> (1,1,1),</pre> <pre class="cl"> (2,3,1),</pre> <pre class="cl"> (3,4,3),</pre> <pre class="cl"> (3,1,2),</pre> <pre class="cl"> (3,3,1),</pre> <pre class="cl"> (4,3,2),</pre> <pre class="cl"> (4,4,2),</pre> <pre class="cl"> (5,1,1),</pre> <pre class="cl"> (5,3,1),</pre> <pre class="cl"> (6,1,1),</pre> <pre class="cl"> (6,2,3),</pre> <pre class="cl"> (6,4,1)</pre> </div> <p>&nbsp;</p> <p>Products:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>ProductId</th> <th>ProductName</th> <th>Price</th> </tr> <tr> <td>1</td> <td>Milk</td> <td>2.99</td> </tr> <tr> <td>2</td> <td>Bread</td> <td>1.99</td> </tr> <tr> <td>3</td> <td>Tomato</td> <td>0.99</td> </tr> <tr> <td>4</td> <td>Grape</td> <td>1.99</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>Stores:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>StoreId</th> <th>StoreName</th> </tr> <tr> <td>1</td> <td>Jewel</td> </tr> <tr> <td>2</td> <td>Dominicks</td> </tr> <tr> <td>3</td> <td>Walmart</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>Orders:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>OrderId</th> <th>StoreId</th> <th>OrderDate</th> </tr> <tr> <td>1</td> <td>1</td> <td>2011-01-11</td> </tr> <tr> <td>2</td> <td>1</td> <td>2011-01-14</td> </tr> <tr> <td>3</td> <td>2</td> <td>2011-02-05</td> </tr> <tr> <td>4</td> <td>3</td> <td>2011-03-17</td> </tr> <tr> <td>5</td> <td>3</td> <td>2011-03-29</td> </tr> <tr> <td>6</td> <td>3</td> <td>2011-04-02</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>OrderProducts:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>OrderId</th> <th>ProductId</th> <th>Qty</th> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>2</td> <td>2</td> </tr> <tr> <td>2</td> <td>3</td> <td>1</td> </tr> <tr> <td>3</td> <td>1</td> <td>2</td> </tr> <tr> <td>3</td> <td>3</td> <td>1</td> </tr> <tr> <td>3</td> <td>4</td> <td>3</td> </tr> <tr> <td>4</td> <td>3</td> <td>2</td> </tr> <tr> <td>4</td> <td>4</td> <td>2</td> </tr> <tr> <td>5</td> <td>1</td> <td>1</td> </tr> <tr> <td>5</td> <td>3</td> <td>1</td> </tr> <tr> <td>6</td> <td>1</td> <td>1</td> </tr> <tr> <td>6</td> <td>2</td> <td>3</td> </tr> <tr> <td>6</td> <td>4</td> <td>1</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>Now our sample relation database is ready for some pivoting. Lets say we want to know what products are sold by what store. Lets pull needed data using regular query:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">SELECT </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; s.StoreName,p.ProductName, op.Qty * p.Price <span class="cb1">AS </span>Totals </pre> <pre class="cl"><span class="cb1">FROM </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Products p <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; OrderProducts op <span class="cb1">ON </span>p.ProductId = op.ProductId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Orders o <span class="cb1">ON </span>op.OrderId = o.OrderId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Stores s <span class="cb1">ON </span>o.StoreId = s.StoreId</pre> </div> <p>&nbsp;</p> <p>Results:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>StoreName</th> <th>ProductName</th> <th>Totals</th> </tr> <tr> <td>Jewel</td> <td>Milk</td> <td>2.99</td> </tr> <tr> <td>Jewel</td> <td>Bread</td> <td>3.98</td> </tr> <tr> <td>Jewel</td> <td>Tomato</td> <td>0.99</td> </tr> <tr> <td>Dominicks</td> <td>Milk</td> <td>5.98</td> </tr> <tr> <td>Dominicks</td> <td>Tomato</td> <td>0.99</td> </tr> <tr> <td>Dominicks</td> <td>Grape</td> <td>5.97</td> </tr> <tr> <td>Walmart</td> <td>Tomato</td> <td>1.98</td> </tr> <tr> <td>Walmart</td> <td>Grape</td> <td>3.98</td> </tr> <tr> <td>Walmart</td> <td>Milk</td> <td>2.99</td> </tr> <tr> <td>Walmart</td> <td>Tomato</td> <td>0.99</td> </tr> <tr> <td>Walmart</td> <td>Milk</td> <td>2.99</td> </tr> <tr> <td>Walmart</td> <td>Bread</td> <td>5.97</td> </tr> <tr> <td>Walmart</td> <td>Grape</td> <td>1.99</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>Those result rows look like a fact table. Lets apply PIVOT to them:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">SELECT </span>* <span class="cb1">FROM</span></pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">SELECT </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s.StoreName,p.ProductName, op.Qty * p.Price <span class="cb1">AS </span>Totals </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">FROM </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Products p <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; OrderProducts op <span class="cb1">ON </span>p.ProductId = op.ProductId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Orders o <span class="cb1">ON </span>op.OrderId = o.OrderId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Stores s <span class="cb1">ON </span>o.StoreId = s.StoreId</pre> <pre class="cl"> ) <span class="cb1">AS </span>Source&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"><span class="cb1">PIVOT</span></pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">SUM</span>(Totals)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">FOR </span>ProductName <span class="cb1">IN </span>([Bread],[Milk],[Tomato],[Grape])&nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> ) <span class="cb1">AS </span>p</pre> </div> <p>&nbsp;</p> <p>PIVOT results:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>StoreName</th> <th>Bread</th> <th>Milk</th> <th>Tomato</th> <th>Grape</th> </tr> <tr> <td>Dominicks</td> <td>NULL</td> <td>5.98</td> <td>0.99</td> <td>5.97</td> </tr> <tr> <td>Jewel</td> <td>3.98</td> <td>2.99</td> <td>0.99</td> <td>NULL</td> </tr> <tr> <td>Walmart</td> <td>5.97</td> <td>5.98</td> <td>2.97</td> <td>5.97</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>Lets do another one: products sales by months.</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">SELECT </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; p.ProductName, <span class="cb1">DATENAME</span>(m,o.OrderDate) <span class="cb1">AS </span>[Month], op.Qty * p.Price <span class="cb1">AS </span>Totals </pre> <pre class="cl"><span class="cb1">FROM </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Products p <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; OrderProducts op <span class="cb1">ON </span>p.ProductId = op.ProductId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Orders o <span class="cb1">ON </span>op.OrderId = o.OrderId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Stores s <span class="cb1">ON </span>o.StoreId = s.StoreId</pre> </div> <p>&nbsp;</p> <p>Results:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>ProductName</th> <th>Month</th> <th>Totals</th> </tr> <tr> <td>Milk</td> <td>January</td> <td>2.99</td> </tr> <tr> <td>Bread</td> <td>January</td> <td>3.98</td> </tr> <tr> <td>Tomato</td> <td>January</td> <td>0.99</td> </tr> <tr> <td>Milk</td> <td>February</td> <td>5.98</td> </tr> <tr> <td>Tomato</td> <td>February</td> <td>0.99</td> </tr> <tr> <td>Grape</td> <td>February</td> <td>5.97</td> </tr> <tr> <td>Tomato</td> <td>March</td> <td>1.98</td> </tr> <tr> <td>Grape</td> <td>March</td> <td>3.98</td> </tr> <tr> <td>Milk</td> <td>March</td> <td>2.99</td> </tr> <tr> <td>Tomato</td> <td>March</td> <td>0.99</td> </tr> <tr> <td>Milk</td> <td>April</td> <td>2.99</td> </tr> <tr> <td>Bread</td> <td>April</td> <td>5.97</td> </tr> <tr> <td>Grape</td> <td>April</td> <td>1.99</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>PIVOT version:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">SELECT </span>* <span class="cb1">FROM</span></pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">SELECT </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; p.ProductName, <span class="cb1">DATENAME</span>(m,o.OrderDate) <span class="cb1">AS </span>[Month], op.Qty * p.Price <span class="cb1">AS </span>Totals </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">FROM </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Products p <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; OrderProducts op <span class="cb1">ON </span>p.ProductId = op.ProductId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Orders o <span class="cb1">ON </span>op.OrderId = o.OrderId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Stores s <span class="cb1">ON </span>o.StoreId = s.StoreId</pre> <pre class="cl"> ) <span class="cb1">AS </span>Source&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"><span class="cb1">PIVOT</span></pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">SUM</span>(Totals)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">FOR </span>[Month] <span class="cb1">IN </span>([January],[February],[March],[April])&nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> ) <span class="cb1">AS </span>p</pre> </div> <p>&nbsp;</p> <p>PIVOT results:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>ProductName</th> <th>January</th> <th>February</th> <th>March</th> <th>April</th> </tr> <tr> <td>Bread</td> <td>3.98</td> <td>NULL</td> <td>NULL</td> <td>5.97</td> </tr> <tr> <td>Grape</td> <td>NULL</td> <td>5.97</td> <td>3.98</td> <td>1.99</td> </tr> <tr> <td>Milk</td> <td>2.99</td> <td>5.98</td> <td>2.99</td> <td>2.99</td> </tr> <tr> <td>Tomato</td> <td>0.99</td> <td>0.99</td> <td>2.97</td> <td>NULL</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>And last one lets do stores sales by months:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">SELECT </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; s.StoreName,<span class="cb1">DATENAME</span>(m,o.OrderDate) <span class="cb1">AS </span>[Month], op.Qty * p.Price <span class="cb1">AS </span>Totals </pre> <pre class="cl"><span class="cb1">FROM </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Products p <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; OrderProducts op <span class="cb1">ON </span>p.ProductId = op.ProductId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Orders o <span class="cb1">ON </span>op.OrderId = o.OrderId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; Stores s <span class="cb1">ON </span>o.StoreId = s.StoreId</pre> </div> <p>&nbsp;</p> <p>Results:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>StoreName</th> <th>Month</th> <th>Totals</th> </tr> <tr> <td>Jewel</td> <td>January</td> <td>2.99</td> </tr> <tr> <td>Jewel</td> <td>January</td> <td>3.98</td> </tr> <tr> <td>Jewel</td> <td>January</td> <td>0.99</td> </tr> <tr> <td>Dominicks</td> <td>February</td> <td>5.98</td> </tr> <tr> <td>Dominicks</td> <td>February</td> <td>0.99</td> </tr> <tr> <td>Dominicks</td> <td>February</td> <td>5.97</td> </tr> <tr> <td>Walmart</td> <td>March</td> <td>1.98</td> </tr> <tr> <td>Walmart</td> <td>March</td> <td>3.98</td> </tr> <tr> <td>Walmart</td> <td>March</td> <td>2.99</td> </tr> <tr> <td>Walmart</td> <td>March</td> <td>0.99</td> </tr> <tr> <td>Walmart</td> <td>April</td> <td>2.99</td> </tr> <tr> <td>Walmart</td> <td>April</td> <td>5.97</td> </tr> <tr> <td>Walmart</td> <td>April</td> <td>1.99</td> </tr> </tbody> </table> <p>&nbsp;</p> <p>PIVOT version:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">SELECT </span>* <span class="cb1">FROM</span></pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">SELECT </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s.StoreName,<span class="cb1">DATENAME</span>(m,o.OrderDate) <span class="cb1">AS </span>[Month], op.Qty * p.Price <span class="cb1">AS </span>Totals </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">FROM </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Products p <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; OrderProducts op <span class="cb1">ON </span>p.ProductId = op.ProductId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Orders o <span class="cb1">ON </span>op.OrderId = o.OrderId <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Stores s <span class="cb1">ON </span>o.StoreId = s.StoreId</pre> <pre class="cl"> ) <span class="cb1">AS </span>Source&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"><span class="cb1">PIVOT</span></pre> <pre class="cl"> (</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">SUM</span>(Totals)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">FOR </span>[Month] <span class="cb1">IN </span>([January],[February],[March],[April])&nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> ) <span class="cb1">AS </span>p</pre> </div> <p>&nbsp;</p> <p>PIVOT results:</p> <table cellspacing="0" cellpadding="0" class="tableDefinition"> <tbody> <tr> <th>StoreName</th> <th>January</th> <th>February</th> <th>March</th> <th>April</th> </tr> <tr> <td>Dominicks</td> <td>NULL</td> <td>12.94</td> <td>NULL</td> <td>NULL</td> </tr> <tr> <td>Jewel</td> <td>7.96</td> <td>NULL</td> <td>NULL</td> <td>NULL</td> </tr> <tr> <td>Walmart</td> <td>NULL</td> <td>NULL</td> <td>9.94</td> <td>10.95</td> </tr> </tbody> </table>Tue, 27 Mar 2012 00:00:00 -07002012-03-27T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/list-sql-server-table-dependencies.htmhttp://www.karpach.com/list-sql-server-table-dependencies.htmT-SQLHow to list sql server table dependencies using T-SQL?<p>You can always use SQL Server Management Studio, just right click on a table in object explorer and select View Dependencies.</p> <p><img alt="View Dependencies" src="http://www.karpach.com/images/uploaded/Object-Dependencies.png" /></p> <p>However this way doesn't give you ability to copy dependencies to clipboard or any other way to export dependencies list. Luckily you can find the same data using <strong>SysObjects</strong> and <strong>SysDepends</strong> tables. Here is a quick T-SQL snippet that you might want to use for this purpose.</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">SELECT DISTINCT </span>dobj.name,dobj.type</pre> <pre class="cl"><span class="cb1">FROM </span>SysObjects obj <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> SysDepends d <span class="cb1">ON </span>obj.id = d.depid <span class="cb1">INNER JOIN </span></pre> <pre class="cl"> SysObjects dobj <span class="cb1">ON </span>d.id = dobj.id</pre> <pre class="cl"><span class="cb1">WHERE </span>obj.name = <span class="cb2">'WorkOrder' </span><span class="cb1">ORDER BY </span>dobj.type,dobj.name</pre> </div>Sun, 19 Feb 2012 00:00:00 -08002012-02-19T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/how-to-measure-performance-stored-procedure.htmhttp://www.karpach.com/how-to-measure-performance-stored-procedure.htmT-SQLHow to measure stored procedure execution time?<p>You can use <a href="http://msdn.microsoft.com/en-us/library/ms190287.aspx" target='_blank'>SET STATISTICS TIME ON</a>, but it would return you timing for all single queries inside of your stored procedure. If you have bunch of queries inside then it might be not really convenient. Here is a little code snippet alternative to STATISTICS TIME:</p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: green; } </style> <div class="cf"> <pre class="cl"><span class="cb1">Declare </span>@d <span class="cb1">datetime</span></pre> <pre class="cl"><span class="cb1">Set </span>@d = <span class="cb1">CURRENT_TIMESTAMP</span></pre> <pre class="cl">&nbsp;</pre> <pre class="cl"><span class="cb2">-- Your stored procedure call goes here</span></pre> <pre class="cl">&nbsp;</pre> <pre class="cl"><span class="cb1">SELECT DATEDIFF</span>(ms,@d,<span class="cb1">CURRENT_TIMESTAMP</span>)</pre> <pre class="cl">&nbsp;</pre> </div> <p>Use <strong>DBCC DROPCLEANBUFFERS</strong> to clear sql server cache for cache independent time measurement.</p> Wed, 15 Feb 2012 00:00:00 -08002012-02-15T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/javascript-properties-get-set.htmhttp://www.karpach.com/javascript-properties-get-set.htmJavascriptGet and Set operators in Javascript<p>I am C# developer, hence I use C# properties a lot. However I just recently discovered that Javascript has properties as well. Get and Set operators supported by Firefox 2.0+, Safari 3.0+, Chrome, Opera 9.5+. However IE still doesn't support properties. May be Microsoft would introduce support in IE10, but so far no luck. Here is a little example of Get and Set declaration:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: maroon; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">var</span> obj = {</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp; get CustomProperty() {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp;&nbsp; <span class="cb1">return</span> 0;</pre> <pre class="cl"> &nbsp;&nbsp; },</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp; set CustomProperty(s) {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp;&nbsp; alert (<span class="cb2">&quot;It is readonly.&quot;</span>);</pre> <pre class="cl"> &nbsp;&nbsp; }</pre> <pre class="cl"> };</pre> </div> <p>&nbsp;</p> <p>Properties can be useful and scary at the same time. A simple variable assignment is not predictable anymore. For example with a little bit of work you can convert your AJAX calls to something like this:</p> <div class="cf"> <pre class="cl"> ajax.url = <span class="cb1">&quot;script.php&quot;</span>;</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> alert(ajax.result);</pre> </div> <p>&nbsp;</p>Sat, 04 Feb 2012 00:00:00 -08002012-02-04T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/yslow-and-asp-net-three-years-later.htmhttp://www.karpach.com/yslow-and-asp-net-three-years-later.htmASP.NETYSlow and ASP.NET: 100 points "A" grade year 2012<p>Almost three years ago I wrote an original <a href="http://www.karpach.com/yslow-and-asp-net-100-points-a-grade.htm">YSlow and ASP.NET: 100 points "A" grade is possible</a> article. Since then I changed couple hosting providers, made numerous updates and converted my blog project to .NET 4.0. Today I decided to revise my blog to see if it still has 100 yslow points with final "A" grade.</p> <p>First surprise, as of January 29, 2012, YSlow addon doesn't work in FireFox 9. However YSlow is now available for <a href="https://chrome.google.com/webstore/detail/ninejjcohidippngpapiilnmkgllmakh" target="_blank">Google Chrome</a>, <a href="https://addons.opera.com/addons/extensions/details/yslow/" target="_blank">Opera</a>, <a href="http://d.yimg.com/jc/safari/yslow.safariextz" target="_blank">Safari</a>. It has <a href="http://developer.yahoo.com/yslow/mobile/" target="_blank">bookmarket version</a> and even <a href="http://developer.yahoo.com/yslow/commandline" target="_blank">command line version</a>. I installed Google Chrome Yslow extension and began investigation. Right out of the box most of sections still had A grade. About a week ago I switched from Google App Engine "CDN" to true CDN <a href="http://aws.amazon.com/cloudfront/" target="_blank">Amazon CloudFront</a>. Some files were not in CDN, so I had to take care of those. In general I highly recommend Amazon CloudFront. It is easy to setup and cost just a few bucks a month based on your traffic usage. Amazon CloudFront mimics your local file settings, so you need to make sure that you have compression, expire header and Etags taken care of. I use IIS7 compression instead of a compression module recommended by me in my previous article. Out of the box IIS7 compression doesn't work with AWS CloudFront, so you need to modify C:\Windows\System32\inetsrv\config\applicationHost.config file on your server to include following: </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } .cb3 { color: red; } </style> <div class="cf"> <pre class="cl"><span class="cb1">&lt;</span><span class="cb2">serverRuntime</span><span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> </span><span class="cb3">frequentHitThreshold</span><span class="cb1">=</span>&quot;<span class="cb1">1</span>&quot;<span class="cb1"> </span><span class="cb3">frequentHitTimePeriod</span><span class="cb1">=</span>&quot;<span class="cb1">00:00:20</span>&quot;<span class="cb1">/&gt;</span></pre> <pre class="cl"><span class="cb1">&lt;</span><span class="cb2">httpCompression</span><span class="cb1"> </span><span class="cb3">directory</span><span class="cb1">=</span>&quot;<span class="cb1">%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files</span>&quot;<span class="cb1"> </span><span class="cb3">noCompressionForHttp10</span><span class="cb1">=</span>&quot;<span class="cb1">False</span>&quot;<span class="cb1"> </span><span class="cb3">noCompressionForProxies</span><span class="cb1">=</span>&quot;<span class="cb1">False</span>&quot;<span class="cb1">&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &lt;</span><span class="cb2">scheme</span><span class="cb1"> </span><span class="cb3">name</span><span class="cb1">=</span>&quot;<span class="cb1">gzip</span>&quot;<span class="cb1"> </span><span class="cb3">dll</span><span class="cb1">=</span>&quot;<span class="cb1">%Windir%\system32\inetsrv\gzip.dll</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &lt;</span><span class="cb2">dynamicTypes</span><span class="cb1">&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">text/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">message/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">application/x-javascript</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">*/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">false</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &lt;/</span><span class="cb2">dynamicTypes</span><span class="cb1">&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &lt;</span><span class="cb2">staticTypes</span><span class="cb1">&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">text/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">message/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">application/javascript</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">true</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &nbsp; &lt;</span><span class="cb2">add</span><span class="cb1"> </span><span class="cb3">mimeType</span><span class="cb1">=</span>&quot;<span class="cb1">*/*</span>&quot;<span class="cb1"> </span><span class="cb3">enabled</span><span class="cb1">=</span>&quot;<span class="cb1">false</span>&quot;<span class="cb1"> /&gt;</span></pre> <pre class="cl"><span class="cb1">&nbsp; &lt;/</span><span class="cb2">staticTypes</span><span class="cb1">&gt;</span></pre> <pre class="cl"><span class="cb1">&lt;/</span><span class="cb2">httpCompression</span><span class="cb1">&gt;</span></pre> </div> <p>&nbsp;</p> <p>Also I modified my cache module to have ETags clean up:</p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } </style> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">void</span> Init(<span class="cb2">HttpApplication</span> context)</pre> <pre class="cl">{&nbsp;&nbsp; </pre> <pre class="cl">&nbsp;&nbsp;&nbsp; ...&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </pre> <pre class="cl">&nbsp;&nbsp;&nbsp; context.PostReleaseRequestState += <span class="cb1">new</span> <span class="cb2">EventHandler</span>(application_PostReleaseRequestState);</pre> <pre class="cl">}</pre> <pre class="cl"> </pre> <pre class="cl"><span class="cb1">void</span> application_PostReleaseRequestState(<span class="cb1">object</span> sender, <span class="cb2">EventArgs</span> e)</pre> <pre class="cl">{</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">HttpContext</span>.Current.Response.Headers.Remove(<span class="cb3">&quot;Server&quot;</span>);</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">HttpContext</span>.Current.Response.Headers.Remove(<span class="cb3">&quot;X-AspNet-Version&quot;</span>);</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; <span class="cb2">HttpContext</span>.Current.Response.Headers.Remove(<span class="cb3">&quot;ETag&quot;</span>);</pre> <pre class="cl">}</pre> </div> <p>&nbsp;</p> <p>YSlow added new section "Use Cookie-free Domains". My blog completely failed this one, since google analytics left cookies in top level karpach.com domain. I modified google analytics init script to have: _gaq.push(['_setDomainName', 'www.karpach.com']), so google cookies set for www.karpach.com instead of top level karpach.com. Then subdomain cdn.karpach.com, which points to Amazon CloudFront, became cookieless. Finally I am back to 100 YSlow points. Run Yslow on <a href="http://www.karpach.com/default.aspx?PageType=NoExternal">no externals version of karpach.com</a> to see it yourself.</p>Sun, 29 Jan 2012 00:00:00 -08002012-01-29T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/undefined-javascript-variable.htmhttp://www.karpach.com/undefined-javascript-variable.htmJavascriptHow to check if a javascript variable is undefined?<p> How to check if a javascript variable is undefined? This is actually a tricky question. There are three cases: </p> <ol> <li>A variable is declared using var keyword, but were never assigned any value.</li> <li>A variable is declared as function parameter, but when function were invoked parameter was not supplied.</li> <li>A variable is never declared or assigned a value and you are trying to access it.</li> </ol> <p> First two cases lead to <b>undefined value</b> of variable. Here is an example of first case: </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #006400; } .cb3 { color: maroon; } </style> <div class="cf"> <pre class="cl"><span class="cb1">var</span> v; <span class="cb2">// Value not defined</span></pre> <pre class="cl"><span class="cb1">if</span> (v) {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; console.log(<span class="cb3">&quot;v=&quot;</span> + v);</pre> <pre class="cl">}</pre> <pre class="cl"><span class="cb1">else</span> {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; console.log(<span class="cb3">&quot;v has undefined value&quot;</span>);</pre> <pre class="cl">}</pre> <pre class="cl">v = <span class="cb3">&quot;some value&quot;</span>;</pre> <pre class="cl"><span class="cb1">if</span> (v) {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; console.log(<span class="cb3">&quot;v = &quot;</span> + v);</pre> <pre class="cl">}</pre> </div> <br/> <p> <br/> Output: <br/> v has undefined value<br/> v = some value<br/> </p> <p> The second case is similar and you can use if statement as in above code or: </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #006400; } </style> <div class="cf"> <pre class="cl"><span class="cb1">function</span> Add(p1, p2) {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; p1 = p1 || 0; <span class="cb2">// set value to 0 if value is undefined</span></pre> <pre class="cl">&nbsp;&nbsp;&nbsp; p2 = p2 || 0; <span class="cb2">// set value to 0 if value is undefined</span></pre> <pre class="cl">&nbsp;&nbsp;&nbsp; <span class="cb1">return</span> p1 + p2; <span class="cb2">// would always work</span></pre> <pre class="cl">}</pre> <pre class="cl">&nbsp;</pre> <pre class="cl">console.log(Add(1, 2));</pre> <pre class="cl">console.log(Add(1));</pre> <pre class="cl">console.log(Add());</pre> </div> <p> <br/> Output: <br/> 3<br/> 1<br/> 0<br/> </p> <p> The third case when a variable is never declared. If you try to use such variable you get exception. If statement or default value assignment won't help. </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: maroon; } </style> <div class="cf"> <pre class="cl"><span class="cb1">if</span>(<span class="cb1">typeof</span> neverDeclared == <span class="cb2">&quot;undefined&quot;</span>) {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; console.log(<span class="cb2">&quot;There is something really wrong!&quot;</span>);</pre> <pre class="cl">}</pre> </div> <p> <br/> You would ask, what kind of sick people try to use a variable that was never declared or assigned a value? However I bet this is one of the most frequent javascript exceptions. Yes, simple variables are usually declared, but exception usually happens when you try to pass complex object with undeclared property. Javascript objects can be easily extended with additional properties and this comes at price of frequent undefined variable exceptions. Let me give you an example: </p> <style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: maroon; } .cb3 { color: #006400; } </style> <div class="cf"> <pre class="cl"><span class="cb1">var</span> obj = { FirstName: <span class="cb2">&quot;Viktar&quot;</span>, LastName: <span class="cb2">&quot;Karpach&quot;</span> };</pre> <pre class="cl">console.log(obj.FirstName);</pre> <pre class="cl">console.log(obj.LastName);</pre> <pre class="cl">console.log(obj.City); <span class="cb3">// Will give exception</span></pre> <pre class="cl">&nbsp;</pre> <pre class="cl"><span class="cb1">var</span> obj = { FirstName: <span class="cb2">&quot;Viktar&quot;</span>, LastName: <span class="cb2">&quot;Karpach&quot;</span> };</pre> <pre class="cl">obj.City = <span class="cb2">&quot;Chicago&quot;</span>;</pre> <pre class="cl">console.log(obj.City); <span class="cb3">// Works!</span></pre> <pre class="cl">&nbsp;</pre> <pre class="cl"><span class="cb1">if</span> (<span class="cb1">typeof</span> obj.State == <span class="cb2">&quot;undefined&quot;</span>) {</pre> <pre class="cl">&nbsp;&nbsp;&nbsp; console.log(<span class="cb2">&quot;State is undefined.&quot;</span>);</pre> <pre class="cl">}</pre> </div> <br/> <p>Undefined variable exception very often happens when you try to consume objects received from third party API or from AJAX requests, always be careful what you consume from partially unreliable resource.</p>Thu, 12 Jan 2012 00:00:00 -08002012-01-12T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/dvdfab-volume-label-autohotkey.htmhttp://www.karpach.com/dvdfab-volume-label-autohotkey.htmC# .NETAutoHotkey DVDFab script for volume label conversion<p>From time to time I use free <a target="_blank" href="http://www.dvdfab.com/hd-decrypter.htm">DVDFab HD Decrypter</a> to rip rented movies, so I can watch them later using my Popcorn Hour A210 media streamer. DVDFab is kind enough to pull volume label from DVD disk. However this label has all uppercase letters and words are concatenated by underscores. For example:</p> <p>THE_LONG_GOODBYE</p> <p><img alt="DVDFab vs AutoHotkey" src="http://www.karpach.com/images/uploaded/DVDFab-vs-AutoHotkey.jpg" /></p> <p>&nbsp;</p> <p>I rename those labels to something like this: The Long Goodbye (1973), where year I lookup on IMDB web site. I don't like tedious repetitive tasks, so one day I decided to write a script for <a href="http://www.autohotkey.com/">AutoHotkey</a> to automate this process.</p> <p>It is easy to replace underscores and correct letter case, but year should be retrieved from some kind of database. Google pointed me in a direction of <a target="_blank" href="http://www.imdbapi.com">IMDB API</a>.</p> <p>It might be a little bit challenging to do IMDB API access using just AutoHotkey script engine. Luckily AutoHotkey scripts can load dll libraries and with magic of <a target="_blank" href="http://www.autohotkey.com/forum/topic22923.html">COM.ahk</a> and <a target="_blank" href="http://www.autohotkey.com/forum/topic26191.html">CLR.ahk</a> you can load even .NET libraries. However those magic scripts can't properly execute .NET 3.5/4.0 libraries, so I had to use .NET 2.0. As you probably know .NET 2.0 doesn't have native support for JSON deserialization, so I had to use Newtonsoft.Json for this purpose. I also could use XML output from IMDB API, but I prefer to use JSON, since response is smaller in a size. Here is my code for .NET part of it:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } .cb4 { color: #3cb371; } .cb5 { color: green; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">Server</span></pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> GetMovieTitleWithYear(<span class="cb1">string</span> volumeLablel)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> request = <span class="cb2">WebRequest</span>.Create(<span class="cb1">new</span> <span class="cb2">Uri</span>(<span class="cb1">string</span>.Format(<span class="cb3">&quot;http://www.imdbapi.com/?t=</span><span class="cb4">{0}</span><span class="cb3">&quot;</span>, </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; volumeLablel.Replace(<span class="cb3">'_'</span>, <span class="cb3">'+'</span>))));</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; request.Method = <span class="cb3">&quot;GET&quot;</span>;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">try</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> response = request.GetResponse();&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> responseStream = response.GetResponseStream();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (responseStream != <span class="cb1">null</span>)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> reader = <span class="cb1">new</span> <span class="cb2">StreamReader</span>(responseStream);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> responseString = reader.ReadToEnd();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">var</span> movie = <span class="cb2">JsonConvert</span>.DeserializeObject&lt;<span class="cb2">Movie</span>&gt;(responseString);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (movie != <span class="cb1">null</span> &amp;&amp; !<span class="cb1">string</span>.IsNullOrEmpty(movie.Title))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb1">string</span>.Format(<span class="cb3">&quot;</span><span class="cb4">{0}</span><span class="cb3"> (</span><span class="cb4">{1}</span><span class="cb3">)&quot;</span>, movie.Title, movie.Year);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">catch</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; { </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> UppercaseWords(volumeLablel.Replace(<span class="cb3">'_'</span>, <span class="cb3">' '</span>).ToLower());</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">static</span> <span class="cb1">string</span> UppercaseWords(<span class="cb1">string</span> value)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">char</span>[] array = value.ToCharArray();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb5">// Handle the first letter in the string.</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (array.Length &gt;= 1)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (<span class="cb1">char</span>.IsLower(array[0]))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[0] = <span class="cb1">char</span>.ToUpper(array[0]);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb5">// Scan through the letters, checking for spaces.</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb5">// ... Uppercase the lowercase letters following spaces.</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">for</span> (<span class="cb1">int</span> i = 1; i &lt; array.Length; i++)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (array[i - 1] == <span class="cb3">' '</span>)</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (<span class="cb1">char</span>.IsLower(array[i]))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; array[i] = <span class="cb1">char</span>.ToUpper(array[i]);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb1">new</span> <span class="cb1">string</span>(array);</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> }</pre> </div> <p>&nbsp;</p> <p>Then using <a target="_blank" href="http://json2csharp.com/">JSON 2 C# Service</a> you get following:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">Movie</span></pre> <pre class="cl"> { </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Title { <span class="cb1">get</span>; <span class="cb1">set</span>; }&nbsp;&nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">int</span> Year { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Rated { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Released { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Genre { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Director { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Writer { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Actors { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Plot { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Poster { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Runtime { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">float</span> Rating { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">int</span> Votes { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> ID { <span class="cb1">get</span>; <span class="cb1">set</span>; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Response { <span class="cb1">get</span>; <span class="cb1">set</span>; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> }</pre> </div> <p>&nbsp;</p> <p>Library is ready. Lets plug it into AutoHotkey:</p> <div class="cf"> <pre class="cl"> #include c:\Program Files (x86)\Autohotkey\Extras\Scripts\CLR.ahk #include c:\Program Files (x86)\Autohotkey\Extras\Scripts\COM.ahk RemoveToolTip: SetTimer, RemoveToolTip, Off ToolTip return #IfWinActive ahk_class QWidget ~Lbutton:: MouseGetPos, xpos,ypos if (xpos&gt;306 and ypos&gt;474 and xpos&lt;605 and ypos&lt;495) { ToolTip, Double click in text box below`nto get IMDB title and year,305,430 SetTimer, RemoveToolTip, 5000 } If (A_PriorHotKey = A_ThisHotKey and A_TimeSincePriorHotkey &lt; 500) { CLR_Start() imdb := CLR_LoadLibrary(&quot;c:\Program Files (x86)\Autohotkey\Extras\Scripts\Karpach.IMDB.dll&quot;) ;Type names must be fully qualified. server:= CLR_CreateObject(imdb,&quot;Karpach.IMDB.Server&quot;) Send ^a Sleep 100 clipboard = Send ^c ClipWait title:=COM_Invoke(server,&quot;GetMovieTitleWithYear&quot;,clipboard) COM_Release(imdb) COM_Release(server) clipboard = %title% Send ^v return } return #IfWinActive </pre> <p>&nbsp;</p> </div> <p>Now whenever you double click in volume label text box content would be converted into proper movie title.</p> <p><a href="http://www.karpach.com/files/DVDFab-Autohotkey-Scripts.zip">AutoHotkey scripts and DLLs</a></p> <p><a href="http://www.karpach.com/files/Karpach.IMDB.zip">IMDB API .NET library source code</a></p> <p><b>Update:</b></p> <p>If you are using AutoHotkey_L then you don't need COM.ahk and you can call GetMovieTitleWithYear method directly:</p> <p>&nbsp;</p> <pre> title:= server.GetMovieTitleWithYear(clipboard) </pre> <p>&nbsp;</p> <p><a href="http://www.karpach.com/files/DVDFab-Autohotkey_L-Scripts.zip">AutoHotkey_L scripts and DLLs</a></p> <p><b>Second Update:</b></p> <p>It is better to use new url for imdb api: http://www.omdbapi.com/</p> Sun, 25 Dec 2011 00:00:00 -08002011-12-25T00:00:00-08:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/interface-based-factory-pattern.htmhttp://www.karpach.com/interface-based-factory-pattern.htmC# .NETSimple factory pattern based on interfaces<p>The main purpose of factory design patterns is to separate object creation process from current implementation context. During coding you use factory to create objects. Based on incoming parameters factory decides which object to create. You don't know exact type of the object. Usually you know either base abstract class type that created object was inherited from or interface that created object is implementing. Based on this knowledge you can use object (call abstract class methods or interface methods). I found a lot of factories examples with abstract class connection, but none with interface connection. However interface connection is most common in commercial factory design pattern implementations. Many developers use Unity Framework or Castle Project (example of connection based on interface). Below I'll show how to implement factory with interface connection like it is done in Unity Framework or Castle Project.</p> <p>Lets build animal fabric, which would create dogs and cats objects. Let's define ICat and IDog interfaces.</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">ICat</span></pre> <pre class="cl"> {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">string</span> Meow();</pre> <pre class="cl"> }</pre> <br /> <pre class="cl"><span class="cb1">public</span> <span class="cb1">interface</span> <span class="cb2">IDog</span></pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">string</span> Bark();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">string</span> Sit();</pre> <pre class="cl"> }</pre> </div> <p>Now, lets define some Dog and Cat classes that implement ICat and IDog interfaces respectevly.</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">Cat</span>:<span class="cb2">ICat</span></pre> <pre class="cl"> {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Meow()</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb3">&quot;Meow meow meow ...&quot;</span>;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> }</pre> <br /> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">Dog</span>: <span class="cb2">IDog</span></pre> <pre class="cl"> {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Bark()</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb3">&quot;Woof woof woof ...&quot;</span>;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">string</span> Sit()</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> <span class="cb3">&quot;I am sitting.&quot;</span>;</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;</pre> <pre class="cl"> }</pre> </div> <p>Now lets create factory:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #2b91af; } .cb3 { color: #a31515; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">public</span> <span class="cb1">class</span> <span class="cb2">DefaultFactory</span></pre> <pre class="cl"> {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; <span class="cb1">public</span> <span class="cb1">static</span> T Create&lt;T&gt;()</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (<span class="cb1">typeof</span>(T) == <span class="cb1">typeof</span>(<span class="cb2">IDog</span>))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> (T)(<span class="cb2">IDog</span>)<span class="cb1">new</span> <span class="cb2">Dog</span>();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">else</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">if</span> (<span class="cb1">typeof</span>(T) == <span class="cb1">typeof</span>(<span class="cb2">ICat</span>))</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">return</span> (T)(<span class="cb2">ICat</span>)<span class="cb1">new</span> <span class="cb2">Cat</span>();</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">else</span></pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span class="cb1">throw</span> <span class="cb1">new</span> <span class="cb2">NotImplementedException</span>(<span class="cb2">String</span>.Format(<span class="cb3">&quot;Creation of {0} interface is not supported yet.&quot;</span>, <span class="cb1">typeof</span>(T)));</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> &nbsp;&nbsp;&nbsp; }</pre> <pre class="cl"> }</pre> </div> <p>And here how you can use it:</p> <p><style type="text/css"> .cf { color: black; } .cl { margin: 0px; } .cb1 { color: #2b91af; } </style></p> <div class="cf"> <pre class="cl"><span class="cb1">IDog</span> dog = <span class="cb1">DefaultFactory</span>.Create&lt;<span class="cb1">IDog</span>&gt;();</pre> <pre class="cl"><span class="cb1">ICat</span> cat = <span class="cb1">DefaultFactory</span>.Create&lt;<span class="cb1">ICat</span>&gt;();</pre> <pre class="cl"><span class="cb1">Console</span>.WriteLine(dog.Bark());</pre> <pre class="cl"><span class="cb1">Console</span>.WriteLine(dog.Sit());</pre> <pre class="cl"><span class="cb1">Console</span>.WriteLine(cat.Meow());</pre> </div> <p><br /> Output:<br /> <br /> Woof woof woof ...<br /> I am sitting.<br /> Meow meow meow ...</p> <p><a target="_blank" href="http://www.karpach.com/files/Karpach.Samples.AnimalFabric.zip">Source code for sample project above.</a></p>Mon, 05 Sep 2011 00:00:00 -07002011-09-05T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/video-links-media-player-chrome-extension.htmhttp://www.karpach.com/video-links-media-player-chrome-extension.htmJavascriptVideo Links in a New Media Player Window<p><img align="left" alt="" src="http://www.karpach.com/images/uploaded/Youtube-Player.png" />Google Chrome extension popups Youtube, IMDB, Vimeo and Twitvid links in new window with just a video player. Video player window can be resized to scale video. Works with online chats that post links dynamically, for example CampFire from 37 Signals.</p> <p>Each video gets its own window, so you can open as many videos as you like. Cntrl-Click to open link regular way.</p> <p>Please leave comments and suggestions.</p> <p><a href="https://chrome.google.com/webstore/detail/njgnenfcakbjnampnknmmolokpcfmbfh"> Install Google Chrome Extension</a></p> <p>After you install extension try to click on sample links below:</p> <p><a href="http://www.imdb.com/video/imdb/vi1639488793/">IMDB</a></p> <p><a href="http://www.youtube.com/watch?v=DDU0bnhfmME&NR=1">Youtube</a></p> <p><a href="http://vimeo.com/11219730">Vimeo</a></p> <p><a href="http://www.twitvid.com/Z6OLO">twitvid</a></p> <p><a href="http://youtu.be/DUQi_R4SgWo">youtu.be</a></p>Thu, 12 May 2011 00:00:00 -07002011-05-12T00:00:00-07:00Copyright (c) 2007 Viktar Karpachhttp://www.karpach.com/how-to-find-column-usage-in-stored-procedure.htmhttp://www.karpach.com/how-to-find-column-usage-in-stored-procedure.htmT-SQLHow to find column usage in stored procedure?<p>Sometimes you want to rename or delete column from a table in MS SQL Server. How to find column name dependency usage in MS SQL Server? There is no clean way of doing this. In management studio you can right click on object and select &quot;View Dependency&quot;, but this would show only object dependency. So you need to write some T-SQL code in order to find dependency. Lets say we need to find dependecy of Title column from the Article table.</p> <p><style type="text/css"> .cf { color: black;} .cl { margin: 0px; } .cb1 { color: blue; } .cb2 { color: #a31515; } </style></p> <div class="cf"> <p class="cl"><span class="cb1">SELECT DISTINCT </span>so.name</p> <p class="cl"><span class="cb1">FROM </span>sys.syscomments sc</p> <p class="cl"><span class="cb1">INNER JOIN </span>sys.objects so <span class="cb1">on </span>sc.id=so.object_id</p> <p class="cl"><span class="cb1">WHERE </span>sc.text <span class="cb1">LIKE </span><span class="cb2">'%Title%'</span></p> </div>Sun, 08 May 2011 00:00:00 -07002011-05-08T00:00:00-07:00Copyright (c) 2007 Viktar Karpach