uuid icon indicating copy to clipboard operation
uuid copied to clipboard

How to search a partial uuid?

Open chateaux opened this issue 2 years ago • 1 comments

My transactions table uses a uuid which is stored as bytes in the database.

I would like to know how to search for a partial uuid? Obviously, since we are using bytes, the data is stored as a binary in the table so in theory I need to convert the partial uuid string to a binary and then to search on that.

Is there anyway to do this natively with UUID?

If I had a full UUID, I am simply going to use something like so:

$qb->andWhere('transactions.uuid = :uuid')->setParameter('uuid', Uuid::fromString($options['transactionUuid'])->getBytes());

But to search a partial uuid, I am looking to do something as follows:

$qb->expr()->like('transaction.uuid', Uuid:fromString(':search')->getBytes()),

But obviously the fromString requires a correctly formed uuid string as apposed to a partial.

I am not sure this is possible and perhaps is one of the downfalls of using bytes in the table as apposed to string.

chateaux avatar Aug 28 '22 15:08 chateaux

Hi @chateaux , you could use lower() and hex() to convert the transaction.uuid into something searchable.

$qb->andWhere('LOWER(HEX(transactions.uuid)) LIKE :uuid')->setParameter('uuid', '%f68f66cd%');

It is not a solution withing this library but it should work. Although I have not tested the above line.

Best Sebastian

7thSENSE-GmbH avatar Jan 16 '23 06:01 7thSENSE-GmbH