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.


**/

/**
	createtable.cpp

	Purpose: Defines classes to parse a CREATE TABLE command

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

#include "sql_lexer.h"
using namespace dsqlm;

#include "mydate.h"
using namespace openutils;

#include "dsql_m_structs.h"
#include "bridge.h"
#include "createtable.h"
#include "sql_utils.h"
using namespace dsqlm;

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

void CreateTable::parse() {
	SQLLexer lexer(sql.c_str());
	vector<string> vct = lexer.tokenize();
	int sz = vct.size();
	if(sz < 5) {
		throw DsqlMException("Not a valid CREATE TABLE command");
	}
	string s = vct[0];
	if((strcmpi(s.c_str(),"CREATE")) != 0) {
		throw DsqlMException("Not a valid CREATE TABLE command");
	}
	s = vct[1];
	if((strcmpi(s.c_str(),"TABLE")) != 0) {
		throw DsqlMException("Not a valid CREATE TABLE command");
	}
	s = vct[2];
	Identifier idn;
	idn.check(s.c_str());
	Table table(s.c_str(),m_db);	
	s = vct[3];
	if((strcmpi(s.c_str(),"(")) != 0) {
		throw DsqlMException("Expected ( not found");
	}	
	s = vct[sz-1];
	if(s != ")") {
		throw DsqlMException("Expected ) at end of command");
	}	
	TokenType tt = ID;
	Column col;
	vector<Column> vct_cols;
	for(int i=4;i<sz;i++) {
		s = vct[i];
		if(tt == ID) {
			idn.check(s.c_str());
			col.setTitle(s.c_str());
			tt = TYPE;
		}else if(tt == TYPE) {
			SQLUtils utils(m_db);
			utils.setType(s.c_str(),col);
			tt = ANY;
		}else if(tt == ANY) {
			if(s == "(") {
				if(col.getType() != CHAR && col.getType() != VARCHAR) {
					char buff[80];
					sprintf(buff,"Unexpected ( at %d",(i+1));
					throw DsqlMException(buff);
				}
				tt = SIZE;
			}else if(s == ",") {
				vct_cols.push_back(col);
				col.refresh();
				tt = ID;
			}else if(s == ")") {
				vct_cols.push_back(col);
				col.refresh();
				break;
			}else if(strcmpi(s.c_str(),"UNIQUE") == 0) {
				col.setUnique(TRUE);
				col.setNullable(FALSE);
				tt = COMMA;
			}else if(strcmpi(s.c_str(),"INDEXED") == 0) {
				col.setIndexed(TRUE);
				tt = COMMA;
			}else if(strcmpi(s.c_str(),"NOT") == 0) {
				i++;
				if(i >= sz) {
					throw DsqlMException("Unexpected end of command after NOT");
				}
				s = vct[i];
				if(strcmpi(s.c_str(),"NULL") == 0) {
					col.setNullable(FALSE);
					col.setIndexed(TRUE);
					continue;
				}else {
					char buff[50];
					sprintf(buff,"Unexpected token at (%d)",(i+1));
					throw DsqlMException(buff);
				}
			}else{
				char buff[50];
				sprintf(buff,"Unexpected token at (%d)",(i+1));
				throw DsqlMException(buff);
			}	
		}else if(tt == SIZE) {
			int sz = atoi(s.c_str());
			if(sz < 0) {
				char buff[50];
				sprintf(buff,"Invalid size %s",s.c_str());
				throw DsqlMException(buff);
			}else {
				if(col.getType() == CHAR) {
					if(sz > 255) {
						char buff[80];
						sprintf(buff,"%d - Column cannot be bigger than 255 (%d)",sz,(i+1));
						throw DsqlMException(buff);
					}
				}
				col.setSize(sz);
			}
			tt = CLS_PAREN;
		}else if(tt == CLS_PAREN) {
			if(s != ")") {
				throw DsqlMException("Expected ) not found");
			}
			tt=ANY;
		}else if(tt == COMMA) {
			vct_cols.push_back(col);
			col.refresh();
			tt = ID;
		}
	}
	table.create();
	for(i=0;i<vct_cols.size();i++) {
		table.addColumn(vct_cols[i]);
	}
}	

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
Web04 | 2.8.150123.1 | Last Updated 23 Mar 2006
Article Copyright 2003 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid