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 datain, dataout;
55 static my_bool isnull, my_error;
56 static unsigned long length;
59 if ( db->prepareStmt == NULL ) {
60 db->prepareStmt = mysql_stmt_init(db->conn);
61 if ( db->prepareStmt == NULL ) {
62 fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
66 #define SEARCH_QUERY "SELECT count(*) FROM ftsbench WHERE MATCH(body) AGAINST ( ? IN BOOLEAN MODE);"
67 if ( mysql_stmt_prepare( db->prepareStmt, SEARCH_QUERY, strlen(SEARCH_QUERY) ) != 0 ) {
68 fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
72 if ( mysql_stmt_param_count(db->prepareStmt) != 1 ) {
73 fprintf(stderr,"mysql_stmt_param_count: invalid parameter count\n");
77 memset(&datain, 0, sizeof(MYSQL_BIND));
78 datain.buffer_type = MYSQL_TYPE_BLOB;
79 datain.length = (unsigned long*)&(db->b.strlen);
81 memset(&dataout, 0, sizeof(MYSQL_BIND));
82 dataout.buffer_type= MYSQL_TYPE_LONG;
83 dataout.buffer= (char *)&intout;
84 dataout.is_null= &isnull;
85 dataout.length= &length;
86 dataout.error= &my_error;
94 if ( db->flags & FLG_OR)
95 sb_add(&db->b, " ", 1);
97 sb_add(&db->b, " +", 2);
99 sb_add(&db->b, *words, -1);
103 datain.buffer = db->b.str;
104 datain.buffer_length = db->b.strlen;
106 if ( mysql_stmt_bind_param(db->prepareStmt, &datain) ) {
107 fprintf(stderr,"mysql_stmt_bind_param failed: %s\n", mysql_error(db->conn));
111 res = mysql_stmt_result_metadata(db->prepareStmt);
113 fprintf(stderr,"mysql_stmt_result_metadata failed: %s\n", mysql_error(db->conn));
117 if ( mysql_stmt_execute( db->prepareStmt ) ) {
118 fprintf(stderr,"mysql_stmt_execute failed: %s\n", mysql_error(db->conn));
122 if (mysql_stmt_bind_result( db->prepareStmt, &dataout)) {
123 fprintf(stderr,"mysql_stmt_bind_result failed: %s\n", mysql_error(db->conn));
127 if ( mysql_stmt_store_result( db->prepareStmt ) ) {
128 fprintf(stderr,"mysql_stmt_store_result failed: %s\n", mysql_error(db->conn));
132 if ( !mysql_stmt_fetch( db->prepareStmt) ) {
133 db->db.nres += intout;
135 fprintf(stderr,"mysql_stmt_fetch returns void result\n");
139 mysql_free_result(res);
141 pthread_mutex_lock(&(db->db.nqueryMutex));
143 pthread_mutex_unlock(&(db->db.nqueryMutex));
147 startCreateScheme(ftsDB *adb, int flags) {
148 ftsMY *db = (ftsMY*)adb;
151 if ( flags & FLG_FUNC )
152 report("Flag 'func' is ignored by MySQL\n");
154 if ( flags & (FLG_GIN | FLG_GIST) )
155 report("MySQL doesn't distinguish 'gin' and 'gist' flags\n");
157 if ( mysql_query(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;")!= 0 ) {
158 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
162 if ( mysql_query(db->conn, "CREATE TABLE ftsbench (id int not null, body text) ENGINE MyISAM;")!= 0 ) {
163 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
169 finishCreateScheme(ftsDB *adb) {
170 ftsMY *db = (ftsMY*)adb;
172 if ( db->flags & (FLG_GIN | FLG_GIST) ) {
173 report("(create index, ");
175 if ( mysql_query(db->conn, "CREATE FULLTEXT INDEX fts ON ftsbench (body);")!= 0 ) {
176 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
184 if ( mysql_query(db->conn, "OPTIMIZE TABLE ftsbench;")!= 0 ) {
185 fprintf(stderr,"mysql_query failed: %s\n", mysql_error(db->conn));
193 InsertRow(ftsDB *adb, int id, char *txt) {
194 ftsMY *db = (ftsMY*)adb;
195 static MYSQL_BIND data[2];
196 static unsigned long txtlen;
198 if ( db->prepareStmt == NULL ) {
199 db->prepareStmt = mysql_stmt_init(db->conn);
200 if ( db->prepareStmt == NULL ) {
201 fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
205 #define INSERT_QUERY "INSERT INTO ftsbench (id, body) VALUES ( ? , ? );"
206 if ( mysql_stmt_prepare( db->prepareStmt, INSERT_QUERY, strlen(INSERT_QUERY) ) != 0 ) {
207 fprintf(stderr,"mysql_stmt_init failed: %s\n", mysql_error(db->conn));
211 if ( mysql_stmt_param_count(db->prepareStmt) != 2 ) {
212 fprintf(stderr,"mysql_stmt_param_count: invalid parameter count\n");
216 memset(data, 0, sizeof(data));
217 data[0].buffer_type = MYSQL_TYPE_LONG;
218 data[1].buffer_type = MYSQL_TYPE_BLOB;
219 data[1].length = &txtlen;
223 data[0].buffer = &id;
225 txtlen = (unsigned long) strlen(txt);
226 data[1].buffer = txt;
227 data[1].buffer_length = txtlen;
229 if ( mysql_stmt_bind_param(db->prepareStmt, data) ) {
230 fprintf(stderr,"mysql_stmt_bind_param failed: %s\n", mysql_error(db->conn));
234 if ( mysql_stmt_execute( db->prepareStmt ) ) {
235 fprintf(stderr,"mysql_stmt_execute failed: %s\n", mysql_error(db->conn));
242 ftsMY *db = (ftsMY*)adb;
244 mysql_close(db->conn);
248 MYInit(char * connstr) {
249 ftsMY *db = (ftsMY*)malloc(sizeof(ftsMY));
251 memset(db,0,sizeof(ftsMY));
253 db->conn = mysql_init(NULL);
255 if ( !mysql_real_connect(db->conn, NULL, NULL, NULL, connstr, 0, NULL, 0) ) {
256 fprintf(stderr,"mysql_real_connect failed: %s\n", mysql_error(db->conn));
260 db->db.execQuery = execQuery;
261 db->db.startCreateScheme = startCreateScheme;
262 db->db.finishCreateScheme = finishCreateScheme;
263 db->db.InsertRow = InsertRow;
264 db->db.Close = Close;