Horse trader printing24 comments
Cara pengiraan untung forex
Mar 20, by chromatic. A recent article on Perl. Recordset module as the standard database interface. While choosing an interface involves trade-offs, the venerable DBI module, used properly, is a fine choice. This response attempts to clear up some misconceptions and to demonstrate a few features that make DBI, by itself, powerful and attractive. Since its inception in , DBI has matured into a powerful and flexible module.
It runs well on many platforms, includes drivers for most popular database systems and even supports flat files and virtual databases.
Its popularity means it is well-tested and well-supported, and its modular design provides a consistent interface across the supported backends. Brannon says that the DBI does not handle this gracefully.
One type of mutation is field reordering. The updated table might resemble:. Any database request that assumes, but does not specify, the order of results is susceptible. The situation is not as bad as it seems. Provided the existing field names do not change, code using this approach will continue to work. Unfortunately, this is less efficient than other fetching methods. More importantly, explicitly specifying the desired fields leads to clearer and more secure code.
This can also improve performance by eliminating unnecessary data from a request. The less work the database must do, the better. The accessor code must use the new indexing approach. Whether the accessors continue to function in the face of this change is irrelevant — someone must update the code!
The same arguments apply to destructive mutations, where someone deletes a field from a table. While less likely than adding a field, this can occur during prototyping. Anyone who deletes a field in a production system will need an approved change plan, an extremely good excuse or a recent CV. A change of this magnitude represents a change in business rules or program internals.
Any system that can handle this reliably, without programmer intervention, is a candidate for Turing testing. It is false laziness to assume otherwise. Various classes of programs will handle this differently.
My preference is to die immediately, noisily alerting the maintainer. Other applications and problem domains might prefer to insert or to store potentially tainted data for cleansing later. Given the hopefully rare occurrence of these mutations and the wide range of options in handling them, the DBI does not enforce one solution over another. Contrary to the explanation in the prior article, this is not a failing of the DBI. See a November PerlMonks discussion at http: This allows savvy users to write intricate queries by hand, while database neophytes can use modules to create their statements for them.
The rest of us can choose between these approaches. SQL statements are plain text, easily manipulated with Perl. An example from the previous article created an INSERT statement with multiple fields and a hash containing insertable data. Where the example was tedious and hard to maintain, a bit of editing makes it general and powerful enough to become a wrapper function. Luckily, the source hash keys correspond to the destination database fields.
It takes only a few lines of code and two clever idioms to produce a sane and generalized function to insert data into a table. The first two lines declare the database table to use and the fields into which to insert data. These could just as well come from function arguments. The join lines transforms lists of fields and values into string snippets used in the SQL statement. Be sure to check the DBD module for your chosen database for other notes regarding quote.
This odd fellow is known as a hash slice. A relational database must parse each new statement, preparing the query. This occurs when a program calls the prepare method. High-end systems often run a query analyzer to choose the most efficient path. Because many queries are repeated, some databases cache prepared queries.
This is especially handy when inserting multiple rows. Instead of interpolating each new row into a unique statement and forcing the database to prepare a new statement each time, adding placeholders to an INSERT statement allows us to prepare the statement once, looping around the execute method.
Each time we call execute on the statement handle, we need to pass the appropriate values in the correct order. Again, a hash slice comes in handy. Note that DBI automatically quotes values with this technique. This example only inserts one row, but it could easily be adapted to loop over a data source, repeatedly calling execute. While it takes slightly more code than interpolating values into a statement and calling do , the code is much more robust.
Additionally, preparing the statement only once confers a substantial performance benefit. Consult the DBI documentation for more details. This is very fast, as it avoids copying returned values, and can simplify code greatly. Binding columns is best illustrated by an example. Here, we loop through all rows of the user table, displaying names and e-mail addresses:. This code does have the flaw of depending on field ordering hardcoded in the SQL statement. It only takes two lines of magic to bind hash values to the result set.
This is the only required step of the example, but the value initialization in the previous line makes it more clear. If we only display names and addresses, this is no improvement over binding simple lexicals. The real power comes with more complicated tasks.
This technique may be used in a function:. Other options include passing in references to populate or returning an object that has a fetch method of its own. The decision to use one module over another depends on many factors.
For certain classes of applications, the nuts and bolts of the underlying database structure is less important than ease of use or rapid development. Some coders may prefer a higher level of abstraction to hide tedious details for simple requirements.
The drawbacks are lessened flexibility and slower access. It is up to the programmer to analyze each situation, choosing the appropriate approach. Perl itself encourages this. When the techniques presented here are too onerous and using a module such as Tangram or DBIx:: Recordset makes the job easier and more enjoyable, do not be afraid to use them. Conversely, a bit of planning ahead and abstraction can provide the flexibility needed for many other applications.
In his spare time, he has been working on helping novices understand stocks and investing. Something wrong with this article? Help us out by opening an issue or pull request on GitHub. The information published on this website may not be suitable for every situation. All work on this website is provided with the understanding that Perl. Is Table Mutation a Big Problem? The updated table might resemble: Making Queries Easier Another of Mr.
Placeholders A relational database must parse each new statement, preparing the query. Here, we loop through all rows of the user table, displaying names and e-mail addresses: This technique may be used in a function: Feedback Something wrong with this article? How to find a programming topic to write about Writing is easier when you know the goal. Hidden Gems of Perl. How does traceroute work? Identifying which routers process an IP request.
Contact Us To get in touch, send an email to: