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 *****************************************************************************/
28 #include "sql_media_library.h"
32 * @brief Generic DELETE function for many medias
33 * Delete a media and all its referencies which don't point
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
41 int Delete( media_library_t *p_ml, vlc_array_t *p_array )
43 char *psz_idlist = NULL, *psz_tmp = NULL;
44 int i_return = VLC_ENOMEM;
46 int i_rows = 0, i_cols = 0;
47 char **pp_results = NULL;
49 if( vlc_array_count( p_array ) <= 0 )
51 i_return = VLC_SUCCESS;
52 goto quit_delete_final;
54 for( int i = 0; i < vlc_array_count( p_array ); i++ )
56 ml_element_t* find = ( ml_element_t * )
57 vlc_array_item_at_index( p_array, i );
58 assert( find->criteria == ML_ID );
61 if( asprintf( &psz_tmp, "( %d", find->value.i ) == -1)
63 goto quit_delete_final;
68 if( asprintf( &psz_tmp, "%s, %d", psz_idlist,
69 find->value.i ) == -1)
71 goto quit_delete_final;
79 if( asprintf( &psz_tmp, "%s )", psz_idlist ? psz_idlist : "(" ) == -1 )
81 goto quit_delete_final;
86 msg_Dbg( p_ml, "Multi Delete id list: %s", psz_idlist );
89 * Below ensures you are emitting media-deleted only
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 )
98 i_return = QuerySimple( p_ml,
99 "DELETE FROM media WHERE media.id IN %s", psz_idlist );
100 if( i_return != VLC_SUCCESS )
103 i_return = QuerySimple( p_ml,
104 "DELETE FROM extra WHERE extra.id IN %s", psz_idlist );
105 if( i_return != VLC_SUCCESS )
109 if( i_return == VLC_SUCCESS )
112 /* Emit delete on var media-deleted */
113 for( int i = 1; i <= i_rows; i++ )
115 var_SetInteger( p_ml, "media-deleted", atoi( pp_results[i*i_cols] ) );
121 FreeSQLResult( p_ml, pp_results );