Framework update

This commit is contained in:
stephb9959
2021-12-07 20:37:26 -08:00
parent 72f0b11f81
commit 2ec792a74b
5 changed files with 358 additions and 276 deletions

2
build
View File

@@ -1 +1 @@
94
96

View File

@@ -552,5 +552,33 @@ namespace OpenWifi::SecurityObjects {
return false;
}
void Token::to_json(Poco::JSON::Object &Obj) const {
field_to_json(Obj,"token",token);
field_to_json(Obj,"refreshToken",refreshToken);
field_to_json(Obj,"tokenType",tokenType);
field_to_json(Obj,"userName",userName);
field_to_json(Obj,"created",created);
field_to_json(Obj,"expires",expires);
field_to_json(Obj,"idleTimeout",idleTimeout);
field_to_json(Obj,"revocationDate",revocationDate);
}
bool Token::from_json(Poco::JSON::Object::Ptr &Obj) {
try {
field_from_json(Obj,"token",token);
field_from_json(Obj,"refreshToken",refreshToken);
field_from_json(Obj,"tokenType",tokenType);
field_from_json(Obj,"userName",userName);
field_from_json(Obj,"created",created);
field_from_json(Obj,"expires",expires);
field_from_json(Obj,"idleTimeout",idleTimeout);
field_from_json(Obj,"revocationDate",revocationDate);
return true;
} catch(...) {
}
return false;
}
}

View File

