start creating a new project: Micro ORM

git-svn-id: svn://ttmath.org/publicrep/morm/trunk@1072 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2018-03-13 22:22:17 +00:00
commit c7327750bd
21 changed files with 2108 additions and 0 deletions

36
src/Makefile Normal file
View File

@ -0,0 +1,36 @@
include Makefile.o.dep
libname=morm.a
ifndef GLOBAL_WORKING_DIR
GLOBAL_WORKING_DIR := $(shell pwd)/../..
endif
current_path := $(shell pwd)
global_relative_working_dir := $(shell relative_path $(current_path) $(GLOBAL_WORKING_DIR))
all: $(libname)
$(libname): $(o)
$(AR) rcs $(libname) $(o)
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) -I$(GLOBAL_WORKING_DIR)/pikotools $<
depend:
makedepend -Y. -I$(global_relative_working_dir)/pikotools -f- *.cpp > Makefile.dep
echo -n "o = " > Makefile.o.dep
ls -1 *.cpp | xargs -I foo echo -n foo " " | sed -E "s/([^\.]*)\.cpp[ ]/\1\.o/g" >> Makefile.o.dep
clean:
rm -f *.o
rm -f $(libname)
include Makefile.dep

65
src/Makefile.dep Normal file
View File

@ -0,0 +1,65 @@
# DO NOT DELETE
baseescaper.o: baseescaper.h ../../pikotools/textstream/textstream.h
baseescaper.o: ../../pikotools/space/space.h
baseescaper.o: ../../pikotools/textstream/types.h ../../pikotools/date/date.h
baseescaper.o: ../../pikotools/convert/convert.h
baseescaper.o: ../../pikotools/convert/inttostr.h
baseescaper.o: ../../pikotools/convert/strtoint.h
baseescaper.o: ../../pikotools/convert/text.h ../../pikotools/convert/misc.h
baseescaper.o: ../../pikotools/membuffer/membuffer.h
baseescaper.o: ../../pikotools/textstream/types.h ../../pikotools/utf8/utf8.h
expression.o: expression.h ../../pikotools/textstream/textstream.h
expression.o: ../../pikotools/space/space.h
expression.o: ../../pikotools/textstream/types.h ../../pikotools/date/date.h
expression.o: ../../pikotools/convert/convert.h
expression.o: ../../pikotools/convert/inttostr.h
expression.o: ../../pikotools/convert/strtoint.h
expression.o: ../../pikotools/convert/text.h ../../pikotools/convert/misc.h
expression.o: ../../pikotools/membuffer/membuffer.h
expression.o: ../../pikotools/textstream/types.h morm_types.h baseescaper.h
expression.o: model.h modelconnector.h
finder.o: finder.h
jsonescaper.o: jsonescaper.h baseescaper.h
jsonescaper.o: ../../pikotools/textstream/textstream.h
jsonescaper.o: ../../pikotools/space/space.h
jsonescaper.o: ../../pikotools/textstream/types.h ../../pikotools/date/date.h
jsonescaper.o: ../../pikotools/convert/convert.h
jsonescaper.o: ../../pikotools/convert/inttostr.h
jsonescaper.o: ../../pikotools/convert/strtoint.h
jsonescaper.o: ../../pikotools/convert/text.h ../../pikotools/convert/misc.h
jsonescaper.o: ../../pikotools/membuffer/membuffer.h
jsonescaper.o: ../../pikotools/textstream/types.h
model.o: model.h ../../pikotools/textstream/textstream.h
model.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h
model.o: ../../pikotools/date/date.h ../../pikotools/convert/convert.h
model.o: ../../pikotools/convert/inttostr.h
model.o: ../../pikotools/convert/strtoint.h ../../pikotools/convert/text.h
model.o: ../../pikotools/convert/misc.h ../../pikotools/membuffer/membuffer.h
model.o: ../../pikotools/textstream/types.h modelconnector.h expression.h
model.o: morm_types.h baseescaper.h
modelconnector.o: modelconnector.h expression.h
modelconnector.o: ../../pikotools/textstream/textstream.h
modelconnector.o: ../../pikotools/space/space.h
modelconnector.o: ../../pikotools/textstream/types.h
modelconnector.o: ../../pikotools/date/date.h
modelconnector.o: ../../pikotools/convert/convert.h
modelconnector.o: ../../pikotools/convert/inttostr.h
modelconnector.o: ../../pikotools/convert/strtoint.h
modelconnector.o: ../../pikotools/convert/text.h
modelconnector.o: ../../pikotools/convert/misc.h
modelconnector.o: ../../pikotools/membuffer/membuffer.h
modelconnector.o: ../../pikotools/textstream/types.h morm_types.h
modelconnector.o: baseescaper.h model.h jsonescaper.h postgresqlescaper.h
postgresqlescaper.o: postgresqlescaper.h baseescaper.h
postgresqlescaper.o: ../../pikotools/textstream/textstream.h
postgresqlescaper.o: ../../pikotools/space/space.h
postgresqlescaper.o: ../../pikotools/textstream/types.h
postgresqlescaper.o: ../../pikotools/date/date.h
postgresqlescaper.o: ../../pikotools/convert/convert.h
postgresqlescaper.o: ../../pikotools/convert/inttostr.h
postgresqlescaper.o: ../../pikotools/convert/strtoint.h
postgresqlescaper.o: ../../pikotools/convert/text.h
postgresqlescaper.o: ../../pikotools/convert/misc.h
postgresqlescaper.o: ../../pikotools/membuffer/membuffer.h
postgresqlescaper.o: ../../pikotools/textstream/types.h

1
src/Makefile.o.dep Normal file
View File

@ -0,0 +1 @@
o = baseescaper.o expression.o finder.o jsonescaper.o model.o modelconnector.o postgresqlescaper.o

91
src/baseescaper.cpp Normal file
View File

@ -0,0 +1,91 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "baseescaper.h"
#include "utf8/utf8.h"
namespace morm
{
BaseEscaper::~BaseEscaper()
{
}
void BaseEscaper::esc(const std::wstring & val, PT::TextStream & stream)
{
char utf8_buf[10];
for(size_t i = 0 ; i < val.size() ; ++i)
{
size_t utf8_len = PT::IntToUTF8((int)val[i], utf8_buf, sizeof(utf8_buf));
for(size_t a = 0 ; a < utf8_len ; ++a)
{
esc(utf8_buf[a], stream);
}
}
}
void BaseEscaper::esc(const wchar_t * val, PT::TextStream & stream)
{
char utf8_buf[10];
for(size_t i = 0 ; val[i] != 0 ; ++i)
{
size_t utf8_len = PT::IntToUTF8((int)val[i], utf8_buf, sizeof(utf8_buf));
for(size_t a = 0 ; a < utf8_len ; ++a)
{
esc(utf8_buf[a], stream);
}
}
}
void BaseEscaper::esc(const std::string & val, PT::TextStream & stream)
{
for(size_t i = 0 ; i < val.size() ; ++i)
{
esc(val[i], stream);
}
}
}

63
src/baseescaper.h Normal file
View File

@ -0,0 +1,63 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_baseescaper
#define headerfile_morm_baseescaper
#include <string>
#include "textstream/textstream.h"
namespace morm
{
class BaseEscaper
{
public:
virtual void esc(char val, PT::TextStream & stream) = 0;
virtual ~BaseEscaper();
void esc(const std::wstring & val, PT::TextStream & stream);
void esc(const wchar_t * val, PT::TextStream & stream);
void esc(const std::string & val, PT::TextStream & stream);
};
}
#endif

57
src/dbinterface.h Normal file
View File

@ -0,0 +1,57 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_dbinterface
#define headerfile_morm_dbinterface
#include <string>
namespace morm
{
class DbInterface
{
};
}
#endif

359
src/expression.cpp Normal file
View File

@ -0,0 +1,359 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "expression.h"
#include "model.h"
namespace morm
{
Expression::Expression()
{
out_stream = nullptr;
model = nullptr;
json_escaper = nullptr;
db_escaper = nullptr;
}
Expression::~Expression()
{
}
void Expression::set_json_escaper(BaseEscaper & json_escaper)
{
this->json_escaper = &json_escaper;
}
void Expression::set_db_escaper(BaseEscaper & db_escaper)
{
this->db_escaper = &db_escaper;
}
void Expression::generate_from_model()
{
before_generate_from_model();
model->map_fields();
after_generate_from_model();
}
void Expression::before_generate_from_model()
{
is_first_field = true;
if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES )
{
if( output_type == MORM_OUTPUT_TYPE_JSON )
{
(*out_stream) << "{";
}
}
}
void Expression::after_generate_from_model()
{
if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES )
{
if( output_type == MORM_OUTPUT_TYPE_JSON )
{
(*out_stream) << "}";
}
}
}
bool Expression::can_field_be_generated(bool insertable, bool updatable, bool is_primary_key)
{
if( output_type == MORM_OUTPUT_TYPE_DB_INSERT )
{
return insertable;
}
else
if( output_type == MORM_OUTPUT_TYPE_DB_UPDATE )
{
return updatable;
}
else
if( output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
{
return is_primary_key;
}
else
{
return true;
}
}
void Expression::field_before()
{
if( !is_first_field )
{
if( output_type == MORM_OUTPUT_TYPE_JSON )
{
(*out_stream) << ",";
}
else
if( output_type == MORM_OUTPUT_TYPE_DB_INSERT ||
output_type == MORM_OUTPUT_TYPE_DB_UPDATE )
{
(*out_stream) << ",";
}
else
if( output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
{
(*out_stream) << " AND ";
}
}
}
void Expression::field_after()
{
is_first_field = false;
}
//void Expression::field(const wchar_t * field_name, Model & field, bool insertable = true, bool updatable = true)
//{
// // IMPLEMENT ME
//}
void Expression::put_field_name(const wchar_t * field_name)
{
before_field_name();
field_name_raw(field_name);
after_field_name();
}
void Expression::before_field_name()
{
if( output_type == MORM_OUTPUT_TYPE_JSON )
{
(*out_stream) << "\"";
}
}
void Expression::after_field_name()
{
if( output_type == MORM_OUTPUT_TYPE_JSON )
{
(*out_stream) << "\"";
}
}
void Expression::before_field_value_string()
{
if( output_type == MORM_OUTPUT_TYPE_JSON )
{
(*out_stream) << "\"";
}
else
if( output_type == MORM_OUTPUT_TYPE_DB_INSERT ||
output_type == MORM_OUTPUT_TYPE_DB_UPDATE ||
output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
{
if( db_type == MORM_DB_TYPE_POSTGRESQL )
{
(*out_stream) << "E'";
}
}
}
void Expression::after_field_value_string()
{
if( output_type == MORM_OUTPUT_TYPE_JSON )
{
(*out_stream) << "\"";
}
else
if( output_type == MORM_OUTPUT_TYPE_DB_INSERT ||
output_type == MORM_OUTPUT_TYPE_DB_UPDATE ||
output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
{
if( db_type == MORM_DB_TYPE_POSTGRESQL )
{
(*out_stream) << "'";
}
}
}
void Expression::before_field_value(const std::wstring &)
{
before_field_value_string();
}
void Expression::before_field_value(const std::string &)
{
before_field_value_string();
}
void Expression::after_field_value(const std::wstring &)
{
after_field_value_string();
}
void Expression::after_field_value(const std::string &)
{
after_field_value_string();
}
void Expression::field_name_raw(const wchar_t * field_name)
{
// IMPROVE ME
// provide esc_field and esc_value methods
esc(field_name, *out_stream);
}
void Expression::field_value_raw(const std::wstring & field_value)
{
// IMPROVE ME
// provide esc_field and esc_value methods
esc(field_value, *out_stream);
}
void Expression::field_value_raw(long field_value)
{
// may it should be moved to escaper?
(*out_stream) << field_value;
}
void Expression::field_value_raw(int field_value)
{
// may it should be moved to escaper?
(*out_stream) << field_value;
}
void Expression::to_json(PT::TextStream & stream, Model & model)
{
work_mode = MORM_WORK_MODE_MODEL_FIELDS_VALUES;
output_type = MORM_OUTPUT_TYPE_JSON;
out_stream = &stream;
this->model = &model;
generate_from_model();
}
void Expression::insert(PT::TextStream & stream, Model & model, int db_type)
{
out_stream = &stream;
output_type = MORM_OUTPUT_TYPE_DB_INSERT;
this->db_type = db_type;
this->model = &model;
stream << "insert into ";
this->model->table_name(stream);
stream << " (";
work_mode = MORM_WORK_MODE_MODEL_FIELDS;
generate_from_model();
stream << ") values (";
work_mode = MORM_WORK_MODE_MODEL_VALUES;
generate_from_model();
stream << ")";
}
void Expression::update(PT::TextStream & stream, Model & model, int db_type)
{
out_stream = &stream;
this->db_type = db_type;
this->model = &model;
stream << "update ";
this->model->table_name(stream);
stream << " set (";
work_mode = MORM_WORK_MODE_MODEL_FIELDS;
output_type = MORM_OUTPUT_TYPE_DB_UPDATE;
generate_from_model();
stream << ") values (";
work_mode = MORM_WORK_MODE_MODEL_VALUES;
output_type = MORM_OUTPUT_TYPE_DB_UPDATE;
generate_from_model();
stream << ") where ";
work_mode = MORM_WORK_MODE_MODEL_FIELDS_VALUES;
output_type = MORM_OUTPUT_TYPE_DB_PRIMARY_KEY;
generate_from_model();
}
/*
template<typename Container>
void field_container(const wchar_t * field_name, Container container, bool insertable = true, bool updatable = true)
{
typename Container::value Value; // czy tu jest Container::value? sprawdzic
if( !is_first_field )
(*out_stream) << ",";
if( work_mode == ORM_STREAM_MODE_CREATING_JSON )
{
(*out_stream) << "[";
boolean is_first = true;
for(Value v : container)
{
if( !is_first )
(*out_stream) << ",";
v.map_fields(this);
is_first = false;
}
(*out_stream) << "]";
}
// a co z bazą danych?
is_first_field = false;
}
*/
} // namespace

295
src/expression.h Normal file
View File

@ -0,0 +1,295 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_expression
#define headerfile_morm_expression
#include "textstream/textstream.h"
#include "morm_types.h"
#include "baseescaper.h"
namespace morm
{
class Model;
class ModelConnector;
class Expression
{
public:
Expression();
virtual ~Expression();
virtual void set_json_escaper(BaseEscaper & json_escaper);
virtual void set_db_escaper(BaseEscaper & db_escaper);
virtual void to_json(PT::TextStream & stream, Model & model);
virtual void insert(PT::TextStream & stream, Model & model, int db_type);
virtual void update(PT::TextStream & stream, Model & model, int db_type);
Expression & eq(const wchar_t * field_name, const std::wstring & field_value)
{
//put_field_name(field_name);
return *this;
}
Expression & eq(const wchar_t * field_name, const std::string & field_value)
{
return *this;
}
template<typename FieldValue>
Expression & eq(const wchar_t * field_name, const FieldValue & field_value)
{
return *this;
}
/*
*
*
*
*
*
*/
protected:
int work_mode; /* what to do: generating fields list, values list or fields-values list */
int output_type; /* what the output string is: for json, for database */
int db_type; /* what type of database we are using if generating a string for a database */
bool is_first_field;
// niech Stream bedzie jakims interfejsem z operatorami << dla standardowych typow
PT::TextStream * out_stream;
Model * model;
BaseEscaper * json_escaper;
BaseEscaper * db_escaper;
/*
ostream& operator<< (bool val);
ostream& operator<< (short val);
ostream& operator<< (unsigned short val);
ostream& operator<< (int val);
ostream& operator<< (unsigned int val);
ostream& operator<< (long val);
ostream& operator<< (unsigned long val);
ostream& operator<< (long long val);
ostream& operator<< (unsigned long long val);
ostream& operator<< (float val);
ostream& operator<< (double val);
ostream& operator<< (long double val);
ostream& operator<< (void* val);
*/
// virtual void map_fields(Model * model)
// {
// work_mode = model->work_mode;
// out_stream = model->out_stream;
// is_insert = model->is_insert;
// is_update = model->is_update;
//
// generate_from_model();
// }
virtual void generate_from_model();
virtual void before_generate_from_model();
virtual void after_generate_from_model();
virtual bool can_field_be_generated(bool insertable, bool updatable, bool is_primary_key);
virtual void field_before();
virtual void field_after();
template<typename FieldValue>
void field(const wchar_t * field_name, const FieldValue & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false)
{
if( can_field_be_generated(insertable, updatable, is_primary_key) )
{
field_before();
if( work_mode == MORM_WORK_MODE_MODEL_FIELDS )
{
put_field_name(field_name);
}
else
if( work_mode == MORM_WORK_MODE_MODEL_VALUES )
{
put_field_value(field_value);
}
else
if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES )
{
print_field_name_value(field_name, field_value);
}
field_after();
}
}
//void field(const wchar_t * field_name, Model & field, bool insertable = true, bool updatable = true);
virtual void put_field_name(const wchar_t * field_name);
template<typename FieldValue>
void put_field_value(const FieldValue & field_value)
{
before_field_value(field_value);
field_value_raw(field_value);
after_field_value(field_value);
}
virtual void before_field_name();
virtual void after_field_name();
virtual void before_field_value_string();
virtual void after_field_value_string();
virtual void before_field_value(const std::wstring &);
virtual void after_field_value(const std::wstring &);
virtual void before_field_value(const std::string &);
virtual void after_field_value(const std::string &);
template<typename FieldValue>
void before_field_value(const FieldValue &)
{
}
template<typename FieldValue>
void after_field_value(const FieldValue &)
{
}
virtual void field_name_raw(const wchar_t * field_name);
virtual void field_value_raw(const std::wstring & field_value);
virtual void field_value_raw(long field_value);
virtual void field_value_raw(int field_value);
template<typename FieldValue>
void print_field_name_value(const wchar_t * field_name, const FieldValue & field_value)
{
put_field_name(field_name);
if( output_type == MORM_OUTPUT_TYPE_JSON )
{
(*out_stream) << ":";
}
else
if( output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
{
(*out_stream) << " = ";
}
put_field_value(field_value);
}
/*
template<typename Container>
virtual void field_container(const wchar_t * field_name, Container container, bool insertable = true, bool updatable = true)
{
typename Container::value Value; // czy tu jest Container::value? sprawdzic
if( !is_first_field )
(*out_stream) << ",";
if( work_mode == ORM_STREAM_MODE_CREATING_JSON )
{
(*out_stream) << "[";
boolean is_first = true;
for(Value v : container)
{
if( !is_first )
(*out_stream) << ",";
v.map_fields(this);
is_first = false;
}
(*out_stream) << "]";
}
// a co z bazą danych?
is_first_field = false;
}
*/
template<typename Field>
void esc(const Field & val, PT::TextStream & stream)
{
if( output_type == MORM_OUTPUT_TYPE_JSON )
{
if( json_escaper )
{
json_escaper->esc(val, stream);
}
}
else
if( output_type == MORM_OUTPUT_TYPE_DB_INSERT ||
output_type == MORM_OUTPUT_TYPE_DB_UPDATE ||
output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
{
if( db_escaper )
{
db_escaper->esc(val, stream);
}
}
}
friend ModelConnector;
};
} // namespace
#endif

54
src/finder.cpp Normal file
View File

@ -0,0 +1,54 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "finder.h"
namespace morm
{
} // namespace

58
src/finder.h Normal file
View File

@ -0,0 +1,58 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_finder
#define headerfile_morm_finder
namespace morm
{
} // namespace
#endif

63
src/jsonescaper.cpp Normal file
View File

@ -0,0 +1,63 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "jsonescaper.h"
namespace morm
{
void JSONEscaper::esc(char val, PT::TextStream & stream)
{
switch( val )
{
case 0: stream << '\\'; stream << '0'; break; // may to skip this character is better?
case '\r': stream << '\\'; stream << 'r'; break;
case '\n': stream << '\\'; stream << 'n'; break;
case '\t': stream << '\\'; stream << 't'; break;
case 0x08: stream << '\\'; stream << 'b'; break;
case 0x0c: stream << '\\'; stream << 'f'; break;
case '\\': stream << '\\'; stream << '\\'; break;
case '"': stream << '\\'; stream << '\"'; break;
default:
stream << val;
}
}
}

58
src/jsonescaper.h Normal file
View File

@ -0,0 +1,58 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_jsonescaper
#define headerfile_morm_jsonescaper
#include "baseescaper.h"
namespace morm
{
class JSONEscaper : public BaseEscaper
{
public:
void esc(char val, PT::TextStream & stream);
};
}
#endif

139
src/model.cpp Normal file
View File

@ -0,0 +1,139 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "model.h"
namespace morm
{
Model::Model()
{
model_connector = nullptr;
}
Model::~Model()
{
}
void Model::table_name(PT::TextStream & stream)
{
}
void Model::set_connector(ModelConnector & connector)
{
model_connector = &connector;
}
void Model::to_json(PT::TextStream & stream)
{
if( model_connector )
{
model_connector->to_json(stream, *this);
}
}
void Model::to_json(std::string & str)
{
PT::TextStream stream;
to_json(stream);
stream.to_string(str);
}
std::string Model::to_json()
{
std::string str;
to_json(str);
return str;
}
void Model::insert(PT::TextStream & stream, int orm_db_type)
{
if( model_connector )
{
model_connector->insert(stream, *this, orm_db_type);
}
}
void Model::insert(PT::TextStream & stream)
{
if( model_connector )
{
model_connector->insert(stream, *this);
}
}
void Model::insert()
{
if( model_connector )
{
model_connector->insert(*this);
}
}
void Model::update(PT::TextStream & stream, int orm_db_type)
{
if( model_connector )
{
model_connector->update(stream, *this, orm_db_type);
}
}
void Model::update(PT::TextStream & stream)
{
if( model_connector )
{
model_connector->update(stream, *this);
}
}
void Model::update()
{
if( model_connector )
{
model_connector->update(*this);
}
}
} // namespace

112
src/model.h Normal file
View File

@ -0,0 +1,112 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_model
#define headerfile_morm_model
#include <string>
#include "textstream/textstream.h"
#include "modelconnector.h"
namespace morm
{
class Model
{
public:
void set_connector(ModelConnector & connector);
/*
* map fields to names
*
*
*/
virtual void map_fields() = 0;
/*
*
* we can use the object for a different purpose than database (e.g. json)
* so let the table_name be non pure-virtual
*
*/
virtual void table_name(PT::TextStream & stream);
virtual void to_json(PT::TextStream & stream);
virtual void to_json(std::string & str);
virtual std::string to_json();
virtual void insert(PT::TextStream & stream, int orm_db_type);
virtual void insert(PT::TextStream & stream);
virtual void insert();
virtual void update(PT::TextStream & stream, int orm_db_type);
virtual void update(PT::TextStream & stream);
virtual void update();
protected:
ModelConnector * model_connector;
Model();
virtual ~Model();
template<typename FieldValue>
void field(const wchar_t * field_name, const FieldValue & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false)
{
if( model_connector)
{
model_connector->field(field_name, field_value, insertable, updatable, is_primary_key);
}
}
//void field(const wchar_t * field_name, Model & field, bool insertable = true, bool updatable = true);
};
} // namespace
#endif

311
src/modelconnector.cpp Normal file
View File

@ -0,0 +1,311 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "modelconnector.h"
#include "model.h"
#include "jsonescaper.h"
#include "postgresqlescaper.h"
namespace morm
{
ModelConnector::ModelConnector()
{
expression = nullptr;
out_stream = nullptr;
json_escaper = nullptr;
postgresql_escaper = nullptr;
expression_allocated = false;
out_stream_allocated = false;
json_escaper_allocated = false;
postgresql_escaper_allocated = false;
assigned_db_type = MORM_DB_TYPE_POSTGRESQL;
}
ModelConnector::~ModelConnector()
{
deallocate_expression();
deallocate_stream();
deallocate_json_escaper();
deallocate_postgresql_escaper();
}
void ModelConnector::deallocate_expression()
{
if( expression_allocated )
{
delete expression;
expression = nullptr;
expression_allocated = false;
}
}
void ModelConnector::allocate_default_expression()
{
deallocate_expression();
expression = new Expression();
expression_allocated = true;
}
void ModelConnector::deallocate_stream()
{
if( out_stream_allocated )
{
delete out_stream;
out_stream = nullptr;
out_stream_allocated = false;
}
}
void ModelConnector::allocate_default_stream()
{
deallocate_stream();
out_stream = new PT::TextStream();
out_stream_allocated = true;
}
void ModelConnector::deallocate_json_escaper()
{
if( json_escaper_allocated )
{
delete json_escaper;
json_escaper = nullptr;
json_escaper_allocated = false;
}
}
void ModelConnector::allocate_default_json_escaper()
{
deallocate_json_escaper();
json_escaper = new JSONEscaper();
json_escaper_allocated = true;
}
void ModelConnector::deallocate_postgresql_escaper()
{
if( postgresql_escaper_allocated )
{
delete postgresql_escaper;
postgresql_escaper = nullptr;
postgresql_escaper_allocated = false;
}
}
void ModelConnector::allocate_default_postgresql_escaper()
{
deallocate_postgresql_escaper();
postgresql_escaper = new PostgreSQLEscaper();
postgresql_escaper_allocated = true;
}
void ModelConnector::allocate_default_stream_if_needed()
{
if( !out_stream )
allocate_default_stream();
}
void ModelConnector::allocate_default_expression_if_needed()
{
if( !expression )
allocate_default_expression();
}
void ModelConnector::allocate_default_json_escaper_if_needed()
{
if( !json_escaper )
allocate_default_json_escaper();
}
void ModelConnector::allocate_default_postgresql_escaper_if_needed()
{
if( !postgresql_escaper )
allocate_default_postgresql_escaper();
}
void ModelConnector::allocate_default_db_escaper_if_needed()
{
if( assigned_db_type == MORM_DB_TYPE_POSTGRESQL )
{
allocate_default_postgresql_escaper_if_needed();
}
}
void ModelConnector::set_expression(Expression & expression)
{
deallocate_expression();
this->expression = &expression;
}
void ModelConnector::set_stream(PT::TextStream & stream)
{
deallocate_stream();
this->out_stream = &stream;
}
Expression * ModelConnector::get_expression()
{
return expression;
}
PT::TextStream * ModelConnector::get_stream()
{
return out_stream;
}
void ModelConnector::to_json(PT::TextStream & stream, Model & model)
{
allocate_default_expression_if_needed();
allocate_default_json_escaper_if_needed();
expression->set_json_escaper(*json_escaper);
expression->to_json(stream, model);
}
void ModelConnector::insert(PT::TextStream & stream, Model & model, int db_type)
{
allocate_default_expression_if_needed();
allocate_default_db_escaper_if_needed(); // FIX ME what about db_type parameter
expression->set_db_escaper(*postgresql_escaper); //FIX ME select correct db escaper
expression->insert(stream, model, db_type);
}
void ModelConnector::update(PT::TextStream & stream, Model & model, int db_type)
{
allocate_default_expression_if_needed();
allocate_default_db_escaper_if_needed(); // FIX ME what about db_type parameter
expression->set_db_escaper(*postgresql_escaper); //FIX ME select correct db escaper
expression->update(stream, model, db_type);
}
void ModelConnector::insert(PT::TextStream & stream, Model & model)
{
allocate_default_expression_if_needed();
allocate_default_db_escaper_if_needed();
expression->set_db_escaper(*postgresql_escaper); //FIX ME select correct db escaper
expression->insert(stream, model, assigned_db_type);
}
void ModelConnector::update(PT::TextStream & stream, Model & model)
{
allocate_default_expression_if_needed();
allocate_default_db_escaper_if_needed();
expression->set_db_escaper(*postgresql_escaper); //FIX ME select correct db escaper
expression->update(stream, model, assigned_db_type);
}
void ModelConnector::to_json(Model & model)
{
allocate_default_expression_if_needed();
allocate_default_stream_if_needed();
allocate_default_json_escaper_if_needed();
expression->set_json_escaper(*json_escaper);
expression->to_json(*out_stream, model);
}
void ModelConnector::insert(Model & model, int db_type)
{
allocate_default_expression_if_needed();
allocate_default_stream_if_needed();
allocate_default_db_escaper_if_needed(); // FIX ME what about db_type parameter?
expression->set_db_escaper(*postgresql_escaper); //FIX ME select correct db escaper
expression->insert(*out_stream, model, db_type);
}
void ModelConnector::update(Model & model, int db_type)
{
allocate_default_expression_if_needed();
allocate_default_stream_if_needed();
allocate_default_db_escaper_if_needed(); // FIX ME what about db_type parameter?
expression->set_db_escaper(*postgresql_escaper); //FIX ME select correct db escaper
expression->update(*out_stream, model, db_type);
}
void ModelConnector::insert(Model & model)
{
allocate_default_expression_if_needed();
allocate_default_stream_if_needed();
allocate_default_db_escaper_if_needed();
expression->set_db_escaper(*postgresql_escaper); //FIX ME select correct db escaper
expression->insert(*out_stream, model, assigned_db_type);
}
void ModelConnector::update(Model & model)
{
allocate_default_expression_if_needed();
allocate_default_stream_if_needed();
allocate_default_db_escaper_if_needed();
expression->set_db_escaper(*postgresql_escaper); //FIX ME select correct db escaper
expression->update(*out_stream, model, assigned_db_type);
}
}

128
src/modelconnector.h Normal file
View File

@ -0,0 +1,128 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_modelconnector
#define headerfile_morm_modelconnector
#include "expression.h"
namespace morm
{
class Model;
class ModelConnector
{
public:
ModelConnector();
~ModelConnector();
// FIX ME
// add c-copy ctr (allocate a new stream and expression)
void set_expression(Expression & expression);
void set_stream(PT::TextStream & stream);
Expression * get_expression();
PT::TextStream * get_stream();
virtual void to_json(PT::TextStream & stream, Model & model);
virtual void insert(PT::TextStream & stream, Model & model, int db_type);
virtual void update(PT::TextStream & stream, Model & model, int db_type);
virtual void insert(PT::TextStream & stream, Model & model);
virtual void update(PT::TextStream & stream, Model & model);
virtual void to_json(Model & model);
virtual void insert(Model & model, int db_type);
virtual void update(Model & model, int db_type);
virtual void insert(Model & model);
virtual void update(Model & model);
protected:
Expression * expression;
PT::TextStream * out_stream; // IMPROVE ME give here an interface to the base stream (implement him)
BaseEscaper * json_escaper;
BaseEscaper * postgresql_escaper;
bool expression_allocated;
bool out_stream_allocated;
bool json_escaper_allocated;
bool postgresql_escaper_allocated;
int assigned_db_type;
void allocate_default_expression();
void allocate_default_expression_if_needed();
void deallocate_expression();
void allocate_default_stream();
void allocate_default_stream_if_needed();
void deallocate_stream();
void allocate_default_json_escaper();
void allocate_default_json_escaper_if_needed();
void deallocate_json_escaper();
void allocate_default_postgresql_escaper();
void allocate_default_postgresql_escaper_if_needed();
void deallocate_postgresql_escaper();
void allocate_default_db_escaper_if_needed();
template<typename FieldValue>
void field(const wchar_t * field_name, const FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key)
{
if( expression )
{
expression->field(field_name, field_value, insertable, updatable, is_primary_key);
}
}
/*
* Model is using field() method
*/
friend Model;
};
}
#endif

49
src/morm.h Normal file
View File

@ -0,0 +1,49 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_morm
#define headerfile_morm_morm
#include "morm_types.h"
#include "model.h"
#include "expression.h"
#include "finder.h"
namespace morm
{
} // namespace
#endif

55
src/morm_types.h Normal file
View File

@ -0,0 +1,55 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_morm_types
#define headerfile_morm_morm_types
#define MORM_WORK_MODE_MODEL_FIELDS 1
#define MORM_WORK_MODE_MODEL_VALUES 2
#define MORM_WORK_MODE_MODEL_FIELDS_VALUES 3
#define MORM_OUTPUT_TYPE_JSON 1
#define MORM_OUTPUT_TYPE_DB_INSERT 2
#define MORM_OUTPUT_TYPE_DB_UPDATE 3
#define MORM_OUTPUT_TYPE_DB_PRIMARY_KEY 4
#define MORM_DB_TYPE_POSTGRESQL 1
namespace morm
{
} // namespace
#endif

0
src/outstream.h Normal file
View File

56
src/postgresqlescaper.cpp Normal file
View File

@ -0,0 +1,56 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "postgresqlescaper.h"
namespace morm
{
void PostgreSQLEscaper::esc(char val, PT::TextStream & stream)
{
switch( val )
{
case '\\': stream << "\\\\"; break;
case '\'': stream << "\\\'"; break; // don't use "''" because we use the method for PQconnectdb too
default:
if( val != 0 )
{
stream << val;
}
}
}
}

58
src/postgresqlescaper.h Normal file
View File

@ -0,0 +1,58 @@
/*
* This file is a part of morm
* and is distributed under the 2-Clause BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2018, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_postgresqlescaper
#define headerfile_morm_postgresqlescaper
#include "baseescaper.h"
namespace morm
{
class PostgreSQLEscaper : public BaseEscaper
{
public:
void esc(char val, PT::TextStream & stream);
};
}
#endif