- DBIx::Custom
- here
order by clause generation
The DBIx::Custom::Order class makes it easy to write an order by clause. For example, you can easily specify a default order first and customize the order for your situation.
To create a DBIx::Custom::Order object, use the order method.
my $order = $dbi->order;
You can get the DBIx::Custom::Order object.
Use the prepare method to specify the order. Parts of the order by clause will be added forward and forward.
$order->prepend('title','price desc'); $order->prepend('author desc');
To finally convert to the string used in the order clause, use the to_string method or evaluate the DBIx::Custom::Order object as a string.
my $order_str = $order->to_string; my $order_str = "$order";
The following order clause is generated:
order by author desc, title, price desc
Now let's prepend more on the ones that contain the column names with the same name.
$order->prepend('price asc');
In this case, there are two prices, but the DBIx::Custom::Order object handles the duplicates properly. Those added later take precedence. You can see that the following order by clause is generated when it is converted to a string.
order by price asc, author desc, title
If the order is fixed, it is easy to write directly without using the DBIx::Custom::Order object. If you want to change the order of the order dynamically, use the DBIx::Custom::Order object.