--[[ Database LUA Actions v1.0.0.1 for AutoPlay Media Studio 6/7, By RizlaUK Database, is a set of actions for useing multiple in-memory databases, this is aimed at simple one line text inputs where a sqlite database can be prue overkill and not to mention very complicated for useing a simple small database Database Actions aims to simplefi the task of working with a small database, the actions are all native AMS style actions and can be accessed through the actions wizard where you can also find some info on the fuctions and there arguments, you can find the xml action file source in a comment at the bottom of this file. ]] --------------------------------------------------------------------------------------------- Database={} --------------------------------------------------------------------------------------------- -- V1.0.0.1 FUNCTIONS --------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------- -- Removes a column from the database, returns true if OK, else false is returned -- Database.RemoveColumn(tbDB,nColumn) function Database.RemoveColumn(tbDB,nColumn) if tbDB then for index, K in tbDB do tbTmp=DelimitedStringToTable(tbDB[index], Delimiter) if tbTmp then Table.Remove(tbTmp, nColumn); tbDB[index] = Table.Concat(tbTmp, Delimiter, 1, TABLE_ALL); end end return true else return false end end --------------------------------------------------------------------------------------------- -- Adds a column to the database, returns column index if OK else -1 is returned. -- Database.AddColumn(tbDB,sColumn) function Database.AddColumn(tbDB,sColumn) if tbDB then for index, K in tbDB do tbTmp=DelimitedStringToTable(tbDB[index], Delimiter) if tbTmp then if index == 1 then tbDB[index] = tbDB[index]..Delimiter..sColumn else tbDB[index] = tbDB[index]..Delimiter.." " end end end return Table.Count(tbTmp); else return -1 end end --------------------------------------------------------------------------------------------- -- Returns the column name from the given index, if any error a blank string ("") is returned. -- Database.GetColumnName(tbDB,nColumn) function Database.GetColumnName(tbDB,nColumn) if tbDB then tbTmp=DelimitedStringToTable(tbDB[1], Delimiter) if tbTmp[nColumn] then return tbTmp[nColumn] else return "" end else return "" end end --------------------------------------------------------------------------------------------- -- Sets the column name at the given index, if OK then true is returned else if any error false is returned. -- Database.SetColumnName(tbDB,nColumn,sText) function Database.SetColumnName(tbDB,nColumn,sText) if tbDB then tbTmp=DelimitedStringToTable(tbDB[1], Delimiter) if tbTmp[nColumn] then tbDB[1] = String.Replace(tbDB[1], tbTmp[nColumn], sText, false); return true else return false end else return false end end --------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------- -- END V1.0.0.1 FUNCTIONS --------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------- -- V1.0.0.0 FUCTIONS --------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------- -- Sets the global delimiter of the database. function Database.SetDelimiter(strDelimiter) Delimiter=strDelimiter end -- make sure the default delimiter is set Database.SetDelimiter("|") --------------------------------------------------------------------------------------------- -- Gets the global delimiter of the database. function Database.GetDelimiter() return Delimiter end --------------------------------------------------------------------------------------------- -- Creates and returns a handle to a database -- Database.Create(delimited string Columns) function Database.Create(strColumns) tbHandle={} tbHandle[1]=strColumns return tbHandle end --------------------------------------------------------------------------------------------- -- Adds a row to the end of the database, return item index if ok else -1 is returned -- Database.Add(tabel Database, delimited string Values) function Database.Add(tbDB,strValues) if tbDB then tbDB[Table.Count(tbDB)+1]=strValues return Table.Count(tbDB)-1 else return -1 end end --------------------------------------------------------------------------------------------- -- Updates/edits a row in the database, the row MUST exists, returns true if ok else returns false -- Database.UpdateRow(tabel Database, delimited string Values, number Index) function Database.UpdateRow(tbDB,strValues,nIndex) if tbDB then if tbDB[nIndex+1] then tbDB[nIndex+1]=strValues return true else return false end else return false end end --------------------------------------------------------------------------------------------- -- Updates/edits a row item in the database, the row item MUST exists, returns true if ok else returns false -- Database.UpdateRow(tabel Database, string Value, number Row, number Column) function Database.UpdateRowItem(tbDB,strValue,nRow,nColumn) if tbDB then for index, strRow in tbDB do if index == nRow+1 then tbTmp=DelimitedStringToTable(strRow, Delimiter) if tbTmp[nColumn] then tbDB[nRow+1] = String.Replace(strRow, tbTmp[nColumn], strValue, false); return true else return false end end end else return false end end --------------------------------------------------------------------------------------------- -- Insert a row into the database at the given index, if the index dose not already exist then the item is added to the end of the database, returns item index if ok, else -1 is returned -- Database.Insert(tabel Database, delimited string Values, number Index) function Database.Insert(tbDB,strValues,nIndex) if tbDB then if tbDB[nIndex+1] then Table.Insert(tbDB, nIndex+1, strValues); error = Application.GetLastError(); if (error ~= 0) then return -1 else return nIndex+1 end else Table.Insert(tbDB, Table.Count(tbDB)+1, strValues); error = Application.GetLastError(); if (error ~= 0) then return -1 else return Table.Count(tbDB)-1 end end else return -1 end end --------------------------------------------------------------------------------------------- -- Remove a row from the database, returns true if ok else false is returned -- Database.RemoveRow(tabel Database, number Index) function Database.RemoveRow(tbDB,nIndex) if tbDB[nIndex] then Table.Remove(tbDB, nIndex+1); error = Application.GetLastError(); if (error ~= 0) then return false else return true end else return false end end --------------------------------------------------------------------------------------------- -- Count the columns, returns -1 if any error else the number of columns is returned -- Database.CountColumns(tabel Database) function Database.CountColumns(tbDB) if tbDB then local tbTmp=DelimitedStringToTable(tbDB[1], "|") local tbCount=Table.Count(tbTmp); error = Application.GetLastError(); if (error ~= 0) then return -1 else return tbCount end else return -1 end end --------------------------------------------------------------------------------------------- -- Count the rows, returns -1 if any error else the number of rows is returned -- Database.CountRows(tabel Database) function Database.CountRows(tbDB) if tbDB then local tbCount=Table.Count(tbDB)-1; error = Application.GetLastError(); if (error ~= 0) then return -1 else return tbCount end else return -1 end end --------------------------------------------------------------------------------------------- -- Reads a database row and returns a numerically indexed table of the rows contents, -- the number of table elements will always be equal to the number of columns for that database, -- calling row item 6 when there are only 5 columns will cause errors so be carefull -- if function fails then nil is returned so check for the tabel: -- tbRow = Database.ReadRow(tabel Database, number Index) -- if tbRow then -- Dialog.Message("Item 1", tbRow[1]); -- Dialog.Message("Item 2", tbRow[2]); -- Dialog.Message("Item 3", tbRow[3]); -- Dialog.Message("Item 4", tbRow[4]); -- end function Database.ReadRow(tbDB,nIndex) if tbDB then local tbTmp=DelimitedStringToTable(tbDB[nIndex+1], Delimiter) if tbTmp then return tbTmp else return nil end else return nil end end --------------------------------------------------------------------------------------------- -- Reads a row item from the database and returns the value as a string, if any error a blank string ("") is returned -- Database.ReadRowItem(tabel Database, number Row, number Column) function Database.ReadRowItem(tbDB,nRow,nColumn) if tbDB then for index, strRow in tbDB do if index == nRow+1 then tbTmp=DelimitedStringToTable(strRow, Delimiter) if tbTmp[nColumn] then return tbTmp[nColumn] else return "" end end end else return "" end end --------------------------------------------------------------------------------------------- -- Saves the database to file, if ok the returns true, if any error false is returned -- Database.SaveToFile(tabel Database, string Filepath) function Database.SaveToFile(tbDB,strFilepath) if tbDB then TextFile.WriteFromTable(strFilepath, tbDB, false); -- Test for error error = Application.GetLastError(); if (error ~= 0) then return false else return true end else return false end end --------------------------------------------------------------------------------------------- -- Loads the database from file and returns a handle to it, if any error then -1 is returned -- Database.LoadFromFile(string Filepath) function Database.LoadFromFile(strFilepath) if File.DoesExist(strFilepath) then result = TextFile.ReadToTable(strFilepath); -- Test for error error = Application.GetLastError(); if (error ~= 0) then return -1 else return result end else return -1 end end --------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------- -- END V1.0.0.0 FUCTIONS --------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------- -- SUPPORT FUNCTIONS --------------------------------------------------------------------------------------------- function DelimitedStringToTable(DelimitedString, Delimiter) tbReturn = {}; local strWorking; local nPos = nil; local strData; local nTableIndex = 1; local nDelimiterLength = String.Length(Delimiter); if(nDelimiterLength < 1)then tbReturn[nTableIndex] = DelimitedString; return tbReturn; end strWorking = DelimitedString; nPos = String.Find(strWorking,Delimiter); while(nPos ~= -1)do strData = String.Left(strWorking,nPos-1); tbReturn[nTableIndex] = strData; nTableIndex = nTableIndex + 1; local nLength = String.Length(strWorking); strWorking = String.Right(strWorking,nLength - (nPos + (nDelimiterLength-1))); nPos = String.Find(strWorking,Delimiter); end if(strWorking ~= "")then tbReturn[nTableIndex] = strWorking; end return tbReturn; end --------------------------------------------------------------------------------------------- -- END SUPPORT FUNCTIONS --------------------------------------------------------------------------------------------- --[[ This is the source code for the Database Actions (v1.0.0.1) Action Wizard, to use this code save the contents ov the below comment to a text file and save it as "Database.xml", then copy it to your AMS 6/7 "Data\Actions" folder. Database.RemoveColumn Removes a column from the database, returns true if OK, else false is returned. boolean Database A handle to a open database. tabel 1 db tabel 0,* Column Index The index of the column you want add to remove from the database. number 1 1 number 0,* Database.AddColumn Adds a column to the database, returns column index if OK, else -1 is returned. number Database A handle to a open database. tabel 1 db tabel 0,* Column Name The name of the column you want add to the database. string 1 "New Column" string 0,* Database.GetColumnName Returns the column name from the given index, if any error a blank string ("") is returned. string Database A handle to a open database. tabel 1 db tabel 0,* Column Index The index of the column you want to get the name of. number 1 1 number 0,* Database.SetColumnName Sets the column name at the given index, if OK then true is returned else if any error false is returned. boolean Database A handle to a open database. tabel 1 db tabel 0,* Column Index The index of the column you want to rename. number 1 1 number 0,* Column Name The new name for this column. string 1 "New Column" string 0,* Database.Create Creates a database in memory and returns a handle to it. tabel Column Names A delimited string of the column names, the delimiter MUST be the same as set in Database.SetDelimiter or ("|") is default. string 1 "Item 1|Item 2|Item 3|Item 4" string 0,* Database.Add Adds a item to the end of the database and returns the index of the new item, if any error (-1) is returned. number Database A handle to a open database. tabel 1 db tabel 0,* Values A delimited string of the values to add, the amount of values must match the amount of columns, the delimiter MUST be the same as set in Database.SetDelimiter or ("|") is default. string 1 "Value 1|Value 2|Value 3|Value 4" string 0,* Database.UpdateRow Updates/edits a row in the database, the row MUST exists, returns true if ok else returns false. boolean Database A handle to a open database. tabel 1 db tabel 0,* Values A delimited string of the new row values, the amount of values must match the amount of columns, the delimiter MUST be the same as set in Database.SetDelimiter or ("|") is default. string 1 "Value 1|Value 2|Value 3|Value 4" string 0,* Row Index The index of the row to update/edit. number 1 1 number 0,* Database.UpdateRowItem Updates/edits a item in the given row, the row and column MUST exists, returns true if ok else returns false. boolean Database A handle to a open database. tabel 1 db tabel 0,* Values A delimited string of the new row values, the amount of values must match the amount of columns, the delimiter MUST be the same as set in Database.SetDelimiter or ("|") is default. string 1 "Value 1|Value 2|Value 3|Value 4" string 0,* Row Index The index of the row to update/edit. number 1 1 number 0,* Column Index The index of the column to update/edit. number 1 1 number 0,* Database.Insert Inserts a item into the database at the given index and returns the new items index, if the index is over the total database count then its added to the end, if any error (-1) is returned. number Database A handle to a open database. tabel 1 db tabel 0,* Values A delimited string of the values to add, the amount of values must match the amount of columns, the delimiter MUST be the same as set in Database.SetDelimiter or ("|") is default. string 1 "Value 1|Value 2|Value 3|Value 4" string 0,* Row Index The index of the row to insert. number 1 1 number 0,* Database.RemoveRow Removes a item from the database, returns true if the item was removed else returns false. boolean Database A handle to a open database. tabel 1 db tabel 0,* Row Index The index of the row to remove. number 1 1 number 0,* Database.CountColumns Returns the number of columns, if any error (-1) is returned. number Database A handle to a open database. tabel 1 db tabel 0,* Database.CountRows Returns the number of rows, if any error (-1) is returned. number Database A handle to a open database. tabel 1 db tabel 0,* Database.ReadRow Reads a database row and returns a numerically indexed table of the rows contents, the number of table elements will always be equal to the number of columns for that database, if any error a blank string ("") is returned tabel Database A handle to a open database. tabel 1 db tabel 0,* Row Index The index of the row to read. number 1 1 number 0,* Database.ReadRowItem Reads a database row item and returns the value as a string,if any error a blank string ("") is returned string Database A handle to a open database. tabel 1 db tabel 0,* Row Index The index of the row to read. number 1 1 number 0,* Item Index The index of the item to read. number 1 1 number 0,* Database.SaveToFile Saves a database to file, returns true if ok else false is returned. boolean Database A handle to a open database. tabel 1 db tabel 0,* Filepath The path of the database file string 1 "AutoPlay\\Docs\\MyDB.db" string 0,* Database.LoadFromFile Loads a database into memory and returns a handle to it. tabel Filepath The path of the database file string 1 "AutoPlay\\Docs\\MyDB.db" string 0,* Database.SetDelimiter Loads a database into memory and returns a handle to it. Delimiter Sets the global delimiter of the database. string 1 "|" string 0,* Database.GetDelimiter Gets the global delimiter of the database. string ]]