Click here to Skip to main content
Click here to Skip to main content
Articles » Database » Database » General » Downloads
 
Add your own
alternative version

DarkSide SQL Mini Version 1, The embedded database

, 23 Mar 2006 BSD
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.


**/

/**
	insert.cpp

	Purpose: Defines classes to parse a INSERT INTO command

	Author: Vijay Mathew Pandyalakal
	Date:	15/11/2003
	Copyright: logicmatrix
**/
#include <string>
#include <vector>
using namespace std;

#include "db_cxx.h"

#include "sql_lexer.h"
using namespace dsqlm;

#include "mydate.h"
using namespace openutils;

#include "dsql_m_structs.h"
#include "bridge.h"
#include "rec.h"
#include "unique.h"
#include "insert.h"
#include "sql_utils.h"
#include "bdb_index.h"
using namespace dsqlm;

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

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

void Insert::parse() {
	SQLLexer lexer(sql.c_str());
	vector<string> vct = lexer.tokenize();
	int sz = vct.size();
	if(sz < 5) {
		throw DsqlMException("Not a valid INSERT command");
	}
	string s = vct[0];
	if((strcmpi(s.c_str(),"INSERT")) != 0) {
		throw DsqlMException("Not a valid INSERT command");
	}
	s = vct[1];
	if((strcmpi(s.c_str(),"INTO")) != 0) {
		throw DsqlMException("Expected keyword INTO not found");
	}
	s = vct[2];
	Table table(s.c_str(),m_db);
	table.open();
	s = vct[3];
	if((strcmpi(s.c_str(),"VALUES")) != 0) {
		throw DsqlMException("Expected keyword VALUES not found");
	}	
	s = vct[4];
	if((strcmpi(s.c_str(),"(")) != 0) {
		throw DsqlMException("Expected (");
	}		
	int val_count = 0;	
	TokenType tt = ANY;
	vector<string> vals;
	for(int i=5;i<sz;i++) {
		s = vct[i];
		if(i == (sz-1)) {
			if(s != ")") {
				throw DsqlMException("Expected )");
			}else {
				continue;
			}
		}
		if(tt == ANY) {
			vals.push_back(s);
			tt = COMMA;
		}else if(tt == COMMA) {
			if(s!=",") {
				char buff[26];
				sprintf(buff,"Expected , at %d",i+1);
				throw DsqlMException(buff);
			}else {
				tt = ANY;
			}
		}
	}
	val_count = vals.size();
	if(val_count != table.getNumberOfColumns()) {
		throw DsqlMException("Not enough values");
	}
	Rec rec(&table,m_db);
	long flen = rec.fileLength();
	char s_flen[20];
	sprintf(s_flen,"%d",flen);
	Column col;
	
	vector<string> vals_2;
	for(i=0;i<table.getNumberOfColumns();i++) {
		col = table.getCol((i+1));
		char* buffer= new char[col.getSize() + 1];
		s = vals[i];
		if(strcmpi(s.c_str(),"true") == 0) {
			s = "1";
		}else if(strcmpi(s.c_str(),"false") == 0) {
			s = "0";
		}
		rec.appendField((i+1),s.c_str(),buffer);	
		vals_2.push_back(buffer);
		delete[] buffer;
	}
	vals.clear();
	for(i=0;i<table.getNumberOfColumns();i++) {
		col = table.getCol((i+1));		
		if(col.isUnique() == TRUE) {			
			char idx_fil[81];
			sprintf(idx_fil,"%s\\%s_%s_x",m_db->getDBName().c_str(),
				table.getName().c_str(),col.getTitle().c_str());
			Unique uk(&table,m_db);
			if(uk.check(col,vals_2[i].c_str(),-1)) {
				char err_buff[101];
				sprintf(err_buff,"Duplicates not permitted for %s",col.getTitle().c_str());
				throw DsqlMException(err_buff);
			}
		}			
	}
	for(i=0;i<table.getNumberOfColumns();i++) {
		col = table.getCol((i+1));
		/*char* buffer= new char[col.getSize() + 1];		
		rec.appendField((i+1),vals[i].c_str(),buffer);		*/
		if(col.isIndexed() == TRUE || col.isUnique() == TRUE) {
			char idx_fil[81];
			sprintf(idx_fil,"%s\\%s_%s_x",m_db->getDBName().c_str(),
				table.getName().c_str(),col.getTitle().c_str());
			bool uk = false;
			/*if(col.isUnique() == TRUE) {
				uk = true;
			}*/
			BdbIndex index(idx_fil,uk);			
			index.put(vals_2[i].c_str(),s_flen);
		}
		//delete[] buffer;
	}
	rec.write(flen);
}

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

Share

About the Author

No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411022.1 | Last Updated 23 Mar 2006
Article Copyright 2003 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid