dbal icon indicating copy to clipboard operation
dbal copied to clipboard

DB numeric type should map to PHP string type

Open JanTvrdik opened this issue 8 years ago • 8 comments

Converting lossless numeric (= decimal) to lossy float is a bad idea.

JanTvrdik avatar May 23 '17 13:05 JanTvrdik

Not sure if this is a good idea. In the end, the majority will still convert it to float, manually or by php auto-coercion.

hrach avatar May 23 '17 13:05 hrach

AFAIK the only reason to use numeric is when you need precise numbers (i.e. money amount). For „normal“ float numbers you should just use real / double precision type.

JanTvrdik avatar May 23 '17 14:05 JanTvrdik

For „normal“ float numbers you should just use real / double precision type.

This describes exactly the problem. The normal float are just regular floats where actually almost nobody care (do something with it) about the precession loss. Dbal itself will preserve the information in string format, but programmer in the end will convert it to float manually.

I'm not strongly against this, but I'm not sure if this actually solves any real problems instead of removing some behavior. In any case, you may the normalization manually turn off, we may better implement a feature for custom column normalization turning off.

hrach avatar May 23 '17 17:05 hrach

My point is that the default normalization which is done automatically by dbal should always by lossless.

JanTvrdik avatar May 23 '17 18:05 JanTvrdik

Hi, any news on this topic or have something in the meantime crossed your mind about how to allow decimals to be converted to string (to use some library like phpdecimals etc.)?

pavelmlejnek avatar Mar 28 '19 08:03 pavelmlejnek

The latest recent news is that there will be a setter for explicit conversion type. WIP here: https://github.com/nextras/dbal/commit/ea47124657be658775e65713e99ebaafd16d7ae2

hrach avatar Mar 28 '19 08:03 hrach

Great, thank you for the information. Can I help somehow?

pavelmlejnek avatar Mar 28 '19 08:03 pavelmlejnek

My workaround for postgres and nextras orm:

use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Helpers\DbalQueryBuilderHelper;
use Nextras\Orm\Mapper\Dbal\DbalMapper;

final class ExamplePostgresMapper extends DbalMapper
{

	public function builder(): QueryBuilder
	{
		$builder = parent::builder();

		$tableName = $this->getTableName();
		$alias = DbalQueryBuilderHelper::getAlias($tableName);
		$builder->addSelect(sprintf('"%s"."column"::text', $alias));

		return $builder;
	}

}

mabar avatar Apr 07 '22 14:04 mabar