mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-15 06:05:58 -06:00
.reference
git-svn-id: svn://ultimatepp.org/upp/trunk@14459 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
0af98b4d73
commit
a07381ddc1
5 changed files with 77 additions and 41 deletions
|
|
@ -6,9 +6,10 @@ uses
|
|||
plugin/sqlite3;
|
||||
|
||||
file
|
||||
multidb.h,
|
||||
mysql_schema.sch,
|
||||
sqlite3_schema.sch,
|
||||
multidb.h,
|
||||
db2schema.cpp,
|
||||
main.cpp;
|
||||
|
||||
mainconfig
|
||||
|
|
|
|||
17
reference/SQL_MultiDB/db2schema.cpp
Normal file
17
reference/SQL_MultiDB/db2schema.cpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include "multidb.h"
|
||||
|
||||
namespace DB2 {
|
||||
|
||||
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
|
||||
#define MODEL <SQL_MultiDB/sqlite3_schema.sch>
|
||||
#include <Sql/sch_source.h>
|
||||
#include <Sql/sch_schema.h>
|
||||
#undef MODEL
|
||||
#undef SCHEMADIALECT
|
||||
|
||||
void DB2_All_Tables(SqlSchema& sch)
|
||||
{ // we need a distinct name for second database schema creation function
|
||||
All_Tables(sch);
|
||||
}
|
||||
|
||||
};
|
||||
|
|
@ -1,55 +1,62 @@
|
|||
#include <MySql/MySql.h>
|
||||
|
||||
using namespace Upp;
|
||||
#include "multidb.h"
|
||||
|
||||
#define SCHEMADIALECT <MySql/MySqlSchema.h>
|
||||
#define MODEL <SQL_MultiDB/mysql_schema.sch>
|
||||
#include <Sql/sch_source.h> // creates the code for mapping schema structures to database rows
|
||||
#include <Sql/sch_source.h> // creates the code for mapping schema structures to database rows and SqlIds
|
||||
#include <Sql/sch_schema.h> // creates the code to generate sql scripts that upload schema to DB
|
||||
#undef MODEL
|
||||
#undef SCHEMADIALECT
|
||||
|
||||
namespace DB2 {
|
||||
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
|
||||
#define MODEL <SQL_MultiDB/sqlite3_schema.sch>
|
||||
#include <Sql/sch_source.h>
|
||||
#include <Sql/sch_schema.h>
|
||||
#undef SCHEMADIALECT
|
||||
}
|
||||
|
||||
CONSOLE_APP_MAIN
|
||||
{
|
||||
StdLogSetup(LOG_COUT|LOG_FILE);
|
||||
|
||||
Sqlite3Session sqlite3;
|
||||
MySqlSession mysql;
|
||||
// edit the connection parameters if necessary
|
||||
if(mysql.Connect("root", "Passw0rd", "test")) {
|
||||
Cout() << "Connected\n";
|
||||
SQL = mysql;
|
||||
|
||||
if(!mysql.Connect("root", "Passw0rd", "test")) {
|
||||
LOG("Failed to connect MySQL!");
|
||||
Exit(1);
|
||||
}
|
||||
mysql.SetTrace();
|
||||
|
||||
if(!sqlite3.Open(ConfigFile("test.db"))) {
|
||||
LOG("Failed to connect Sqlite3!");
|
||||
Exit(1);
|
||||
}
|
||||
sqlite3.SetTrace();
|
||||
|
||||
SQL = mysql; // MySql will be our default database
|
||||
|
||||
{ // Create/Upgrade DB schema in MySql
|
||||
SqlSchema sch(MY_SQL);
|
||||
All_Tables(sch);
|
||||
// create the table if necessary
|
||||
SqlPerformScript(sch.Upgrade());
|
||||
SqlPerformScript(sch.Attributes());
|
||||
SQL.ClearError();
|
||||
|
||||
try {
|
||||
// insert some random data
|
||||
SQL & Insert(TEST_TABLE)(VALUE, Uuid::Create().ToString());
|
||||
// fetch some data
|
||||
Sql sql;
|
||||
sql * Select(ID, VALUE).From(TEST_TABLE)
|
||||
.OrderBy(Descending(ID))
|
||||
.Limit(5);
|
||||
while(sql.Fetch())
|
||||
Cout() << AsString(sql[0]) << ": " << AsString(sql[VALUE]) << "\n";
|
||||
}
|
||||
catch(SqlExc &ex) {
|
||||
Cerr() << "ERROR: " << ex << "\n";
|
||||
SetExitCode(1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Cerr() <<"ERROR: Unable to connect to database\n";
|
||||
SetExitCode(1);
|
||||
|
||||
{ // Create/Upgrade DB schema in Sqlite3
|
||||
SqlSchema sch(SQLITE3);
|
||||
DB2::DB2_All_Tables(sch);
|
||||
SqlPerformScript(sqlite3, sch.Upgrade());
|
||||
SqlPerformScript(sqlite3, sch.Attributes());
|
||||
SQL.ClearError();
|
||||
}
|
||||
|
||||
{ // Do something with MySql
|
||||
SQL * Insert(TEST_TABLE)(VALUE, FormatIntBase(Random(), 36));
|
||||
Sql sql;
|
||||
sql * Select(ID, VALUE).From(TEST_TABLE);
|
||||
while(sql.Fetch())
|
||||
LOG("ID: " << sql[ID] << ", VALUE: " << sql[VALUE]);
|
||||
}
|
||||
|
||||
{ // Do something with Sqlite3; ID and VALUE SqlIds are from mysql schema, but these are just text, so no problem
|
||||
Sql sql(sqlite3); // need to specify non-default session in the constructor!
|
||||
sql * Insert(TEST_TABLE)(VALUE, FormatIntBase(Random(), 36))(DB2::NAME, AsString(Random()));
|
||||
sql * Select(ID, VALUE, DB2::NAME).From(DB2::TEST_TABLE); // the value of DB2::TEST_TABLE and TEST_TABLE is the same, using DB2:: here just for demonstration
|
||||
while(sql.Fetch())
|
||||
LOG("ID: " << sql[ID] << ", VALUE: " << sql[VALUE] << ", NAME: " << sql[DB2::NAME]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,27 @@
|
|||
#ifndef _SQL_MultiDB_multidb_h_
|
||||
#define _SQL_MultiDB_multidb_h_
|
||||
|
||||
#include <MySql/MySql.h>
|
||||
#include <plugin/sqlite3/Sqlite3.h>
|
||||
|
||||
using namespace Upp;
|
||||
|
||||
#define SCHEMADIALECT <MySql/MySqlSchema.h>
|
||||
#define MODEL <SQL_MultiDB/mysql_schema.sch>
|
||||
#include <Sql/sch_header.h>
|
||||
#undef SCHEMADIALECT
|
||||
#undef MODEL // with multiple schemas, we need to do cleanup so that MODEL and
|
||||
#undef SCHEMADIALECT // SCHEMDIALECT can be redefined to second schema values
|
||||
|
||||
namespace DB2 {
|
||||
|
||||
#define SCHEMADIALECT <plugin/sqlite3/Sqlite3Schema.h>
|
||||
#define MODEL <SQL_MultiDB/sqlite3_schema.sch>
|
||||
#include <Sql/sch_header.h>
|
||||
#undef MODEL
|
||||
#undef SCHEMADIALECT
|
||||
|
||||
void DB2_All_Tables(SqlSchema& sch);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
TABLE(TEST_TABLE) // Note that column names definition is simply shared between schemas
|
||||
INT (ID) PRIMARY_KEY AUTO_INCREMENT
|
||||
STRING (VALUE, 32)
|
||||
TABLE_(TEST_TABLE)
|
||||
INT_ (ID) PRIMARY_KEY AUTO_INCREMENT
|
||||
STRING_ (VALUE, 32)
|
||||
STRING_ (NAME, 200)
|
||||
END_TABLE
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue