One for Torben I think?
(and I hope you and your family are all well?)
In a script, I need to connect to a mAirListDB, find a mAirListDB folder by name, then make an internal list (or collection, or similar) of all the Items in that folder. I can see some objects and methods in mAirListScript I will probably need to use, like IDatabase and IDatabaseTree, but I can’t see any obvious methods to navigate around mAirListDB. Can you help, please?
I am wondering if I’ll need to use the ntFolder constant to identify a Folder (then check its Name), then find its child nodes and use ntItem to find Items ‘in’ that Folder, as part of a larger process to walk the mAirListDB ‘tree?’
Ultimately my aim is to place selected Items into the running playlist.
In v4.1 and newer, you can use the ProcessRequest method which allows access to a HTTP-like API to the database. (Actually the same API that is provided by the new mAirListDB Server component.)
In earlier versions, you can access the database via SQL.
For example, the following (untested) script picks a random item from a folder named “Some Folder” and inserts it at the top of the playlist:
var
conn: IZConnection;
stmt: IZPreparedStatement;
rs: IZResultSet;
folderID: integer;
itemID: integer;
item: IPlaylistItem;
begin
conn := ImAirListDB(Database(0)).GetConnection;
stmt := conn.PrepareStatement('SELECT idx FROM folders WHERE name=?');
stmt.SetString(1, 'Some Folder');
rs := stmt.ExecuteQueryPrepared;
if not rs.Next then begin
SystemLog('Folder not found');
exit;
end;
folderID := rs.GetInt(1);
stmt := conn.PrepareStatement('SELECT item FROM item_folders WHERE folder=? ORDER BY RANDOM()');
rs := stmt.ExecuteQueryPrepared;
if not rs.Next then begin
SystemLog('There are no items in that folder');
exit;
end;
itemID := rs.GetInt(1);
item := Database(0).CreatePlaylistItem(IntToStr(itemID));
CurrentPlaylist.Insert(0, item);
end.
First of all, Database(0) returns an IDatabase interface which only allows access to the general routines of a database, but not the SQL part - remember that not every database connection in an SQL database, iTunes is a notable exception.
So generally, you must typecast the return value from IDatabase to the correct interface type in order to access the more specific methods. For mAirListDB, that interface is ImAirListDB (in v3.x) or IDBConnection (from v4.x), respectively.
Now in v4.2, things are even more complicated, because not every mAirListDB is an SQL database - there is now “mAirListDB Internet Client” which accesses the database not directly through SQL, but through the new mAirListDB Server. So IDBConnection does not have any method GetConnection anymore.
Instead there is a method GetBackend which will return a reference to the “backend object” of type IDBBackend, which can be used to issue the HTTP-like requests I mentioned earlier. For a direct SQL-based mAirListDB connection, the backend can be typecasted into ISQLDBBackend, where you can finally request a reference to the connection object.
I’d use the words ‘mildly awkward,’ rather than ‘pretty difficult.’ If I were a software vendor, I’d obviously describe it as ‘flexible,’ ‘future-proof,’ and ‘designed for future expansion.’ ;D
Seriously though, I’ll try that later. I’m going to a panto tonight (“oh no you’re not!”). Oh yes I am. :’( I’m not a fan of panto, just going as moral support for a friend who’s MD of the whole fiasco. :-\
Well, the script interface is exactly the same as the internal interface inside mAirList’s code. And “easy scripting” is not the main design goal of the internal code.
By all means: I’m always happy to learn, as you know!
I will try build 1670 later: I have to make a fish pie (starting with filleting and skinning a line-caught trout donated by a friend), and that always seems to take forever.
Thanks again, and I look forward to seeing how to implement the HTTP-ish interface thingamabob. I’m guessing (probably incorrectly) that XML is likely to be involved somewhere?