From 074be938ca10b2bbff18dd3586d5a4faf8f0f23e Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 21 Aug 2019 15:59:03 +0000 Subject: [PATCH] added 'samples' directory for samples and tests git-svn-id: svn://ttmath.org/publicrep/morm/trunk@1202 e52654a7-88a9-db11-a3e9-0013d4bc506e --- samples/Makefile | 63 ++++++++++++++++++++ samples/Makefile.dep | 23 ++++++++ samples/Makefile.o.dep | 1 + samples/basesample.h | 76 ++++++++++++++++++++++++ samples/language.h | 98 ++++++++++++++++++++++++++++++ samples/main.cpp | 131 +++++++++++++++++++++++++++++++++++++++++ samples/person.h | 97 ++++++++++++++++++++++++++++++ samples/sample01.h | 87 +++++++++++++++++++++++++++ 8 files changed, 576 insertions(+) create mode 100644 samples/Makefile create mode 100644 samples/Makefile.dep create mode 100644 samples/Makefile.o.dep create mode 100644 samples/basesample.h create mode 100644 samples/language.h create mode 100644 samples/main.cpp create mode 100644 samples/person.h create mode 100644 samples/sample01.h diff --git a/samples/Makefile b/samples/Makefile new file mode 100644 index 0000000..0a3dd00 --- /dev/null +++ b/samples/Makefile @@ -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 diff --git a/samples/Makefile.dep b/samples/Makefile.dep new file mode 100644 index 0000000..5180171 --- /dev/null +++ b/samples/Makefile.dep @@ -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 diff --git a/samples/Makefile.o.dep b/samples/Makefile.o.dep new file mode 100644 index 0000000..89ad445 --- /dev/null +++ b/samples/Makefile.o.dep @@ -0,0 +1 @@ +o = main.o \ No newline at end of file diff --git a/samples/basesample.h b/samples/basesample.h new file mode 100644 index 0000000..8aa8d06 --- /dev/null +++ b/samples/basesample.h @@ -0,0 +1,76 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * 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 + diff --git a/samples/language.h b/samples/language.h new file mode 100644 index 0000000..f1a962f --- /dev/null +++ b/samples/language.h @@ -0,0 +1,98 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * 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 +#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 + diff --git a/samples/main.cpp b/samples/main.cpp new file mode 100644 index 0000000..a557f49 --- /dev/null +++ b/samples/main.cpp @@ -0,0 +1,131 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * 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(); + +} + + + diff --git a/samples/person.h b/samples/person.h new file mode 100644 index 0000000..c0496aa --- /dev/null +++ b/samples/person.h @@ -0,0 +1,97 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * 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 +#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 + diff --git a/samples/sample01.h b/samples/sample01.h new file mode 100644 index 0000000..ebd6605 --- /dev/null +++ b/samples/sample01.h @@ -0,0 +1,87 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * 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; + } + + + +}; + + +} +} +