Skip to content
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

feature: support for all databases supported by laravel to the functionality for sorting alphanumeric values #149

Merged
merged 8 commits into from
Nov 25, 2021

Conversation

vs0uz4
Copy link
Contributor

@vs0uz4 vs0uz4 commented Nov 25, 2021

I found after some analysis that the syntax being used to sort alphanumeric values field+0 is not compatible with all databases supported by Laravel. I also discovered that some logs were being dropped into the database, as illustrated below.

Output Logs on MySQL Server

Truncated incorrect DOUBLE value: 'Dish 2'
Truncated incorrect DOUBLE value: 'Dish B'
Truncated incorrect DOUBLE value: 'Dish C'
Truncated incorrect DOUBLE value: 'Dish D'
Truncated incorrect DOUBLE value: 'Dish E'
Truncated incorrect DOUBLE value: 'Dish F'
Truncated incorrect DOUBLE value: 'Dish G'
Truncated incorrect DOUBLE value: 'Zebra Dish H'
Truncated incorrect DOUBLE value: 'Dish I'
Truncated incorrect DOUBLE value: 'Dish J'
Truncated incorrect DOUBLE value: 'Dish 1'
Truncated incorrect DOUBLE value: '1b'
Truncated incorrect DOUBLE value: 'Dish L'
Truncated incorrect DOUBLE value: '1a'
Truncated incorrect DOUBLE value: 'Dish 2'
Truncated incorrect DOUBLE value: 'Dish B'
Truncated incorrect DOUBLE value: 'Dish C'
Truncated incorrect DOUBLE value: 'Dish D'
Truncated incorrect DOUBLE value: 'Dish E'
Truncated incorrect DOUBLE value: 'Dish F'
Truncated incorrect DOUBLE value: 'Dish G'
Truncated incorrect DOUBLE value: 'Zebra Dish H'
Truncated incorrect DOUBLE value: 'Dish I'
Truncated incorrect DOUBLE value: 'Dish J'
Truncated incorrect DOUBLE value: 'Dish 1'
Truncated incorrect DOUBLE value: '1b'
Truncated incorrect DOUBLE value: 'Dish L'
Truncated incorrect DOUBLE value: '1a'

So I did some research and came up with some possible syntaxes for such sorting. But each database used its own syntax to perform the task, because of this difference in syntax I implemented a function called getSortSqlByDriver where with it we can successfully perform an alphanumeric sort. All we need to do is to inform some parameters when we call the function:

  • string $sortField;
  • string $driverName;
  • string $driverVersion.

The parameter $driverVersion is optional, if we do not pass this parameter, the function will assume the default value '*'. This parameter is a safety measure, so that if in the future some newer version of one of the databases no longer supports the syntax, we can make sure that everything continues to work.

Some auxiliary methods have also been developed, for example:

  • getSortFieldType() : Determines what data type of the column to be sorted is.
  • isValidSortFieldType() : Determines if the type of the column to sort is valid.

Since the sort is of type alphanumeric, only data of type string is accepted. Whether they are (varchar, char, etc)

Each query was tested against its respective database, and all returned the same database records. Below we can see the returned records already sorted alphanumerically.

Query Results
query_order-by

After implementation, the tests were run and all passed successfully.

Tests Results
tests

Larastan and PHP CS Fixer Results
phpstan_php-cs-fixer


EXTRA
In order to minimize the inconvenience that may be caused by installing and configuring databases, and to avoid diverging scenarios, I am also proposing to add a docker-compose file to the project repository, so that we can easily orchestrate Docker containers, for all currently supported database versions.
orquestration

I increased the number of the database ports by 1 so that I could avoid any kind of conflict due to local versions running on the default ports. Thus, we have the following connection ports for the database containers :

  • MySQL (3307)
  • PostgreSQL (5433)
  • SQL Server (1434)

I suggest that the test environments be set to the given ports, thus maintaining a standard and avoiding conflicts with local versions of the databases.

References

@luanfreitasdev
Copy link
Collaborator

Thanks @vs0uz4 !.

As always their PR's are very well explored and significant to the powergrid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants