]> git.sesse.net Git - vlc/blob - modules/media_library/sql_delete.c
transform: special cases for packed YUV
[vlc] / modules / media_library / sql_delete.c
1 /*****************************************************************************
2  * sql_delete.c: SQL-based media library: all database delete functions
3  *****************************************************************************
4  * Copyright (C) 2008-2010 The VideoLAN Team and AUTHORS
5  * $Id$
6  *
7  * Authors: Antoine Lejeune <phytos@videolan.org>
8  *          Jean-Philippe André <jpeg@videolan.org>
9  *          Rémi Duraffort <ivoire@videolan.org>
10  *          Adrien Maglo <magsoft@videolan.org>
11  *          Srikanth Raju <srikiraju at gmail dot com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include "sql_media_library.h"
33
34
35 /**
36  * @brief Generic DELETE function for many medias
37  * Delete a media and all its referencies which don't point
38  * an anything else.
39  *
40  * @param p_ml This media_library_t object
41  * @param p_array list of ids to delete
42  * @return VLC_SUCCESS or VLC_EGENERIC
43  * TODO: Expand to delete media/artist/album given any params
44  */
45 int Delete( media_library_t *p_ml, vlc_array_t *p_array )
46 {
47     char *psz_idlist = NULL, *psz_tmp = NULL;
48     int i_return = VLC_ENOMEM;
49
50     int i_rows = 0, i_cols = 0;
51     char **pp_results = NULL;
52
53     if( vlc_array_count( p_array ) <= 0 )
54     {
55         i_return = VLC_SUCCESS;
56         goto quit_delete_final;
57     }
58     for( int i = 0; i < vlc_array_count( p_array ); i++ )
59     {
60         ml_element_t* find = ( ml_element_t * )
61             vlc_array_item_at_index( p_array, i );
62         assert( find->criteria == ML_ID );
63         if( !psz_idlist )
64         {
65             if( asprintf( &psz_tmp, "( %d", find->value.i ) == -1)
66             {
67                 goto quit_delete_final;
68             }
69         }
70         else
71         {
72             if( asprintf( &psz_tmp, "%s, %d", psz_idlist,
73                     find->value.i ) == -1)
74             {
75                 goto quit_delete_final;
76             }
77         }
78         free( psz_idlist );
79         psz_idlist = psz_tmp;
80         psz_tmp = NULL;
81     }
82     free( psz_tmp );
83     if( asprintf( &psz_tmp, "%s )", psz_idlist ? psz_idlist : "(" ) == -1 )
84     {
85         goto quit_delete_final;
86     }
87     psz_idlist = psz_tmp;
88     psz_tmp = NULL;
89
90     msg_Dbg( p_ml, "Multi Delete id list: %s", psz_idlist );
91
92     /**
93      * Below ensures you are emitting media-deleted only
94      * for existant media
95      */
96     Begin( p_ml );
97     i_return = Query( p_ml, &pp_results, &i_rows, &i_cols,
98             "SELECT id FROM media WHERE id IN %s", psz_idlist );
99     if( i_return != VLC_SUCCESS )
100         goto quit;
101
102     i_return = QuerySimple( p_ml,
103             "DELETE FROM media WHERE media.id IN %s", psz_idlist );
104     if( i_return != VLC_SUCCESS )
105         goto quit;
106
107     i_return = QuerySimple( p_ml,
108             "DELETE FROM extra WHERE extra.id IN %s", psz_idlist );
109     if( i_return != VLC_SUCCESS )
110         goto quit;
111
112 quit:
113     if( i_return == VLC_SUCCESS )
114     {
115         Commit( p_ml );
116         /* Emit delete on var media-deleted */
117         for( int i = 1; i <= i_rows; i++ )
118         {
119             var_SetInteger( p_ml, "media-deleted", atoi( pp_results[i*i_cols] ) );
120         }
121     }
122     else
123         Rollback( p_ml );
124 quit_delete_final:
125     FreeSQLResult( p_ml, pp_results );
126     free( psz_tmp );
127     free( psz_idlist );
128     return i_return;
129 }