David Beroff (d4b) wrote,
David Beroff
d4b

Sometimes it's the simple things that make me happy

I'm up to about ten tables in the AboutTh.is database, and now I'm starting to flesh out the details of their layout. I don't like having unnamed magic numbers floating about, so I tried the following approach to documenting constant values using PHP right in my database design document:
<?php

// Comment
$DB_NAME = '762775_aboutthis';
$COUNTER_BYTE_SIZE = 4;
$LEN_LONG_URL = 10;

echo <<<EOT
<!doctype html><html><body><pre>
USE $DB_NAME;

CREATE Table Test (
# Comment
TestID int($COUNTER_BYTE_SIZE) unsigned NOT NULL auto_increment PRIMARY KEY,
Second varchar($LEN_LONG_URL) NOT NULL
) ENGINE = InnoDB, COMMENT = 'Test table';
</pre></body></html>
EOT;
?>

This generates the following MySQL code that I can copy/paste right into phpMyAdmin:
USE 762775_aboutthis;

CREATE Table Test (
# Comment
TestID int(4) unsigned NOT NULL auto_increment PRIMARY KEY,
Second varchar(10) NOT NULL
) ENGINE = InnoDB, COMMENT = 'Test table';

Simplistic, but I'm happy. (I didn't use actual PHP constants because they won't expand in a heredoc.) I guess I could use MySQL's user-defined variables, but they seem a bit kludgy, and don't seem to mentally "parse" as easily as this approach.
Tags: mtat, webdev
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments