added 'samples' directory for samples and tests

git-svn-id: svn://ttmath.org/publicrep/morm/trunk@1202 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2019-08-21 15:59:03 +00:00
parent 265197ce47
commit 074be938ca
8 changed files with 576 additions and 0 deletions

63
samples/Makefile Normal file
View File

@ -0,0 +1,63 @@
include Makefile.o.dep
name=mormsample
ifndef GLOBAL_WORKING_DIR
GLOBAL_WORKING_DIR := $(shell pwd)/../..
endif
CXX = g++7
#CXX = clang++
# -fsanitize=address
# -Wl,-rpath=/usr/local/lib/gcc5 or just compile with -static-libstdc++
CXXFLAGS = -Wl,-rpath=/usr/local/lib/gcc7 -Wfatal-errors -fPIC -Wall -pedantic -O0 -g3 -gdwarf-2 -pthread -std=c++17 -I/usr/local/include -I$(GLOBAL_WORKING_DIR)/pikotools -I$(GLOBAL_WORKING_DIR)/morm/src
LDFLAGS = -L/usr/local/lib
export CXX
export CXXFLAGS
export LDFLAGS
export GLOBAL_WORKING_DIR
current_path := $(shell pwd)
global_relative_working_dir := $(shell relative_path $(current_path) $(GLOBAL_WORKING_DIR))
all: morm $(name)
$(name): $(o)
$(CXX) -o $(name) $(CXXFLAGS) $(LDFLAGS) $(o) $(GLOBAL_WORKING_DIR)/morm/src/morm.a $(GLOBAL_WORKING_DIR)/pikotools/log/log.a $(GLOBAL_WORKING_DIR)/pikotools/space/space.a $(GLOBAL_WORKING_DIR)/pikotools/mainspaceparser/mainspaceparser.a $(GLOBAL_WORKING_DIR)/pikotools/date/date.a $(GLOBAL_WORKING_DIR)/pikotools/convert/convert.a $(GLOBAL_WORKING_DIR)/pikotools/utf8/utf8.a $(LDFLAGS) -lpq -lpthread
.PHONY: morm
morm:
@cd $(GLOBAL_WORKING_DIR)/morm/src ; $(MAKE) -e
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $<
depend:
makedepend -Y. -I$(global_relative_working_dir)/pikotools -I$(global_relative_working_dir)/morm/src -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:
@cd $(GLOBAL_WORKING_DIR)/morm/src ; $(MAKE) -e clean
rm -f *.o
rm -f $(name)
include Makefile.dep

23
samples/Makefile.dep Normal file
View File

@ -0,0 +1,23 @@
# DO NOT DELETE
main.o: ../../pikotools/mainspaceparser/mainspaceparser.h
main.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h
main.o: sample01.h basesample.h ../../morm/src/morm.h
main.o: ../../morm/src/morm_types.h ../../morm/src/model.h
main.o: ../../pikotools/textstream/textstream.h ../../pikotools/date/date.h
main.o: ../../pikotools/convert/inttostr.h
main.o: ../../pikotools/membuffer/membuffer.h
main.o: ../../pikotools/textstream/types.h ../../morm/src/modelconnector.h
main.o: ../../morm/src/clearer.h ../../morm/src/dbconnector.h
main.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h
main.o: ../../morm/src/queryresult.h ../../morm/src/flatconnector.h
main.o: ../../morm/src/dbexpression.h ../../morm/src/baseexpression.h
main.o: ../../morm/src/modelenv.h ../../morm/src/modeldata.h
main.o: ../../morm/src/cursorhelper.h ../../morm/src/finderhelper.h
main.o: ../../morm/src/flatexpression.h ../../morm/src/finder.h
main.o: ../../pikotools/utf8/utf8.h ../../morm/src/cursor.h
main.o: ../../morm/src/jsonexpression.h ../../morm/src/postgresqlexpression.h
main.o: ../../morm/src/dochtmlexpression.h ../../morm/src/jsonconnector.h
main.o: ../../morm/src/postgresqlconnector.h
main.o: ../../morm/src/postgresqlqueryresult.h
main.o: ../../morm/src/dochtmlconnector.h person.h language.h

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

@ -0,0 +1 @@
o = main.o

76
samples/basesample.h Normal file
View File

@ -0,0 +1,76 @@
/*
* 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) 2019, 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_samples_basesample
#define headerfile_morm_samples_basesample
#include "morm.h"
namespace morm
{
namespace samples
{
class BaseSample
{
public:
virtual void make() = 0;
virtual ~BaseSample()
{
}
void set_connector(ModelConnector & model_connector)
{
// the lifetime of model_connector must exceed the lifetime of this object
this->model_connector = &model_connector;
}
protected:
ModelConnector * model_connector;
};
}
}
#endif

98
samples/language.h Normal file
View File

@ -0,0 +1,98 @@
/*
* 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) 2019, 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_samples_language
#define headerfile_morm_samples_language
#include <string>
#include "morm.h"
namespace morm
{
namespace samples
{
/*
CREATE TABLE public.language (
id bigserial,
english_name varchar(64),
local_name varchar(64),
code_str varchar(4),
code_int int,
primary key(id)
);
*/
class Language : public morm::Model
{
public:
long id;
std::wstring english_name;
std::wstring local_name;
std::wstring code_str;
int code_int;
void map_fields()
{
field(L"id", id, false, false, true);
field(L"english_name", english_name);
field(L"local_name", local_name);
field(L"code_str", code_str);
field(L"code_int", code_int);
}
void table_name(PT::TextStream & stream)
{
// schema.table_name or just table_name
stream << "public.language";
}
};
}
}
#endif

131
samples/main.cpp Normal file
View File

@ -0,0 +1,131 @@
/*
* 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) 2019, 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 "mainspaceparser/mainspaceparser.h"
#include "sample01.h"
namespace morm
{
namespace samples
{
void print_syntax()
{
std::cout << "mormsample" << std::endl;
std::cout << "options:" << std::endl;
std::cout << " -h " << std::endl;
std::cout << " --help - print this help and exit" << std::endl;
std::cout << std::endl;
}
}
}
int main(int argc, const char ** argv)
{
PT::Space args;
PT::Space & args_options = args.FindAddSpace(L"options");
args_options.Add(L"c", 1); // one argument - config file
args_options.Add(L"config", 1);
PT::MainSpaceParser main_parser;
main_parser.UTF8(true);
main_parser.SetSpace(args);
PT::MainSpaceParser::Status args_status = main_parser.Parse(argc, argv);
if( args_status != PT::MainSpaceParser::status_ok )
{
std::cout << "Syntax error in arguments" << std::endl;
return 1;
}
if( args.GetFirstValue(L"h") || args.GetFirstValue(L"help") )
{
morm::samples::print_syntax();
return 0;
}
morm::ModelConnector model_connector;
morm::JSONConnector json_connector;
morm::PostgreSQLConnector postgresql_connector;
std::wstring db_name = L"morm_test";
std::wstring db_user = L"morm_test";
std::wstring db_pass = L"morm_test";
/*
* PostgreSQL
*
* you can create a new user with:
* create user morm_test LOGIN ENCRYPTED PASSWORD 'morm_test';
*
* and a new database with:
* create database morm_test owner morm_test;
*
*/
postgresql_connector.set_conn_param(db_name, db_user, db_pass);
postgresql_connector.wait_for_connection();
model_connector.set_flat_connector(json_connector);
model_connector.set_db_connector(postgresql_connector);
PT::Log log;
PT::FileLog file_log;
PT::WTextStream log_buffer;
file_log.init(L"log.txt", true, 4, true);
log.SetLogBuffer(&log_buffer);
log.SetFileLog(&file_log);
model_connector.set_logger(log);
postgresql_connector.set_log_queries(true);
postgresql_connector.set_logger(log);
morm::samples::Sample01 sample_01;
sample_01.set_connector(model_connector);
sample_01.make();
}

97
samples/person.h Normal file
View File

@ -0,0 +1,97 @@
/*
* 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) 2019, 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_samples_person
#define headerfile_morm_samples_person
#include <string>
#include "morm.h"
#include "language.h"
namespace morm
{
namespace samples
{
/*
CREATE TABLE public.person (
id bigserial,
first_name varchar(64),
last_name varchar(64),
email varchar(64),
language_id bigint,
primary key(id)
);
*/
class Person : public morm::Model
{
public:
long id;
std::wstring first_name;
std::wstring last_name;
std::wstring email;
Language language;
void map_fields()
{
field(L"id", id, false, false, true);
field(L"first_name", first_name);
field(L"last_name", last_name);
field(L"email", email);
field(L"language_id", language);
}
void table_name(PT::TextStream & stream)
{
// schema.table_name or just table_name
stream << "public.person";
}
};
}
}
#endif

87
samples/sample01.h Normal file
View File

@ -0,0 +1,87 @@
/*
* 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) 2019, 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 "basesample.h"
#include "person.h"
#include "language.h"
namespace morm
{
namespace samples
{
class Sample01 : public BaseSample
{
public:
void make()
{
person.set_connector(model_connector);
load_defaults(person);
person.insert();
}
private:
Person person;
static void load_defaults(Person & person)
{
person.id = 200;
person.first_name = L"MyFirstName";
person.last_name = L"MyLastName";
person.email = L"myemail@mydomain.ltd";
person.language.id = 100;
person.language.english_name = L"english";
person.language.local_name = L"polish";
person.language.code_str = L"en";
person.language.code_int = 200;
}
};
}
}