Subversion Repositories mdb

Rev

Rev 14 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 14 Rev 18
Line 32... Line 32...
32
#include "play.h"
32
#include "play.h"
33
#include "search.h"
33
#include "search.h"
34
 
34
 
35
int searchTerm(int s1, const char *type, const char *term, int start, int length)
35
int searchTerm(int s1, const char *type, const char *term, int start, int length)
36
{
36
{
37
	char query[1024], hv0[128], buffer[8192];
37
	char query[1024], field[128], hv0[128], buffer[8192];
38
	char fname[256];
38
	char fname[256];
39
	sqlite3 *db;
39
	sqlite3 *db;
40
	int rc, id, total, pos;
40
	int rc, id, total, pos, line;
41
	sqlite3_stmt *res;
41
	sqlite3_stmt *res;
42
	char id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
42
	char id3_title[256], id3_artist[256], id3_album[256], id3_genre[256];
43
	char *title, *artist, *album;
43
	char *title, *artist, *album;
44
 
44
 
45
	strcpy(fname, configs.home);
45
	strcpy(fname, configs.home);
Line 53... Line 53...
53
		strcpy(query, "ERROR:USER:Error opening database;");
53
		strcpy(query, "ERROR:USER:Error opening database;");
54
		write (s1, query, strlen(query));
54
		write (s1, query, strlen(query));
55
		return FALSE;
55
		return FALSE;
56
	}
56
	}
57
 
57
 
58
	strcpy (query, "select id, title, interpret, album, genre from musicdb where ");
58
	memset(field, 0, sizeof(field));
59
 
59
 
60
	if (!strcasecmp(type, "TITLE"))
60
	if (!strcasecmp(type, "TITLE"))
61
		strcat (query, "title like \"%");
61
		sprintf(field, "title like \"%%%s%%\"", term);
62
	else if (!strcasecmp(type, "ARTIST"))
62
	else if (!strcasecmp(type, "ARTIST"))
63
		strcat (query, "interpret like \"%");
63
		sprintf(field, "interpret like \"%%%s%%\"", term);
64
	else if (!strcasecmp(type, "ALBUM"))
64
	else if (!strcasecmp(type, "ALBUM"))
65
		strcat (query, "album like \"%");
65
		sprintf(field, "album like \"%%%s%%\"", term);
66
	else if (!strcasecmp(type, "GENRE"))
66
	else if (!strcasecmp(type, "GENRE"))
67
		strcat (query, "genre like \"%");
67
		sprintf(field, "genre like \"%%%s%%\"", term);
-
 
68
 
-
 
69
	strcpy (query, "select count(*) from musicdb where ");
-
 
70
	strcat (query, field);
-
 
71
 
-
 
72
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
-
 
73
	{
-
 
74
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
-
 
75
		strcpy(query, "ERROR:SEARCH:Error preparing a SQL statement;");
-
 
76
		write(s1, query, strlen(query));
-
 
77
		return FALSE;
-
 
78
	}
-
 
79
	
-
 
80
	if ((rc = sqlite3_step(res)) == SQLITE_ROW)
-
 
81
	{
-
 
82
		total = sqlite3_column_int(res, 0);
-
 
83
		sprintf(hv0, "TOTAL:%d;", total);
-
 
84
		write (s1, hv0, strlen(hv0));
-
 
85
	}
-
 
86
	else
-
 
87
		total = 0;
68
 
88
 
69
	strcat (query, term);
89
	strcpy (query, "select id, title, interpret, album, genre from musicdb where ");
70
	strcat (query, "%\"");
90
	strcat (query, field);
71
 
91
 
72
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
92
	if (sqlite3_prepare(db, query, -1, &res, NULL) != SQLITE_OK)
73
	{
93
	{
74
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
94
		syslog(LOG_DAEMON, "Error preparing SQL statement [%s]: %s", query, sqlite3_errmsg(db));
75
		strcpy(query, "ERROR:SEARCH:Error preparing a SQL statement;");
95
		strcpy(query, "ERROR:SEARCH:Error preparing a SQL statement;");
76
		write(s1, query, strlen(query));
96
		write(s1, query, strlen(query));
77
		return FALSE;
97
		return FALSE;
78
	}
98
	}
79
 
99
 
80
	total = sqlite3_column_count(res);
-
 
81
	sprintf(hv0, "TOTAL:%d;", total);
-
 
82
	write (s1, hv0, strlen(hv0));
-
 
83
	pos = 0;
100
	pos = 0;
-
 
101
	line = 1;
84
 
102
 
85
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
103
	while ((rc = sqlite3_step(res)) == SQLITE_ROW)
86
	{
104
	{
87
		if (pos < (start - 1))
105
		if (pos < (start - 1))
88
		{
106
		{
Line 122... Line 140...
122
		{
140
		{
123
			strncpy(id3_album, album, sizeof(id3_album));
141
			strncpy(id3_album, album, sizeof(id3_album));
124
			free(album);
142
			free(album);
125
		}
143
		}
126
 
144
 
127
		sprintf (buffer, "SEARCH:%s:%d:%s:%s:%s:%s;", type, id, id3_title, id3_artist, id3_album, id3_genre);
145
		sprintf (buffer, "SEARCH:%s:%d:%d:%s:%s:%s:%s;", type, id, line, id3_title, id3_artist, id3_album, id3_genre);
128
		write (s1, buffer, strlen(buffer));
146
		write (s1, buffer, strlen(buffer));
129
		pos++;
147
		pos++;
-
 
148
		line++;
130
	}
149
	}
131
	
150
	
132
	sqlite3_finalize(res);
151
	sqlite3_finalize(res);
133
	sqlite3_close(db);
152
	sqlite3_close(db);
134
	return TRUE;
153
	return TRUE;