Add a ProRes fourCC
[vlc.git] / modules / media_library / sql_delete.c
blob10d4bf8b51fbc4520dac53e28589e6fab9ea6eb6
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$
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 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "sql_media_library.h"
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.
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 );
63 if( !psz_idlist )
65 if( asprintf( &psz_tmp, "( %d", find->value.i ) == -1)
67 goto quit_delete_final;
70 else
72 if( asprintf( &psz_tmp, "%s, %d", psz_idlist,
73 find->value.i ) == -1)
75 goto quit_delete_final;
78 free( psz_idlist );
79 psz_idlist = psz_tmp;
80 psz_tmp = NULL;
82 free( psz_tmp );
83 if( asprintf( &psz_tmp, "%s )", psz_idlist ? psz_idlist : "(" ) == -1 )
85 goto quit_delete_final;
87 psz_idlist = psz_tmp;
88 psz_tmp = NULL;
90 msg_Dbg( p_ml, "Multi Delete id list: %s", psz_idlist );
92 /**
93 * Below ensures you are emitting media-deleted only
94 * for existant media
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;
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;
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;
112 quit:
113 if( i_return == VLC_SUCCESS )
115 Commit( p_ml );
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] ) );
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;