Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Uid] Add some ulid helpers to manipulate the binary field saved in db #42966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
maidmaid opened this issue Sep 9, 2021 · 8 comments
Closed

Comments

@maidmaid
Copy link
Contributor

maidmaid commented Sep 9, 2021

Description
As ulid is saved as a binary field in the db and represented as base32 in the application side, it's hard and unconvenient to manipulate them. It would be interesting to improve the DX.

Example

1st idea : bin/console inspect:ulid could show an hexadecimal representation of the ulid :


/app # bin/console ulid:inspect 01FF2QE789KQSBK3HWT65S9H7S
 ---------------------- -------------------------------------- 
  Label                  Value                                 
 ---------------------- -------------------------------------- 
  toBase32 (canonical)   01FF2QE789KQSBK3HWT65S9H7S            
...
  toHex                  0x017BC5771D099DF2B98E3CD18B94C4F9  

Thus, the hex representation can be used directly in a SQL request to filter a specific ulid :

SELECT * FROM my_table WHERE id=0x017BC5771D099DF2B98E3CD18B94C4F9

A second idea is to bring some SQL routimes to manipulate ulid directly in SQL as you can see in this gist.

SELECT * FROM my_table WHERE id=ULID_DECODE('01FF2QE789KQSBK3HWT65S9H7S')

It could be a Doctrine migration that the developer is free to use in his app.

@derrabus
Copy link
Member

derrabus commented Sep 9, 2021

Idea 1 sounds reasonable. About idea 2: I don't think we want to maintain stored procedures for various database engines. 🤔

@alexandre-daubois
Copy link
Member

I might be wrong but isn't the RFC 4122 representation the same as hexadecimal representation of your UUID?

@maidmaid
Copy link
Contributor Author

RFC 4122 representation is like 0178b5a6-67d3-0d9b-889c-7f205750b09d, so it's not an hex representation.

@alexandre-daubois
Copy link
Member

I'm telling this because after trying a few things, I realized that RFC 4122 seems equal to the hex representation but with additional dashes. I tested with the ULID you gave, 01FF2QE789KQSBK3HWT65S9H7S. Here's my output:

$ php bin/console ulid:inspect 01FF2QE789KQSBK3HWT65S9H7S
 ---------------------- -------------------------------------- 
  Label                  Value                                 
 ---------------------- -------------------------------------- 
  toBase32 (canonical)   01FF2QE789KQSBK3HWT65S9H7S            
  toBase58               1BdEv7iG455d3N8R8cVcGx                
  toRfc4122              017bc577-1d09-9df2-b98e-3cd18b94c4f9  
  toHex                  0x017bc5771d099df2b98e3cd18b94c4f9    
 ---------------------- -------------------------------------- 
  Time                   2021-09-08 12:52:02.953 UTC           
 ---------------------- --------------------------------------

If this is correct, I'm not sure of the additional value of adding the hex representation?

@Tetragramat
Copy link
Contributor

I believe that inspect should return uid in all awailable formats which includes binary formated as hex.

@carsonbot
Copy link

Thank you for this suggestion.
There has not been a lot of activity here for a while. Would you still like to see this feature?

@carsonbot
Copy link

Could I get a reply or should I close this?

@bigfoot90
Copy link

I think this is a useful feature, don't close this.

@carsonbot carsonbot removed the Stalled label May 17, 2022
@fabpot fabpot closed this as completed Jul 20, 2022
fabpot added a commit that referenced this issue Jul 20, 2022
…urimasniekis)

This PR was merged into the 6.2 branch.

Discussion
----------

[Uid] Added toHexString method to AbstractUid class

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Fix | #42966
| License       | MIT

Added a small method to Uid component `AbstractUid` class called `toHex` which would output binary value of identifier as prefixed hex string for e.g. `0x0000000`. This is really useful when using with Doctrine for Id generation as the storage of Uid is a binary format in the database, and in most cases, it is usually used with hex format to query it.

This PR is from #45939 as I needed to change the source branch to 6.1

Commits
-------

dad28d6 Added toHexString method to AbstractUid class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants