Ejemplo n.º 1
0
 /**
  * @param Query\Row $row
  * @param Query\Field[] $fields
  *
  * @return array
  * @throws Exception
  */
 public static function RowValues($row, array $fields)
 {
     $values = array();
     // Row values are packed into a single buffer.
     // See the docs for the Row message in query.proto.
     $start = 0;
     $buf = $row->getValues();
     $lengths = $row->getLengths();
     foreach ($lengths as $key => $len) {
         $fieldKey = $fields[$key]->getName();
         $val = null;
         if ($len < 0) {
             // This indicates a MySQL NULL value,
             // to distinguish it from a zero-length string.
             $val = NULL;
         } else {
             $val = substr($buf, $start, $len);
             if ($val === FALSE || strlen($val) !== $len) {
                 throw new Exception('Index out of bounds while decoding Row values');
             }
             $start += $len;
         }
         $values[$fieldKey] = $val;
         $values[$key] = $val;
     }
     return $values;
 }
Ejemplo n.º 2
0
 /**
  *
  * @param Query\Row $row
  * @param Query\Field[] $fields
  *
  * @return array
  * @throws Exception
  */
 public static function RowValues($row, array $fields)
 {
     $values = array();
     // Row values are packed into a single buffer.
     // See the docs for the Row message in query.proto.
     $start = 0;
     $buf = $row->getValues();
     $buflen = strlen($buf);
     $lengths = $row->getLengths();
     foreach ($lengths as $key => $len) {
         $fieldKey = $fields[$key]->getName();
         // $len < 0 indicates a MySQL NULL value,
         // to distinguish it from a zero-length string.
         $val = null;
         if ($len >= 0) {
             if ($start == $buflen) {
                 // Different PHP versions treat this case differently in
                 // substr(), so we handle it manually.
                 $val = '';
             } else {
                 $val = substr($buf, $start, $len);
                 if ($val === FALSE || strlen($val) !== $len) {
                     throw new Exception("Index out of bounds while decoding Row values (start={$start}, len={$len}). Raw protobuf: " . var_export($row, TRUE));
                 }
             }
             $start += $len;
         }
         $values[$fieldKey] = $val;
         $values[$key] = $val;
     }
     return $values;
 }