DAT
DAT (Data Access Thing) is a PHP library that will make accessing your MySQL database a lot easier. It also includes an easy to use library with functions to convert to and from each of the following: PHP Arrays, XML, JSON, YAML.
To use DAT you run a DAT command and get back a DAT result. DAT commands follow the same structure as JSON RPC.
Here is a sample DAT query:
{
"method" : "GetData",
"params" : {
"table" : "users",
"columns" : ['userID', 'userFirstName'],
"returnQuery" : true,
"where" : {
"userID" : {
"greaterThan" : 4
},
"userFirstName" : {
"contains" : "kris"
}
}
},
"id" : 23
}
And here is a sample return:
{
"result" : {
"meta" : {
"numRecords" : 1,
"recordOffset" : 0,
"numRecordsRequested" : 0,
"outputTimestamp" : 11111111111,
"outputDateTime" : "2008-03-04 12:12:12",
"success" : 1
}
"data" : [{
"userID" : 5,
"userFirstName" : "Kris"
}]
}
"error" : null,
"id" : 23
}
There are shortcuts to do DAT commands, here are the functions:
- arrayFor(table, params, collapse, leftjoins): get all rows
- allDataFor(table, params, collapse, leftjoins): get 20 rows and return result set along with pagination data
- rowFor(table, params, collapse, leftjoins): get one row
- columnFor(table, column, params, collapse, leftjoins): get a single column
- countFor(table, params): return a row count without getting all of the data
- updateFor(table, updateparams, whereparams): update data
- insertFor(table, params): insert data
- columnsOf(table, excludecolumns): get the columns in a table not returning ones you want to exclude
- runProcedure(procedurename, arguments, collapse): run a defined DAT procedure (in the procedures directory)
An easy example of how to use this would be if you wanted to get a session from a table named sessions where the column "sessionID" is the hash that you have set in a cookie (or run through a url or something) named "sessionID":
"sessionID" => $_REQUEST["sessionID"], ));
Now, lets say that the sessions table has a column named "userID" and that there is a table named "users" where "userID" is the primary key. If you wanted to get the user that this session was for (if there is one) then you would do:
"sessionID" => $_REQUEST["sessonID"], ), true);
The "users" => array(), part tells DAT that you want it to figure out how to get data from the users table based on the rows returned in the sessions table. DAT will see that the primary key of the users table "userID" is also a field in the sessions table and it will use that to grab the user data. The results will look something like this:
"sessionID" => "fddsasddfasfq3443af34fa34fa4wa34", "userID" => 2, "other_session_table_field" => "value", "userID" => 2, "userFirstName" => "blah", "userLastName" => "blah2", "userUsername" => "kris", "userPassword" => "dsf34jjlk34lk34j34lk2hjnn32jk23oi", ), ), );
It REALLY gets cool when you do things like:
"since" => "an hour ago", ), ));
I currently do not have a release for DAT packaged up so in order to get it, you would have to rip it out of an Encompas archive if you have one, or you would have to ask me for it.
