Skip to content

Commit a9da836

Browse files
author
zhao.zhiqiang
committed
fix decimal negative number
1 parent c42a3b6 commit a9da836

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

bin/BinLogPack.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,21 +311,46 @@ public function read_length_coded_pascal_string($size)
311311
return $this->read($length);
312312
}
313313

314+
/**
315+
* @param $size
316+
* @return float|int
317+
*/
314318
public function read_int_be_by_size($size) {
315319
//Read a big endian integer values based on byte number
316320
if ($size == 1) {
317-
return unpack('c', $this->read($size))[1];
318-
} elseif( $size == 2)
319-
return unpack('n', $this->read($size))[1];
321+
$re = unpack('c', $this->read($size))[1];
322+
if($re>127) {
323+
return $re-pow(2,8);
324+
}
325+
return $re;
326+
} elseif( $size == 2) {
327+
$re = unpack('n', $this->read($size))[1];
328+
if($re>32767) {
329+
return $re-pow(2,16);
330+
}
331+
return $re;
332+
}
320333
elseif( $size == 3)
321334
return $this->read_int24_be();
322-
elseif( $size == 4)
323-
return unpack('N', $this->read($size))[1];
335+
elseif( $size == 4){
336+
$re = unpack('N', $this->read($size))[1];
337+
if($re > 2147483647) {
338+
return ($re-pow(2,32));
339+
}
340+
return $re;
341+
342+
}
324343
elseif( $size == 5)
325344
return $this->read_int40_be();
326345
//TODO
327-
elseif( $size == 8)
328-
return unpack('N', $this->read($size))[1];
346+
elseif($size == 8) {
347+
$re = unpack('N', $this->read($size))[1];
348+
return $re;
349+
if($re > 2147483647) {
350+
return ($re-pow(2,64));
351+
}
352+
return $re;
353+
}
329354
}
330355

331356
/**

pack/RowEvent.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -500,28 +500,30 @@ private static function _read_new_decimal($column) {
500500
self::$PACK->unread(pack('C', $value ^ 0x80));
501501
$size = $compressed_bytes[$comp_integral];
502502
if ($size > 0) {
503-
$value = self::$PACK->read_int_be_by_size($size) ^ $mask;
503+
$f = self::$PACK->read_int_be_by_size($size);
504+
$value = $f ^ $mask;
504505
$res .= (string)$value;
505506
}
506507

507-
508508
for($i=0;$i<$uncomp_integral;$i++) {
509-
$value = unpack('N', self::$PACK->read(4))[1] ^ $mask;
509+
$value = self::$PACK->read_int_be_by_size(4) ^ $mask;
510510
$res .= sprintf('%09d' , $value);
511511
}
512512

513+
513514
$res .= ".";
514515
for($i=0;$i<$uncomp_fractional;$i++) {
515-
$value = unpack('N', self::$PACK->read(4))[1] ^ $mask;
516+
$value = self::$PACK->read_int_be_by_size(4) ^ $mask;
516517
$res .= sprintf('%09d' , $value);
517518
}
518519

519520
$size = $compressed_bytes[$comp_fractional];
520521
if ($size > 0) {
521-
$value = self::$PACK->read_int_be_by_size($size) ^ $mask;
522-
522+
$f = self::$PACK->read_int_be_by_size($size);
523+
$value = $f ^ $mask;
523524
$res.=sprintf('%0'.$comp_fractional.'d' , $value);
524525
}
526+
//todo 超过int范围无法解析此函数 ex:11111111111199876665554567.001
525527
return number_format($res,$comp_fractional,'.','');
526528
}
527529

@@ -531,7 +533,7 @@ private static function _getUpdateRows($result, $len) {
531533
$rows = [];
532534
while(!self::$PACK->isComplete(self::$PACK_SIZE)) {
533535

534-
$value['beform'] = self::_read_column_data($result['bitmap1'], $len);
536+
$value['before'] = self::_read_column_data($result['bitmap1'], $len);
535537
$value['after'] = self::_read_column_data($result['bitmap2'], $len);
536538
$rows[] = $value['after'];
537539
}

0 commit comments

Comments
 (0)