@@ -8,10 +8,12 @@
#pragma once
#include <string>
#include "framework/OpenWifiTypes.h"
#include "Poco/JSON/Object.h"
namespace OpenWifi::SecurityObjects {
namespace OpenWifi {
namespace SecurityObjects {
struct AclTemplate {
bool Read_ = true;
@@ -127,7 +129,7 @@ namespace OpenWifi::SecurityObjects {
std::string securityPolicy;
uint64_t securityPolicyChange = 0 ;
std::string currentPassword;
Types::StringVec lastPasswords;
OpenWifi::Types::StringVec lastPasswords;
std::string oauthType;
std::string oauthUserInfo;
@@ -264,4 +266,19 @@ namespace OpenWifi::SecurityObjects {
bool from_json(Poco::JSON::Object::Ptr &Obj);
};
struct Token {
std::string token;
std::string refreshToken;
std::string tokenType;
std::string userName;
uint64_t created=0;
uint64_t expires=0;
uint64_t idleTimeout=0;
uint64_t revocationDate=0;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(Poco::JSON::Object::Ptr &Obj);
};
}
}

View File

@@ -64,6 +64,7 @@ using namespace std::chrono_literals;
#include "cppkafka/cppkafka.h"
#include "framework/OpenWifiTypes.h"
#include "framework/KafkaTopics.h"
#include "framework/RESTAPI_protocol.h"
#include "framework/RESTAPI_errors.h"

View File

@@ -32,7 +32,8 @@ namespace ORM {
FT_BIGINT,
FT_TEXT,
FT_VARCHAR,
FT_BLOB
FT_BLOB,
FT_BOOLEAN
};
enum Indextype {
@@ -96,6 +97,7 @@ namespace ORM {
case FT_INT: return "INT";
case FT_BIGINT: return "BIGINT";
case FT_TEXT: return "TEXT";
case FT_BOOLEAN: return "BOOLEAN";
case FT_VARCHAR:
if(Size)
return std::string("VARCHAR(") + std::to_string(Size) + std::string(")");
@@ -162,8 +164,8 @@ namespace ORM {
Poco::Data::SessionPool & Pool,
Poco::Logger &L,
const char *Prefix):
Type(dbtype),
DBName(TableName),
Type_(dbtype),
DBName_(TableName),
Pool_(Pool),
Logger_(L),
Prefix_(Prefix)
@@ -185,7 +187,7 @@ namespace ORM {
SelectList_ += "(";
}
CreateFields_ += i.Name + " " + FieldTypeToChar(Type, i.Type,i.Size) + (i.Index ? " unique primary key" : "");
CreateFields_ += i.Name + " " + FieldTypeToChar(Type_, i.Type,i.Size) + (i.Index ? " unique primary key" : "");
SelectFields_ += i.Name ;
UpdateFields_ += i.Name + "=?";
SelectList_ += "?";
@@ -195,11 +197,11 @@ namespace ORM {
SelectList_ += ")";
if(!Indexes.empty()) {
if(Type==OpenWifi::DBType::sqlite || Type==OpenWifi::DBType::pgsql) {
if(Type_==OpenWifi::DBType::sqlite || Type_==OpenWifi::DBType::pgsql) {
for(const auto &j:Indexes) {
std::string IndexLine;
IndexLine = std::string("CREATE INDEX IF NOT EXISTS ") + j.Name + std::string(" ON ") + DBName + " (";
IndexLine = std::string("CREATE INDEX IF NOT EXISTS ") + j.Name + std::string(" ON ") + DBName_+ " (";
bool first_entry=true;
for(const auto &k:j.Entries) {
assert(FieldNames_.find(k.FieldName) != FieldNames_.end());
@@ -209,10 +211,10 @@ namespace ORM {
first_entry = false;
IndexLine += k.FieldName + std::string(" ") + std::string(k.Type == Indextype::ASC ? "ASC" : "DESC") ;
}
IndexLine += " );";
IndexCreation += IndexLine;
IndexLine += " )";
IndexCreation_.template emplace_back(IndexLine);
}
} else if(Type==OpenWifi::DBType::mysql) {
} else if(Type_==OpenWifi::DBType::mysql) {
bool firstIndex = true;
std::string IndexLine;
for(const auto &j:Indexes) {
@@ -231,7 +233,7 @@ namespace ORM {
}
IndexLine += " ) ";
}
IndexCreation = IndexLine;
IndexCreation_.template emplace_back(IndexLine);
}
}
}
@@ -241,6 +243,11 @@ namespace ORM {
[[nodiscard]] const std::string & SelectList() const { return SelectList_; };
[[nodiscard]] const std::string & UpdateFields() const { return UpdateFields_; };
inline std::string OP(const char *F, SqlComparison O , bool V) {
assert( FieldNames_.find(F) != FieldNames_.end() );
return std::string{"("} + F + SQLCOMPS[O] + (V ? "true" : "false") + ")" ;
}
inline std::string OP(const char *F, SqlComparison O , int V) {
assert( FieldNames_.find(F) != FieldNames_.end() );
return std::string{"("} + F + SQLCOMPS[O] + std::to_string(V) + ")" ;
@@ -280,32 +287,59 @@ namespace ORM {
inline bool Create() {
std::string S;
if(Type==OpenWifi::DBType::mysql) {
if(IndexCreation.empty())
S = "create table if not exists " + DBName +" ( " + CreateFields_ + " )" ;
else
S = "create table if not exists " + DBName +" ( " + CreateFields_ + " ), " + IndexCreation + " )";
} else if (Type==OpenWifi::DBType::pgsql || Type==OpenWifi::DBType::sqlite) {
S = "create table if not exists " + DBName + " ( " + CreateFields_ + " ); " + IndexCreation ;
}
// std::cout << "CREATE-DB: " << S << std::endl;
switch(Type_) {
case OpenWifi::DBType::mysql: {
try {
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement CreateStatement(Session);
CreateStatement << S;
CreateStatement.execute();
std::string Statement = IndexCreation_.empty() ? "create table if not exists " + DBName_ +" ( " + CreateFields_ + " )" :
"create table if not exists " + DBName_ +" ( " + CreateFields_ + " ), " + IndexCreation_[0] + " )";
Session << Statement , Poco::Data::Keywords::now;
return true;
} catch (const Poco::Exception &E) {
std::cout << "Exception while creating DB: " << E.name() << std::endl;
Logger_.error("Failure to create MySQL DB resources.");
Logger_.log(E);
}
return false;
}
break;
case OpenWifi::DBType::sqlite: {
try {
Poco::Data::Session Session = Pool_.get();
std::string Statement = "create table if not exists " + DBName_ + " ( " + CreateFields_ + " )";
Session << Statement , Poco::Data::Keywords::now;
for(const auto &i:IndexCreation_) {
Session << i , Poco::Data::Keywords::now;
}
return true;
} catch (const Poco::Exception &E) {
Logger_.error("Failure to create SQLITE DB resources.");
Logger_.log(E);
}
}
break;
case OpenWifi::DBType::pgsql: {
try {
Poco::Data::Session Session = Pool_.get();
std::string Statement = "create table if not exists " + DBName_ + " ( " + CreateFields_ + " )";
Session << Statement , Poco::Data::Keywords::now;
for(const auto &i:IndexCreation_) {
Session << i , Poco::Data::Keywords::now;
}
return true;
} catch (const Poco::Exception &E) {
Logger_.error("Failure to create POSTGRESQL DB resources.");
Logger_.log(E);
}
}
break;
}
return false;
}
[[nodiscard]] std::string ConvertParams(const std::string & S) const {
if(Type!=OpenWifi::DBType::pgsql)
if(Type_!=OpenWifi::DBType::pgsql)
return S;
std::string R;
@@ -336,7 +370,7 @@ namespace ORM {
RecordTuple RT;
Convert(R, RT);
std::string St = "insert into " + DBName + " ( " + SelectFields_ + " ) values " + SelectList_;
std::string St = "insert into " + DBName_ + " ( " + SelectFields_ + " ) values " + SelectList_;
Insert << ConvertParams(St) ,
Poco::Data::Keywords::use(RT);
Insert.execute();
@@ -356,7 +390,7 @@ namespace ORM {
Poco::Data::Statement Select(Session);
RecordTuple RT;
std::string St = "select " + SelectFields_ + " from " + DBName + " where " + FieldName + "=?" ;
std::string St = "select " + SelectFields_ + " from " + DBName_ + " where " + FieldName + "=?" ;
Select << ConvertParams(St) ,
Poco::Data::Keywords::into(RT),
@@ -384,7 +418,7 @@ namespace ORM {
Poco::Data::Statement Select(Session);
RecordTuple RT;
std::string St = "select " + SelectFields_ + " from " + DBName
std::string St = "select " + SelectFields_ + " from " + DBName_
+ " where " + FieldName[0] + "=? and " + FieldName[1] + "=?" ;
Select << ConvertParams(St) ,
Poco::Data::Keywords::into(RT),
@@ -409,7 +443,7 @@ namespace ORM {
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Select(Session);
RecordList RL;
std::string St = "select " + SelectFields_ + " from " + DBName +
std::string St = "select " + SelectFields_ + " from " + DBName_ +
(Where.empty() ? "" : " where " + Where) + OrderBy +
ComputeRange(Offset, HowMany) ;
@@ -442,7 +476,7 @@ namespace ORM {
Convert(R, RT);
std::string St = "update " + DBName + " set " + UpdateFields_ + " where " + FieldName + "=?" ;
std::string St = "update " + DBName_ + " set " + UpdateFields_ + " where " + FieldName + "=?" ;
Update << ConvertParams(St) ,
Poco::Data::Keywords::use(RT),
Poco::Data::Keywords::use(Value);
@@ -461,7 +495,7 @@ namespace ORM {
Poco::Data::Statement Select(Session);
RecordTuple RT;
std::string St = "select " + SelectFields_ + " from " + DBName + " where " + FieldName + "=?" ;
std::string St = "select " + SelectFields_ + " from " + DBName_ + " where " + FieldName + "=?" ;
RecordType R;
Select << ConvertParams(St) ,
Poco::Data::Keywords::into(RT),
@@ -486,7 +520,7 @@ namespace ORM {
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Delete(Session);
std::string St = "delete from " + DBName + " where " + FieldName + "=?" ;
std::string St = "delete from " + DBName_ + " where " + FieldName + "=?" ;
Delete << ConvertParams(St) ,
Poco::Data::Keywords::use(Value);
Delete.execute();
@@ -503,7 +537,7 @@ namespace ORM {
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Delete(Session);
std::string St = "delete from " + DBName + " where " + WhereClause;
std::string St = "delete from " + DBName_ + " where " + WhereClause;
Delete << St;
Delete.execute();
return true;
@@ -591,7 +625,7 @@ namespace ORM {
Poco::Data::Session Session = Pool_.get();
Poco::Data::Statement Select(Session);
std::string st{"SELECT COUNT(*) FROM " + DBName + " " + (Where.empty() ? "" : (" where " + Where)) };
std::string st{"SELECT COUNT(*) FROM " + DBName_ + " " + (Where.empty() ? "" : (" where " + Where)) };
Select << st ,
Poco::Data::Keywords::into(Cnt);
@@ -724,32 +758,34 @@ namespace ORM {
}
[[nodiscard]] inline std::string ComputeRange(uint64_t From, uint64_t HowMany) {
if(From<1) From=1;
switch(Type) {
if(From<1) From=0;
switch(Type_) {
case OpenWifi::DBType::sqlite:
return " LIMIT " + std::to_string(From-1) + ", " + std::to_string(HowMany) + " ";
return " LIMIT " + std::to_string(From) + ", " + std::to_string(HowMany) + " ";
case OpenWifi::DBType::pgsql:
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + " ";
case OpenWifi::DBType::mysql:
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + " ";
default:
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From-1) + " ";
return " LIMIT " + std::to_string(HowMany) + " OFFSET " + std::to_string(From) + " ";
}
}
Poco::Logger & Logger() { return Logger_; }
protected:
Poco::Data::SessionPool &Pool_;
Poco::Logger &Logger_;
private:
OpenWifi::DBType Type;
std::string DBName;
OpenWifi::DBType Type_;
std::string DBName_;
std::string CreateFields_;
std::string SelectFields_;
std::string SelectList_;
std::string UpdateFields_;
std::string IndexCreation;
std::vector<std::string> IndexCreation_;
std::map<std::string,int> FieldNames_;
Poco::Data::SessionPool &Pool_;
Poco::Logger &Logger_;
// Poco::Data::SessionPool &Pool_;
std::string Prefix_;
};
}