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

DarkSide SQL Mini Version 1, The embedded database

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


**/

/**
	rec.cpp

	Purpose: Defines classes to records on disk

	Author: Vijay Mathew Pandyalakal
	Date:	14/11/2003
	Copyright: logicmatrix
**/

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

#include "db_cxx.h"

#include "datastore.h"

#include "mydate.h"
using namespace openutils;

#include "dsql_m_structs.h"
#include "bridge.h"
#include "bdb_index.h"
#include "rec.h"
#include "delflag.h"
#include "text.h"
#include "sql_utils.h"
using namespace dsqlm;


Rec::Rec(Table* table,Database* db) {
	if(db == 0) {
		throw DsqlMException("Rec.cpp - database not inited");
	}
	m_db = db;
	this->table = table;
	seq_rec = 0;
	seq_file_len = 0;
	seq_open = false;
	seq_rec_len = 0;
	initBuffer();
}

Rec::~Rec() {
	/*if(buffer != 0) {
		delete[] buffer;
	}*/
}

void Rec::write(long pos) {
	char filnm[80];
	sprintf(filnm,"%s\\%s_data",m_db->getDBName().c_str(),
		table->getName().c_str());
	FILE* file = fopen(filnm,"rb+");	
	if(file == 0) {
		file = fopen(filnm,"wb+");
		if(file == 0) {
			throw DsqlMException("Failed to open data file for writing");
		}
	}
	if(pos >= 0) {
		fseek(file,pos,SEEK_CUR);
	}else {
		fseek(file,0,SEEK_END);
	}
	char *buff = new char[buffer.size() + 1];
	strcpy(buff,buffer.c_str());
	int r = fwrite((char*)buff,strlen(buff),1,file);
	delete[] buff;
	fclose(file);
}

void Rec::read(long pos) {
	char filnm[80];
	sprintf(filnm,"%s\\%s_data",m_db->getDBName().c_str(),
		table->getName().c_str());
	FILE* file = fopen(filnm,"rb+");
	if(file == 0) {
		throw DsqlMException("Failed to open data file for reading");
	}
	if(pos >= 0) {
		fseek(file,pos,SEEK_CUR);
	}else {
		throw DsqlMException("Invalid file pointer");
	}
	int len = initBuffer();
	char *buff = new char[len+1];
	fread((char*)buff,len,1,file);
	buff[len] = '\0';
	buffer = buff;
	delete[] buff;
	fclose(file);
}

void Rec::appendField(int col_num,const char* data,char* ret) {	
	int i = 0;		
	Column col = table->getCol(col_num);
	char *buff = new char[col.getSize() + 1];
	strcpy(buff,"");
	SQLUtils utils(m_db);
	try {
		utils.checkFieldData(table->getName().c_str(),col,data,buff);
	}catch(DsqlMException ex) {
		delete[] buff;
		throw DsqlMException(ex.getMessage().c_str());
	}	
	//int pos = field_pos[col_num-1];
	if(strcmpi(data,"NULL") == 0) {
		if(col.isUnique() || (col.isNullable() == false)) {
			delete[] buff;
			throw DsqlMException("Column cannot be null");
		}
		strcpy(buff,"");
		buffer += '1';
	}else {
		buffer += '0';
	}
	strcpy(ret,buff);
	int sz = col.getSize();	
	int len = strlen(buff);
	for(i=0;i<len;i++) {
		char c = buff[i];
		buffer += c;
	}
	int diff = sz - len;
	if(diff > 0) {
		for(i=0;i<diff;i++) {
			buffer += ' ';
		}
	}
	delete[] buff;
}

void Rec::updateField(int col_num,const char* data,char* ret) {	
	int i = 0;	
	Column col = table->getCol(col_num);
	char *buff = new char[col.getSize() + 1];	
	strcpy(buff,"");	
	SQLUtils utils(m_db);
	try {
		utils.checkFieldData(table->getName().c_str(),col,data,buff);
	}catch(DsqlMException ex) {
		delete[] buff;		
		throw DsqlMException(ex.getMessage().c_str());
	}
	strcpy(ret,buff);
	int f_pos = field_pos[col_num-1];	
	if(strcmpi(data,"NULL") == 0) {
		if(col.isUnique() || (col.isNullable() == false)) {
			delete[] buff;			
			throw DsqlMException("Column cannot be null");
		}
		buffer[f_pos] = '1';
	}else {
		buffer[f_pos] = '0';
	}
	int sz = col.getSize();	
	int len = strlen(buff);
	f_pos++;
	for(i=0;i<len;i++) {
		char c = buff[i];
		buffer[f_pos++] = c;
	}
	int diff = sz - len;
	if(diff > 0) {
		for(i=0;i<diff;i++) {
			buffer[f_pos++] = ' ';
		}
	}	
	delete[] buff;	
}

string Rec::getField(int col_num) {
	Column col = table->getCol(col_num);
	int pos = field_pos[col_num-1];
	int sz = col.getSize();	
	if(buffer[pos] == '1') {
		return "NULL";
	}
	char *buff = new char[sz+1];
	pos++;
	int j = 0;
	for(j=0;j<sz;j++) {
		buff[j] = buffer[pos];
		pos++;
	}
	buff[j] = '\0';
	string ret = buff;
	delete[] buff;
	return ret;
}

