1 /*****************************************************************************
2 * sql_search.c: SQL-based media library: all find/get 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"
34 int Find( media_library_t *p_ml, vlc_array_t *p_result_array, ... )
39 va_start( args, p_result_array );
40 returned = FindVa( p_ml, p_result_array, args );
47 * @brief Generic find in Media Library, returns arrays of psz or int
49 * @param p_ml the media library object
50 * @param result A pointer to a result array
51 * @param criterias list of criterias used in SELECT
52 * @return VLC_SUCCESS or VLC_EGENERIC
54 int FindVa( media_library_t *p_ml,
55 vlc_array_t *p_result_array, va_list criterias )
57 int i_ret = VLC_SUCCESS;
59 ml_result_type_e result_type;
60 char **pp_results = NULL;
66 i_ret = BuildSelectVa( p_ml, &psz_query, &result_type, criterias );
67 if( i_ret != VLC_SUCCESS )
70 if( Query( p_ml, &pp_results, &i_rows, &i_cols, "%s", psz_query )
73 msg_Err( p_ml, "Error occurred while making the query to the database" );
77 i_ret = SQLToResultArray( p_ml, p_result_array, pp_results, i_rows, i_cols,
81 FreeSQLResult( p_ml, pp_results );
87 * @brief Generic find in Media Library, returns arrays of psz or int
89 * @param p_ml the media library object
90 * @param result a pointer to a result array
91 * @param selected_type the type of the element we're selecting
92 * @param criterias list of criterias used in SELECT
93 * @return VLC_SUCCESS or VLC_EGENERIC
96 int FindAdv( media_library_t *p_ml, vlc_array_t *p_result_array,
97 ml_select_e selected_type, const char* psz_lvalue, ml_ftree_t *tree )
99 int i_ret = VLC_SUCCESS;
101 ml_result_type_e result_type;
102 char **pp_results = NULL;
105 if( !p_result_array )
108 i_ret = BuildSelect( p_ml, &psz_query, &result_type, psz_lvalue,
109 selected_type, tree );
111 if( i_ret != VLC_SUCCESS )
114 if( Query( p_ml, &pp_results, &i_rows, &i_cols, "%s", psz_query )
117 msg_Err( p_ml, "Error occurred while making the query to the database" );
121 i_ret = SQLToResultArray( p_ml, p_result_array, pp_results, i_rows, i_cols,
125 FreeSQLResult( p_ml, pp_results );
131 * @brief Generic SELECT query builder with va_list parameter
133 * @param p_ml This media_library_t object
134 * @param ppsz_query *ppsz_query will contain query
135 * @param p_result_type see enum ml_result_type_e
136 * @param criterias list of criterias used in SELECT
137 * @return VLC_SUCCESS or a VLC error code
138 * NOTE va_list criterias must end with ML_END or this will fail (segfault)
140 * This function handles results of only one column (or two if ID is included),
141 * of 'normal' types: int and strings
143 int BuildSelectVa( media_library_t *p_ml, char **ppsz_query,
144 ml_result_type_e *p_result_type, va_list criterias )
147 ml_ftree_t* p_ftree = NULL;
148 char* psz_lvalue = NULL;
150 /* Get the name of the data we want */
151 ml_select_e selected_type = va_arg( criterias, int );
152 if( selected_type == ML_PEOPLE || selected_type == ML_PEOPLE_ID ||
153 selected_type == ML_PEOPLE_ROLE )
154 psz_lvalue = va_arg( criterias, char * );
156 /* Loop on every arguments */
159 ml_ftree_t *p_find = ( ml_ftree_t* ) calloc( 1, sizeof( ml_ftree_t ) );
162 p_find->criteria = va_arg( criterias, int );
163 p_find->comp = ML_COMP_EQUAL;
164 switch( p_find->criteria )
167 p_ftree = ml_FtreeSpecAsc( p_ftree, va_arg( criterias, char* ) ); break;
169 p_ftree = ml_FtreeSpecDesc( p_ftree, va_arg( criterias, char* ) ); break;
171 p_ftree = ml_FtreeSpecDistinct( p_ftree ); break;
173 p_ftree = ml_FtreeSpecLimit( p_ftree, va_arg( criterias, int ) );
176 /* This is OK because of a shallow free find */
177 p_find->lvalue.str = (char *)ML_PERSON_ARTIST;
178 p_find->value.str = va_arg( criterias, char* );
179 p_ftree = ml_FtreeFastAnd( p_ftree, p_find );
182 p_find->lvalue.str = va_arg( criterias, char* );
183 p_find->value.str = va_arg( criterias, char* );
184 p_ftree = ml_FtreeFastAnd( p_ftree, p_find );
187 p_find->lvalue.str = va_arg( criterias, char* );
188 p_find->value.i = va_arg( criterias, int );
189 p_ftree = ml_FtreeFastAnd( p_ftree, p_find );
195 switch( ml_AttributeIsString( p_find->criteria ) )
198 p_find->value.i = va_arg( criterias, int );
201 p_find->value.str = va_arg( criterias, char* );
204 p_ftree = ml_FtreeFastAnd( p_ftree, p_find );
209 int i_ret = BuildSelect( p_ml, ppsz_query, p_result_type, psz_lvalue,
210 selected_type, p_ftree );
212 ml_ShallowFreeFindTree( p_ftree );
217 * @brief Append a string and format it using SQL vmprintf
219 static int AppendStringFmtVa( media_library_t *p_ml,
220 char **ppsz_dst, const char *psz_fmt,
223 char *psz_tmp = NULL, *psz_fullexp = NULL;
225 assert( ppsz_dst != NULL );
230 *ppsz_dst = sql_VPrintf( p_ml->p_sys->p_sql, psz_fmt, args );
236 /* Create new expression B */
237 psz_tmp = sql_VPrintf( p_ml->p_sys->p_sql, psz_fmt, args );
241 if( asprintf( &psz_fullexp, "%s%s", *ppsz_dst, psz_tmp ) == -1 )
248 *ppsz_dst = psz_fullexp;
254 static int AppendStringFmt( media_library_t *p_ml,
255 char **ppsz_dst, const char *psz_fmt, ... )
258 va_start( args, psz_fmt );
259 int i_ret = AppendStringFmtVa( p_ml, ppsz_dst, psz_fmt, args );
264 /* Early Declaration of Where String Generator */
265 static int BuildWhere( media_library_t* p_ml, char **ppsz_where, ml_ftree_t* tree,
266 char** sort, int* limit, const char** distinct, char*** pppsz_frompersons,
267 int* i_frompersons, int* join );
269 # define table_media (1 << 0)
270 # define table_album (1 << 1)
271 # define table_people (1 << 2)
272 # define table_extra (1 << 3)
274 static void PackFromPersons( char*** pppsz_frompersons, int i_num_frompersons )
276 for( int i = 0; i < i_num_frompersons; i++ )
278 if( *pppsz_frompersons[i] == NULL )
280 for( int j = i+1; j < i_num_frompersons; j++ )
282 if( strcmp( *pppsz_frompersons[i], *pppsz_frompersons[j] ) == 0 )
284 *pppsz_frompersons[j] = NULL;
290 * @brief Generic SELECT query builder
292 * @param p_ml This media_library_t object
293 * @param ppsz_query *ppsz_query will contain query
294 * @param p_result_type see enum ml_result_type_e
295 * @param selected_type the type of the element we're selecting
296 * @param tree the find tree
297 * @return VLC_SUCCESS or VLC_EGENERIC
299 int BuildSelect( media_library_t *p_ml,
300 char **ppsz_query, ml_result_type_e *p_result_type,
301 const char *psz_selected_type_lvalue, ml_select_e selected_type,
304 /* Basic verification */
308 int i_ret = VLC_SUCCESS;
309 char *psz_query = NULL;
311 /* Building psz_query :
312 psz_query = "SELECT psz_distinct psz_select
313 FROM psz_from [JOIN psz_join ON psz_on]
314 [JOIN psz_join2 ON psz_on2]
315 [WHERE psz_where[i] [AND psz_where[j] ...]]
316 [LIMIT psz_limit] [ORDER BY psz_select psz_sort] ;"
318 char *psz_select = NULL;
319 const char *psz_distinct = ""; /* "DISTINCT" or "" */
322 char *psz_from = NULL;
323 int i_from = 0; /* Main select table */
325 char **ppsz_frompersons = NULL;
326 int i_num_frompersons = 0;
327 char *psz_peoplerole = NULL; /* Person to get selected */
329 /* JOIN ... ON ... */
330 char *psz_join = NULL;
331 char *psz_join2 = NULL;
333 char *psz_on2 = NULL;
334 int i_join = 0; /* Tables that need to be joined */
337 char *psz_where = NULL;
338 char *psz_sort = NULL; /* ASC or DESC or NULL */
339 char *psz_tmp = NULL;
343 /* Build the WHERE condition */
344 BuildWhere( p_ml, &psz_where, tree, &psz_sort, &i_limit,
345 &psz_distinct, &ppsz_frompersons, &i_num_frompersons, &i_join );
347 PackFromPersons( &ppsz_frompersons, i_num_frompersons );
349 /* What is the result type? */
350 ml_result_type_e res_type = ML_TYPE_PSZ;
353 /* Note that a DISTINCT select makes id of result non sense */
354 switch( selected_type )
357 psz_select = ( !*psz_distinct ) ?
358 strdup( "album.id, album.title AS album_title" )
359 : strdup( "album.title AS album_title" );
360 i_from = table_album;
363 psz_select = ( !*psz_distinct ) ?
364 strdup( "album.id, album.cover" ) : strdup( "album.cover" );
365 i_from = table_album;
368 psz_select = strdup( "album.id" );
369 psz_distinct = "DISTINCT";
370 i_from = table_album;
371 res_type = ML_TYPE_INT;
374 psz_select = ( !*psz_distinct ) ?
375 strdup( "people_Artist.id, people_Artist.name" )
376 : strdup( "people_Artist.name" );
377 i_from = table_people;
378 psz_peoplerole = strdup( ML_PERSON_ARTIST );
381 psz_select = strdup( "people_Artist.id" );
382 psz_distinct = "DISTINCT";
383 i_from = table_people;
384 res_type = ML_TYPE_INT;
385 psz_peoplerole = strdup( ML_PERSON_ARTIST );
388 psz_select = ( !*psz_distinct ) ?
389 strdup( "media.id, media.cover" ) : strdup( "media.cover" );
390 i_from = table_media;
393 psz_select = ( !*psz_distinct ) ?
394 strdup( "media.id, extra.comment" ) : strdup( "extra.comment" );
395 i_from = table_extra;
398 psz_select = ( !*psz_distinct ) ?
399 strdup( "media.id, media.genre" ) : strdup( "media.genre" );
400 i_from = table_media;
403 psz_select = ( !*psz_distinct ) ?
404 strdup( "COUNT()" ) : strdup( "COUNT( DISTINCT media.id )" );
405 i_from = table_media;
406 res_type = ML_TYPE_INT;
409 psz_select = ( !*psz_distinct ) ?
410 strdup( "COUNT()" ) : strdup( "COUNT( DISTINCT album.id )" );
411 i_from = table_album;
412 res_type = ML_TYPE_INT;
414 case ML_COUNT_PEOPLE:
415 psz_select = ( !*psz_distinct ) ?
416 strdup( "COUNT()" ) : strdup( "COUNT( DISTINCT people.id )" );
417 i_from = table_people;
418 res_type = ML_TYPE_INT;
421 psz_select = strdup( "media.filesize" );
422 i_from = table_media;
423 res_type = ML_TYPE_INT;
426 psz_select = strdup( "media.id" ); /* ID: must be distinct */
427 psz_distinct = "DISTINCT";
428 i_from = table_media;
429 res_type = ML_TYPE_INT;
432 psz_select = strdup( "extra.language" );
433 psz_distinct = "DISTINCT";
434 i_from = table_extra;
436 case ML_MEDIA_SPARSE:
437 i_ret = AppendStringFmt( p_ml, &psz_select, "media.id AS id,"
439 "media.type AS type,"
440 "media.title AS title,"
441 "media.duration AS duration,"
442 "media.original_title AS original_title,"
443 "media.album_id AS album_id,"
444 "media.cover AS cover,"
445 "media.preview AS preview,"
446 "media.disc AS disc,"
447 "media.track AS track,"
448 "media.year AS year,"
449 "media.genre AS genre,"
450 "media.played_count AS played_count,"
451 "media.last_played AS last_played,"
452 "media.first_played AS first_played,"
453 "media.import_time AS import_time,"
454 "media.skipped_count AS skipped_count,"
455 "media.last_skipped AS last_skipped,"
456 "media.vote AS vote,"
457 "media.score AS score,"
458 "media.comment AS comment,"
459 "media.filesize AS filesize,"
460 "album.title AS album_title,"
461 "album.cover AS album_cover,"
462 "(SELECT name FROM media_to_people JOIN people "
463 "ON (people_id = id) WHERE media_id = media.id AND role = %Q LIMIT 1) AS people_%s",
464 ML_PERSON_ARTIST, ML_PERSON_ARTIST );
465 if( i_ret != VLC_SUCCESS )
467 i_from = table_media;
468 i_join |= ( table_album | table_people );
469 psz_distinct = "DISTINCT";
470 res_type = ML_TYPE_MEDIA;
473 /* Who said this was over-complicated ?? */
475 psz_select = strdup( "media.id AS id,"
477 "media.type AS type,"
478 "media.title AS title,"
479 "media.duration AS duration,"
480 "media.original_title AS original_title,"
481 "media.album_id AS album_id,"
482 "media.cover AS cover,"
483 "media.preview AS preview,"
484 "media.disc as disc,"
485 "media.track AS track,"
486 "media.year AS year,"
487 "media.genre AS genre,"
488 "media.played_count AS played_count,"
489 "media.last_played AS last_played,"
490 "media.first_played AS first_played,"
491 "media.import_time AS import_time,"
492 "media.last_skipped AS last_skipped,"
493 "media.skipped_count AS skipped_count,"
494 "media.vote AS vote,"
495 "media.score AS score,"
496 "media.comment AS comment,"
497 "media.filesize AS filesize,"
498 "album.title AS album_title,"
499 "album.cover AS album_cover,"
500 "people.id AS people_id,"
501 "people.name AS people_name,"
502 "people.role AS people_role,"
503 "extra.language AS language,"
504 "extra.extra AS extra" );
505 i_from = table_media;
506 i_join |= ( table_album | table_people | table_extra );
507 psz_distinct = "DISTINCT";
508 res_type = ML_TYPE_MEDIA;
511 psz_select = strdup( "media.id AS id,"
512 "people.id AS people_id,"
513 "people.name AS people_name,"
514 "people.role AS people_role,"
515 "extra.extra AS extra,"
516 "extra.language AS language" );
517 i_from = table_media;
518 i_join |= ( table_album | table_people | table_extra );
519 psz_distinct = "DISTINCT";
520 res_type = ML_TYPE_MEDIA;
522 case ML_ORIGINAL_TITLE:
523 psz_select = ( !*psz_distinct ) ?
524 strdup( "media.id, media.original_title" ) : strdup( "media.original_title" );
525 i_from = table_media;
527 /* For people, if lvalue = "", then we want ANY people. */
529 assert( psz_selected_type_lvalue );
530 i_ret = AppendStringFmt( p_ml, &psz_select, "people%s%s.name",
531 *psz_selected_type_lvalue ? "_" : "",
532 *psz_selected_type_lvalue ? psz_selected_type_lvalue : "" );
533 if( i_ret != VLC_SUCCESS )
537 i_ret = AppendStringFmt( p_ml, &psz_select, ", people%s%s.name",
538 *psz_selected_type_lvalue ? "_" : "",
539 *psz_selected_type_lvalue ? psz_selected_type_lvalue : "" );
540 if( i_ret != VLC_SUCCESS )
543 i_from = table_people;
544 psz_peoplerole = strdup( psz_selected_type_lvalue );
547 assert( psz_selected_type_lvalue );
548 i_ret = AppendStringFmt( p_ml, &psz_select, "people%s%s.id",
549 *psz_selected_type_lvalue ? "_" : "",
550 *psz_selected_type_lvalue ? psz_selected_type_lvalue : "" );
551 if( i_ret != VLC_SUCCESS )
555 i_ret = AppendStringFmt( p_ml, &psz_select, ", people%s%s.id",
556 *psz_selected_type_lvalue ? "_" : "",
557 *psz_selected_type_lvalue ? psz_selected_type_lvalue : "" );
558 if( i_ret != VLC_SUCCESS )
561 psz_distinct = "DISTINCT";
562 i_from = table_people;
563 psz_peoplerole = strdup( psz_selected_type_lvalue );
564 res_type = ML_TYPE_INT;
567 psz_select = strdup( "people.role" );
568 psz_distinct = "DISTINCT";
569 i_from = table_people;
572 psz_select = ( !*psz_distinct ) ?
573 strdup( "media.id, media.title" ) : strdup( "media.title" );
574 i_from = table_media;
577 psz_select = ( !*psz_distinct ) ?
578 strdup( "media.id, media.type" ): strdup( "media.type" );
579 i_from = table_media;
580 res_type = ML_TYPE_INT;
583 psz_select = ( !*psz_distinct ) ?
584 strdup( "media.id, media.uri" ) : strdup( "media.uri" );
585 i_from = table_media;
588 psz_select = ( !*psz_distinct ) ?
589 strdup( "media.id, media.vote" ) : strdup( "media.vote" );
590 i_from = table_media;
591 res_type = ML_TYPE_INT;
594 psz_select = ( !*psz_distinct ) ?
595 strdup( "media.id, media.year" ) : strdup( "media.year" );
596 i_from = table_media;
597 res_type = ML_TYPE_INT;
604 msg_Dbg( p_ml, "unknown select (%d) in BuildSelect", selected_type );
608 /* Let's build full psz_query ! */
611 /* Figure out select and join tables */
620 case 2: i_join = 0; break;
622 case 3: i_from = table_media; i_join = table_album; break;
626 case 7: i_from = table_media; i_join = table_album | table_people; break;
630 case 11: i_from = table_media; i_join = table_extra | table_album; break;
634 case 15: i_from = table_media; i_join = table_extra | table_album | table_people; break;
642 case 1: i_from = table_media; i_join = table_people; break;
644 case 3: i_from = table_media; i_join = table_album | table_people; break;
646 /* Determine if a join from media is required */
647 if( i_num_frompersons > 1 )
648 i_from = table_media;
652 case 5: i_from = table_media; i_join = table_people; break;
654 case 7: i_from = table_media; i_join = table_album | table_people; break;
656 case 9: i_from = table_media; i_join = table_people | table_extra; break;
658 case 11: i_from = table_media; i_join = table_people | table_album | table_extra; break;
660 case 13: i_from = table_media; i_join = table_people | table_extra; break;
662 case 15: i_from = table_media; i_join = table_people | table_album | table_extra; break;
670 case 1: i_from = table_media; i_join = table_extra; break;
672 case 3: i_from = table_media; i_join = table_extra | table_album; break;
674 case 5: i_from = table_media; i_join = table_extra | table_people; break;
676 case 7: i_from = table_media; i_join = table_extra | table_people | table_album; break;
677 case 8: i_from = table_extra; i_join = 0; break;
678 case 9: i_from = table_media; i_join = table_extra; break;
680 case 11: i_from = table_media; i_join = table_extra | table_album; break;
682 case 13: i_from = table_media; i_join = table_extra | table_people; break;
684 case 15: i_from = table_media; i_join = table_extra | table_people | table_album; break;
688 default: msg_Warn( p_ml, "You can't be selecting from this table!!" );
689 i_ret = VLC_EGENERIC;
693 assert( !( i_from & table_album && i_join & table_album ) );
694 assert( !( i_from & table_people && i_join & table_people ) );
695 assert( !( i_from & table_extra && i_join & table_extra ) );
697 /* Generate FROM - psz_from */
698 if( i_from == table_media )
699 i_ret = AppendStringFmt( p_ml, &psz_from, "media" );
700 else if( i_from == table_album )
701 i_ret = AppendStringFmt( p_ml, &psz_from, "album" );
702 else if( i_from == table_extra )
703 i_ret = AppendStringFmt( p_ml, &psz_from, "extra" );
704 else if( i_from == table_people )
706 i_ret = AppendStringFmt( p_ml, &psz_from, "people AS people%s%s",
707 psz_peoplerole ? "_" : "", psz_peoplerole );
708 if( i_ret < 0 ) goto exit;
710 /* The ugly next statement is only required if persons are being
711 * selected. Otherwise the joins will handle this */
712 if( psz_peoplerole && *psz_peoplerole )
714 i_ret = AppendStringFmt( p_ml, &psz_where, "%s people_%s.role = %Q ",
715 ( psz_where && *psz_where ) ? " AND" : "",
716 psz_peoplerole, psz_peoplerole );
717 if( i_ret < 0 ) goto exit;
720 if( i_ret < 0 ) goto exit;
722 i_ret = AppendStringFmt( p_ml, &psz_query,
723 "SELECT %s %s ", psz_distinct, psz_select );
724 if( i_ret < 0 ) goto exit;
726 i_ret = AppendStringFmt( p_ml, &psz_query, "FROM %s ", psz_from );
727 if( i_ret < 0 ) goto exit;
729 /* Create join conditions */
730 if( i_join & table_people )
732 /* we can join psz_peoplerole safely because
733 * if i_join = people, then i_from != people */
735 for( int i = 0; i < i_num_frompersons ; i++ )
737 /* We assume ppsz_frompersons has unique entries and
738 * if ppsz_frompersons[i] is empty(but not NULL), then it
739 * means we accept any role */
740 if( ppsz_frompersons[i] && *ppsz_frompersons[i] )
742 if( strcmp( psz_peoplerole, ppsz_frompersons[i] ) == 0 )
744 AppendStringFmt( p_ml, &psz_join, "%smedia_to_people AS people_%sx ",
745 psz_join == NULL ? "" : ",", ppsz_frompersons[i] );
746 /* This is possible because from is usually the media table */
747 AppendStringFmt( p_ml, &psz_on, "%speople_%sx.media_id = media.id ",
748 psz_on == NULL ? "" : " AND ", ppsz_frompersons[i] );
749 AppendStringFmt( p_ml, &psz_join2, "%speople AS people_%s ",
750 psz_join2 == NULL ? "" : ",", ppsz_frompersons[i] );
751 AppendStringFmt( p_ml, &psz_on2, "%s ( people_%sx.people_id = people_%s.id AND "
752 "people_%s.role = %Q )", psz_on2 == NULL ? "" : " AND ",
753 ppsz_frompersons[i], ppsz_frompersons[i],
754 ppsz_frompersons[i], ppsz_frompersons[i] );
756 else if( ppsz_frompersons[i] )
758 if( strcmp( psz_peoplerole, ppsz_frompersons[i] ) == 0 )
760 AppendStringFmt( p_ml, &psz_join, "%smedia_to_people AS peoplex ",
761 psz_join == NULL ? "" : "," );
762 /* This is possible because from is usually the media table */
763 AppendStringFmt( p_ml, &psz_on, "%speoplex.media_id = media.id ",
764 psz_on == NULL ? "" : " AND " );
765 AppendStringFmt( p_ml, &psz_join2, "%speople AS people ",
766 psz_join2 == NULL ? "" : "," );
767 AppendStringFmt( p_ml, &psz_on2, "%s peoplex.people_id = people.id",
768 psz_on2 == NULL ? "" : " AND " );
773 if( psz_peoplerole && *psz_peoplerole )
775 AppendStringFmt( p_ml, &psz_join, "%smedia_to_people AS people_%sx ",
776 psz_join == NULL ? "" : ",", psz_peoplerole );
777 /* This is possible because from is always the media table */
778 AppendStringFmt( p_ml, &psz_on, "%speople_%sx.media_id = media.id ",
779 psz_on == NULL ? "" : " AND ", psz_peoplerole );
780 AppendStringFmt( p_ml, &psz_join2, "%speople AS people_%s ",
781 psz_join2 == NULL ? "" : ",", psz_peoplerole );
782 AppendStringFmt( p_ml, &psz_on2, "%s ( people_%sx.people_id = people_%s.id AND "
783 "people_%s.role = %Q )", psz_on2 == NULL ? "" : " AND ",
784 psz_peoplerole, psz_peoplerole,
785 psz_peoplerole, psz_peoplerole );
789 AppendStringFmt( p_ml, &psz_join, "%smedia_to_people AS peoplex ",
790 psz_join == NULL ? "" : "," );
791 /* This is possible because from is usually the media table */
792 AppendStringFmt( p_ml, &psz_on, "%speoplex.media_id = media.id ",
793 psz_on == NULL ? "" : " AND " );
794 AppendStringFmt( p_ml, &psz_join2, "%speople ",
795 psz_join2 == NULL ? "" : "," );
796 AppendStringFmt( p_ml, &psz_on2, "%s peoplex.people_id = people.id",
797 psz_on2 == NULL ? "" : " AND " );
802 if( i_join & table_album )
804 AppendStringFmt( p_ml, &psz_join, "%salbum", psz_join == NULL ? "" : "," );
805 AppendStringFmt( p_ml, &psz_on, "%s album.id = media.album_id ",
806 psz_on == NULL ? "" : " AND " );
808 if( i_join & table_extra )
810 AppendStringFmt( p_ml, &psz_join, "%sextra", psz_join == NULL ? "" : "," );
811 AppendStringFmt( p_ml, &psz_on, "%s extra.id = media.id ",
812 psz_on == NULL ? "" : " AND " );
815 /* Complete the join clauses */
818 AppendStringFmt( p_ml, &psz_query,
819 "JOIN %s ON %s ", psz_join, psz_on );
823 AppendStringFmt( p_ml, &psz_query,
824 "JOIN %s ON %s ", psz_join2, psz_on2 );
826 if( psz_where && *psz_where )
828 AppendStringFmt( p_ml, &psz_query,
829 "WHERE %s ", psz_where );
831 /* TODO: FIXME: Limit on media objects doesn't work! */
834 AppendStringFmt( p_ml, &psz_query,
835 "LIMIT %d ", i_limit );
840 AppendStringFmt( p_ml, &psz_query,
841 "ORDER BY %s %s", psz_select, psz_sort );
844 if( i_ret > 0 ) i_ret = VLC_SUCCESS;
846 if( p_result_type ) *p_result_type = res_type;
847 if( !psz_query ) i_ret = VLC_EGENERIC;
848 else *ppsz_query = strdup( psz_query );
860 free( psz_peoplerole );
861 free( ppsz_frompersons );
863 if( i_ret != VLC_SUCCESS )
864 msg_Warn( p_ml, "an unknown error occurred (%d)", i_ret );
870 #define CASE_INT( casestr, fmt, table ) \
872 assert( tree->comp != ML_COMP_HAS && tree->comp != ML_COMP_STARTS_WITH \
873 && tree->comp != ML_COMP_ENDS_WITH ); \
874 *ppsz_where = sql_Printf( p_ml->p_sys->p_sql, "%s %s %d", fmt, \
875 tree->comp == ML_COMP_LESSER ? "<" : \
876 tree->comp == ML_COMP_LESSER_OR_EQUAL ? "<=" : \
877 tree->comp == ML_COMP_GREATER ? ">" : \
878 tree->comp == ML_COMP_GREATER_OR_EQUAL ? ">=" : "=", tree->value.i ); \
879 if( *ppsz_where == NULL ) \
885 #define CASE_PSZ( casestr, fmt, table ) \
887 assert( tree->comp == ML_COMP_HAS || tree->comp == ML_COMP_EQUAL \
888 || tree->comp == ML_COMP_STARTS_WITH \
889 || tree->comp == ML_COMP_ENDS_WITH ); \
890 *ppsz_where = sql_Printf( p_ml->p_sys->p_sql, "%s LIKE '%s%q%s'", fmt, \
891 tree->comp == ML_COMP_HAS \
892 || tree->comp == ML_COMP_STARTS_WITH? "%%" : "", \
894 tree->comp == ML_COMP_HAS \
895 || tree->comp == ML_COMP_ENDS_WITH? "%%" : "" ); \
896 if( *ppsz_where == NULL ) \
901 #define SLDPJ sort, limit, distinct, pppsz_frompersons, i_frompersons, join
902 static int BuildWhere( media_library_t* p_ml, char **ppsz_where, ml_ftree_t* tree,
903 char** sort, int* limit, const char** distinct,
904 char*** pppsz_frompersons, int* i_frompersons, int* join )
906 assert( ppsz_where && sort && distinct );
907 if( !tree ) /* Base case */
912 int i_ret = VLC_EGENERIC;
913 char* psz_left = NULL;
914 char* psz_right = NULL;
920 i_ret = BuildWhere( p_ml, &psz_left, tree->left, SLDPJ );
921 if( i_ret != VLC_SUCCESS )
923 i_ret = BuildWhere( p_ml, &psz_right, tree->right, SLDPJ );
924 if( i_ret != VLC_SUCCESS )
926 if( psz_left == NULL || psz_right == NULL )
928 msg_Err( p_ml, "Parsing failed for AND/OR" );
929 i_ret = VLC_EGENERIC;
932 if( asprintf( ppsz_where, "( %s %s %s )", psz_left,
933 ( tree->op == ML_OP_AND ? "AND" : "OR" ), psz_right ) == -1 )
940 i_ret = BuildWhere( p_ml, &psz_left, tree->left, SLDPJ );
941 if( i_ret != VLC_SUCCESS )
943 if( psz_left == NULL )
945 msg_Err( p_ml, "Parsing failed at NOT" );
946 i_ret = VLC_EGENERIC;
949 if( asprintf( ppsz_where, "( NOT %s )", psz_left ) == -1 )
956 i_ret = BuildWhere( p_ml, &psz_right, tree->right, SLDPJ );
957 if( i_ret != VLC_SUCCESS )
959 i_ret = BuildWhere( p_ml, &psz_left, tree->left, SLDPJ );
960 if( i_ret != VLC_SUCCESS )
962 /* Ignore right parse tree as this is a special node */
963 *ppsz_where = strdup( psz_left ? psz_left : "" );
971 switch( tree->criteria )
974 assert( tree->comp == ML_COMP_HAS
975 || tree->comp == ML_COMP_EQUAL
976 || tree->comp == ML_COMP_STARTS_WITH
977 || tree->comp == ML_COMP_ENDS_WITH );
978 *ppsz_where = sql_Printf( p_ml->p_sys->p_sql,
979 "people%s%s.name LIKE '%s%q%s'",
980 tree->lvalue.str ? "_" : "",
981 tree->lvalue.str ? tree->lvalue.str : "",
982 tree->comp == ML_COMP_HAS
983 || tree->comp == ML_COMP_STARTS_WITH ? "%%" : "",
985 tree->comp == ML_COMP_HAS
986 || tree->comp == ML_COMP_ENDS_WITH ? "%%" : "" );
987 if( *ppsz_where == NULL )
989 *pppsz_frompersons = realloc( *pppsz_frompersons,
990 ++*i_frompersons * sizeof( char* ) );
991 *pppsz_frompersons[ *i_frompersons - 1 ] = tree->lvalue.str;
992 *join |= table_people;
995 assert( tree->comp == ML_COMP_EQUAL );
996 *ppsz_where = sql_Printf( p_ml->p_sys->p_sql,
997 "( people%s%s.id = %d )", tree->lvalue.str ? "_":"",
998 tree->lvalue.str ? tree->lvalue.str:"",
1000 if( *ppsz_where == NULL )
1002 *pppsz_frompersons = realloc( *pppsz_frompersons,
1003 ++*i_frompersons * sizeof( char* ) );
1004 *pppsz_frompersons[ *i_frompersons - 1 ] = tree->lvalue.str;
1005 *join |= table_people;
1007 case ML_PEOPLE_ROLE:
1008 assert( tree->comp == ML_COMP_HAS
1009 || tree->comp == ML_COMP_EQUAL
1010 || tree->comp == ML_COMP_STARTS_WITH
1011 || tree->comp == ML_COMP_ENDS_WITH );
1012 *ppsz_where = sql_Printf( p_ml->p_sys->p_sql,
1013 "people%s%s.role LIKE '%s%q%s'",
1014 tree->lvalue.str ? "_" : "",
1015 tree->lvalue.str ? tree->lvalue.str : "",
1016 tree->comp == ML_COMP_HAS
1017 || tree->comp == ML_COMP_STARTS_WITH ? "%%" : "",
1019 tree->comp == ML_COMP_HAS
1020 || tree->comp == ML_COMP_ENDS_WITH ? "%%" : "" );
1021 if( *ppsz_where == NULL )
1023 *pppsz_frompersons = realloc( *pppsz_frompersons,
1024 ++*i_frompersons * sizeof( char* ) );
1025 *pppsz_frompersons[ *i_frompersons - 1 ] = tree->lvalue.str;
1026 *join |= table_people;
1028 CASE_PSZ( ML_ALBUM, "album.title", table_album );
1029 CASE_PSZ( ML_ALBUM_COVER, "album.cover", table_album );
1031 assert( tree->comp == ML_COMP_EQUAL );
1032 *ppsz_where = sql_Printf( p_ml->p_sys->p_sql,
1033 "album.id = %d", tree->value.i );
1034 if( *ppsz_where == NULL )
1036 *join |= table_album;
1038 CASE_PSZ( ML_COMMENT, "media.comment", table_media );
1039 CASE_PSZ( ML_COVER, "media.cover", table_media );
1040 CASE_INT( ML_DURATION, "media.duration", table_media );
1041 CASE_PSZ( ML_EXTRA, "extra.extra", table_extra );
1042 CASE_INT( ML_FILESIZE, "media.filesize", table_media );
1043 CASE_PSZ( ML_GENRE, "media.genre", table_media );
1045 assert( tree->comp == ML_COMP_EQUAL );
1046 *ppsz_where = sql_Printf( p_ml->p_sys->p_sql,
1047 "media.id = %d", tree->value.i );
1048 if( *ppsz_where == NULL )
1050 *join |= table_media;
1052 CASE_PSZ( ML_LANGUAGE, "extra.language", table_extra );
1053 CASE_INT( ML_LAST_PLAYED, "media.last_played", table_media );
1054 CASE_PSZ( ML_ORIGINAL_TITLE, "media.original_title", table_media );
1055 msg_Warn( p_ml, "Deprecated Played Count tags" );
1056 CASE_INT( ML_PLAYED_COUNT, "media.played_count", table_media );
1057 CASE_INT( ML_SCORE, "media.score", table_media );
1058 CASE_PSZ( ML_TITLE, "media.title", table_media );
1059 CASE_INT( ML_TRACK_NUMBER, "media.track", table_media);
1060 CASE_INT( ML_TYPE, "media.type", table_media );
1061 CASE_PSZ( ML_URI, "media.uri", table_media );
1062 CASE_INT( ML_VOTE, "media.vote", table_media );
1063 CASE_INT( ML_YEAR, "media.year", table_media );
1066 *limit = tree->value.i;
1068 msg_Warn( p_ml, "Double LIMIT found" );
1071 *sort = sql_Printf( p_ml->p_sys->p_sql, "%s%s%s DESC ",
1072 sort ? *sort : "", sort ? ", " : "",
1078 *sort = sql_Printf( p_ml->p_sys->p_sql, "%s%s%s ASC ",
1079 sort ? *sort : "", sort ? ", " : "",
1086 *distinct = "DISTINCT";
1088 msg_Warn( p_ml, "Double DISTINCT found!" );
1091 msg_Err( p_ml, "Invalid select type or unsupported: %d", tree->criteria );
1095 msg_Err( p_ml, "Broken find tree!" );
1096 i_ret = VLC_EGENERIC;
1100 i_ret = VLC_SUCCESS;
1113 # undef table_people