Skip to content

Commit

Permalink
Added constraint resolution clauses to Table Constraints. Version 1.9…
Browse files Browse the repository at this point in the history
…9.7.0

Signed-off-by: Federico Fuga <[email protected]>
  • Loading branch information
studiofuga committed Nov 27, 2023
1 parent 74c092f commit 24ed64b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.8)
project(msqlitecpp VERSION 1.99.6.3 LANGUAGES C CXX)
project(msqlitecpp VERSION 1.99.7.0 LANGUAGES C CXX)

set(msqlitecpp_DESCRIPTION "a Modern SQLite CPP library")

Expand Down
43 changes: 39 additions & 4 deletions include/msqlitecpp/v1/sqlitestatementformatters.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,29 @@ class CreateTable : public StatementFormatter {
}

struct TableConstraint {
struct OnConflict {
struct Rollback {
[[nodiscard]] const char *toString() const { return " ON CONFLICT ROLLBACK"; }
};
struct Abort {
[[nodiscard]] const char *toString() const { return " ON CONFLICT ABORT"; }
};
struct Fail {
[[nodiscard]] const char *toString() const { return " ON CONFLICT FAIL"; }
};
struct Ignore {
[[nodiscard]] const char *toString() const { return " ON CONFLICT IGNORE"; }
};
struct Replace {
[[nodiscard]] const char *toString() const { return " ON CONFLICT REPLACE"; }
};

template<typename T>
static const char *toString(T t) {
return t.toString();
}
};

class ForeignKey {
std::string statement;
std::string actionString;
Expand Down Expand Up @@ -596,6 +619,7 @@ class CreateTable : public StatementFormatter {

class Unique {
std::string statement;
std::string conflictClause;
public:
template<typename ...FLDS>
explicit
Expand All @@ -607,16 +631,22 @@ class CreateTable : public StatementFormatter {
<< ")";
statement = ss.str();
}
std::string toString() const
[[nodiscard]] std::string toString() const
{
std::ostringstream ss;
ss << statement;
ss << statement << conflictClause;
return ss.str();
}

template <typename T>
void onConflict(T t) {
conflictClause = statements::CreateTable::TableConstraint::OnConflict::toString(t);
}
};

class PrimaryKey {
std::string statement;
std::string conflictClause;
public:
template<typename ...FLDS>
explicit
Expand All @@ -628,12 +658,17 @@ class CreateTable : public StatementFormatter {
<< ")";
statement = ss.str();
}
std::string toString() const
[[nodiscard]] std::string toString() const
{
std::ostringstream ss;
ss << statement;
ss << statement << conflictClause;
return ss.str();
}

template <typename T>
void onConflict(T t) {
conflictClause = statements::CreateTable::TableConstraint::OnConflict::toString(t);
}
};
};
};
Expand Down
20 changes: 20 additions & 0 deletions tests/v1/sqlformatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,31 @@ TEST(V1SqlFormatting, UniqueAndPrimaryKey)
sqlite::statements::CreateTable::TableConstraint::Unique c2("u", fldId, fldName);
ASSERT_EQ(c2.toString(), "CONSTRAINT u UNIQUE (id,name)");

sqlite::statements::CreateTable::TableConstraint::Unique c3("u", fldId, fldName);
c3.onConflict(sqlite::statements::CreateTable::TableConstraint::OnConflict::Replace {});
ASSERT_EQ(c3.toString(), "CONSTRAINT u UNIQUE (id,name) ON CONFLICT REPLACE");

sqlite::statements::CreateTable::TableConstraint::Unique c4("u", fldId, fldName);
c4.onConflict(sqlite::statements::CreateTable::TableConstraint::OnConflict::Abort {});
ASSERT_EQ(c4.toString(), "CONSTRAINT u UNIQUE (id,name) ON CONFLICT ABORT");

sqlite::statements::CreateTable::TableConstraint::Unique c5("u", fldId, fldName);
c5.onConflict(sqlite::statements::CreateTable::TableConstraint::OnConflict::Fail {});
ASSERT_EQ(c5.toString(), "CONSTRAINT u UNIQUE (id,name) ON CONFLICT FAIL");

sqlite::statements::CreateTable::TableConstraint::Unique c6("u", fldId, fldName);
c6.onConflict(sqlite::statements::CreateTable::TableConstraint::OnConflict::Ignore {});
ASSERT_EQ(c6.toString(), "CONSTRAINT u UNIQUE (id,name) ON CONFLICT IGNORE");

sqlite::statements::CreateTable::TableConstraint::PrimaryKey p1("pk", fldId);
ASSERT_EQ(p1.toString(), "CONSTRAINT pk PRIMARY KEY (id)");

sqlite::statements::CreateTable::TableConstraint::PrimaryKey p2("pk", fldId, fldName);
ASSERT_EQ(p2.toString(), "CONSTRAINT pk PRIMARY KEY (id,name)");

sqlite::statements::CreateTable::TableConstraint::PrimaryKey p3("pk", fldId, fldName);
p3.onConflict(sqlite::statements::CreateTable::TableConstraint::OnConflict::Rollback {});
ASSERT_EQ(p3.toString(), "CONSTRAINT pk PRIMARY KEY (id,name) ON CONFLICT ROLLBACK");
}

TEST(V1SqlFormatting, createTableStatement)
Expand Down

0 comments on commit 24ed64b

Please sign in to comment.