diff options
author | tomsmeding <hallo@tomsmeding.nl> | 2015-05-22 21:13:51 +0200 |
---|---|---|
committer | tomsmeding <hallo@tomsmeding.nl> | 2015-05-22 21:13:51 +0200 |
commit | 059a8baa815f0b50d11546ad26b854572ef52182 (patch) | |
tree | 87009b91f388e7a6603c6c409941e371b22d35d3 | |
parent | 675e8972384e808f2b622922df5e8deef418e165 (diff) |
CreateQuery, InsertQuery, add message in QueryResult
-rw-r--r-- | engine.cpp | 48 | ||||
-rw-r--r-- | enginedata.cpp | 12 | ||||
-rw-r--r-- | enginedata.h | 6 | ||||
-rw-r--r-- | query.cpp | 56 | ||||
-rw-r--r-- | query.h | 27 |
5 files changed, 99 insertions, 50 deletions
@@ -30,31 +30,43 @@ int main(int argc,char **argv){ header[0]={RH_INT32,0}; header[1]={RH_UINT32,0}; header[2]={RH_BYTES,10}; - //tables.emplace("hoi",Table("hoi",3,header)); - createTable("hoi",3,header); + + CreateQuery cqu; + cqu.tablename="hoi"; + cqu.nc=3; + cqu.setHeader(header); + QueryResult res=cqu.execute(tables); + + cout<<"Create, Result: "<<res.res<<": (msg=\""<<res.msg<<"\")"<<endl; + for(const Row &r : res.rows)cout<<"- "<<r<<endl; + Table *hoitb=&tables.at("hoi"); + Row row(3,hoitb); row.items[0].rh_int32=UINT_MAX; row.items[1].rh_uint32=UINT_MAX; row.items[2].rh_bytes=new unsigned char[10]; strcpy((char*)row.items[2].rh_bytes,"hallo daar"); - // cerr<<row.items[2].rh_bytes<<endl; - if(!hoitb->insert(row))cout<<"Failed to insert row!"<<endl; else cout<<"Success."<<endl; - if(!hoitb->insert(row))cout<<"Failed to insert row!"<<endl; else cout<<"Success."<<endl; + + InsertQuery iqu(move(row)); + iqu.tablename="hoi"; + + res=iqu.execute(tables); + + cout<<"Insert, Result: "<<res.res<<": (msg=\""<<res.msg<<"\")"<<endl; + for(const Row &r : res.rows)cout<<"- "<<r<<endl; + cout<<"hoitb has "<<hoitb->rows.size()<<" row"<<(hoitb->rows.size()==1?"":"s")<<'.'<<endl; - /*list<Row> found=hoitb->find(serialise((int32_t)-1)); - cout<<"Found for key -1:"<<endl; - for(Row r : found){ - cout<<r<<endl; - }*/ - FindQuery qu; - qu.from="hoi"; + + FindQuery fqu; + fqu.tablename="hoi"; RowItem ri; ri.rh_int32=-1; - qu.where.emplace_back(0,ri); - QueryResult res=qu.execute(tables); - cout<<"Result: "<<res.res<<":"<<endl; - for(const Row &r : res.rows){ - cout<<"- "<<r<<endl; - } + fqu.where.emplace_back(0,ri); + + res=fqu.execute(tables); + + cout<<"Find, Result: "<<res.res<<": (msg=\""<<res.msg<<"\")"<<endl; + for(const Row &r : res.rows)cout<<"- "<<r<<endl; + return 0; } diff --git a/enginedata.cpp b/enginedata.cpp index 95a2b11..fb60521 100644 --- a/enginedata.cpp +++ b/enginedata.cpp @@ -6,6 +6,7 @@ using namespace std; +Row::Row(void):nc(0),table(NULL){} Row::Row(const int _nc,const Table *const _t):nc(_nc),table(_t){ items=new RowItem[nc]; } @@ -30,7 +31,6 @@ ostream& operator<<(ostream &os,const Row &r){ } -//copies from _hd, so you can freely delete or edit the passed colheader array Table::Table(const string &_n,const int _nc,const ColHeader *const _hd):name(_n),nc(_nc){ header=new ColHeader[nc]; memcpy(header,_hd,nc*sizeof(ColHeader)); @@ -45,16 +45,6 @@ Table::Table(Table &&other):name(move(other.name)),nc(other.nc),header(other.hea other.header=nullptr; } -bool Table::insert(Row &&row){ - string key=serialise(this->header[0],row.items[0]); - cerr<<debug<<"Inserting into table '"<<this->name<<"' key="<<key<<" row="<<row<<endl; - return rows.emplace(key,move(row)).second; -} -bool Table::insert(Row &row){ - string key=serialise(this->header[0],row.items[0]); - cerr<<debug<<"Inserting into table '"<<this->name<<"' key="<<key<<" row="<<row<<endl; - return rows.emplace(key,row).second; -} string serialise(const ColHeader &header,const RowItem &rowitem){ diff --git a/enginedata.h b/enginedata.h index 9cf0b53..2b7ca96 100644 --- a/enginedata.h +++ b/enginedata.h @@ -32,6 +32,7 @@ struct Row{ const Table *table; //pointer to the parent table; don't delete! RowItem *items; + Row(void); Row(const int _nc,const Table *const _t); ~Row(void); Row(Row &&other); //move constr @@ -46,13 +47,10 @@ struct Table{ ColHeader *header; map<string,Row> rows; //map key is serialised version of the first column - Table(const string &_n,const int _nc,const ColHeader *const _hd); + Table(const string &_n,const int _nc,const ColHeader *const _hd); //copies from _hd, so you can freely delete or edit the passed colheader array ~Table(void); Table(const Table &other); Table(Table &&other); - - bool insert(Row &&row); - bool insert(Row &row); }; string serialise(const ColHeader &header,const RowItem &rowitem); @@ -11,25 +11,39 @@ WhereClause::WhereClause(const int _c,const RowItem &_v):col(_c),value(_v){} QueryResult::QueryResult(void):res(0){} QueryResult::QueryResult(int _res):res(_res){} QueryResult::QueryResult(int _res,const vector<Row> &_rows):res(_res),rows(_rows){} +QueryResult::QueryResult(int _res,const vector<Row> &_rows,const string &_m):res(_res),rows(_rows),msg(_m){} +QueryResult::QueryResult(int _res,const string &_m):res(_res),msg(_m){} -Query::Query(void):limit(-1){} +QueryResult Query::execute(map<string,Table> &tables){return QueryResult(-1);} -QueryResult Query::execute(const map<string,Table> &tables){return QueryResult(-1);} +CreateQuery::CreateQuery(void):nc(0),header(NULL){} -FindQuery::FindQuery(void):sort(0){}; +void CreateQuery::setHeader(const ColHeader *hd){ + header=new ColHeader[nc]; + memcpy(header,hd,nc*sizeof(ColHeader)); +} + +QueryResult CreateQuery::execute(map<string,Table> &tables){ + if(nc==0)return QueryResult(0,"zero nc"); + if(header==NULL)return QueryResult(0,"null header"); + const map<string,Table>::const_iterator tit=tables.find(tablename); + if(tit!=tables.cend())return QueryResult(0,"existing table"); + tables.emplace(piecewise_construct, + forward_as_tuple(tablename), + forward_as_tuple(tablename,nc,header)); + return QueryResult(1); +} -QueryResult FindQuery::execute(const map<string,Table> &tables){ - if(limit==0)return QueryResult(0); + +FindQuery::FindQuery(void):limit(-1),sort(0){}; + +QueryResult FindQuery::execute(map<string,Table> &tables){ + if(limit==0)return QueryResult(0,"zero limit"); int i,j; - /*for(i=0;i<(int)tables.size();i++){ - if(tables[i].name==from)break; - } - if(i==(int)tables.size())return QueryResult(0); - const Table &table=tables[i];*/ - const map<string,Table>::const_iterator tit=tables.find(from); - if(tit==tables.cend())return QueryResult(0); + const map<string,Table>::const_iterator tit=tables.find(tablename); + if(tit==tables.cend())return QueryResult(0,"non-existent table"); const Table &table=tit->second; for(i=0;i<(int)where.size();i++){ @@ -62,7 +76,25 @@ QueryResult FindQuery::execute(const map<string,Table> &tables){ if(i==(int)where.size()){ ret.res++; ret.rows.emplace_back(it->second); + if(ret.res==limit)return ret; } } return ret; } + + +InsertQuery::InsertQuery(const Row &_r):row(_r){} +InsertQuery::InsertQuery(Row &&_r):row(move(_r)){} + +QueryResult InsertQuery::execute(map <string,Table> &tables){ + map<string,Table>::iterator tit=tables.find(tablename); + if(tit==tables.end())return QueryResult(0,"non-existent table"); + Table &table=tit->second; + + if(table.nc!=row.nc)return QueryResult(0,"non-conformant row width"); + const string primkey=serialise(table.header[0],row.items[0]); + const map<string,Row>::const_iterator rit=table.rows.find(primkey); + if(rit!=table.rows.cend())return QueryResult(0,"existing primary key"); + table.rows.insert(make_pair(primkey,row)); + return QueryResult(1); +} @@ -19,23 +19,40 @@ struct WhereClause{ struct QueryResult{ int res; vector<Row> rows; + string msg; //empty message indicates success QueryResult(void); QueryResult(int); QueryResult(int,const vector<Row>&); + QueryResult(int,const vector<Row>&,const string&); + QueryResult(int,const string&); }; struct Query{ - string from; - int limit; //-1=unlimited - Query(void); - virtual QueryResult execute(const map<string,Table>&); + string tablename; + virtual QueryResult execute(map<string,Table>&); +}; + +struct CreateQuery : public Query{ + int nc; + ColHeader *header; + CreateQuery(void); + void setHeader(const ColHeader *hd); + QueryResult execute(map<string,Table> &tables); }; struct FindQuery : public Query{ + unsigned int limit; //-1=unlimited int sort; //0=no sort, 1=sort, -1=reverse sort vector<WhereClause> where; FindQuery(void); - QueryResult execute(const map<string,Table>&); + QueryResult execute(map<string,Table> &tables); +}; + +struct InsertQuery : public Query{ + Row row; + InsertQuery(const Row &_r); + InsertQuery(Row &&_r); + QueryResult execute(map<string,Table> &tables); }; |