Yoooder.com Just another WordPress weblog

8Jul/110

SQL – Joining two tables without related data – joining without an on clause

I'm loading test data, and have a situation where I have a list of ID's I need to generate test-data for, and a list of sample data I'd like to populate for each test ID.  Something like this:

TestIDs
---------
1
2
3

TestData
--------
A
B

What I want is a join that produces:

Result
--------
1 A
1 B
2 A
2 B
3 A
3 B

This is essentially a join without an "on" clause, however the trick in SQL is that the "join" keyword isn't needed either.  The select statement is simply:

select * from TestIDs, TestData

Notice that there is a comma seperating the tables to select from, and no join keyword anywhere.  Handy!

14Jul/090

VB.NET (and C# 4.0) Optional Features – Great for Unit-testing structures and DTO’s!

Here's a great use-case for Optional parameters in VB.NET (and to be introduced in C# 4.0), it's saved me lots of code and kept my unit tests small.

Some data structures (DTO's to some folk, and classes as well) have a large number of properties with the same return type, such as:

public struct MyData
{
public MyData(string firstName, string middleName, string lastName, string surName, string formerName)
{ // set the properties }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string SurName { get; set; }
public string FormerName { get; set; }
// ...and so on...
}

Imagine that this strucuture is implemented with field-backed properties (instead of automatically-implemented properties) and we wanted unit tests to ensure that setting SurName didn't change the backing field for FormerName.  Each unit test would need to instantiate an instance of MyData by passing a laundry-list of parameters.  Here's a sample of a unit test (and accompanying BuiltTarget() method) showcasing how ugly this can be (notice that BuildTarget() is an ungainly mess; and that there would normally be one or more tests for each parameter).

Public Function BuildTarget(ByVal firstName As String, _
ByVal middleName As String, _
ByVal lastName As String, _
ByVal surName As String, _
ByVal formerName As String) As MyData
Return New MyData(firstName, middleName, lastName, surName, formerName)
End Function

<TestMethod()> _
Public Sub New_WhenFirstNameProvided_KeepsValue()
Dim firstName = "Steve"
Dim target = BuildTarget(firstName, _
Nothing, _
Nothing, _
Nothing, _
Nothing)

Dim expected = firstName
Dim actual = target.FirstName

Assert.AreEqual(expected, actual)

End Sub

To work around this my tests contain a single "BuildTarget()" factory which declares each parameter as optional, and provides a default value.  Then when I need to set just a single parameter I can do so by name and let the default values provide the remaing parameters.  The above sample cleans up to look like:

Public Function BuildTarget(Optional ByVal firstName As String = Nothing, _
Optional ByVal middleName As String = Nothing, _
Optional ByVal lastName As String = Nothing, _
Optional ByVal surName As String = Nothing, _
Optional ByVal formerName As String = Nothing) As MyData
Return New MyData(firstName, middleName, lastName, surName, formerName)
End Function

<TestMethod()> _
Public Sub New_WhenFirstNameProvided_KeepsValue()
Dim firstName = "Steve"
Dim target = BuildTarget(firstName:=firstName)

Dim expected = firstName
Dim actual = target.FirstName

Assert.AreEqual(expected, actual)

End Sub

As you can imaging, the small amount of extra code in the BuildTarget() method is insignificant compareed to the savings in each TestMethod.

28Mar/080

.NET: Referencing a 2.0 DLL from a 1.1 DLL

*NOTE* This only works if your executable is running under the 2.0 Framework!

At work we are writing a .NET 2.0 application that has its roots back in 1.1.  Some ancillary applications were also written in 1.1, and shared some common libraries; so when it came time for this application to be upgraded to 2.0 some libraries had to be left behind for compatibility reasons.

One such library has the task of loading data from databases, and was designed to read from MS Access, SQL Server, or MSDE; however in the ongoing development of our application we are scraping the MS Access use in favor of SQL Compact Edition.  For those familiar with SQL CE, you'll know it's not compatible with the 1.1 framework--so we were faced with the issue of either duplicating the functionality of the 1.1 DLL in a 2.0 DLL, so that old applications could be left alone, or with doing massive work to overhaul the old applications and try to bring them all up to 2.0...  neither option was terribly appealing (although the first option was obviously preferable).

Our project architect put the task to me, and had a plan of attack that he figured would let us make the 1.1 DLL talk to the SQL CE database via a 2.0 DLL proxy--however you'll know that 1.1 projects cannot reference 2.0 projects.  Here's where a little trickery comes in...

For a quick recap we have: A Framework 2.0 application, which calls a 1.1 Library to retrieve data.  We need: A Framework 2.0 application to call the 1.1 Library to call a 2.0 Library to retrieve the data from SQL CE.

To achieve this we added an interface to the 1.1 Library, and implemented that interface in the new 2.0 library which uses the interface to fetch the needed data from the database.  The 1.1 library uses reflection to late-load the 2.0 library and instantiates it by its interface.

So the workflow is:

  • 2.0 Application makes a call to the 1.1 Library
    •  1.1 Library uses an Assembly.Load() call to late-load the new 2.0 Library and casts it to the 1.1's interface definition.
    • 1.1 Library then can call the Interface member, which...
      • Talk through the 2.0 framework to our SQL CE database.
Tagged as: , , No Comments
6Oct/070

Recovery from a dead FAT32 Windows drive

My landlord's old PC has suffered several failures over the years, including several fans and power supplies and at least one hard drive. When I moved in a year ago they called me over to replace yet another PSU for them, and a few months later to figure out why the machine would not even begin to boot.

The suspect was immediately either the IDE controller or both of its drive. After transplanting the drives to a seperate machine and failing to find their partitions, and after trying a new drive in the machine it was looking as though possibly all were fried.

A couple months have passed and my landlord has had me build him a new computer and the day has come to try to recover his data... Below is the process I've used to diagnose and recover his files.

6Oct/070

Determining / Recovering a Partition’s UUID (Univeral Unique Identifier)

I made a boo-boo after reinstalling my base Ubuntu system, and accidently copied over the entirety of the fresh installation's /etc/fstab file with my old /etc/fstab.

The UUIDs of my new installation were lost, so after a little Googling I found that there's a helpful little utility called "vol_id" specifically for determining this type of info...

vol_id [partition]

and voila, I've got my fstab back in fine shape :-)

25Sep/070

Penetration testing my apartment

Problem: It's 1:00am, in a town where you don't really know anyone, you're locked out of your apartment, and the only things you have on you are a cell phone and a toy RC helicopter (it has LED's that blink, I thought it would be cool to fly in the dark).

Objective: Gain entry to your dwelling without having to a) wake up the neighbor or b) explain to a cop that your ID is just behind the door you're trying to jimmy.

