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.
It looks like the GetBackend method isnât exposed to mAirListScript: Unknown identifier âGETBACKENDâ.
Also, the string GetBackend doesnât exist anywhere in mAirList.exe. Thatâs usually a good clue that a method, variable, etc. hasnât been exposed.
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?