insert into (select) with oracle sequence

Colleague needed some SQL today for an upgrade script. Needed to add default data to entities that didn’t have a specific parameter.

As usual I reduced the problem to a snippet. Although he was trying to squeeze it into a single command with INSERT INTO (SELECT…), due to time constraints I felt a stored proc was faster to get over it. Maybe someone can post the single command here.

In this case the TestTable already had some data, with refids 1, 2, and 3. Ref 2 already had the xyz param, but the others don’t. So the task is to add the field to all other refids, using the given TestSeq.


CREATE SEQUENCE TestSeq START WITH 7;

CREATE TABLE TestTable (
	id NUMBER PRIMARY KEY,
	refid NUMBER,
	param VARCHAR2(64),
	other NUMBER
);

INSERT INTO TestTable VALUES(1, 1, 'abc', 0);
INSERT INTO TestTable VALUES(2, 1, 'def', 0);
INSERT INTO TestTable VALUES(3, 2, 'abc', 0);
INSERT INTO TestTable VALUES(4, 2, 'xyz', 0);
INSERT INTO TestTable VALUES(5, 3, 'abc', 0);
INSERT INTO TestTable VALUES(6, 3, 'def', 0);

DECLARE
	newid TestTable.id%type;
	CURSOR dataCursor IS
		SELECT UNIQUE refid, other FROM TestTable WHERE refid NOT IN 
		(SELECT refid FROM TestTable WHERE param = 'xyz');
BEGIN
	FOR dataRow IN dataCursor
	LOOP
		SELECT TestSeq.nextval INTO newid FROM DUAL;
		INSERT INTO TestTable VALUES(newid, dataRow.refid, 'xyz', dataRow.other);
	END LOOP;
END;
/

VirtualBox + Ubuntu + SSH + x11

My hands got itchy today, so I thought of having Unix available remotely, since I do almost everything remotely. I had an old version of VirtualBox (1.6 I think) installed on one of my (remote) Windows box, so I downloaded an Ubuntu 8.10 image and threw it in as a guest. It installed perfectly, and had immediate network access, but the guest additions wouldn’t work. At first it appeared to be a privilege problem, so I went to Terminal and ran the following:

cd /media/cdrom
sudo ./VBox-LinuxAdditions.run

and it failed with an Xorg can’t be installed error. So I pulled the new VirtualBox 2.1.2 and did the same thing, this time it passed without a glitch. The additions are like “drivers” for the VirtualBox “hardware”, giving the guest a larger screen resolution than 800×600.

SSH was a breeze too, with this line:

sudo apt-get install openssh-server

Now problem 2 was getting the Guest machine to accept incoming connections. And this guy gave the best solution for me:

> cd \Program Files\Sun\xVM VirtualBox
> VBoxManage setextradata Intrepid 
VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort 22
> VBoxManage setextradata Intrepid 
VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort 22
> VBoxManage setextradata Intrepid 
VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol TCP

This sets up port forwarding such that incoming connections on port 22 on my Windows host will go into the Ubuntu guest, which hosts the SSH server. All was good, after a VM restart and I could PuTTY in smoothly.

Next, problem 3. I had Xming installed, but wasn’t very sure how to use it. I had hoped it wouldn’t be difficult to use or configure. Fortunately, Wikipedia had a nice link to a video tutorial for my exact situation.

The steps:

  1. Install Xming with Portable PuTTY. Make sure the Xming server is started.
  2. On the Intrepid guest, edit /etc/ssh/sshd_config, search and make sure “X11Forwarding yes” is uncommented. For my case this was already forwarded by default, so I didn’t even bother to restart my sshd.
  3. Fire up your PuTTY, go to the SSH > X11 tab, Enable X11 forwarding and specify “localhost:0” as the X display location. Connect to the Ubuntu guest.
  4. Start up a GUI application, such as “gedit &”.

In X11, the client is the Unix machine, and the server is the display where the GUI will appear.

