Feeds

Get into data with Groovy

Part 2: Object grabber

Security for virtualized datacentres

Of course, under the surface there's still some SQL going on and we can look at it if we want to using the sql and parameters properties of the DataSet. For example, in the case of not_harry this maps to:

println not_harry.sql
println not_harry.parameters

Which gives us:

select * from users where (user_id > ? and user_name != ?)
[2, "harry"]

Can we go further with a DataSet? So far we've restricted ourselves to querying and filtering table data. What if we want to write data back to the database? The good news is that you can add new rows of data, the bad news is that deletes or updates are not yet implemented. We'll have to hope and wait for a future release that adds this functionality.

To illustrate the add row functionality we're going to implement a common enough scenario - we're going to populate our MySQL table with data that's been dumped from a spreadsheet or another RDBMS into a CSV file. It's the kind of data manipulation task that scripting languages traditionally excel at.

The first thing to do is work out how to grab the data from the CSV file and parse it correctly. Here's an extract from our users.csv file:

fred,fred@flintstone.com,10
barney,barney@rubble.net,11
wilma,wilma@flintstone.co.uk,12
bambam,bambam@bambam.org,13
betty,betty@betty.com,14

As should be clear, Groovy is big on iterators, and in this case we can just grab the file and iterate over each line, splitting it on the comma character. As a test we can run the following code:

new File('users.csv').splitEachLine(',') {fields ->
  println fields[0] + " " + fields[1] + " " + fields[2]
  }

Here each line is parsed, tokenised on the comma and bound to a variable that's called fields. The above code will cycle through each line of the file and print the different fields for us. What we want to do next is add each line of data to the database, but instead of INSERT queries we're going to use our DataSet directly:

new File('users.csv').splitEachLine(',') {fields ->
  ds.add(
    user_name: fields[0],
    user_id: fields[2],
    email: fields[1]
  )
}
  
ds.each { println it.user_name }

Running the above code will add the rows from the CSV file and then dump the list of user_names to verify that it's worked.

In just a few lines of code we've managed to read a file, parse it, and then add the data to a database, with minimal amounts of house-keeping code or boilerplate Java.

Of course, the fact the DataSet only works on tables and not on more complex structures (such as the result of a JOIN) means you can't get away from using SQL altogether, but Groovy makes it easy to mix and match approaches. And in the case of complex queries, it's fairly straightforward to use the Sql object to create a database view and then to use the DataSet object to access that.

In all then, Groovy offers a set of high-level objects that make database interaction a relative breeze. ®

Website security in corporate America

More from The Register

next story
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
'Windows 9' LEAK: Microsoft's playing catchup with Linux
Multiple desktops and live tiles in restored Start button star in new vids
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
'People have forgotten just how late the first iPhone arrived ...'
Plus: 'Google's IDEALISM is an injudicious justification for inappropriate biz practices'
Mathematica hits the Web
Wolfram embraces the cloud, promies private cloud cut of its number-cruncher
Mozilla shutters Labs, tells nobody it's been dead for five months
Staffer's blog reveals all as projects languish on GitHub
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 Healthkit gets a bug SO Apple KILLS it. That's real healthcare!
Not fit for purpose on day of launch, says Cupertino
prev story

Whitepapers

Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.
WIN a very cool portable ZX Spectrum
Win a one-off portable Spectrum built by legendary hardware hacker Ben Heck
Storage capacity and performance optimization at Mizuno USA
Mizuno USA turn to Tegile storage technology to solve both their SAN and backup issues.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
The next step in data security
With recent increased privacy concerns and computers becoming more powerful, the chance of hackers being able to crack smaller-sized RSA keys increases.