|
/**
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.
**/
/**
bridge.cpp
Purpose: provide interfaces to DSQLM kernel
These interfaces are used directly by
client applications
Author: Vijay Mathew Pandyalakal
Date: 17/11/2003
Copyright: logicmatrix
**/
#include <string>
#include <vector>
using namespace std;
#include <direct.h>
#include "datastore.h"
#include "mydate.h"
using namespace openutils;
/*#include "mytime.h"
using namespace openutils;*/
#include "dsql_m_structs.h"
#include "rec.h"
#include "sql_lexer.h"
#include "createtable.h"
#include "insert.h"
#include "update.h"
#include "delete.h"
#include "bridge.h"
#include "select.h"
using namespace dsqlm;
void Database::init() {
_mkdir(db_name.c_str());
char filnm[26];
sprintf(filnm,"db.ini");
DataStore<DBIni> ds(filnm,db_name.c_str());
ds.Open(true);
if(ds.GetRecordCount() <= 0) {
db_ini.setCr("logicmatrix");
db_ini.setVersion(1.1);
ds.AddRecord(db_ini);
}else {
db_ini = ds.FindRecord(0);
if(strcmpi(db_ini.getCr().c_str(),"logicmatrix") != 0) {
throw DsqlMException("Invalid ini configuration");
}
}
ds.Close();
inited = true;
}
long Database::execute(const char* sql) {
if(!inited) {
throw DsqlMException("Database not inited");
}
if(sql == 0) {
throw DsqlMException("NULL command !");
}else if(strlen(sql) < 3) {
throw DsqlMException("Invalid command !");
}
if(sql[0] == 'U' || sql[0] == 'u') {
SQLLexer lexer(sql);
vector<string> vct = lexer.tokenize();
if(strcmpi(vct[0].c_str(),"use") == 0) {
useDatabase(vct[1].c_str());
return 1;
}else {
Update update(sql,this);
return update.parse();
}
}else if(sql[0] == 'C' || sql[0] == 'c') {
CreateTable ct(sql,this);
ct.parse();
return 1;
}else if(sql[0] == 'I' || sql[0] == 'i') {
Insert insert(sql,this);
insert.parse();
return 1;
}else if(sql[0] == 'D' || sql[0] == 'd') {
SQLLexer lexer(sql);
vector<string> vct = lexer.tokenize();
string s = vct[0];
if(strcmpi(s.c_str(),"DELETE") == 0) {
Delete del(sql,this);
return del.parse();
}else if(strcmpi(s.c_str(),"DROP") == 0) {
if(vct.size() != 3) {
throw DsqlMException("Invalid DROP command");
}
s = vct[1];
if(strcmpi(s.c_str(),"TABLE") == 0) {
s = vct[2];
Table table(s.c_str(),this);
table.drop();
return 1;
}else if(strcmpi(s.c_str(),"DATABASE") == 0) {
Database db(vct[2].c_str());
db.dropDatabase();
return 1;
}else {
throw DsqlMException("Invalid DROP command");
}
}else {
throw DsqlMException("Unsupported command");
}
}else if(sql[0] == 'O' || sql[0] == 'o') {
SQLLexer lexer(sql);
vector<string> vct = lexer.tokenize();
if(vct.size() != 2) {
throw DsqlMException("Invalid OPTIMIZE command");
}
string s = vct[0];
if(strcmpi(s.c_str(),"OPTIMIZE") == 0) {
s = vct[1];
Table table(s.c_str(),this);
return table.optimize();
}
}else {
throw DsqlMException("Unsupported command");
}
}
ResultSet Database::executeQuery(const char* sql) {
if(!inited) {
throw DsqlMException("Database not inited");
}
if(sql == 0) {
throw DsqlMException("NULL command !");
}else if(strlen(sql) < 3) {
throw DsqlMException("Invalid command !");
}
if(sql[0] == 'S' || sql[0] == 's') {
Select sel(sql,this);
return sel.parse();
}else {
throw DsqlMException("Invalid SELECT statement");
}
}
ResultSet::ResultSet() {
this->cur_pos = 0;
}
ResultSet::ResultSet(Table table,Database* db) {
this->cur_pos = 0;
this->table = table;
m_db = db;
}
void ResultSet::setColumns(vector<Column> vct) {
this->vct_cols.clear();
for(int i=0;i<vct.size();i++) {
vct_cols.push_back(vct[i]);
}
}
void ResultSet::setCursor(vector<long> vct) {
this->vct_cursor.clear();
for(int i=0;i<vct.size();i++) {
vct_cursor.push_back(vct[i]);
}
}
bool ResultSet::next() {
cur_pos++;
if(cur_pos > vct_cursor.size()) {
cur_pos = vct_cursor.size();
return false;
}
return true;
}
bool ResultSet::prev() {
cur_pos--;
if(cur_pos < 1) {
cur_pos = 1;
return false;
}
return true;
}
bool ResultSet::first() {
cur_pos = 1;
if(vct_cursor.size() <= 0) {
return false;
}
return true;
}
bool ResultSet::last() {
cur_pos = vct_cursor.size();
if(vct_cursor.size() <= 0) {
return false;
}
return true;
}
string ResultSet::getString(int col_num) {
if(col_num > vct_cols.size()) {
throw DsqlMException("Invalid column number");
}
Rec rec(&table,m_db);
rec.read(vct_cursor[cur_pos-1]);
Column col = vct_cols[col_num-1];
string field = rec.getField(col.getId());
return field;
}
string ResultSet::getString(const char* col_name) {
int col_num = -1;
for(int i=0;i<vct_cols.size();i++) {
if(strcmpi(vct_cols[i].getTitle().c_str(),col_name) == 0) {
col_num = vct_cols[i].getId();
break;
}
}
if(col_num == -1) {
throw DsqlMException("Column not found in resultset");
}
Rec rec(&table,m_db);
rec.read(vct_cursor[cur_pos-1]);
string field = rec.getField(col_num);
return field;
}
int ResultSet::getInt(int col_num) {
string rec = getString(col_num);
return atoi(rec.c_str());
}
int ResultSet::getInt(const char* col_name) {
string rec = getString(col_name);
return atoi(rec.c_str());
}
float ResultSet::getFloat(int col_num) {
string rec = getString(col_num);
return atof(rec.c_str());
}
float ResultSet::getFloat(const char* col_name) {
string rec = getString(col_name);
return atof(rec.c_str());
}
double ResultSet::getDouble(int col_num) {
string rec = getString(col_num);
return (double)atof(rec.c_str());
}
double ResultSet::getDouble(const char* col_name) {
string rec = getString(col_name);
return (double)atof(rec.c_str());
}
long ResultSet::getLong(int col_num) {
string rec = getString(col_num);
return atol(rec.c_str());
}
long ResultSet::getLong(const char* col_name) {
string rec = getString(col_name);
return atol(rec.c_str());
}
openutils::MyDate ResultSet::getDate(int col_num) {
string rec = getString(col_num);
try {
openutils::MyDate date(rec.c_str());
return date;
}catch(openutils::MyDateException ex) {
throw DsqlMException(ex.getMessage().c_str());
}
}
openutils::MyDate ResultSet::getDate(const char* col_name) {
string rec = getString(col_name);
try {
openutils::MyDate date(rec.c_str());
return date;
}catch(openutils::MyDateException ex) {
throw DsqlMException(ex.getMessage().c_str());
}
}
/*openutils::MyTime ResultSet::getTime(int col_num) {
string rec = getString(col_num);
try {
openutils::MyTime time(rec.c_str());
return time;
}catch(openutils::MyTimeException ex) {
throw DsqlMException(ex.getMessage().c_str());
}
}
openutils::MyTime ResultSet::getTime(const char* col_name) {
string rec = getString(col_name);
try {
openutils::MyTime time(rec.c_str());
return time;
}catch(openutils::MyTimeException ex) {
throw DsqlMException(ex.getMessage().c_str());
}
}
*/
bool ResultSet::getBool(int col_num) {
string rec = getString(col_num);
if(strcmpi(rec.c_str(),"true") == 0) {
return true;
}else if(strcmpi(rec.c_str(),"1") == 0) {
return true;
}else if(strcmpi(rec.c_str(),"false") == 0) {
return false;
}else if(strcmpi(rec.c_str(),"0") == 0) {
return 0;
}else {
throw DsqlMException("Not a valid boolean");
}
}
bool ResultSet::getBool(const char* col_nm) {
string rec = getString(col_nm);
if(strcmpi(rec.c_str(),"true") == 0) {
return true;
}else if(strcmpi(rec.c_str(),"1") == 0) {
return true;
}else if(strcmpi(rec.c_str(),"false") == 0) {
return false;
}else if(strcmpi(rec.c_str(),"0") == 0) {
return 0;
}else {
throw DsqlMException("Not a valid boolean");
}
}
void ResultSet::setDb(Database* db) {
m_db = db;
}
void ResultSet::operator = (ResultSet rslt) {
vct_cols.clear();
for(int i=0;i<rslt.vct_cols.size();i++) {
vct_cols.push_back(rslt.vct_cols[i]);
}
vct_cursor.clear();
for(i=0;i<rslt.vct_cursor.size();i++) {
vct_cursor.push_back(rslt.vct_cursor[i]);
}
table = rslt.table;
cur_pos = rslt.cur_pos;
m_db = rslt.m_db;
}
|
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.