int Rec::initBuffer() {
	int numcols = table->getNumberOfColumns();
	int len = 1;
	SQLUtils utils(m_db);
	vector<int> vct_pos;
	field_pos.clear();
	for(int i=0;i<numcols;i++) {
		Column col = table->getCol((i+1));
		int sz = utils.getSize(col.getType());
		field_pos.push_back(len);
		if(sz == -1) {
			len += col.getSize();
		}else {
			len += sz;
		}
		vct_pos.push_back(len+1);
		len += 1; // for null flag
	}
	buffer = "";
	buffer += '0';
	return len;
}

long Rec::fileLength() {
	char filnm[80];
	sprintf(filnm,"%s\\%s_data",m_db->getDBName().c_str(),
		table->getName().c_str());
	FILE* file = fopen(filnm,"rb+");
	if(file == 0) {
		return 0;
	}
	fseek(file,0,SEEK_END);
	long len = ftell(file);
	fclose(file);
	return len;
}

int Rec::recLength() {
	int numcols = table->getNumberOfColumns();
	int len = 1;
	SQLUtils utils(m_db);	
	for(int i=0;i<numcols;i++) {
		Column col = table->getCol((i+1));
		int sz = utils.getSize(col.getType());
		field_pos.push_back(len);
		if(sz == -1) {
			len += col.getSize();
		}else {
			len += sz;
		}		
		len += 1; // for null flag
	}	
	return len;
}

void Rec::openCursor() {
	if(seq_open) {
		throw DsqlMException("File is already open for sequential access");
	}
	seq_open = true;
	seq_rec = 0;
	seq_rec_len = recLength();
	seq_file_len = fileLength();
}

bool Rec::next(long* ret_pos) {
	if(!seq_open) {
		throw DsqlMException("Cursor is closed");
	}	
	long pos = seq_rec * seq_rec_len;
	if(pos >= seq_file_len) {
		return false;
	}
	seq_rec++;
	read(pos);
	*ret_pos = pos;
	return true;
}

void Rec::del() {
	buffer[0] = '1';
}

void Rec::del(long pos) {
	char filnm[80];
	sprintf(filnm,"%s_data",table->getName().c_str());
	DataStore<DelFlag> ds(filnm,m_db->getDBName().c_str());
	ds.Open(true);
	DelFlag df;
	df.flag[0]='1';
	ds.ModifyRecord(df,pos);
	ds.Close();	
}

void Rec::closeCursor() {
	seq_open = false;
	seq_rec = 0;
	seq_rec_len = 0;
	seq_file_len = 0;
}

bool Rec::isDeleted() {
	if(buffer[0] == '1') {
		return true;
	}
	return false;
}

bool Rec::isDeleted(long pos) {
	char filnm[80];
	sprintf(filnm,"%s_data",table->getName().c_str());
	DataStore<DelFlag> ds(filnm,m_db->getDBName().c_str());
	ds.Open(true);
	DelFlag df = ds.FindRecord(pos);
	ds.Close();
	if(df.flag[0] == '1') return true;
	return false;
}

bool Rec::isNull(int col_num) {
	Column col = table->getCol(col_num);
	int pos = field_pos[col_num-1];
	int sz = col.getSize();	
	if(buffer[pos] == '1') {
		return true;
	}	
	return false;
}


long Rec::optimize() {
	char tmp_filnm[101];
	sprintf(tmp_filnm,"%s\\%s_tmp",m_db->getDBName().c_str(),
		table->getName().c_str());
	FILE* tmp_file = fopen(tmp_filnm,"wb+");
	if(tmp_file == 0) {
		throw DsqlMException("Failed to open temporory data file");
	}
	openCursor();
	long pos = 0;
	long ret = 0;
	int num_cols = table->getNumberOfColumns();
	char db_name[101];
	strcpy(db_name,"");
	Column col;	
	for(int i=0;i<num_cols;i++) {
		col = table->getCol(i+1);
		if(col.isIndexed() || col.isUnique()) {
			sprintf(db_name,"%s\\%s_%s_x",m_db->getDBName().c_str(),
					table->getName().c_str(),col.getTitle().c_str());
			if(unlink(db_name) != 0) {
				char err_buff[150];
				sprintf(err_buff,"Optimization failed on %s",db_name);
				throw DsqlMException (err_buff);
			}
		}
	}
	long rec_pos = 0;
	int rec_sz = this->recLength();
	while(next(&pos)) {
		if(!isDeleted()) {
			fwrite((char*)buffer.c_str(),buffer.size(),1,tmp_file);
			for(int i=0;i<num_cols;i++) {
				col = table->getCol(i+1);
				if(col.isIndexed() || col.isUnique()) {
					char db_name[101];
					sprintf(db_name,"%s\\%s_%s_x",m_db->getDBName().c_str(),
						table->getName().c_str(),col.getTitle().c_str());
					BdbIndex index(db_name,false);
					string key = this->getField(i+1);
					Text text;
					key = text.trim(key);
					char dat[24];
					sprintf(dat,"%d",rec_pos);					
					index.put(key.c_str(),dat);					
				}
			}
			rec_pos += rec_sz;
		}else {
			ret++;
		}
	}
	fclose(tmp_file);
	closeCursor();
	char dat_file[101];
	sprintf(dat_file,"%s\\%s_data",m_db->getDBName().c_str(),
		table->getName().c_str());
	unlink(dat_file);
	rename(tmp_filnm,dat_file);
	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.

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 | Mobile
Web02 | 2.8.140926.1 | Last Updated 23 Mar 2006
Article Copyright 2003 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid