All posts by Chui

Model Driven Development – The MetaModel

A good friend showed me a code generator that their company had whipped up over a couple of days, and I was intrigued by the approach. It is reasonably straightforward, and I thought there must be more to this and there was!

Kathleen Dollard’s Pluralsight course

Kathleen has a very good course on Understanding Metaprogramming. This podcast on Visual Studio retooling from TextTemplatingToolkit and Custom Code Generator to Roslyn Code Generation.

Hundreds of million of dollars have been sunk into code generation.

Tailored Code Generators

Presentation

Tool: Essential Key take aways:

  • Generation of Sample Data
  • The templating should be clean and easy to grasp
  • Model checker
  • Composable – partial model definition, extensibility
  • Naming policy
  • Consistent UI Patterns – Master-Detail, Filter, Collection of objects, Navigation, Command, Selection, Task
  • Conceptual UI Patterns – Login, Instance, Population, Wizard, Filter http://pjmolina.com/cuip
    • Filter
    • Order criteria
    • Display set
    • Actions
    • Navigation
    • Rendering – Grid
    • Table
    • List

The real advantage:

  • Technology Forward Escape

The guy who wrote this presentation also runs radarc which is an impressive suite.

Strongly-typed metadata

Kathleen Dollard has a post and some sample code like Strongly Typed Metadata Classes. There’s a finite amount of metadata. By drawing a line between metadata and software, it works around having bad architecture.

Learning Cobol Round #2

COBOL programs are very similar to FORTRAN ones. Arguments are passed via a DATA DIVISION block. Local variables are declared in a WORKING STORAGE section in the DATA DIVISION.

Interestingly, the variables can have limited range as defined by a string format. For example:

01 THE-NUMBER    PICTURE IS 99.

means the variable THE-NUMBER can hold any numbers from 0-99. (maximum length is 18 digits)

Length of alphanumeric variables are declared using letter X. (maximum length dependent on implementation)

01 THE-MESSAGE PICTURE IS XXXXXXXXXX.
02 NEW-MESSAGE PIC X(10).

One can also declare variables using a shorthand that is reminiscent of SQL (see NEW-MESSAGE)

Formulas are declared using COMPUTE.

COMPUTE THE-RESULT = FIRST-NUMBER + SECOND-NUMBER.

Assignment is performed using the MOVE verb. For example MOVE "Samuel" TO FIRST-NAME.. Interestingly, mathematical operations are done in-place. For example:

 MOVE 5 TO THE-NUMBER.
 MULTIPLY 10 BY THE-NUMBER.
 ADD 1 TO THE-NUMBER.
*GIVES 51

Alternatively, MULTIPLY 10 BY THE-NUMBER GIVING THE-NUMBER. is probably the clearest.

GOSUBs

COBOL has a basic idea of jumping to subroutines. Labels are simply declared without any keywords. The STOP-RUN is used to prevent the program from flowing into the subroutine area.

       PROGRAM-BEGIN.      
       
           PERFORM SAY-HELLO.
           PERFORM SAY-HELLO.
       
       PROGRAM-DONE.
       
           STOP RUN.
       
       SAY-HELLO.
       
           DISPLAY "Hello World"
       

Learning Cobol Round #1

My mum learnt COBOL and FORTRAN when she was in university back in the 60s. Although I have used FORTRAN77 in a professional capacity, I still haven’t had the opportunity to try COBOL.

Since being retro is the harbinger of cool, I’ll spend the next few articles blogging about my experience working with COBOL.

Visual Cobol

There is a free non-commercial use license granted by Visual Cobol. This is sufficient for my learning purposes.

In terms of resources, Teach yourself COBOL is 21 days seems to be a good starting point.

The download is a little tricky. After receiving the confirmation email, I was presented with a screen but the download link wasn’t apparent. It’s tucked under the Software/Licenses tab (see red arrow). The download was 417MB, and I was a little concerned I’ll not have much space left on my SSD, which is burgeoning with several virtual machines. However, the promise of the familiarity of Visual Studio and having access to the .NET framework beckons me to try it out.

In comparison, the OPENCOBOL 1.1 source download is 1 Mb. That’s very inviting, although I don’t really like the idea of building GNU autoconf project on Windows.  There’s never a nice ending.  I found prebuilt-binaries for OPENCOBOL on Windows, and given the download size of 7.3 Mb,  I downloaded it as well.

Upon creating the first COBOL project with Visual Studio, I was prompted for a license key. It wasn’t immediately apparent, but all I had to do was to provide the email address I had used to register with Microfocus, and that activated my free copy automatically.

Running Hello World from a Visual Cobol Console project was simple enough.

       identification division.
       program-id. Program1.

       environment division.
       configuration section.

       data division.
       working-storage section.

       procedure division.

       PROGRAM-BEGIN.      
       DISPLAY "Hello World".
       PROGRAM-DONE.
       
       end program Program1.

OpenCobol

OpenCobol was straightforward. Unzip the 64bit binaries to C:\OpenCobol, and run vcvars64.bat to set up the path to Visual C (OpenCobol translates COBOL to C and then uses the platform compiler to build executables). The command below builds hello.exe

cobc -x hello.cob

Project scope

In terms of project, I’d like to try my standard project, involving authorization, data validation and persistence (both NoSQL and SQL). I’m mindful that my projects will be un-COBOL-like, but that can be refined with time.

Next article – Round #2

Using Goose with MySQL

goose is a Database Migration manager written in Golang. (There’s another migration tool in Python called goose as well).

The documentation is a bit thin on the ground at present, and required a bit of code-reading. Conceptually, it is simple and easy to grok. Goose generates a series of timestamped migration files, which contains either raw SQL or go code, which can then execute Raw SQL statements. For better or worse, there is no ORM involved.

Here’s the basic configuration for connecting to mysql databases. (Note the driver is mymysql not mysql)

#file: db/dbconf.yml
development:
  driver: mymysql
  open: DBNAME/USERNAME/PASSWORD

For completeness, the ziutek mysql driver supports all the following connection strings.

DBNAME/USER/PASSWD
unix:SOCKPATH*DBNAME/USER/PASSWD
unix:SOCKPATH,OPTIONS*DBNAME/USER/PASSWD
tcp:ADDR*DBNAME/USER/PASSWD
tcp:ADDR,OPTIONS*DBNAME/USER/PASSWD
cloudsql:INSTANCE*DBNAME/USER/PASSWD

The simplest way to write the migration is using SQL (since I didn’t know much Go at all)

goose create SampleTable sql

in the db/migrations/20140601001151_SampleTable.sql we can modify it to the following

-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied

CREATE TABLE sample
(id int primary key auto_increment,
 description varchar(50));

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

DROP TABLE sample;

Then, it’s a matter of running

goose up

to migrate to the latest version

Working with 4D database via ODBC

There are some peculiarities when working with 4D databases and .NET

Date time 0/00/0 12:00

0/00/0 12:00 is valid in ODBC, but not valid in .NET.
Recommendation – wrap all date calls with a CAST
e.g.
SELECT CAST(BIRTHDAY AS VARCHAR) AS BIRTHDAY

alternatively, use a CASE statement to convert this call to something palatable to 4D e.g.

CASE CAST(LASTSEENDATE AS VARCHAR) WHEN '0/00/0 12:00:00' THEN CAST('1/1/1970' AS TIMESTAMP) ELSE LASTSEENDATE END AS LASTSEENDATE

Other alternatives simply don’t work. e.g. ... THEN NULL ELSE ... and NULLIF()

Saw this in my email today wizpert – billed as “is a fast growing community of experts, where users seek advice and coaching on a range of programming languages, including Python, HTML, PHP, JavaScript and more.

Bookmarked!

Powershell equivalent of xargs

I wanted to be able to build extmap.xml for all the files that were in my packages\ directory. I had a Powershell script called ToExtMap which automated most of my tasks, and a little experimentation with Powershell gave me this one liner:


PS C:\Users\Chui\Projects\SAM-GET\packages>
Get-ChildItem -Filter *.dll -Recurse -name |
%{ ToExtMap $_ | Out-File ($_ -replace "dll", "extmap.xml") -encoding ASCII}

A bit of explanation might be in order:

  1. Get-ChildItem -Recurse finds all the files I require.
  2. The %{} is the shortcut for the foreach operator.
  3. Powershell is object oriented. $_ -replace ... executes a string substitution