Tuesday, January 20, 2009

Evil Access

I was thinking today about database APIs when inspiration struck. I ended up hacking out the following class, which I think demonstrates a rather interesting approach to interfacing with a database (interesting enough at least to post here).
class DBQuery implements Iterator
{
protected $_db;
protected $_query;
protected $_result;
protected $_index;
protected $_num_rows;

public function __construct($host, $dbname, $username,
$password) {
$this->_db = new PDO("mysql:dbname=$dbname;host=$host",
$username, $password);
}

public function __get($query) {
$this->_query = $query;
$this->_result = $this->_db->query($query);
return $this->_num_rows = $this->_result->rowCount();
}

public function quote($value) {
return PDO::quote($value);
}

public function __call($query, $values) {
$this->_query = $query;
$this->_result = $this->_db->prepare($this->_query);
$this->_result->execute($values[0]);
return $this->_num_rows = $this->_result->rowCount();
}

public function clear() {
$this->_index = 0;
$this->_num_rows = 0;
$this->_query = '';
$this->_result->closeCursor();
}

public function rewind() {
$this->_index = 0;
}

public function current() {
return $this->_result->fetch(PDO::FETCH_ASSOC,
PDO::FETCH_ORI_ABS, $this->_index);
}

public function key() {
return $this->_index;
}

public function next() {
$this->_index++;
}

public function valid() {
return ($this->_index < $this->_num_rows);
}

public function __toString() {
return $this->_query;
}
}
DBQuery isn't your typical database access class. In fact, I would suggest it's slightly evil since it distorts traditional PHP syntax by abusing taking advantage of three specific PHP features.
  1. PHP allows special characters in an identifier if the string is quoted and is enclosed by {}. So, $myValue and ${"my value"} are both equally valid variable identifiers.

  2. The magic overloading methods allow you handle undefined properties and methods in your class. Specifically, I've made use of __get() and __call().

  3. A class that implements the Iterator interface can be traversed using a foreach loop.
Here's a look at how DBQuery would be used:
// connect to the database
$dbq = new DBQuery("localhost", "test", "dbuser",
"dbpassword");

// query the database if the user is authorized
$username = "administrator";
$password = sha1("password");
if (!$dbq->{"SELECT * FROM admin_user WHERE username=? " .
"AND password=?"}(array($username, $password))) {
die("Unauthorized.");
}

// query the database and display some records
$dbq->{"SELECT id, first_name, last_name FROM employee"};
foreach ($dbq as $result) {
print_r($result);
}

// casting the object as a string yields the query string
echo "Query: $dbq";
Don't try this at home, though, my friends. Just because you can write code like this doesn't mean you should.

Wednesday, January 14, 2009

Motivational Posters

I've been having a hell of a time fighting with some Perl code at work lately. I tossed together a humorous Perl demotivational poster to release some of my frustration, and thought others might appreciate it.

But why stop there? There are other languages worth making fun of besides Perl! So, I tossed together some more snarky posters to be fair.

Sunday, January 4, 2009

7 Things You Didn't Care to Know About Me

I've been tagged by Chris Cornutt and Jeff Jones to share some things you probably don't know about me. (It's nice to see chain mails flow through the blogosphere too, eh?) So, here's seven little-known facts about yours truly...
  1. My first career choice was to be a diocesan priest, and I briefly attended Seminary after I graduated from high school. My family isn't overly religious, and my parents didn't push me to go; I think I just like to help people and felt drawn to the rich history of the Roman Catholic Church. Life has a funny way of playing out differently than we intend for it, doesn't it?
  2. After I left seminary, I attended a state university and majored in elementary education with a concentration in music history and literature. I'm not overly fond of children, though, so I didn't complete the program. I would only need to take two more classes to be a state-certified elementary teacher.
  3. I studied French for 5 years in high school, a year of Latin in the seminary, and then learned Esperanto independently.
  4. Someday I'd like to run for Governor of New York, though I'm certain nobody would vote for me. My biggest push would be to reform of the state's tax laws, though I'd probably legalize prostitution... people are going to do it anyway so the state might as well make money off it.
  5. I try to make sure I have a bunch of $1 bills in my pocket when I go to parties; you never know what you can pay a drunk person to do for your own personal amusement. I once had a guy prancing about in a blue speedo for $5. Another time, the same guy chewed on a ball of tinfoil for 30 seconds for $1. I think that officially makes me an asshole.
  6. I crawled on top of the dining room table when I was a baby, fell off, and tore the frenulum under my tongue. I didn't gain any super-human abilities with my tongue... just reduced frenula tissue.
  7. I don't use shaving cream. I would inevitably get it in an ear or up a nostril and it would bother me. Instead, I shave in the shower after the warm water has softened my skin and use a disposable razor that has an aloe strip.
So there you have it. I'm supposed to tag other people and have them share seven things about themselves as well, though everyone to whom I would have sent it has already participated. Sorry... That's all, folks!

If you're curious about the deep, dark secrets of others in the PHP Community, you may want to checkout the list Cornutt compiled.

Thursday, January 1, 2009

Happy New Year

I've never been one to make New Year's resolutions. If I really want to change something about myself or my life, why what until January 1st to set goals for myself? It doesn't matter to me if it's January 1st, April 15th, July 4th, or December 26th... I just set goals and go for them!

Instead, I prefer to choose an over-reaching theme for the year. This suits me better because there's no success or failure benchmarks to meet; it's something I keep in mind to help guide me as I tackle the challenges and opportunities that come my way.

2006 was my "Year of Adventure."

2007 was my "Year of Change."

2008 was my "Year of Success."

What will 2009 be?

I've chosen to designate 2009 as a "Year of Balance" for myself. Like everyone, there's good things and bad things in my life; putting the balance back into my life again will empower me to better appreciate the good and deal with the bad more productively.

So, it was very fitting the other day when I saw this beautiful video featured on the Astronomy Picture of the Day website. The site's goal is to present images, videos and descriptions to inspire people to learn more about astronomy, though I found this particular video inspiring in a different way.

In the short 4-minutes or so that it took to watch the video, I found myself pondering the beauty of nature that surrounds us, and how sad it is that so many of us never appreciate it because we're too busy going about our daily lives. I then found myself thinking about what things in life are truly important, and what I should focus on as I live my life.

Each day I will work and be productive, I will play and relax, I will laugh, love, and enjoy the company of friends, and I will find new things in my life deserving of my appreciation.



Happy New Year; I wish you and all your loved ones the best in 2009.