Skip to main content

Posts

The pains of browser page refresh

I like to play Chess sometimes, and I like the MKGI Chess Club . It is great for guys like me who do not have time to play a complete game in one sitting. The site uses a "postal chess" model, where I can play a move in several games, then wait for my opponent to move, or go on with my day, and check later or the next day. The site has an unfortunate quirk. It allows you to write notes about the game, but if it is the opponent's turn, the game page has a 30 second refresh. It is <meta http-equiv="refresh" content="30" >. I tried deleting this from the page using Grease Monkey (an HTML rewriting tool for FireFox), but even after I remove the element, the refresh occurs. So finally, I go into FireFox's about:config and type refresh in the finder. There is "accessibility.blockautorefresh"! So now, I get a notice that the page wants to refresh, instead of losing my notes!

Python 2.6 beta 1 and 3.0 beta 1

Python is sticking pretty close to their release schedule for Python 2.6 and 3.0 as documented in PEP 361 . They released Python 2.6 beta 1 and 3.0 beta 1 today. If you&apos're curious about their progress, check the 2.6 release notes . By the way, I noticed that they fixed a problem with one of the rotating log handlers - it was using a simple filename instead of the full file path when deleting old log files. I have been wanting to check my code in both 2.6 and 3.0 latest releases. Now that it is in beta, I should be able to get a pretty good idea of what has changed. All I want is for it to "feel" like python.

Monitoring SQL Server on the cheap

Use WMI with Python . Thanks to Tim Golden , Python on Win32 (I use ActiveState Python ) can use COM to talk to WMI. WMI allows you to run SQL-like queries to determine available memory, free disk space, database size, transaction log size, transaction log space available, when a database was last backed up, ... all of the good stuff. Any DBA knows that databases need free disk space and frequent backups. I don't want to imply that this is the only work a DBA would perform, but I have worked at shops where there really wasn't a DBA in our department. My programs occasionally failed because there was no disk space, and the transaction logs had not been backed up for a few weeks (sure, the database was backed up, but not the logs). If you don't want to use WMI, SqlServerCentral has some great articles for SQL 2000 and 2005. Give them time and they will have 2008 as well. If you want to to check a backup, try this .

Custom Python Logging Handler

Python has some nice basic logging facilities, but I really wanted a rotating logger that would rotate on each execution. I tried the obvious - just specifying class=MyModule.MyClassName, but I received a NameError exception for my trouble. After some hacking on /python25/lib/logging (a big no-no!), I did another google search and discovered that I could just import the class in my application, and set a reference to my custom class. Hard-coding a logging handler doesn't feel very dynamic, but at least I did't have to hack any more. from MyCustomHandlers import SessionRotatingFileHandler logging.SessionRotatingFileHandler = SessionRotatingFileHandler logfile_name = "test.l4p" logging.config.fileConfig(logfile_name) This allows me to place my custom log handler in my application directory. I still need to figure out how to add things to site-packages.

Perl or Python?

Recently at work, we have been discussing the question of Perl versus Python for file copy, ftp, email and other daily, weekly or monthly automation / integration tasks. We considered Perl because some existing tasks had already been written in it, and Perl has a reputation as the System Admin's language of choice. I took a task that needed automated and wrote it in Perl 5.8, then refined it using CPAN (sweet CPAN) for ArgvFile, Log4Perl, Carp, and Date::Calc. I find Perl to be very functional and completely able to meet my needs, but very quirky or perhaps unsettling. You see, I have been an object oriented programmer for a while now, and Perl's class system just feels "bolted on" to me. And I really had to jump through hoops to do a try/catch, but using eval with a return flag and an END block to test the return worked acceptably. I didn't test the exception module, as I did not discover it in time. Next I tried Python 2.5. After a quick brush-up on the...

Moving to NHibernate 2.0

NHibernate 2.0 is not even close to release, but it is shaping up nicely. Oren Eini (His blog is called Ayende @ Rahien) has a list of interesting new features . Dario Quintana has something to say as well . He has contributed some code to the uNHAddins for Active Record Detached Query support . The Castle development team is considering pointing to NHibernate Trunk (which will be released as 2.0 eventually) so they/we can take advantage of the new features. If Castle Active Record uses NHibernate trunk, I would advise you to be diligent wit! your test code, and don't forget to run the Castle test code too. You can track Castle Project changes on FishEye .

Mono and ProMesh.NET

I follow the Mono Blog aggregator , and Miguel de Icaza mentioned that a user had tested ProMesh.NET on Mono , and it ran without any changes! That in in itself was cool, but when I took a look at ProMesh.Net , it looked like a well designed framework, perhaps comparable to MonoRail . The framework is ORM agnostic, and the biggest weakness might be it's view templates, but they have a great tutorial , so judge for yourself!

My take on Enums in SQL Server

I am a registered user on SQL Server Central , and I read a good article there on " Enums in SQL Server ". I thought that I would quickly present my own humble solution to this problem, although I do not mean to imply that my way is better - you can decide for yourself. First, the problem to be solved is to represent a limited set of choices, where new choices would typically require a developer's intervention to implement the business logic. An example would be a list of task priorities in a to-do list. CREATE TABLE Priorities( ID tinyint not null identity(1,1) PRIMARY KEY, Code char(3) not null unique, Priority varchar(20) not null ) ; INSERT INTO Priorities (Code, Priority) VALUES ('911', 'Emergency') ; INSERT INTO Priorities (Code, Priority) VALUES ('HIG', 'High') ; INSERT INTO Priorities (Code, Priority) VALUES ('NOR', 'Normal') ; INSERT INTO Priorities (Code, Priority) VALUES ('LOW', 'Low') ; IN...

Auditing a SQL Server database

I do not believe that I have covered this before, but the Active Record Code Generator can also generate audit records for your database tables. The template that I wrote ( SqlAuditTrigger.vm ) adds records to an AuditChanges table, with a related AuditFields table containing the table and field name. I think that this is a fairly compact solution, but I also added code to the trigger to ignore SA (system administrator) changes. This may not be appropriate for your application, but it is easily removed. I also have a "TODO" item to detect related lookup tables (really ENUMs) and translate IDs to Names, so that changes to the FavoriteColor_ID field would look at the FavoriteColor table, and instead of recording FavoriteColor_ID changed from 2 to 5, it would say changed from "Ruby Red" to "Passionate Purple". The reason that I did not actually implement it is that I do not have enough information. My code generation schema does not list all of the f...

I love Bouncy Castle!

Today, I needed to send some private data to another company, and I could not get FTP with SSL to authenticate over our firewall, so I had to sent an encrypted file over FTP. The other company has a GPG public key, so I looked around for a bit on the web (yes, I googled c# gpg encrypt open source) and found a message that mentioned Bouncy Castle on the 4th or 5th page (why was it so far down the list???). I then googled Bouncy Castle to finally find that they have a wonderful c# implementation of GPG encryption (and many others), and their test code includes an example that reads a file and writes an encrypted file. This made integration VERY simple. I did move their test folder into their test project, so I would not be deploying all of the test code to production.

What happened to VS2005 Data Pro Power Tools?

I was reading a blog on MSDN, with a link to some cool Power Toys for Visual Studio 2005 Team Edition for Database Professionals. So today, I had some time to install the VS2005 TE Service Release 1 so I could grab the power toys, but the microsoft link is dead! The developer went on a vacation (literally), so it might be a while until this gets fixed. Oh, by the way, O’Reilly Hacking Visual Studio mentions a cool documentation tool called GhostDoc . It looks slick! Update: Here is the link , thanks to Michael!

NHibernate hbm xml template

Friday, I added an NHibernate mapping.hbm.xml template. I mainly wanted to show that the code generator can be used for other purposes than just Castle ActiveRecord, and NHibernate is an easy target. Jim R. has requested that I add HasMany (aka ManyToOne) mapping properties and attributes. I think this is going to push me to refactor the schema extraction code. Some of my future goals for the application are: 1. Full Model Generation (list of children, whether directly or indirectly through a many-to-many table) 2. User controllable plural-to-singular mapping. 3. Multi-database, using the Database Provider Factory. 4. Refactor code generation into a library (as I do with all projects) 5. Command-line code generation (supply at least a template and a list of tables) 6. Templates for other ORM tools (pure NHibernate, Rocky Lhotka's CSLA Business Objects, etc.)

Improved Model generation in Code Generator

I have improved the Inequality test in the model template, as requested by JimR . This was another case where it worked well for me, only because I followed the convention that primary keys are named ID, and foreign keys are named SingularOfTable_ID. I know that I should really push the Inequality test into the template, but for now, I have corrected it in the DbFieldInfo class.

Features of the Code Generator

I just updated my code generator to optionally generate validation attributes. This simple change includes App.config file entries for all check boxes, and a new checkbox for "Validation" - aka validation attibute generation. While I was making this change, I realized that I really need to pass a CodeGenerationContext object to the DbTable, DbField and ModelGenerator classes. The requester can populate the context, and pass it to the code generator. Anyway, enough about the code, let's talk about the templates. I made a simple template this weekend to generate a DataGridView column array, suitable for databinding. I'm sure my new template will need some tweaks to handle Foreign Keys better (it currently just displays them as TextBox). Let's look at a template. ##FILENAME:PR_${table.GetClassName()}_Insert.sql ## ## Generate a SQL stored procedure to insert a record into the ## specified table and return the newly created primary key ## ## FUTURE: The generat...

ActiveRecord Code Generator

Thanks to some recent interest in the community, I have made a few more changes to ActiveRecord Code Generator . First, I added a browse button and a dialog, using some code supplied by Jim R . I don't care for the folder browser dialog, but it is built in to .Net 2, so I use it. Second, I fixed a few bugs that crop up when a table does not match my arbitrary table and field naming scheme. This project was written quickly as a pre-requisite to a large project, and it shows, but it is steadily improving. Evening Update: I added a POCO (Plain Old C# Object) template as a starting point for an NHibernate cs and xbm template set.

Updated ActiveRecord Code Generator

Today, I updated the ActiveRecord Code Generator a bit. I checked in changes to use primary and foreign key details from INFORMATION_SCHEMA. The original code used naming conventions to decide what various fields were used for - ID = Primary Key, Field_ID = Foreign Key to table Fields. If you want to use naming conventions, let me know and I can add a setting in App.Config to allow this (along with any "real" key constraints).

Hacking FireFox 2 Menus

Today, I was inspired to hack the context menu in Firefox 2.0, removing "Send To" and "Send Image". I googled for a solution, and found step-by-step directions for Firefox 1.0 . So, I started digging, and the over-all instructions were good, but I only needed to comment out the menu options in browser.xul, and not touch the javascript in browser.js. The original instructions were a bit vague, but in the end, it worked!

Castle Active Record Code Generator

I have finally released my Code Generator to Google Code as Active-Record-Gen . What does it generate? It generates ActiveRecord classes mainly, but I have used it to generate stored procedures and sys-admin scripts as well. This code generator does not (yet) generate a full Windows application project or a Mono-Rail web site, but the generated code could be used in either. In fact, with a few tweaks, this could be used to generate NHibernate "poco" and .xbm files. If you want to know more, look at the screen shots above, or head over to Google Code and run it. In my haste to make my first EXE release before supper, I forgot to add the Template directory, which should be at the same directory level as the EXE and config files. I just (1.5 hours later) uploaded a new EXE, but 2 people have already downloaded the EXE (not the source though). As for the basic table object, it is built with the following assumptions: Table name is plural, class name is singular. Field ...

Castle ActiveRecord class with stored procedures

Update 05/22/2007: No response from the community. I think Hammett is working on releasing a new version of Castle (It's much more than just ActiveRecord!), so I have not received any feedback on my patch. I have just posted patch AR-156 to Castle's ActiveRecord project to add support for XML free mapping of insert, update and delete stored procedures to ActiveRecord classes. This requires a bit of work, since the stored procedures must match the field order generated by NHibernate , rather than being controlled by the application developer. This means that we have to create the class normally, run it through a test cycle with logging turned on, capture the prepared statement's field order, and then re-write the stored procedure to match what NHibernate wants. This is because behind the scenes the stored procedure replaces a prepared sql statement that NHibernate would otherwise be using. This will be a mess to maintain until or unless someone contributes a patch to ...

Reflector, Doubler and Unit Testing

Update: the latest versions of Reflector and Doubler are working well together. I just noticed that Jay Flowers has released a version of the Doubler Add-In that works with the current Reflector by Lutz Roeder . I tried installing Doubler in a sub-directory under Reflector, but it failed to load, so I had to move it into the Reflector directory. The current version of Doubler generates test stubs with a mis-spelled attribute (Memeber instead of Member), but it will generate test stubs, interfaces, recording wrappers, mock objects, etc. It seems like it will be quite handy, with support for NUnit, MBUnit, etc. although Doubler forgets all of your settings every time you move to a new class. That is almost a deal breaker there, but at least you can select an assembly and generate all tests at once. I have to change the output language to c#, change the output path (or just build where the tool wants to go, and copy it), change the default test framework, and set "WithTestAtt...