00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "db.h"
00028 #include "ctb_log.h"
00029
00030
00031
00032 static const char* MDB_COLUMN_NAMES[] =
00033 {
00034 [COL_FILENAME] = "filename" ,
00035 [COL_DIRECTORY_PATH] = "directory_path" ,
00036 [COL_SORT_PRIORITY] = "sort_priority" ,
00037 [COL_IS_DIRECTORY] = "is_directory" ,
00038 [COL_FILETYPE] = "file_type" ,
00039 [COL_FILESIZE] = "file_size" ,
00040 [COL_FILETIME_MODIFIED] = "file_time_modified" ,
00041 [COL_FILETIME_LASTREAD] = "file_time_lastread" ,
00042 [COL_FILETIME_ADDED] = "file_time_added" ,
00043 [COL_TITLE] = "title" ,
00044 [COL_AUTHOR] = "author" ,
00045 [COL_THUMB_MINI] = "thumb_data_mini" ,
00046 [COL_THUMB_SMALL] = "thumb_data_small" ,
00047 [COL_THUMB_MEDIUM] = "thumb_data_medium" ,
00048 [COL_THUMB_LARGE] = "thumb_data_large" ,
00049 [N_METADATA_COLUMNS] = NULL
00050 };
00051
00052
00053
00054
00055
00056 static const int SORT_COLUMN_NAME_IDX[ N_CTB_SORT_ORDER ] =
00057 {
00058 [CTB_SORT_BY_NAME] = COL_TITLE,
00059 [CTB_SORT_BY_TYPE] = COL_FILETYPE,
00060 [CTB_SORT_BY_SIZE] = COL_FILESIZE,
00061 [CTB_SORT_BY_DATE_ADDED] = COL_FILETIME_ADDED,
00062 [CTB_SORT_BY_DATE_READ] = COL_FILETIME_LASTREAD,
00063 [CTB_SORT_BY_AUTHOR] = COL_AUTHOR,
00064 };
00065
00066
00067
00068 static const int MAX_WAIT_DATABASE = 30;
00069
00070 static erMetadb g_metadb = NULL;
00071 metadata_table *g_query_names = NULL;
00072 gboolean g_query_in_progress = FALSE;
00073
00074
00075 erMetadb get_database()
00076 {
00077 return g_metadb;
00078 }
00079
00080
00081 int open_global_database(const gchar *directory)
00082 {
00083 if (g_metadb && strcmp(directory, ermetadb_get_dir(g_metadb)) == 0 )
00084 {
00085 return ER_OK;
00086 }
00087
00088 close_database();
00089 g_metadb = ermetadb_global_open(directory, FALSE);
00090 if (!g_metadb) {
00091 ERRORPRINTF("error opening global database in at [%s]", directory);
00092 return ER_FAIL;
00093 }
00094 return ER_OK;
00095 }
00096
00097
00098 void close_database()
00099 {
00100 if (g_metadb)
00101 {
00102 ermetadb_close(g_metadb);
00103 g_metadb = NULL;
00104 }
00105 }
00106
00107
00108 int db_query_create( int column1, ... )
00109 {
00110 va_list ap;
00111 int col;
00112
00113 if (g_query_in_progress)
00114 {
00115 WARNPRINTF("trying to start a new query mid-query");
00116 if (g_query_names)
00117 metadata_table_free(g_query_names);
00118 }
00119
00120 g_query_in_progress = TRUE;
00121 g_query_names = metadata_table_new();
00122 if (!g_query_names)
00123 return ER_FAIL;
00124
00125 va_start(ap, column1);
00126 for (col = column1; col!=-1; col = va_arg(ap, int))
00127 {
00128 int rc = metadata_table_add_column(g_query_names, MDB_COLUMN_NAMES[col]);
00129 if (rc!=ER_OK)
00130 {
00131 va_end(ap);
00132 return rc;
00133 }
00134 }
00135 va_end(ap);
00136 return ER_OK;
00137 }
00138
00139
00140 static const gchar *get_sort_order_name( int sort_order, gboolean show_filenames )
00141 {
00142 int sort_column = SORT_COLUMN_NAME_IDX[sort_order];
00143 if (show_filenames && sort_order == CTB_SORT_BY_NAME) sort_column = COL_FILENAME;
00144 return MDB_COLUMN_NAMES[sort_column];
00145 }
00146
00147
00148 int db_query_execute(int sort_order,
00149 gboolean sort_asc,
00150 metadata_table **values,
00151 const gchar* tag_filter)
00152 {
00153 int rc = ermetadb_global_select_files( g_metadb,
00154 get_sort_order_name(sort_order, FALSE),
00155 sort_asc,
00156 g_query_names,
00157 values,
00158 tag_filter );
00159 metadata_table_free(g_query_names);
00160 g_query_in_progress = FALSE;
00161 return rc;
00162 }
00163
00164
00165 int db_query_execute_recent(metadata_table **values, int limit)
00166 {
00167 int rc = ermetadb_global_select_recent( g_metadb,
00168 get_sort_order_name(CTB_SORT_BY_DATE_ADDED, FALSE),
00169 limit,
00170 g_query_names,
00171 values);
00172 metadata_table_free(g_query_names);
00173 g_query_in_progress = FALSE;
00174 return rc;
00175 }
00176
00177
00178 int db_query_execute_search_filter(int sort_order,
00179 gboolean sort_asc,
00180 metadata_table **values,
00181 const gchar *search_filter)
00182 {
00183 int rc = ermetadb_global_select_search( g_metadb,
00184 get_sort_order_name(sort_order, FALSE),
00185 sort_asc,
00186 g_query_names,
00187 values,
00188 search_filter);
00189 metadata_table_free(g_query_names);
00190 g_query_in_progress = FALSE;
00191 return rc;
00192 }
00193
00194
00195 int db_query_execute_path_filter(int sort_order,
00196 gboolean sort_asc,
00197 metadata_table **values,
00198 const gchar* path_filter,
00199 gboolean show_filenames)
00200 {
00201 int rc = ermetadb_global_select_subdir( g_metadb,
00202 get_sort_order_name(sort_order, show_filenames),
00203 sort_asc,
00204 g_query_names,
00205 values,
00206 path_filter);
00207 metadata_table_free(g_query_names);
00208 g_query_in_progress = FALSE;
00209 return rc;
00210 }
00211
00212
00213 int db_query_get_metadata(const gchar* filename,
00214 const gchar* dirpath,
00215 metadata_table **values)
00216 {
00217 int rc = ermetadb_global_get_file( g_metadb,
00218 dirpath,
00219 filename,
00220 g_query_names,
00221 values);
00222 metadata_table_free(g_query_names);
00223 g_query_in_progress = FALSE;
00224 return rc;
00225 }
00226
00227
00228 int db_query_update_lastread(const GString *filename, const GString *directory, int value)
00229 {
00230 metadata_table *query = metadata_table_new();
00231
00232 int rc = metadata_table_add_column(query, MDB_COLUMN_NAMES[COL_FILETIME_LASTREAD]);
00233 if (rc == ER_OK) rc = metadata_table_set_int64(query, 0, value);
00234 if (rc == ER_OK) rc = ermetadb_global_change_file(g_metadb,
00235 directory->str,
00236 filename->str,
00237 query);
00238 metadata_table_free(query);
00239 return rc;
00240 }
00241