Skip to main content

Posts

Wading through mud with Castle ActiveRecord

Lately, it feels like I've been wading through mud. As if the ActiveRecord library has been doing nothing but preventing me from getting my work done. In my dreams, I could call MyEntity.Find() with a list of ICriterion objects, and get back what I want. I was having problems with that, because I want to select several object, left joining to a notes table, and return only the most recent note (with max(NoteDate)). While I can think it, and write it in raw SQL, it has been tough doing this in an ActiveRecord query. Just to see if it is possible, I tried HQL, and finally resorted to raw SQL. I learned that "raw SQL" has several requirements imposed on it: * The returned result set must include the complete field list of all entities that you want to return (as far as I can tell). * NHibernate will replace {nt.*} with a complete field list if you include "nt" as an alias for typeof(MyNote). * Do not list an alias unless you are returning that entity in your ...

Castle ActiveRecord with Criteria and Alias

Update May 25, 2007: ActiveRecord now supports DetachedCriteria, which eliminates the need for the SlicedFindAll that I wrote below. It is nice when a library moves to add support for such commonly needed functions. So in summary, use Detached criteria instead of the code below. It is still a nice example of using NHibernate sessions. I have a history log, where each history record "belongs to" a service record. I have to treat this as a child-to-parent join, since some children are orphans. I wanted to use the FindAll(Criteria), but I wanted the option to have optional criteria, orders and aliases. My solution was to create an ARAlias class to represent an Associated Entity and an alias, and then build an ARBusinessBase class with the following method: public static T[] SlicedFindAll(int firstResult, int maxResults, Order[] orders, ARAlias[] aliases, params ICriterion[] criteria) { IList list = null; ISessionFactoryHolder holder = ActiveRecordMediator.GetSessionF...

What is an Active Record anyway?

While looking for Castle ActiveRecord examples, I found a blog by David Hayden - post 1 (design pattern overview) , post 2 (some details on a possible implementation) and post 3 (using the Castle Project). David had a series of articles about Rocky Lhotka's .NET Business Objects (one for each chapter), which I have also read and tried to apply. Don't get me wrong, Rocky's CSLA library works, but as of .Net 1.1 it required a great deal of code on my part. There are reasons why I and many others were investigating code generation tools. I have been studying Castle's ActiveRecord framework for a week and a half, and it is awesome for new development and clean data. During this discussion, remember that ActiveRecord mostly generates the XML mapping files for you and delegates the heavy lifting to NHibernate . If you have a legacy database with "sometimes there" keys, the framework will punish you severely. Below is an example of a legacy scenario that wi...

Finding database model information in Castle and ActiveRecord

Since this is my first post on the subject, let me introduce the Castle project - a .Net framework that I have been exploring recently. It has a project that tries to mimic ActiveRecord in Ruby-On-Rails, although the Castle version of ActiveRecord uses NHibernate. The good news is that an ActiveRecord class doesn't require any XML configuration or mapping files. The somewhat bad news is that you DO have to declare your classes and populate them with properties, private members and AR mapping attributes. Since I will probably use SQLServer 2000, I will just write a few queries to extract the data type and field list for each table that I want to support. More on this later. For today, I wanted to extract the class-to-database mapping information that Castle's ActiveRecords feeds to NHibernate. I finally looked through the source code a bit and after a blind alley (NHibernate.Cfg Configuration is mostly for database connections), I ran across the ActiveRecordModel class...

Table Functions in SQL Server 2000 and up

I recently added a customer tagging feature to our billing system. Users needed to be able to see a list of associated tags in their reports, so I needed to be able to collect all tags for a customer into a single string. Below is a rough approximation of the table schema. I did some research, and ended up creating a user-defined function (UDF) that returns a TABLE variable. Inside the UFD, I used a table variable, a cursor (which I wanted to avoid) and ran a query with the COALESCE() function for each customer. I was able to use my UDF as if it were a temporary table in my reports. Otherwise, I would have had to duplicate this code for each report. Before accepting this solution, I tried various flavors of selecting, updating, etc. but I couldn't find a solution without using a cursor. The code below is an untested generic adaptation of my working solution. TABLE: Customers Customer_ID INTEGER, CustomerName VARCHAR(50) TABLE: Membership Member_ID INTEGER, ...

The Rails Way

There is an excellent article on recommended coding practices "The Rails Way" . The authors are espousing a commonly held coding practice - use base classes or mix-ins (Modules in Ruby) only when code would otherwise be repeated. This reinforces the DRY (Don't Repeat Yourself) philosophy that Ruby encourages and facilitates. A helpful reader offers further explanation for those Rails programmers who have not dug deeply into Ruby yet at Artistic Coding . Ruby is such an amazing and flexible language, but my "Day Job" is .Net, so I can not wait for Ruby.Net , Ruby CLR (or maybe here ), or whatever works. Ruby.Net and Ruby 2.0 (see YARV ). Microsoft is working on IQF (Integrated Query Framework), which will try to offer .Net users the same ease-of-use and elegance that ActiveRecords offers (ActiveRecords is a part of the Rails framework, but can be used separately). If Mono also implements IQF, I will be interested. We shall see.

Online Gaming with Eternal Lands

Note: I wrote this entry while I was still playing Eternal Lands. I have recently stopped playing, but I thought I should share my thoughts anyway. For the past few months, I have been spending time in a free online role playing game - Eternal Lands . The game allows "player kill", but only on certain maps or certain areas, and you receive a warning message before you enter these areas. I haven't joined a guild yet, and the game doesn't force you to do so. All-in-all, I like it, although it could use a lot more quests (I'm working on submitting a few ideas ). I have a few tips for new players. First, concentrate on fighting and building up will at least to 12 first, then start building physique. Do not add to ANY OTHER attributes EVER. Only add pairs of "pick points" to attributes, as odd values are wasted. Don't take any negative perks until you have reached level 25. I would advise against taking any positive perks as well. I took the art...

Installing Ubuntu Dapper Drake

A fellow IT guy walked past my desk and asked about the 400 MHz Celeron box that I had tied to my switch box. I informed him that this was my Linux test box. He said that we have much better boxes than that in our storage room, and invited me to shop. I pulled out a Dell 4100 - with an Intel 815 chipset and ATI Rage 128 Pro graphics. I plan on installing this one as a true server - Apache, Ruby, Rails, MySQL 5.0, and SSH. Here's how I did it: 1. Make an install ISO of Dapper Drake Flight 3 . 2. Select [Install As Server] at the install menu. 3. modify inittab for 3 consoles (put a # in front of other consoles). 4. edit /etc/apt/sources.list to add universe and multiverse 5. apt-get update & apt-get upgrade 6. a. apt-get install openssh-server mysql-client mysql-server ruby apache2 b. apt-get install libapache2-mod-scgi unzip rdoc 7. edit etc/ssh/sshd_config to dis-allow root access, change port number, and AllowLogin for my user id. 8. upgrade linux kernel to latest 686 v...

SQL Quirks - count and sum

I was reminded of a little quirk of SQL recently, and I thought I would share it with my readers. I will be going over the SQL in a manner suitable for beginners, but the quirk that I refer to could catch an intermediate student of SQL. I will use some simple SQL to illustrate my point. -- 1. create temporary table CREATE TABLE #test1 ( id int not null , name varchar(50) not null ) ; -- 2. declare variables DECLARE @cnt int, @sum int ; -- 3. compute statistics SELECT @cnt = count(*) FROM #test1 ; SELECT @sum = sum(id) FROM #test1 ; -- 4. print results to query analyzer PRINT '@cnt = ' + ISNULL( CONVERT(varchar, @cnt), '[NULL]') ; PRINT '@sum = ' + ISNULL( CONVERT(varchar, @sum), '[NULL]') ; -- 5. cleanup DROP TABLE #test1 ; If you will paste this sequence into query analyzer, and run it in a non-production database, you will see that it produces: @cnt = 0 @sum = [NULL] In step 1, we create an empty table. In step 2, we declare two variables, and their...

AppTrain: A Rails Form Generator

If you've been thinking about trying out Ruby On Rails , you just have to check out the AppTrain project on Ruby Forge. There is a step-by-step guide to form generation as well. AppTrain is currently an alpha release, but it still looks quite handy. If you want to install it, use the quick-start . The AppTrain Project seems very committed to making it easy to build rails apps. First, they created a Table Generator , and now they are working on a form generator. That's all some simpler administrative apps need!

SQLServer2000: Capture output of EXEC statements

About 2 years ago, I discovered that I could capture the results of an EXEC (@string_variable) statement. We had a server process that logged in as System Admin (SA), so it could perform a directory scan, bulk insert any files it found, and move those files to a "processed" subdirectory. This process can be used to run any DOS command, and inserts each line of text as a record into a table. We still use this trick to log program responses to a journal table. When reading the sample code, notice that we are using two single-quote characters back-to-back to cause a single quoted argument to be sent to the xp_cmdshell extended command (really a registered DLL accessible only to SA). Here's an example: DECLARE @s varchar(255); CREATE TABLE #cmdOutput(Line varchar(200)) ; SET @s='INSERT INTO #cmdOutput EXEC master.dbo.xp_cmdshell ''dir c:\temp\*.txt /b'''; EXEC (@s) ; SELECT * FROM #cmdOutput WHERE Line IS NOT NULL; DROP TABLE #cmdOutput; A word of c...

Use and Abuse of XML

This may be considered a rant by some, but here goes. XML is a data structure which is easily parsed by 3rd party applications. This does not mean that the data can be easily interpreted as information, that is the responsibility of the XML data designer. Depending on your audience, you may have to spell out units (pounds, kilograms, minutes, seconds, etc.). Also, XML IS NOT FAST. Do not use XML between internal components in your application if at all possible. Thank you for your attention.

How to flag spam on BlogSpot

I like Blogger.com, but it has a fair amount of blog-spam. I was searching on the keyword "blacksmith" today, and ran across a number of spammer pages, with javascript that removed the "Flag Content as Objectionable" button. If you are a programmer, you can easily flag these pages anyway, by viewing the HTML source, and searching for "flag". Look for var ID = 1234567; and surf to http://www.blogger.com/flag-blog.g?nav=1&toFlag= ID_GOES_HERE to help Blogger find these nasties. Once you've done it a few times, it doesn't take long at all.

How does Rails scaffolding select HTML input tags?

Recently, a reader saw my fix for SQL Server booleans, and asked me a followup question: why does Rails display a yes/no selection instead of a checkbox? The short answer is look in {RUBY_HOME} /lib/ruby/gems/1.8 /gems/actionpack-1.10.2 /lib/action_view/helpers, but your path may vary depending on whether you are using gem, "edge rails", etc. Anyway, look in the file "active_record_helper.rb" for a method called "all_input_tags", and notice that it calls "default_input_block" if you don't supply an input_block. Now notice that "default_input_block" creates a label and calls "input(record, column.name)" which in turn calls "InstanceTag#to_tag" which finally looks at the datatype and maps boolean to a select tag. Perhaps a wiser Rails explorer can provide us with the rationale for this, but I guess we could add a MixIn for InstanceTag that redefines the to_tag() method, or just do a dirty and unmaintainable hack l...

Data warehousing - finally!

I finally helped some friends set up a data cube. It mostly involved creating a specialized view that had one numeric field (in our case - a count), a date field, and a number of categorical "dimension" fields. We used Seagate Info , now from Business Objects to actually work with the data. So no, I didn't write my own data cube explorer. The client couldn't afford that, and I didn't really want to do it. What did I do? I took a conglomerated (non-normalized) survey table and built a set of relational tables for surveys, questions, answers, results (the actual answers), etc. Then I wrote an "importer" to smartly read the "scanned surveys" table and populate my relational data set with any "new" surveys, based on an "IsProcessed" flag, and whether that ScannedSurvey_ID existed in the relational table collection. This allows their OCR process to add records at any time, and a daily process runs under SQL Agent to import new s...

Planning for failure - embedded systems

Here's a short story about total system design, and planning for failure. Last friday night, I was filling up my truck with gas, and I did the "pay at the pump" thing, using my credit card. After I filled my tank, the pump's display helpfully asked "Do you want a receipt? Yes / No". I usually say no, but I wanted to remember the price per gallon, so I said "Yes." Within 2 seconds, it said "Printer Not Available." Why did they offer to print a receipt if the printer is not working? A more helpful system would have simply stated "See attendant if you want a receipt," knowing that the printer was down. To make this system more effective, they would have to: a) know when the attendant had replaced the paper roll b) remember that last time they tried to print, it didn't work. c) reset the printer state when the attendant did a test print, or closed the access panel. Perhaps cost was the prohibitive factor.

SSPI connection for Rails SQLServer connection adapter

Recently, I added support for SSPI (a connection that uses your local user account to authenticate you on the database server without supplying another password). To use an SSPI connection, do not supply a username or a password. sqlserver_adapter.rb: username = config[:username] ? config[:username].to_s : '' ... # Use integrated security if config[:integrated] or (username == '' and password == '') conn = DBI.connect("DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};Integrated Security=SSPI;") else conn = DBI.connect("DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};User Id=#{username};Password=#{password};") end

Rails connection_adapter SqlServer fix for boolean fields

I tried to use Ruby on Rails with Microsoft SQLServer recently, and I kept having problems with my "bit" fields. Looking on the web, the Rails repository claims to have fixed this, so I downloaded the new adapter from svn. It didn't work for me, so I started hacking. I really need to send this as a diff to the ActiveRecord maintainers, with test code to prove that it works. By the way, I am using ADO mode ... maybe their original code works for ODBC mode. I suspect that tests should be run for both modes. Anyway, enough chatter, here is the fix! In file sqlserver_adapter.rb, add the #cast_to_bit and modify #quote as following: def cast_to_bit(value) case value when nil then nil when '1' then 1 when '0' then 0 when 1 then 1 when 0 then 0 when 'true' then 1 when 'false' then 0 else # puts "Invalid Boolean #{value}" 0 end end def qu...

.Net Strings - LastIndexOf in c#

Today, I tried to use string methods IndexOf and LastIndexOf to parse a directory entry string that I received across an SSH channel. I guess I should have read the method description closer, but I expected LastIndexOf to find the last space character in a string. The signature I wanted to use is: public int LastIndexOf(char, int, int); Reports the index position of the last occurrence of a specified String within this instance. The search starts at a specified character position and examines a specified number of character positions. It turns out that LastIndex will decrement it's starting index for "count" characters. Here was my incorrect code: private void method () { string dirEntry = "-rwxr-xr-x 1 abcde fghij 44458 Feb 22 1987 prt01.txt"; int m, n; // I thought this would scan from 32 to 44 and find the last space char m = dirEntry.LastIndexOf(' ', 32, 12); // it returned 32 // code that does what I w...

Upgrading Ubuntu from Hoary to Breezy

For those of you still running Ubuntu Hoary, I have upgraded 2 computers to Breezy now, and I like the improvements just fine. There is a more up-to-date software selection, including PHP5, Ruby, Rails, Mono 1.8.x and MonoDevelop. If you want to pull from Packages.DotDeb.Org , you can even try out MySQL 5.x. I haven't done that yet, but I have found some instructions on the Ubuntu Forums that I plan to use as a guideline. When you upgrade, you may have to stop your email server . I had to on both computers. If you want an easier installation, there are Breezy Colony CDs that you can download with BitTorrent. I haven't done that yet, but if I install on another PC before Breezy goes live, I will try it. Best of luck to my fellow ubuntu-ites.