2 * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
37 #include <netinet/in.h>
42 typedef struct ftsMY {
46 MYSQL_STMT *prepareStmt;
51 execQuery(ftsDB *adb, char **words, int flags) {
52 ftsMY *db = (ftsMY*)adb;
53 static MYSQL_BIND data;
56 if ( db->prepareStmt == NULL ) {
57 db->prepareStmt = mysql_stmt_init(db->conn);
58 if ( db->prepareStmt == NULL ) {
59 fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
63 #define SEARCH_QUERY "SELECT count(*) FROM ftsbench WHERE MATCH(body) AGAINST ( ? IN BOOLEAN MODE);"
64 if ( mysql_stmt_prepare( db->prepareStmt, SEARCH_QUERY, strlen(SEARCH_QUERY) ) != 0 ) {
65 fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
69 if ( mysql_stmt_param_count(db->prepareStmt) != 1 ) {
70 fprintf(stderr,"mysql_stmt_param_count: invalid parameter count\n");
74 memset(&data, 0, sizeof(MYSQL_BIND));
75 data.buffer_type = MYSQL_TYPE_BLOB;
76 data.length = (unsigned long*)&(db->b.strlen);
84 if ( db->flags & FLG_OR)
85 sb_add(&db->b, " ", 1);
87 sb_add(&db->b, " +", 2);
89 sb_add(&db->b, *words, -1);
93 data.buffer = db->b.str;
94 data.buffer_length = db->b.strlen;
96 if ( mysql_stmt_bind_param(db->prepareStmt, &data) ) {
97 fprintf(stderr,"mysql_stmt_bind_param failed: %s\n", mysql_error(db->conn));
101 if ( mysql_stmt_execute( db->prepareStmt ) ) {
102 fprintf(stderr,"mysql_stmt_execute failed: %s\n", mysql_error(db->conn));
106 res = mysql_store_result(db->conn);
108 if ( mysql_errno(db->conn)!=0 ) {
109 fprintf(stderr,"mysql_store_result failed: %s\n", mysql_error(db->conn));
113 mysql_free_result(res);
115 pthread_mutex_lock(&(db->db.nqueryMutex));
117 pthread_mutex_unlock(&(db->db.nqueryMutex));
121 startCreateScheme(ftsDB *adb, int flags) {
122 ftsMY *db = (ftsMY*)adb;
125 if ( flags & FLG_FUNC )
126 printf("Flag 'func' is ignored by MySQL\n");
128 if ( flags & (FLG_GIN | FLG_GIST) )
129 printf("MySQL doesn't distinguish 'gin' and 'gist' flags\n");
131 if ( mysql_query(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;")!= 0 ) {
132 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
136 if ( mysql_query(db->conn, "CREATE TABLE ftsbench (id int not null, body text) ENGINE MyISAM;")!= 0 ) {
137 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
143 finishCreateScheme(ftsDB *adb) {
144 ftsMY *db = (ftsMY*)adb;
146 if ( db->flags & (FLG_GIN | FLG_GIST) ) {
147 printf("(create index, ");
150 if ( mysql_query(db->conn, "CREATE FULLTEXT INDEX fts ON ftsbench (body);")!= 0 ) {
151 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
161 if ( mysql_query(db->conn, "OPTIMIZE TABLE ftsbench;")!= 0 ) {
162 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
171 InsertRow(ftsDB *adb, int id, char *txt) {
172 ftsMY *db = (ftsMY*)adb;
173 static MYSQL_BIND data[2];
174 static unsigned long txtlen;
176 if ( db->prepareStmt == NULL ) {
177 db->prepareStmt = mysql_stmt_init(db->conn);
178 if ( db->prepareStmt == NULL ) {
179 fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
183 #define INSERT_QUERY "INSERT INTO ftsbench (id, body) VALUES ( ? , ? );"
184 if ( mysql_stmt_prepare( db->prepareStmt, INSERT_QUERY, strlen(INSERT_QUERY) ) != 0 ) {
185 fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
189 if ( mysql_stmt_param_count(db->prepareStmt) != 2 ) {
190 fprintf(stderr,"mysql_stmt_param_count: invalid parameter count\n");
194 memset(data, 0, sizeof(data));
195 data[0].buffer_type = MYSQL_TYPE_LONG;
196 data[1].buffer_type = MYSQL_TYPE_BLOB;
197 data[1].length = &txtlen;
201 data[0].buffer = &id;
203 txtlen = (unsigned long) strlen(txt);
204 data[1].buffer = txt;
205 data[1].buffer_length = txtlen;
207 if ( mysql_stmt_bind_param(db->prepareStmt, data) ) {
208 fprintf(stderr,"mysql_stmt_bind_param failed: %s\n", mysql_error(db->conn));
212 if ( mysql_stmt_execute( db->prepareStmt ) ) {
213 fprintf(stderr,"mysql_stmt_execute failed: %s\n", mysql_error(db->conn));
219 MYInit(char * connstr) {
220 ftsMY *db = (ftsMY*)malloc(sizeof(ftsMY));
222 memset(db,0,sizeof(ftsMY));
224 db->conn = mysql_init(NULL);
226 if ( !mysql_real_connect(db->conn, NULL, NULL, NULL, connstr, 0, NULL, 0) ) {
227 fprintf(stderr,"mysql_real_connect failed: %s\n", mysql_error(db->conn));
231 db->db.execQuery = execQuery;
232 db->db.startCreateScheme = startCreateScheme;
233 db->db.finishCreateScheme = finishCreateScheme;
234 db->db.InsertRow = InsertRow;