|
/**
Copyright 2000 - 2003 LogicMatrix. All rights reserved.
This software is distributed under the LogicMatrix Free Software License. This software may be used for any purpose, personal or commercial. Redistributions in binary /source code form are permitted. Commercial redistribution of larger works derived from, or works which bundle this software requires a "Commercial Redistribution License" which can be purchased from LogicMatrix. Contact LogicMatrix for details
Redistributions qualify as Free and non-commercial under one of the following terms:
1) Redistributions are made at no charge beyond the reasonable cost of materials and delivery.
2) Redistributions in binary/source code form must reproduce this Copyright Notice,these license terms, and the disclaimer/limitation of liability set forth as below, in the documentation and/or other materials
provided with the distribution.
Disclaimer
==========
The Software is provided on an "AS IS" basis. No warranty is
provided that the Software is free of defects, or fit for a
particular purpose.
Limitation of Liability
=======================
LogicMatrix shall not be liable for any damages suffered by the Licensee or any third party resulting from use of the Software.
**/
/**
delete.cpp
Purpose: Handles DELETE command
Author: Vijay Mathew Pandyalakal
Date: 17/11/2003
Copyright: logicmatrix
**/
#include <string>
#include <vector>
using namespace std;
#include "db_cxx.h"
#include "mydate.h"
using namespace openutils;
#include "dsql_m_structs.h"
#include "bridge.h"
#include "sql_utils.h"
#include "cmpr.h"
#include "sql_lexer.h"
#include "bdb_index.h"
#include "rec.h"
#include "delete.h"
using namespace dsqlm;
Delete::Delete(const char* sql,Database *db) {
this->sql = sql;
if(db == 0) {
throw DsqlMException("Delete.cpp - database not inited");
}
m_db = db;
}
long Delete::parse() {
SQLLexer lexer(sql.c_str());
vector<string> vct = lexer.tokenize();
int tsz = vct.size();
if(tsz < 3) {
throw DsqlMException("Not a valid DELETE statement");
}
string s = vct[0];
if(strcmpi(s.c_str(),"DELETE") != 0) {
throw DsqlMException("Not a valid DELETE statement");
}
s = vct[1];
if(strcmpi(s.c_str(),"FROM") != 0) {
throw DsqlMException("FROM keyword not found where expected");
}
s = vct[2];
Table table(s.c_str(),m_db);
table.open();
Rec rec(&table,m_db);
if(tsz == 3) { // no "where" clause
char filnm[101];
sprintf(filnm,"%s\\%s_data",m_db->getDBName().c_str(),
table.getName().c_str());
long filsz = rec.fileLength();
int reclen = rec.recLength();
unlink(filnm);
int cols = table.getNumberOfColumns();
for(int i=0;i<cols;i++) {
Column col = table.getCol(i+1);
if(col.isIndexed() == TRUE || col.isUnique() == TRUE) {
sprintf(filnm,"%s\\%s_%s_x",m_db->getDBName().c_str(),
table.getName().c_str(),
col.getTitle().c_str());
unlink(filnm);
}
if(col.getType() == AUTOID) {
sprintf(filnm,"%s\\%s_%s_aid",m_db->getDBName().c_str(),
table.getName().c_str(),
col.getTitle().c_str());
unlink(filnm);
}
}
return (long)(filsz/reclen);
}else { // has a WHERE clause
s = vct[3];
if(strcmpi(s.c_str(),"WHERE") != 0) {
throw DsqlMException("Expected WHERE caluse not found");
}else if(tsz < 7) {
throw DsqlMException("Invalid WHERE condition");
}
int idx = 4;
vector<long> vct_pos;
/**
Copied from select.cpp
**/
TokenType tt = ID;
vector<Column> vct_cols;
vector<ComparissonOperator> vct_coprs;
vector<string> vct_vals;
vector<LogicalOperator> vct_loprs;
SQLUtils utils(m_db);
for(int i=idx;i<tsz;i++) {
s = vct[i];
if(tt == ID) {
Column col = table.getCol(s.c_str());
tt = COPR;
vct_cols.push_back(col);
}else if(tt == COPR) {
vct_coprs.push_back(utils.createComparissonOperator(s.c_str()));
tt = ANY;
}else if(tt == ANY) {
vct_vals.push_back(s);
tt = LOPR;
}else if(tt == LOPR) {
vct_loprs.push_back(utils.createLogicalOperator(s.c_str()));
tt = ID;
}else {
char err_buff[50];
sprintf(err_buff,"Unexpected token at %d",(i+1));
}
}
Cmpr cmpr(&table,m_db,vct_cols,vct_coprs,vct_vals,vct_loprs);
cmpr.run();
long csz = cmpr.getCursorLength();
for(long k=0;k<csz;k++) {
vct_pos.push_back(cmpr.getPos(k));
}
/**
**/
// actual deletion
long ret = 0;
for(long h=0;h<vct_pos.size();h++) {
long pos = vct_pos[h];
rec.read(pos);
if(!rec.isDeleted()) {
rec.del();
for(int c=0;c<table.getNumberOfColumns();c++) {
Column col = table.getCol((c+1));
if(col.isIndexed() == TRUE || col.isUnique() == TRUE) {
bool uk = false;
/*if(col.isUnique() == TRUE) {
uk = true;
}*/
char fil[101];
sprintf(fil,"%s\\%s_%s_x",m_db->getDBName().c_str(),
table.getName().c_str(),
col.getTitle().c_str());
BdbIndex index(fil,uk);
char data[24];
sprintf(data,"%d",pos);
index.del(rec.getField((c+1)).c_str(),data);
}
}
rec.write(pos);
ret++;
}
}
return ret;
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.