1 /*****************************************************************************
2 * sql_delete.c: SQL-based media library: all database delete functions
3 *****************************************************************************
4 * Copyright (C) 2008-2010 The VideoLAN Team and AUTHORS
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>
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.
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.
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 *****************************************************************************/
32 #include "sql_media_library.h"
36 * @brief Generic DELETE function for many medias
37 * Delete a media and all its referencies which don't point
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
45 int Delete( media_library_t *p_ml, vlc_array_t *p_array )
47 char *psz_idlist = NULL, *psz_tmp = NULL;
48 int i_return = VLC_ENOMEM;
50 int i_rows = 0, i_cols = 0;
51 char **pp_results = NULL;
53 if( vlc_array_count( p_array ) <= 0 )
55 i_return = VLC_SUCCESS;
56 goto quit_delete_final;
58 for( int i = 0; i < vlc_array_count( p_array ); i++ )
60 ml_element_t* find = ( ml_element_t * )
61 vlc_array_item_at_index( p_array, i );
62 assert( find->criteria == ML_ID );
65 if( asprintf( &psz_tmp, "( %d", find->value.i ) == -1)
67 goto quit_delete_final;
72 if( asprintf( &psz_tmp, "%s, %d", psz_idlist,
73 find->value.i ) == -1)
75 goto quit_delete_final;
83 if( asprintf( &psz_tmp, "%s )", psz_idlist ? psz_idlist : "(" ) == -1 )
85 goto quit_delete_final;
90 msg_Dbg( p_ml, "Multi Delete id list: %s", psz_idlist );
93 * Below ensures you are emitting media-deleted only
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 )
102 i_return = QuerySimple( p_ml,
103 "DELETE FROM media WHERE media.id IN %s", psz_idlist );
104 if( i_return != VLC_SUCCESS )
107 i_return = QuerySimple( p_ml,
108 "DELETE FROM extra WHERE extra.id IN %s", psz_idlist );
109 if( i_return != VLC_SUCCESS )
113 if( i_return == VLC_SUCCESS )
116 /* Emit delete on var media-deleted */
117 for( int i = 1; i <= i_rows; i++ )
119 var_SetInteger( p_ml, "media-deleted", atoi( pp_results[i*i_cols] ) );
125 FreeSQLResult( p_ml, pp_results );