1
/* Copyright (C) 2009 PrimeBase Technologies GmbH, Germany
3
* PrimeBase Media Stream for MySQL
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
* System variables table.
26
#include <drizzled/common.h>
27
#include <drizzled/session.h>
28
#include <drizzled/sql_lex.h>
29
#include <drizzled/field/blob.h>
30
#include <drizzled/field/varstring.h>
33
#include "cslib/CSConfig.h"
35
#include <sys/types.h>
40
//#include "mysql_priv.h"
41
#include "cslib/CSGlobal.h"
42
#include "cslib/CSStrUtil.h"
43
#include "cslib/CSLog.h"
44
#include "cslib/CSPath.h"
50
#include "database_ms.h"
51
#include "open_table_ms.h"
52
#include "discover_ms.h"
53
#include "systab_util_ms.h"
56
#include "systab_httpheader_ms.h"
58
#define METADATA_HEADER_FILE "http-meta-data-headers"
59
#define MIN_METADATA_HEADER_FILE_SIZE 3
61
SysTabRec *MSHTTPHeaderTable::gDefaultMetaDataHeaders;
63
DT_FIELD_INFO pbms_metadata_headers_info[]=
65
{"Name", 32, NULL, MYSQL_TYPE_VARCHAR, &UTF8_CHARSET, NOT_NULL_FLAG, "HTTP field name"},
66
{NULL, NOVAL, NULL, MYSQL_TYPE_STRING, NULL, 0, NULL}
69
DT_KEY_INFO pbms_metadata_headers_keys[]=
75
* -------------------------------------------------------------------------
78
void MSHTTPHeaderTable::releaseDefaultMetaDataHeaders()
80
if (gDefaultMetaDataHeaders)
81
gDefaultMetaDataHeaders->release();
82
gDefaultMetaDataHeaders = NULL;
85
void MSHTTPHeaderTable::setDefaultMetaDataHeaders(const char *defaults)
90
if (!gDefaultMetaDataHeaders)
91
new_(gDefaultMetaDataHeaders, SysTabRec("", METADATA_HEADER_FILE".dat", METADATA_HEADER_NAME));
93
gDefaultMetaDataHeaders->clear();
96
for (ptr = defaults; *ptr && (*ptr != ':'); ptr++){}
97
if (ptr != defaults) {
98
gDefaultMetaDataHeaders->beginRecord();
99
gDefaultMetaDataHeaders->setStringField(defaults, ptr - defaults);
100
gDefaultMetaDataHeaders->endRecord();
111
//----------------------------
112
void MSHTTPHeaderTable::loadTable(MSDatabase *db)
115
SysTabRec *headerData = NULL;
120
path = getSysFile(RETAIN(db->myDatabasePath), METADATA_HEADER_FILE, MIN_METADATA_HEADER_FILE_SIZE);
123
if (path->exists()) {
127
new_(headerData, SysTabRec(db->myDatabaseName->getCString(), METADATA_HEADER_FILE".dat", METADATA_HEADER_NAME));
130
file = path->openFile(CSFile::READONLY);
132
size = file->getEOF();
133
headerData->setLength(size);
134
file->read(headerData->getBuffer(0), 0, size, size);
138
} else if (gDefaultMetaDataHeaders) { // Load the defaults if they exist.
139
headerData = gDefaultMetaDataHeaders;
143
while (headerData->nextRecord()) {
144
const char *header = headerData->getStringField();
146
if (headerData->isValidRecord()) {
147
db->iHTTPMetaDataHeaders.add(CSString::newString(header));
151
if (headerData == gDefaultMetaDataHeaders)
152
gDefaultMetaDataHeaders->resetRecord();
154
release_(headerData);
164
//----------------------------
165
void MSHTTPHeaderTable::saveTable(MSDatabase *db)
168
SysTabRec *headerData;
173
new_(headerData, SysTabRec(db->myDatabaseName->getCString(), METADATA_HEADER_FILE".dat", METADATA_HEADER_NAME));
176
// Build the table records
178
lock_(&db->iHTTPMetaDataHeaders);
180
// Note: the object returned by itemAt() is not returnd referenced.
181
for (uint32_t i =0; (str = (CSString*) db->iHTTPMetaDataHeaders.itemAt(i)); i++) {
182
headerData->beginRecord();
183
headerData->setStringField(str->getCString());
184
headerData->endRecord();
186
unlock_(&db->iHTTPMetaDataHeaders);
188
restoreTable(RETAIN(db), headerData->getBuffer(0), headerData->length(), false);
190
release_(headerData);
195
//--------------------
196
CSStringBuffer *MSHTTPHeaderTable::dumpTable(MSDatabase *db)
200
CSStringBuffer *dump;
205
path = getSysFile(RETAIN(db->myDatabasePath), METADATA_HEADER_FILE, MIN_METADATA_HEADER_FILE_SIZE);
209
new_(dump, CSStringBuffer(20));
212
if (path->exists()) {
216
file = path->openFile(CSFile::READONLY);
219
size = file->getEOF();
220
dump->setLength(size);
221
file->read(dump->getBuffer(0), 0, size, size);
230
//--------------------
231
void MSHTTPHeaderTable::restoreTable(MSDatabase *db, const char *data, size_t size, bool reload)
239
path = getSysFile(RETAIN(db->myDatabasePath), METADATA_HEADER_FILE, MIN_METADATA_HEADER_FILE_SIZE);
242
file = path->openFile(CSFile::CREATE | CSFile::TRUNCATE);
245
file->write(data, 0, size);
259
//--------------------
260
void MSHTTPHeaderTable::transferTable(MSDatabase *dst_db, MSDatabase *src_db)
268
path = CSPath::newPath(RETAIN(src_db->myDatabasePath), METADATA_HEADER_FILE".dat");
270
if (path->exists()) {
272
bu_path = CSPath::newPath(RETAIN(dst_db->myDatabasePath), METADATA_HEADER_FILE".dat");
273
path->copyTo(bu_path, true);
283
void MSHTTPHeaderTable::removeTable(CSString *db_path)
288
path = getSysFile(db_path, METADATA_HEADER_FILE, 0);
296
MSHTTPHeaderTable::MSHTTPHeaderTable(MSSystemTableShare *share, TABLE *table):
297
MSOpenSystemTable(share, table),
303
MSHTTPHeaderTable::~MSHTTPHeaderTable()
308
void MSHTTPHeaderTable::use()
310
myShare->mySysDatabase->iHTTPMetaDataHeaders.lock();
314
void MSHTTPHeaderTable::unuse()
317
saveTable(RETAIN(myShare->mySysDatabase));
320
myShare->mySysDatabase->iHTTPMetaDataHeaders.unlock();
325
void MSHTTPHeaderTable::seqScanInit()
330
bool MSHTTPHeaderTable::seqScanNext(char *buf)
332
TABLE *table = mySQLTable;
335
MY_BITMAP *save_write_set;
341
header = (CSString*) (myShare->mySysDatabase->iHTTPMetaDataHeaders.itemAt(iHeaderIndex));
346
name = header->getCString();
348
save_write_set = table->write_set;
349
table->write_set = NULL;
352
memset(buf, 0xFF, table->getNullBytes());
354
memset(buf, 0xFF, table->s->null_bytes);
356
for (Field **field=GET_TABLE_FIELDS(table); *field ; field++) {
358
save = curr_field->ptr;
359
#if MYSQL_VERSION_ID < 50114
360
curr_field->ptr = (byte *) buf + curr_field->offset();
363
curr_field->ptr = (byte *) buf + curr_field->offset(curr_field->getTable()->getInsertRecord());
365
curr_field->ptr = (byte *) buf + curr_field->offset(curr_field->table->record[0]);
368
switch (curr_field->field_name[0]) {
370
ASSERT(strcmp(curr_field->field_name, "Name") == 0);
371
curr_field->store(name, strlen(name), &UTF8_CHARSET);
372
setNotNullInRecord(curr_field, buf);
376
curr_field->ptr = save;
379
table->write_set = save_write_set;
383
void MSHTTPHeaderTable::seqScanPos(unsigned char *pos)
385
int32_t index = iHeaderIndex -1;
387
index = 0; // This is probably an error condition.
389
mi_int4store(pos, index);
392
void MSHTTPHeaderTable::seqScanRead(unsigned char *pos, char *buf)
394
iHeaderIndex = mi_uint4korr(pos);
398
void MSHTTPHeaderTable::insertRow(char *data)
404
getFieldValue(data, 0, &name);
406
header = CSString::newString(name.c_str());
407
myShare->mySysDatabase->iHTTPMetaDataHeaders.add(header);
413
void MSHTTPHeaderTable::deleteRow(char *data)
419
getFieldValue(data, 0, &name);
421
header = CSString::newString(name.c_str());
423
myShare->mySysDatabase->iHTTPMetaDataHeaders.remove(header);
430
void MSHTTPHeaderTable::updateRow(char *old_data, char *new_data)