Re: [Fwd: Re: [PEAR-DEV] serialize datatype for MDB2]

From: Date: Sat, 10 Jun 2006 10:03:22 +0000
Subject: Re: [Fwd: Re: [PEAR-DEV] serialize datatype for MDB2]
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-42911@lists.php.net to get a copy of this message
Hi, ok below in [I] you see the datatype_map stuff in action (working in CVS). in the example i map the 'telephone' type onto 'text' and the 'serialize' datatype is handled by a callback. The overhead is essentially a few additional if statements, nothing too drastic. like i said before in theory you should also be able to write a wrapper. an untested example can be seen in [II]. This has been possible ever since MDB2 has gone stable. So what do people prefer? Should I keep [I] in for the next MDB2 release? regards, Lukas [I] function serialize_callback(&$db, $method, $parameters) { switch($method) {
    case 'quote':
      $value = serialize($parameters['value']);
      return $db->datatype->quote($value, 'text');
    case 'convertResult':
      $value = $db->datatype->convertResult($parameters['value'], 'text');
      return @unserialize($value);
    case 'getDeclaration':
      return $db->datatype->getDeclaration('text', $parameters['name'], $parameters['field']);
    case 'mapPrepareDatatype':
      return 'text';
} } $mdb2->setOption('datatype_map', array('serialize' => 'serialize', 'telephone' => 'text')); $mdb2->setOption('datatype_map_callback', array('serialize' => 'serialize_callback')); $location = $mdb2->quote(array('foo' => 'bar'), 'serialize'); $telephone = $mdb2->quote('(030) 23615467', 'telephone'); $query = "INSERT INTO branches VALUES ($location, $telephone)"; var_dump($query); // 'INSERT INTO branches VALUES ('a:1:{s:3:\"foo\";s:3:\"bar\";}', '(030) 23615467')' (length=80) $result = $mdb2->exec($query); $query = "SELECT location, telephone FROM branches"; $result = $mdb2->queryAll($query, array('serialize', 'telephone'), MDB2_FETCHMODE_ASSOC); var_dump($result); /* array 0 =>
    array
      'location' =>
        array
          'foo' => 'bar' (length=3)
      'telephone' => '(030) 23615467' (length=14)
*/ [II] $mdb2->loadModule('MyDatatypeModule', 'datatype', false); class MyDatatypeModule extends MDB2_Module { var $datatype; function MyDatatypeModule() {
    $db =& $this->getDBInstance();
    if (PEAR::isError($db)) {
        return $db;
    }
    $this->datatype = $mdb2->loadModule('Datatype', null, true);
} function __call($method, $params) {
    // implement any custom magic here ..
    return call_user_func_array(array(&$this->datatype, $method), $params);
} }

« previous php.pear.dev (#42911) next »