]> git.sesse.net Git - vlc/blob - modules/media_library/sql_delete.c
I422_YUY2: clobber lists for MMX and SSE2
[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 #include "sql_media_library.h"
29
30
31 /**
32  * @brief Generic DELETE function for many medias
33  * Delete a media and all its referencies which don't point
34  * an anything else.
35  *
36  * @param p_ml This media_library_t object
37  * @param p_array list of ids to delete
38  * @return VLC_SUCCESS or VLC_EGENERIC
39  * TODO: Expand to delete media/artist/album given any params
40  */
41 int Delete( media_library_t *p_ml, vlc_array_t *p_array )
42 {
43     char *psz_idlist = NULL, *psz_tmp = NULL;
44     int i_return = VLC_ENOMEM;
45
46     int i_rows = 0, i_cols = 0;
47     char **pp_results = NULL;
48
49     if( vlc_array_count( p_array ) <= 0 )
50     {
51         i_return = VLC_SUCCESS;
52         goto quit_delete_final;
53     }
54     for( int i = 0; i < vlc_array_count( p_array ); i++ )
55     {
56         ml_element_t* find = ( ml_element_t * )
57             vlc_array_item_at_index( p_array, i );
58         assert( find->criteria == ML_ID );
59         if( !psz_idlist )
60         {
61             if( asprintf( &psz_tmp, "( %d", find->value.i ) == -1)
62             {
63                 goto quit_delete_final;
64             }
65         }
66         else
67         {
68             if( asprintf( &psz_tmp, "%s, %d", psz_idlist,
69                     find->value.i ) == -1)
70             {
71                 goto quit_delete_final;
72             }
73         }
74         free( psz_idlist );
75         psz_idlist = psz_tmp;
76         psz_tmp = NULL;
77     }
78     free( psz_tmp );
79     if( asprintf( &psz_tmp, "%s )", psz_idlist ? psz_idlist : "(" ) == -1 )
80     {
81         goto quit_delete_final;
82     }
83     psz_idlist = psz_tmp;
84     psz_tmp = NULL;
85
86     msg_Dbg( p_ml, "Multi Delete id list: %s", psz_idlist );
87
88     /**
89      * Below ensures you are emitting media-deleted only
90      * for existant media
91      */
92     Begin( p_ml );
93     i_return = Query( p_ml, &pp_results, &i_rows, &i_cols,
94             "SELECT id FROM media WHERE id IN %s", psz_idlist );
95     if( i_return != VLC_SUCCESS )
96         goto quit;
97
98     i_return = QuerySimple( p_ml,
99             "DELETE FROM media WHERE media.id IN %s", psz_idlist );
100     if( i_return != VLC_SUCCESS )
101         goto quit;
102
103     i_return = QuerySimple( p_ml,
104             "DELETE FROM extra WHERE extra.id IN %s", psz_idlist );
105     if( i_return != VLC_SUCCESS )
106         goto quit;
107
108 quit:
109     if( i_return == VLC_SUCCESS )
110     {
111         Commit( p_ml );
112         /* Emit delete on var media-deleted */
113         for( int i = 1; i <= i_rows; i++ )
114         {
115             var_SetInteger( p_ml, "media-deleted", atoi( pp_results[i*i_cols] ) );
116         }
117     }
118     else
119         Rollback( p_ml );
120 quit_delete_final:
121     FreeSQLResult( p_ml, pp_results );
122     free( psz_tmp );
123     free( psz_idlist );
124     return i_return;
125 }