19Mar/070

Creating an encrypted volume

I needed to meet my company's required security policy for taking source code offsite: 256bit AES encryption.

Since the source code I had on my laptop was within a Virtual Machine, I thought it would be a good solution to make an encrypted filesystem big enough for the VM, and only mount it when I wanted to work.

Here's my requirements:
- Encrypted FS that is (un)mountable whenever need be
- Passphrase to mount the filesystem
- 30GB of storage within the filesystem, to accomodate the 30GB VM disk.

19Mar/070

Wiping a hard drive

Wiping a HardDrive is a common task at the workplace or whenever you're getting ready to sell a PC. There is a utility to do just such a thing in almost all Linux Bootdisks called shred.

Shred can be used to wipe files, or whole drives. To wipe a drive, the following parameters are good to know
-z After shredding, one more pass will be made to write all zeros to the drive, not necessary, but it hides the fact that the disk has been shredded
-v Display messages about progress, which is handy since shredding a drive takes a fair deal of time
-n x x is the number of passes you wish to make. 3 is a good number, and if you use the -z option it will be 3 shredding passes followed by a 4th pass to zero out all the data

Put it together and pass it a drive to wipe:
shred -zvn 3 /dev/sda

And voila, all your private data goes bye-bye

Filed under: Linux, Security, Tips No Comments
18Mar/070

Restoring a lost RAID array

After an upgrade from Edgy to Feisty my RAID array disappeared (aka, I shit my pants).

Doing a 'cat /proc/mdstat' produced:
Personalities : [raid6] [raid5] [raid4]
<none>

Thankfully I had backups of my mdadm.conf and lvm.conf files so I knew that as long as I didn't alter the data on the drives I could always fall back to Edgy and still have my files.

After trying a few things with mdadm, all unsuccessfully, I decided to uninstall it entirely. So I made a (second) backup of my mdadm.conf and lvm.conf files, and did a:
apt-get --purge remove mdadm lvm2

This uninstalls the packages and also removes the config files (that's what the --purge option is there for).

After uninstalling them I verified the configs were gone and reinstalled the packages through Synaptic inside of GNOME. During the install it prompted which arrays were required to boot. On my failed upgrade I had configured it with "all" but I really didn't need my array at boot time, my root partition is on a standalone IDE drive, so this time I told it "none".

I expected to have more tinkering to do, but after the install I again did a "cat /proc/mdstat" and to my surprise my array was back! *yay*

Now I just had to get my lvm partitions back. I've found the Gentoo LVM2 installation guide very usefull, and a good resource for LVM, so I turned to it to see how to recover volumes. The commands to restore my volumes were:
vgscan
vgchange -a y

And voila, my array and it's partitions were good as new. A quick "mount -a" brought them all up without a glitch!

Filed under: Linux, Tips, Ubuntu No Comments
18Mar/070

Edgy to Feisty Upgrade

I've been running Edgy as a deskop/mythTV box for several months, and have tried Feisty on a spare laptop at work. Feisty includes a large number of multimedia-integration improvements, so I thought I'd try it on my myth-machine.

The process is easy-as-pie. Just run "update-manager -c -d" and it's a simple walk-through process.

Obligatory Warning: Feisty won't be officially released until April, and it's still labeled an Alpha release, so be warned it has lots of bugs and is not stable--personally I see my gnome-panel crash quite often, as well as a number of other oddities. You've been warned

Filed under: Linux, Tips, Ubuntu No Comments