My Newest Little Project – Dahl
Hello all, I’d like to give you a quick introduction to my latest little project. I like to call it ‘Dahl’, or if you like Acronyms Data Abstraction & Handling Layer. It does pretty much what it says on the tin. It’s a layer that can be built into websites / applications to provide data access and data handling. Let me briefly take you through it.
What is a Database Abstraction Thingamajig?
Good Question! In General, abstraction means simplifying the problem by removing unnecessary details, that’s just what a DAL (Data Abstraction Layer) does. It provides a consistent interface to the application regardless of database. For example, let’s say you make a site, you want to make queries to a MySQL database so you write in all the code to use MySQL, then you find out the place you’ll be hosting don’t use MySQL (yes, I know it’s insane, but just suppose) and that they use PostgreSQL instead. All the code that you wrote is now useless because it’s using the wrong set of function calls for the database you’re using. With an abstraction layer, that’s not a problem. You write the application to call the abstraction layer, and then the abstraction layer deals with the database stuff, keeping it completely separate from program logic, so that in this case all you need to do is change the DAL instead of the whole application.
So why did you do it?
You may remember a while back I posted about my little Content Management System (SnapCMS, I promise I’ll do another post on it at some point). I wrote a basic abstraction layer for that, but the thing that always bugged me about it was that it relied on SQL. If you’d wanted to say use a data source that didn’t use SQL, you’d have to write some kind of SQL lexer / parser, which I really, really didn’t want to be on the other end of. So, over the past few days or so I’ve been trying to think up an elegant way around it, and I think I’ve managed to get away with it. I decided to write it because I really like making my code as modular as possible, so that something I write now might be able to be used as many times as possible in the future, so I don’t have to write it all again, I only need to include something and we’re ready to go. My main motivation was mainly to do it for fun, if I’m totally honest, but my other motivation was to try and make something cool that will come in useful at some point in the future, but mainly the fun thing if I’m honest.
So, how does it work?
Okay, let me take you through some of my proposed syntax and what the hell it does.
To get dal working, all you have to do is to fill in the configuration file, which I’ll probably make a lot easier if I ever get happy enough with it to release it, and include the framework like this.
$dahl = Dahl:getInstance();
By this point all the conficguration is loaded and your are connected to the data source. So how do you run queries? Simples!
[sourcecode language=’php’]//Include DAHL Core Framework
$dahl = Dahl::getInstance();
$Dataset = $dahl->Table(‘TableName’)->Fetch();[/sourcecode]
The last line of that little snippet retrieves a dataset object from the data source, where all the magic happens. This loads the table into a dataset, after this you can manipulate the retrieved dataset simply through the dataset object, like so.
//Include DAHL Core Framework
$dahl = Dahl::getInstance();
$Dataset = $dahl->Table("sc_settings")->Fetch();
//Filter Out Results
$Dataset->Filters->Add("FirstName = 'Fred'");
$Dataset->OrderBy = "LastName";
$Dataset->Order = "Ascending";
$Dataset->Limit = 5;
That last snippet of code has the effect of limiting down the dataset, by filtering out the records where only the first name is Fred, and limiting it to 5 rows returned. Also it’s sorting it into ascending order by last name. You then call the refresh so that the dataset can rebuffer the data, I’m working on having it do that automagically at the moment.
//This is the code used to get the value of a field from the table
$name = $Dataset['FirstName'];
//This is the code used to set a value of a field in the table
$Dataset['FirstName'] = "George";
This is what I’m currently working on, among other features. If the program wishes to access a value from the table, all they need to do is use the usual syntax for array access, the first offset defines the row and the second offset defines the attribute. This is where the ‘Handling’ part of the title comes in. When you access a field within the table, the dataset first ensures that it is up to date checking to see if the filters have been modified since it was last refreshed and if they have, it refreshes from the data source. When the program sets a value within the dataset, it doesn’t immediately apply the changes to the database, but it tracks what changes are made and when the program has finished modifying the data it calls the Update() method of the dataset and all those changes are saved to the dataset. Just the same, if the program decides it doesn’t want to save those changes it can call the Cancel() method and all changes are removed.
Sounds good. How is it going?
I’ve only been working on it a couple of days so far so it’s very, very early days but so far it’s going well. I have so far implemented the Dataset object and it’s returning data from MySQL tables. Currently working the Array Access stuff that I just discussed, but the framework is at least there for the future. Also, I’ve got the foundations laid for some kind of Caching subsystem, to try and reduce the stress on databases for repeated queries. Hope you liked reading about my PHP hackery and I hope to update about my projects soon.
Thanks for Reading