2
* Copyright (C) 2010 Djellel Eddine Difallah
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* * Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* * Neither the name of Djellel Eddine Difallah nor the names of its 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27
* THE POSSIBILITY OF SUCH DAMAGE.
31
#include <drizzled/session.h>
32
#include "query_cache_service.h"
33
#include <drizzled/table_list.h>
40
QueryCacheService::CacheEntries QueryCacheService::cache;
41
QueryCacheService::CachedTablesEntries QueryCacheService::cachedTables;
43
message::Resultset *QueryCacheService::setCurrentResultsetMessage(Session *in_session)
45
message::Resultset *resultset= in_session->getResultsetMessage();
47
if (unlikely(resultset == NULL))
49
/* Allocate and initialize a new Resultset message
50
* for this Session object. Session is responsible for
51
* deleting Resultset message when done with it.
53
resultset= new message::Resultset();
54
in_session->setResultsetMessage(resultset);
56
/* for Atomic purpose, reserve an entry for the following query
58
cache[in_session->query_cache_key]= message::Resultset();
63
void QueryCacheService::setResultsetHeader(message::Resultset &resultset,
67
/* Set up the Select header */
68
message::SelectHeader *header= resultset.mutable_select_header();
72
/* Extract all the tables mentioned in the query and
73
* add the metadata to the SelectHeader
76
for (TableList* tmp_table= in_table; tmp_table; tmp_table= tmp_table->next_global)
78
message::TableMeta *table_meta= header->add_table_meta();
79
table_meta->set_schema_name(tmp_table->db, strlen(tmp_table->db));
80
table_meta->set_table_name(tmp_table->table_name, strlen(tmp_table->table_name));
81
/* Populate the cached tables hash */
82
QueryCacheService::cachedTables[strcat(tmp_table->db, tmp_table->table_name)].push_back(in_session->query_cache_key);
85
/* Extract the returned fields
86
* and add the field data to the SelectHeader
88
List<Item>::iterator it(in_session->lex().select_lex.item_list);
93
item->make_field(&field);
95
message::FieldMeta *field_meta= header->add_field_meta();
96
field_meta->set_field_name(field.col_name, strlen(field.col_name));
97
field_meta->set_schema_name(field.db_name, strlen(field.db_name));
98
field_meta->set_table_name(field.table_name, strlen(field.table_name));
99
field_meta->set_field_alias(field.org_col_name, strlen(field.org_col_name));
100
field_meta->set_table_alias(field.org_table_name, strlen(field.org_table_name));
104
bool QueryCacheService::addRecord(Session *in_session, List<Item> &list)
106
message::Resultset *resultset= in_session->getResultsetMessage();
108
if (resultset != NULL)
110
message::SelectData *data= resultset->mutable_select_data();
111
data->set_segment_id(1);
112
data->set_end_segment(true);
113
message::SelectRecord *record= data->add_record();
115
List<Item>::iterator li(list);
117
char buff[MAX_FIELD_WIDTH];
118
String buffer(buff, sizeof(buff), &my_charset_bin);
121
while ((current_field= li++))
123
if (current_field->is_null())
125
record->add_is_null(true);
126
record->add_record_value("", 0);
130
String *string_value= current_field->val_str(&buffer);
131
record->add_is_null(false);
132
record->add_record_value(string_value->c_ptr(), string_value->length());
133
string_value->free();
141
bool QueryCacheService::isCached(string query)
143
CacheEntries::iterator it= QueryCacheService::cache.find(query);
144
if (it != QueryCacheService::cache.end())
148
} /* namespace drizzled */