Now I have what I wanted, it was fun, I hope some point in time I’d need such a configuration. Probably when I need quick Unix access for testing. Otherwise it’ll be for helping to answer other people’s Unix questions!

inconvertible types when casting

I was unlucky enough to hit a compiler bug, such that Eclipse IDE passes compilation, but it failed on my server using ant with Sun’s compiler. From the bug report it happens to JDK6 and will only be fixed in JDK7, so in the meantime I read of 2 workarounds that work.

1. Up-cast to an Object so that the down-cast become compiler-legal.

(ChildType)(Object)getParentType();

2. Use the Class.cast() method.

ChildType.class.cast( getParentType() );

log4j to console programatically

I thought I logged this, but when I searched I couldn’t find it. I guess I have lots more things I forget to log here. And it shows they’re useful, because I do come back here to search for it.

I often write small driver snippets to test my code in small units, and I need an easy way to configure logging so that I can see the output on my console without running to a file. And here’s the solution:


Logger root = Logger.getRootLogger();
root.addAppender(new ConsoleAppender(
    new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));

Source: http://robertmaldon.blogspot.com/2007/09/programmatically-configuring-log4j-and.html

CruiseControl “Deployments by this build”

I was asked what I did to show the jar files listed in “Deployments by this build” in CC’s build report. I didn’t even publish any artifacts. A quick search shows that CC uses distributables.xsl to filter stuff from the ant log file.

Continuous integration is the next tool you ought to have after source control. It’s funny I had to learn them myself (no school I knew teaches it)…

Source: http://markmail.org/message/4qmotsivebcula4a

Personal Encryption

I finally took the time to search for some personal encryption while waiting for a long svn merge and commit. I wanted a small tool, preferably a single executable, works on multiple platforms, to encrypt files with keys. GPG initially felt a little too obscure to me, until I found this guide.

In short:

  • gpg --gen-key and answer the prompts to generate a key pair. Make sure your remember your passphrase.
  • gpg -k to list the keys.
  • gpg -r [keyname] -e [clearfile] to encrypt the file using the particular key. By default the output is the same clearfile name with .gpg extension. Use -o option to change it.
  • gpg -o [clearfile] -d [encryptedfile] to decrypt the file. -o option is needed here because this command outputs to console by default.
  • gpg -a -r [keyname] -o [keyfile] --export-secret-keys to export the secret key. I need the same key on another computer to decrypt the contents. The -a option is synonymous with –armor, which generates a text representation instead of binary.
  • gpg --import [keyfile] to import the key on to another computer.

A bonus feature was it actually compresses as it encrypts. I was thinking of manually scripting it to do so.

Security settings in cmd

Windows Explorer wasn’t responding well to a shared folder, so I was using cmd to quickly access what I wanted. Turns out I needed to change security settings on a folder to enable another domain user to access it. Then I realized I didn’t know how (in cmd). The network was local so I had no Internet access to help me.

Now that I have, I easily found the answer to be “cacls”. Looking at the syntax help I should be able to reproduce what I do with the GUI on the command line.

Section page orientation in OpenOffice Writer

Used to Microsoft Word’s style of creating a single document with different page orientations (portrait/landscape), I was having much trouble doing the same in OpenOffice Writer.

In Word, you could insert a new section, and go to Page Setup to switch the section page orientation to landscape.

In Writer, a section has a different meaning, although it could be used for columns as well.

To do the same in Writer, you have to:

  1. ‘Styles and Formatting’ box, click ‘Page Styles’.
  2. Right click on ‘Default’, select ‘New’.
  3. Name it ‘Default Landscape’.
  4. change the ‘Next Style’ to ‘Default Landscape’.
  5. Click on the ‘Page’ tab and set the orientation to Landscape.
  6. Click ‘OK’.
  7. Create another style called ‘Default Portrait’, this time setting the orientation to Portrait.

Now you can switch orientation by inserting a page break and selecting a different page style.

Reference: http://www.linuxforums.org/applications/word_to_writer.html