vendredi 23 octobre 2015

Session variable in SQLite or workaround to create a view variable dependent

I'm trying to improve an existing app that use both sqlite and mysql as database (user choice). It's a video database and player app. Here's a simplified version of my current database

CREATE TABLE movie (idMovie INTEGER PRIMARY KEY, idFile INTEGER, title TEXT, plot TEXT)
CREATE TABLE files (idFile INTEGER PRIMARY KEY, idPath INTEGER, strFilename TEXT, 
                     playCount INTEGER, lastPlayed TEXT, dateAdded TEXT)
CREATE VIEW movie_view AS SELECT movie.*, 
            files.strFileName AS strFileName, 
            files.playCount AS playCount, 
            files.lastPlayed AS lastPlayed,  
            files.dateAdded AS dateAdded 
            FROM movie
                JOIN files ON
                    files.idFile=movie.idFile

The goal of my change is to make some resource user dependent adding an entry for every user and filtering based on the user id. here's the updated database schema without the view:

CREATE TABLE movie     (idMovie INTEGER PRIMARY KEY, idFile INTEGER, title TEXT, plot TEXT)
CREATE TABLE files     (idFile INTEGER PRIMARY KEY, idPath INTEGER, strFilename TEXT, 
                         dateAdded TEXT) 
CREATE TABLE filestate (idFile INTEGER, playCount INTEGER, lastPlayed TEXT, idUser INTEGER)

The problem is that the current code is based heavily on the view stored in the database so I need to update the movie_view query to check for profile id. The problem is that multiple users can access to the database so I can't store the user id in the database or create everytime the views. In mysql the solution is simple.. I can set every session the id as a session variable and then use this view query

CREATE VIEW movie_view AS SELECT movie.*, 
            files.strFileName AS strFileName, 
            filestate.playCount AS playCount, 
            filestate.lastPlayed AS lastPlayed,  
            filestate.dateAdded AS dateAdded 
            FROM movie
                JOIN files ON
                    files.idFile=movie.idFile
                LEFT JOIN filestate ON
                    filestate.idFile = files.idFile AND filestate.idUser = @id

Is there something similar to session variables or some workaround I can use to do it in SQLite?

Aucun commentaire:

Enregistrer un commentaire