📄 api.cpp
字号:
// API.cpp: implementation of the API class.
//
//////////////////////////////////////////////////////////////////////
#include "API.h"
#include "RecordManager.h"
#include <memory.h>
#include <string.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
API::API()
{
temp_table_count = 0;
memcpy(temp_table_names[0], "tmp0\0",20);
memcpy(temp_table_names[1], "tmp1\0",20);
memcpy(temp_table_names[2], "tmp2\0",20);
memcpy(temp_table_names[3], "tmp3\0",20);
memcpy(temp_table_names[4], "tmp4\0",20);
memcpy(temp_table_names[5], "tmp5\0",20);
memcpy(temp_table_names[6], "tmp6\0",20);
memcpy(temp_table_names[7], "tmp7\0",20);
memcpy(temp_table_names[8], "tmp8\0",20);
memcpy(temp_table_names[9], "tmp9\0",20);
}
API::~API()
{
}
opn_res * API::create_table(table_info *table)
{
opn_res* result= CM->create_table(table);
return result;
}
void API::set_BufferManager(BufferManager *Buffer_Manager)
{
BM = Buffer_Manager;
}
opn_res * API::create_index(char *table_name, char *index_attribute, char *index_name)
{
return 0;
}
/* Algorithm distrition:
opn_res* api_drop_table( char* table_name);
{
//real drop
{
Im_drop_index
Cm_delete_index
Cm_get_table_info()
Rm_drop()
api_delete_block_list(Rm_drop());
Cm_drop_table
}
}
*/
opn_res* API::drop_table(char *table_name)
{
opn_res* result = new opn_res;
result->error_info = 0;
struct table_info * ti;
if (!(ti = CM->get_table_info(table_name) ))
{
result->error_info = -1;
return result;
}
// drop index
// need add more here
int index_blockNo = CM->get_table_index_blockNo(table_name);
int record_blockNo = CM->get_table_record_blockNo(table_name);
RecordManager RM;
RM.init();
RM.drop();
CM->drop(table_name);
return result;
}
opn_res* API::drop_index(char *index_name)
{
return 0;
}
/*返回表结果
opn_res* Api_select_result(char* table_name,int is_projection, char projection[10][20])
// select * 时, is_projection = 0 , 否则 is_projection = 选择出来的数目
*/
opn_res* API::select_result(char *table_name, int is_projection, char projection[][20])
{
opn_res * result = new opn_res;
result->error_info = 1;
struct table_info* ti;
ti = CM->get_table_info(table_name);
if (! ti ) //error handling
{
result->error_info = -1;
result->query_res = new char [20];
memcpy(result->query_res,"Table does not exist\0",20);
return result;
}
// if is_projection == 0
//means select *
// copy all the attribute names into projection
if (!is_projection)
{
memcpy(projection, ti->attribute_name , sizeof(ti->attribute_name));
is_projection = ti->n_attribute;
}
RecordManager RM;
RM.init();
int i = 4096*2; //记录返回的大小
int pos = 0; //记录写到哪个位置了
char* records = new char[i];
// 需要给result 的东西赋值!×××××××
result->attr_count = is_projection;
int k ;
int n = 0;
for (k =0 ; k< ti->n_attribute ; k++)
if (check_projection(ti->attribute_name[k] , is_projection, projection))
{
memcpy(result->attr_name[n] , ti->attribute_name[k], 20);
result->tuple_length[n] = ti->type_length[k];
n++;
}
char* a_record;
struct st_where * where_conditions = new struct st_where;
a_record = RM.get_first_record(where_conditions);
while(a_record)
{
for (k =0 ; k< ti->n_attribute ; k++)
if (check_projection(ti->attribute_name[k] , is_projection, projection))
{
//memcpy(records+pos, (struct
}
if (i-pos < 4096) //free space is less than a block
{
char* newplace = new char[i*2];
memcpy(newplace, records, i);
i *= 2;
delete [] records; //free the old place
records = newplace;
}
a_record = RM.get_next_record(where_conditions);
}
result->query_res = records;
return result;
}
// join talbe1 and table2 into a temp table
char* API::join(char *table1, char *table2)
{
RecordManager RM1,RM2, RMnew;
RM1.init();
RM2.init();
struct table_info* ti1;
ti1 = CM->get_table_info(table1); // error ?
struct table_info* ti2;
ti2 = CM->get_table_info(table2); // error ?
struct table_info* newti = new struct table_info;
memcpy(newti,ti1,sizeof(struct table_info));
newti->n_attribute += ti2->n_attribute;
if (newti->n_attribute > 10) //new table is tooooo large
{
return 0; //need other method to tell error occurence
}
int i;
for (i = 0 ; i < ti2->n_attribute; i++)
{
memcpy( newti->attribute_name[ti1->n_attribute + i] , ti2->attribute_name[i], 20);
newti->is_unique[ti1->n_attribute + i] = ti2->is_unique[i];
newti->type[ti1->n_attribute + i] = ti2->type[i];
newti->type_length[ti1->n_attribute + i] = ti2->type_length[i];
}
char* new_table_name = new char[20];
memcpy(new_table_name, this->temp_table_names[this->temp_table_count++],20);
// get tmp talbe name
memcpy(newti->table_name , new_table_name, 20);
this->create_table(newti);
RMnew.init(); // input newti
char values[10][20];
char values2[10][20];
struct st_where* where_conditions = new struct st_where;
memcpy(&values , RM1.get_first_record(where_conditions), 10*20);
memcpy(&values2 , RM2.get_first_record(where_conditions), 10*20);
while(values[0][0] != '\0')
{
while(values2[0][0] != '\0')
{
for ( i = 0; i< ti2->n_attribute ; i++)
memcpy(values[ti1->n_attribute + i], values2[i] , 20);
RMnew.insert(values);
memcpy(&values2 , RM2.get_next_record(where_conditions), 10*20);
} //may be error
memcpy(&values , RM1.get_next_record(where_conditions), 10*20); //RM1 ++
}
//读取所有满足条件的record
//添加到新的临时表中
delete where_conditions;
return new_table_name;
}
/*
char* Api_where(char* table_name, struct st_where* where_conditions)
{
rm new;
rm old;
ct = Cm_get_table_info(table_name)
new_table = ct;
new_table.table_name = new_tmp_table_name
new_table.tmp = 1;
Api_create_table(struct table_info * ct)
Tuple = old.Rm_get_record_first(ct.block_no)
While( tuple)
{
If ( check(ct, tuple , where_conditions) )
New.Rm_insert_record(tuple)
Tuple= old.rm_get_record_next();
}
return new_table.table_name;
}
*/
char* API::where(char *table_name, st_where *where_conditions)
{
RecordManager newRM,oldRM;
oldRM.init();
struct table_info* oldti ;
oldti = CM->get_table_info(table_name); // error ?
struct table_info* newti = new struct table_info;
memcpy(newti,oldti,sizeof(struct table_info));
char* new_table_name = new char[20];
memcpy(new_table_name, this->temp_table_names[this->temp_table_count++],20);
// get tmp talbe name
memcpy(newti->table_name , new_table_name, 20);
this->create_table(newti);
newRM.init(); // input newti
char values[10][20];
memcpy(&values , oldRM.get_first_record(where_conditions), 10*20);
do
{
newRM.insert(values);
memcpy(&values , oldRM.get_next_record(where_conditions), 10*20);
}while(values[0][0] != '\0'); //may be error
//读取所有满足条件的record
//添加到新的临时表中
return new_table_name;
}
opn_res* API::select_sum(char* table_name, char* attribute)
{
return 0;
}
opn_res* API::select_count(char* table_name, char* attribute)
{
return 0;
}
opn_res* API::select_avg(char* table_name, char* attribute)
{
return 0;
}
opn_res* API::select_min(char* table_name, char* attribute)
{
return 0;
}
opn_res* API::select_max(char* table_name, char* attribute)
{
return 0;
}
/*
opn_res* Api_insert( char* table, char values[10][20]);
API:
{
Cm_get_table_info(table_name)
Rm_add_record(attribute_info);
if there is index of the table
{
for each index
Im_index_add(value,block)
}
}
*/
opn_res* API::insert(char *table, char values[][20])
{
opn_res * result = new opn_res;
result->error_info = 1;
struct table_info* ti;
ti = CM->get_table_info(table);
if (! ti ) //error handling
{
result->error_info = -1;
result->query_res = new char [20];
memcpy(result->query_res,"Table does not exist\0",20);
return result;
}
RecordManager RM; //send in ti to RM
RM.init();
RM.insert(values);
// for each index
// IM.add( RM. blockno )
return result;
}
opn_res* API::update(char *table_name, char *attribute_name, char *new_value, st_where *where_conditions)
{
opn_res * result = new opn_res;
result->error_info = 1;
struct table_info* ti;
ti = CM->get_table_info(table_name);
if (! ti ) //error handling
{
result->error_info = -1;
result->query_res = new char [20];
memcpy(result->query_res,"Table does not exist\0",20);
return result;
}
RecordManager RM; //send in ti to RM
RM.init();
RM.update_record(attribute_name, new_value, where_conditions);
// for each index
// IM.update( value, RM. blockno )
return result;
}
opn_res* API::delete_tuples(char* table_name, struct st_where* where_conditions)
{
opn_res * result = new opn_res;
result->error_info = 1;
struct table_info* ti;
ti = CM->get_table_info(table_name);
if (! ti ) //error handling
{
result->error_info = -1;
result->query_res = new char [20];
memcpy(result->query_res,"Table does not exist\0",20);
return result;
}
RecordManager RM; //send in ti to RM
RM.init();
RM.delete_record(where_conditions);
// for each index
// IM.delete( value, RM. blockno )
return result;
}
opn_res* API::end_transaction()
{
opn_res* result = new opn_res;
result->error_info = 1;
while(this->temp_table_count)
this->drop_table(this->temp_table_names[--this->temp_table_count]);
return 0;
}
void API::set_CatalogManger(CatalogManager *Catalog_Manager)
{
CM = Catalog_Manager;
}
void API::free_a_empty_block(int blockNo)
{
CM->delete_a_block(blockNo);
}
int API::check_projection(char attri_name[], int is_projection, char projection[][20])
{
int i;
for ( i = 0 ;i < is_projection ; i++)
if (strcmp(attri_name,projection[i]) == 0)
return 1;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -