Click here to Skip to main content
15,891,567 members
Articles / Database Development / SQL Server

DarkSide SQL Mini Version 1, The embedded database

Rate me:
Please Sign up or sign in to vote.
3.50/5 (27 votes)
23 Mar 2006BSD2 min read 157.5K   2.9K   57  
An embedded database library in C++.
/**
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.


**/

/**
select.cpp

  Purpose: Handles SELECT command
  
	Author: Vijay Mathew Pandyalakal
	Date:	16/11/2003
	Copyright: logicmatrix
**/

#include <string>
#include <vector>
using namespace std;

#include "mydate.h"
using namespace openutils;

#include "dsql_m_structs.h"
#include "bridge.h"
#include "sql_lexer.h"
#include "cmpr.h"
#include "bridge.h"
#include "rec.h"
#include "sql_utils.h"
#include "select.h"
using namespace dsqlm;

Select::Select(const char* sql,Database* db) {
	this->sql = sql;
	if(db == 0) {
		throw DsqlMException("Select.cpp - database not inited");
	}
	m_db = db;
}

void Select::setSql(const char* sql) {
	this->sql = sql;
}

ResultSet Select::parse() {
	SQLLexer lexer(sql.c_str());
	vector<string> vct = lexer.tokenize();	
	int tsz = vct.size();	
	if(tsz < 4) {
		char buff[50];
		sprintf(buff,"Invalid SELECT statement %d",tsz);
		throw DsqlMException(buff);
	}
	string s = vct[0];
	if(strcmpi(s.c_str(),"SELECT") != 0) {
		throw DsqlMException("Not a select statement");
	}
	SQLUtils utils(m_db);
	int fi = utils.findPos(vct,"FROM");
	if(fi <= 1) {
		throw DsqlMException("FROM not found where expected");
	}
	s = vct[fi+1];	
	Table table(s.c_str(),m_db);
	table.open();
	ResultSet rslt(table,m_db);
	vector<Column> vct_cols;
	s = vct[1];	
	if(s == "*") {
		int num_cols = table.getNumberOfColumns();
		for(int i=0;i<num_cols;i++) {
			vct_cols.push_back(table.getCol((i+1)));
		}
	}else {
		TokenType tt = ANY;
		for(int i=1;i<fi;i++) {
			s = vct[i].c_str();
			if(tt == ANY) {
				vct_cols.push_back(table.getCol(s.c_str()));			
				tt = COMMA;
			}else if(tt == COMMA) {
				if(s != ",") {
					throw DsqlMException("Expected comma");
				}
				tt = ANY;
			}
		}
	}
	rslt.setColumns(vct_cols);
	vct_cols.clear();
	vector<long> vct_pos;
	if(tsz == (fi+2)) { // no WHERE clause
		Rec rec(&table,m_db);
		rec.openCursor();
		long pos = 0;
		while(rec.next(&pos)) {
			if(rec.isDeleted()) {
				continue;
			}else {
				vct_pos.push_back(pos);
			}
		}
		rec.closeCursor();
	}else {
		int idx = fi+2;
		s = vct[idx];
		if(strcmpi(s.c_str(),"WHERE") != 0) {
			throw DsqlMException("Expected WHERE clause not found");
		}
		idx++;
		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();
		Rec rec(&table,m_db);
		for(long k=0;k<csz;k++) {			
			if(rec.isDeleted(cmpr.getPos(k))) {
				continue;
			}
			vct_pos.push_back(cmpr.getPos(k));
		}
	}
	rslt.setCursor(vct_pos);
	rslt.setDb(this->m_db);
	return rslt;
}

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.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions