docs added code block titles

This commit is contained in:
silverqx
2023-10-08 14:30:44 +02:00
parent 521bc5c7a1
commit 4e30a29d98
3 changed files with 133 additions and 126 deletions
+29 -27
View File
@@ -93,7 +93,7 @@ cd HelloWorld
vim vcpkg.json
```
```json
```json title='vcpkg.json'
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
"name": "tinyorm-helloworld",
@@ -156,37 +156,39 @@ To paste a source code correctly in `vim`, press <kbd>Shift</kbd> + <kbd>p</kbd>
And paste the following code.
#include <QDebug>
```cpp title='main.cpp'
#include <QDebug>
#ifdef _WIN32
# include <qt_windows.h>
#endif
#ifdef _WIN32
# include <qt_windows.h>
#endif
#include <orm/db.hpp>
#include <orm/db.hpp>
using Orm::DB;
using Orm::DB;
int main(int /*unused*/, char */*unused*/[])
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
// SetConsoleOutputCP(1250);
#endif
int main(int /*unused*/, char */*unused*/[])
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
// SetConsoleOutputCP(1250);
#endif
// Ownership of a shared_ptr()
auto manager = DB::create({
{"driver", "QSQLITE"},
{"database", qEnvironmentVariable("TINYORM_HELLOWORLD_DB_SQLITE_DATABASE",
"../../HelloWorld.sqlite3")},
{"check_database_exists", true},
});
// Ownership of a shared_ptr()
auto manager = DB::create({
{"driver", "QSQLITE"},
{"database", qEnvironmentVariable("TINYORM_HELLOWORLD_DB_SQLITE_DATABASE",
"../../HelloWorld.sqlite3")},
{"check_database_exists", true},
});
auto posts = DB::select("select * from posts");
auto posts = DB::select("select * from posts");
while(posts.next())
qDebug() << posts.value("id").toULongLong()
<< posts.value("name").toString();
}
while(posts.next())
qDebug() << posts.value("id").toULongLong()
<< posts.value("name").toString();
}
```
## Hello world with CMake
@@ -478,7 +480,7 @@ vim HelloWorld.pro
To paste a source code correctly in `vim`, press <kbd>Shift</kbd> + <kbd>p</kbd>.
:::
```qmake
```qmake title='HelloWorld.pro'
QT -= gui
TEMPLATE = app
@@ -511,7 +513,7 @@ Read the [Consume TinyOrm library (qmake)](tinyorm.mdx#consume-tinyorm-library-q
Create the `.qmake.conf` file in the `HelloWorld` project root folder with the following content.
```qmake
```qmake title='.qmake.conf'
# Path to the PARENT folder of the TinyORM source folder
TINY_MAIN_DIR = $$clean_path($$PWD/../../TinyORM/)
# To find .env and .env.$$QMAKE_PLATFORM files
+102 -97
View File
@@ -95,7 +95,7 @@ cd tom
vim vcpkg.json
```
```json
```json title='vcpkg.json'
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
"name": "tom",
@@ -162,70 +162,72 @@ And paste the following code.
<a id='string-constants-example' />
#include <orm/db.hpp>
```cpp title='main.cpp'
#include <orm/db.hpp>
#include <tom/application.hpp>
#include <tom/application.hpp>
#include "migrations/2014_10_12_000000_create_posts_table.hpp"
#include "migrations/2014_10_12_000000_create_posts_table.hpp"
#include "seeders/databaseseeder.hpp"
#include "seeders/databaseseeder.hpp"
using Orm::DatabaseManager;
using Orm::DB;
using Orm::DatabaseManager;
using Orm::DB;
using TomApplication = Tom::Application;
using TomApplication = Tom::Application;
using namespace Migrations; // NOLINT(google-build-using-namespace)
using namespace Seeders; // NOLINT(google-build-using-namespace)
using namespace Migrations; // NOLINT(google-build-using-namespace)
using namespace Seeders; // NOLINT(google-build-using-namespace)
/*! Create the database manager instance and add a database connection. */
std::shared_ptr<DatabaseManager> setupDatabaseManager();
/*! C++ main function. */
int main(int argc, char *argv[])
{
try {
// Ownership of the shared_ptr()
auto db = setupDatabaseManager();
return TomApplication(argc, argv, std::move(db), "TOM_MIGRATIONS_ENV")
.migrations<CreatePostsTable>()
.seeders<DatabaseSeeder>()
// Fire it up 🔥🚀✨
.run();
} catch (const std::exception &e) {
TomApplication::logException(e);
}
return EXIT_FAILURE;
}
std::shared_ptr<DatabaseManager> setupDatabaseManager()
{
using namespace Orm::Constants; // NOLINT(google-build-using-namespace)
/*! Create the database manager instance and add a database connection. */
std::shared_ptr<DatabaseManager> setupDatabaseManager();
/*! C++ main function. */
int main(int argc, char *argv[])
{
try {
// Ownership of the shared_ptr()
return DB::create({
{driver_, QMYSQL},
{host_, qEnvironmentVariable("DB_MYSQL_HOST", H127001)},
{port_, qEnvironmentVariable("DB_MYSQL_PORT", P3306)},
{database_, qEnvironmentVariable("DB_MYSQL_DATABASE", EMPTY)},
{username_, qEnvironmentVariable("DB_MYSQL_USERNAME", EMPTY)},
{password_, qEnvironmentVariable("DB_MYSQL_PASSWORD", EMPTY)},
{charset_, qEnvironmentVariable("DB_MYSQL_CHARSET", UTF8MB4)},
{collation_, qEnvironmentVariable("DB_MYSQL_COLLATION", UTF8MB40900aici)},
{timezone_, TZ00},
/* Specifies what time zone all QDateTime-s will have, the overridden default is
the Qt::UTC, set to the Qt::LocalTime or QtTimeZoneType::DontConvert to use
the system local time. */
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{strict_, true},
},
QStringLiteral("tinyorm_tom_mysql")); // shell:connection
auto db = setupDatabaseManager();
return TomApplication(argc, argv, std::move(db), "TOM_EXAMPLE_ENV")
.migrations<CreatePostsTable>()
.seeders<DatabaseSeeder>()
// Fire it up 🔥🚀✨
.run();
} catch (const std::exception &e) {
TomApplication::logException(e);
}
return EXIT_FAILURE;
}
std::shared_ptr<DatabaseManager> setupDatabaseManager()
{
using namespace Orm::Constants; // NOLINT(google-build-using-namespace)
// Ownership of the shared_ptr()
return DB::create({
{driver_, QMYSQL},
{host_, qEnvironmentVariable("DB_MYSQL_HOST", H127001)},
{port_, qEnvironmentVariable("DB_MYSQL_PORT", P3306)},
{database_, qEnvironmentVariable("DB_MYSQL_DATABASE", EMPTY)},
{username_, qEnvironmentVariable("DB_MYSQL_USERNAME", EMPTY)},
{password_, qEnvironmentVariable("DB_MYSQL_PASSWORD", EMPTY)},
{charset_, qEnvironmentVariable("DB_MYSQL_CHARSET", UTF8MB4)},
{collation_, qEnvironmentVariable("DB_MYSQL_COLLATION", UTF8MB40900aici)},
{timezone_, TZ00},
/* Specifies what time zone all QDateTime-s will have, the overridden default is
the Qt::UTC, set to the Qt::LocalTime or QtTimeZoneType::DontConvert to use
the system local time. */
{qt_timezone, QVariant::fromValue(Qt::UTC)},
{strict_, true},
},
QStringLiteral("tinyorm_tom_mysql")); // shell:connection
}
```
:::tip
If you have defined more database connections then you can tag the lines with the database connection names with the `// shell:connection` comment and this connection names will be provided to the bash, zsh, pwsh completions for the `--database=` option 😎, [example](https://github.com/silverqx/TinyORM/blob/main/examples/tom/main.cpp#L74).
:::
@@ -270,39 +272,40 @@ vim database/migrations/2014_10_12_000000_create_posts_table.hpp`}
And paste the following code.
#pragma once
```cpp title='database/migrations/2014_10_12_000000_create_posts_table.hpp'
#pragma once
#include <tom/migration.hpp>
#include <tom/migration.hpp>
namespace Migrations
namespace Migrations
{
struct CreatePostsTable : Migration
{
/*! Filename of the migration file. */
T_MIGRATION
struct CreatePostsTable : Migration
/*! Run the migrations. */
void up() const override
{
/*! Filename of the migration file. */
T_MIGRATION
/*! Run the migrations. */
void up() const override
Schema::create("posts", [](Blueprint &table)
{
Schema::create("posts", [](Blueprint &table)
{
table.id();
table.id();
table.string(NAME);
table.timestamps();
});
}
table.string(NAME);
table.timestamps();
});
}
/*! Reverse the migrations. */
void down() const override
{
Schema::dropIfExists("posts");
}
};
} // namespace Migrations
/*! Reverse the migrations. */
void down() const override
{
Schema::dropIfExists("posts");
}
};
} // namespace Migrations
```
:::tip
The `TinyORM` source tree contains the [`CreatePostsTable`](https://github.com/silverqx/TinyORM/blob/main/tests/database/migrations/2014_10_12_000000_create_posts_table.hpp#L5) example migration that also acts as the full-fledged example migration. It has defined and also nicely commented all possible features that migration classes can use or define.
@@ -341,27 +344,29 @@ vim database/seeders/databaseseeder.hpp`}
And paste the following code.
#pragma once
```cpp title='database/seeders/databaseseeder.hpp'
#pragma once
#include <tom/seeder.hpp>
#include <tom/seeder.hpp>
namespace Seeders
namespace Seeders
{
/*! Main database seeder. */
struct DatabaseSeeder : Seeder
{
/*! Main database seeder. */
struct DatabaseSeeder : Seeder
/*! Run the database seeders. */
void run() override
{
/*! Run the database seeders. */
void run() override
{
DB::table("posts")->insert({
{{"name", "1. post"}},
{{"name", "2. post"}},
});
}
};
DB::table("posts")->insert({
{{"name", "1. post"}},
{{"name", "2. post"}},
});
}
};
} // namespace Seeders
} // namespace Seeders
```
:::tip
The `TinyORM` source tree contains the [`DatabaseSeeder`](https://github.com/silverqx/TinyORM/blob/main/tests/database/seeders/databaseseeder.hpp#L8) root seeder example class that also acts as the full-fledged example seeder. It has defined and also nicely commented all possible features that seeder classes can use or define.
@@ -685,7 +690,7 @@ vim tom.pro
To paste a source code correctly in `vim`, press <kbd>Shift</kbd> + <kbd>p</kbd>.
:::
```qmake
```qmake title='tom.pro'
QT -= gui
TEMPLATE = app
@@ -822,7 +827,7 @@ Configuring by the `.qmake.conf` and `.env` files has one big advantage, which i
Create `database/migrations.pri` file and paste the following code.
```qmake
```qmake title='database/migrations.pri'
INCLUDEPATH *= $$PWD
HEADERS += \
@@ -833,7 +838,7 @@ HEADERS += \
Create `database/seeders.pri` file and paste the following code.
```qmake
```qmake title='database/seeders.pri'
INCLUDEPATH *= $$PWD
HEADERS += \
+2 -2
View File
@@ -534,7 +534,7 @@ Important `CMake` options.
In your application or library `CMakeLists.txt` file add following `find_package()` call.
```cmake
```cmake title='CMakeLists.txt'
find_package(TinyOrm 0.16.0 CONFIG REQUIRED)
```
@@ -814,7 +814,7 @@ Let's explain one by one.
Create the `.qmake.conf` file in your application root folder with the following content.
```qmake
```qmake title='.qmake.conf'
# Path to the PARENT folder of the TinyORM source folder
TINY_MAIN_DIR = $$clean_path(<your_path>)
# To find .env and .env.$$QMAKE_PLATFORM files in YOUR project