My own database abstraction class

Back in 2006 July, I decided that all other database classes are not worth it, and created my own one, incorporating best features from MySQL and PHP world. It resulted in this brilliant code, which I showed to few colleagues, and got such quote:

i like your nonframework. it gives a fuzzy feeling to the poor souls that think they need an abstraction layer. — Kristian Köhntopp

This was written using TIC pattern and can be used in variety of applications:

class MyDB {
  var $conn = null;

  function MyDB($database=null,$user='root',
                $password='',$host='localhost') {
    $this->conn=mysql_connect($host,$user,$password) and
    $database?mysql_select_db($database, $this->conn):null;
  }

  function _escape($s) {
    return mysql_real_escape_string($s,$this->conn);
  }

  function _quote($s) {
    return "'" . $this->_escape($s) . "'";
  }

  function __call($method,$arguments) {
    $query=preg_replace_callback('([A-Z]|\d+)',
      create_function('$s',
         'return " ".strtolower($s[0]);'),
         $method);
    $query=str_replace(' everything ',' * ',$query);
    $first=array_shift($arguments);
    if ($first) {
      if (is_array($first)) {
        $query .= ' (' . implode(',',
                            array_map(array(&$this,'_escape'),
                            $first)) . ') ';
      } else {
        while($argument = array_shift($arguments)) {
          $first = preg_replace('/\?/',
                     $this->_quote($argument),$first,1);
        }
        $query .= $first;
      }
    }
    $ret=array();
    $res=mysql_query($query,$this->conn);
    if (!$res) { print mysql_error(); exit(); }
    while($row=mysql_fetch_assoc($res)) {
      $ret[]=$row;
    }
    return $ret;

  }
}

$x = new MyDB('test');
$x->selectEverythingFromMytableWhereIdIn(" (?,?,?) ","a'b",2,3);
$x->SelectBlahFromMytableWhereIdIn(array(1,2,3));
$x->InsertIntoBlahValues(array(1,2,3));
$x->truncateBlah();

Now I wonder where I should build community for this, Google Code or Sourceforge? Or should that be the darling MySQL Forge.

As I started digging my stuff from history, I also managed to upload my past six month pictures to flickr too – including Vienna, Taipei and few other trips.

A perfect christmas story

This made me laugh. Providers were fighting who will give better ‘free sms’ plans. Then my GSM provider decided that they have enough resources for even better campaign – they offered adding 0.02 LTL to account balance for every SMS received. So, smart kids saw business opportunity – they started spamming SMS messages from one phone (free SMS!) to another (get paid for SMS received). Smarter kids started automating the process using their computers (though didn’t see to many “how to use kannel” guides – most public solutions were using gui tools and automated mouse movers :).

The best part is that smartest kids immediately found ways to cash out the ‘GSM LTLs’ – by using ‘call to pay’ service providers, and getting 50% cash efficiency.

One GSM provider (Omnitel, TeliaSonera company) reacted by establishing daily SMS limit that works (only 6LTL worth of SMSes per day), whereas other provider (Tele2) established limit that doesn’t (phones would get disconnected next day only).

And of course, this has brought down GSM providers, or at least their SMS networks – at Christmas. Way to go, marketing people. Way to go.

For all the international people: 1 EUR= 3.4528LTL

Edit: 0.02LTL for SMS

Optimization operator

I have introduced this to quite a few colleagues in a form of question “what is the optimization operator in C++/PHP/…?”
The answers varied a lot, people would come up with branch prediction stuff (likely(),etc), and many other ideas, though never right ones.
The answer was pretty straightforward. It works in quite a lot of programming languages:

//

Simply commenting out code optimizes things better than any other way. Go, try it.

Someone please run REPAIR TABLE, I am hungry!

Today I did hit some irony. I went to my favorite ‘order home delivery food online’ place, and the shopping cart failed to grow after my ordering clicks. Then I noticed a small line at the bottom of the page:

 (F) [3] mysql. Neteisinga užklausa SELECT * FROM krepselis WHERE uzsakymas_id=220479 ORDER BY laikas. Klaida: Can’t open file: ‘krepselis.MYI’ (errno: 145). File:core.php Line:1824 

‘krepselis’ being ‘shopping cart’ and ‘uzsakymas’ being ‘order’, I realized, that error 145 will keep me hungry. Ah, and error 145 is ‘Table was marked as crashed and should be repaired’. So please, run REPAIR TABLE, I am hungry!!!

Bumping up the version

As Wikipedia is part of the Web 2.0 revolution, there has been already pressure to upgrade to Web 3.0. I personally believe we should take more drastic approach, and go directly for Web 4.0.
This Albert Einstein quote now rings in my head:

I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.

First Delta experience

Lost my luggage (for the first time). On a domestic flight. Saw it last time in JFK. Yay Delta. I got an ’emergency’ t-shirt from them. Pity it doesn’t say “I lost my luggage and all I got is this lousy t-shirt” (too small, BTW). Need some sleep now.

Update: Actually, today all flights from NY (and other north-east American locations) are canceled due to snow storms. We still have people grounded at Newark and Boston.

Update 2: D.E.L.T.A. – Don’t Expect Luggage To Arrive (wisdom of a barmaid, beer is good here :). I’m wondering if my flight back will happen…

Update 3: D.E.L.T.A. – Doesn’t Even Leave The Airport – wisdom of a hotel clerk, though hotel already participated in this story. Apparently they did reject a luggage, as didn’t find me in their database (I share a room…) – now I have hotel manager assisting in me not getting my luggage further. I’m leaving tomorrow.

Update 4: Nothing fancy, did claim my baggage at the airport, then immediately handed it back to Delta for trip home. It arrived now properly.

HTTP 2.0

Tim discovered inefficiency in communication between squids and backend servers (and eventually, clients) – the problem was lack of Content-Length: header for dynamically generated content. Though HTTP/1.1 uses chunked encoding inside keep-alive connections, HTTP/1.0 completely relies on Content-Length:, so lack of it simply forces connections to close (and have expensive reopens afterwards).

Generally, HTTP/1.0 lacked a footer – additional metadata which could be calculated after whole request. So add Content-Length footer, content compression which smoothes the content bits, and you’ve got HTTP 2.0 – with headers, footers, and rounded corners.

Ah, and solution could be various – from increasing compression buffers, to overhauling whole output buffering code. It should make the site faster anyway – good job, Tim.

Wikimaniaaaaa!!!

It is somewhat too late to post about Wikimania, so I won’t keep it long. Finally my lightning talk bits are on internet. Please note, that the idea of presentation came just few minutes before giving it. Pity there’s no video recording.

Anyway, the trip was pretty cool. Besides meeting all usual suspects at Wikimania, the whole world of amazing academia (this time, in a good sense) was revealed by my hosts. MIT rocks (especially my place of stay, TEP). Boston was pretty nice too – I quite enjoyed long walks there.

The conference itself had technical stuff extracted into ‘Hacking days’, so I ended up at quite tense technical track rather than hacking sessions. It wasn’t that bad, so I could create ‘socializing track’ at the main conference, and just hang around without any real agenda.

We had there quite less of startup spirit, that was so vivid last year. The current theme is “What we’re doing ” instead of “What we’re going to do”. For some it is maturity, for me it is boring :) I hope that was wrong impression.

I’ll sure try going there next year too. Just to see the usual suspects.

P.S. Wikipedia tremendous growth is back again.