Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ __pycache__/
build/
**/*.avf
**/*.db
.cursor/
37 changes: 37 additions & 0 deletions include/simdb/sqlite/DatabaseManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ class DatabaseManager
return INSERT_(std::move(table), std::move(cols), std::move(vals));
}

/// \brief INSERT into a table with values for all columns, in schema order.
/// Equivalent to INSERT(table, SQL_COLUMNS(...), vals) with every
/// column of the table. The number and order of values must match
/// the table's columns.
std::unique_ptr<SqlRecord> INSERT(SqlTable&& table, SqlValues&& vals)
{
if (table.getName().find("internal$") == 0)
{
throw DBException("Cannot perform INSERT. This is an internal table.");
}
std::vector<std::string> col_names;
for (const auto& col : schema_.getTable(table.getName()).getColumns())
{
col_names.push_back(col->getName());
}
SqlColumns cols(col_names);
return INSERT_(std::move(table), std::move(cols), std::move(vals));
}

/// This INSERT() overload is to be used for tables that were defined with
/// at least one default value for its column(s).
std::unique_ptr<SqlRecord> INSERT(SqlTable&& table)
Expand All @@ -177,6 +196,24 @@ class DatabaseManager
return prepareINSERT_(std::move(table), std::move(cols));
}

/// \brief Create a prepared statement for inserting into all columns of a table.
/// Equivalent to prepareINSERT(table, SQL_COLUMNS(...)) with every column
/// of the table, in schema order.
std::unique_ptr<PreparedINSERT> prepareINSERT(SqlTable&& table)
{
if (table.getName().find("internal$") == 0)
{
throw DBException("Cannot perform INSERT. This is an internal table.");
}
std::vector<std::string> col_names;
for (const auto& col : schema_.getTable(table.getName()).getColumns())
{
col_names.push_back(col->getName());
}
SqlColumns cols(col_names);
return prepareINSERT_(std::move(table), std::move(cols));
}

/// \brief Execute an arbitrary SQL command on this database.
void EXECUTE(const std::string& sql_cmd, bool in_transaction = true)
{
Expand Down
24 changes: 24 additions & 0 deletions test/sqlite/Insert/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ int main()
record1->setPropertyInt64("SomeInt64", TEST_INT64 / 2);
EXPECT_EQUAL(record1->getPropertyInt64("SomeInt64"), TEST_INT64 / 2);

// Verify INSERT(table, SQL_VALUES(...)) overload: all columns in schema order, no SQL_COLUMNS.
auto record_all_cols = db_mgr.INSERT(
SQL_TABLE("AllIntegerTypes"),
SQL_VALUES(TEST_INT32, TEST_INT64, static_cast<uint32_t>(TEST_INT32), static_cast<uint64_t>(TEST_INT64)));
EXPECT_EQUAL(record_all_cols->getPropertyInt32("SomeInt32"), TEST_INT32);
EXPECT_EQUAL(record_all_cols->getPropertyInt64("SomeInt64"), TEST_INT64);
EXPECT_EQUAL(record_all_cols->getPropertyUInt32("SomeUInt32"), static_cast<uint32_t>(TEST_INT32));
EXPECT_EQUAL(record_all_cols->getPropertyUInt64("SomeUInt64"), static_cast<uint64_t>(TEST_INT64));

// Verify INSERT for floating-point types
auto record2 = db_mgr.INSERT(
SQL_TABLE("FloatingPointTypes"),
Expand Down Expand Up @@ -154,11 +163,26 @@ int main()
EXPECT_EQUAL(high_volume_record2->getPropertyUInt32("EndTick"), UINT32_MAX);
EXPECT_EQUAL(high_volume_record2->getPropertyBlob<int>("DataBlob"), data_vec);

// Verify prepareINSERT(SqlTable) overload: all columns from schema, no SQL_COLUMNS.
auto all_cols_insert = db_mgr.prepareINSERT(SQL_TABLE("AllIntegerTypes"));
all_cols_insert->setColumnValue(0, TEST_INT32);
all_cols_insert->setColumnValue(1, TEST_INT64);
all_cols_insert->setColumnValue(2, static_cast<uint32_t>(TEST_INT32));
all_cols_insert->setColumnValue(3, static_cast<uint64_t>(TEST_INT64));
auto all_cols_id = all_cols_insert->createRecord();
auto all_cols_record = db_mgr.findRecord("AllIntegerTypes", all_cols_id);
EXPECT_EQUAL(all_cols_record->getPropertyInt32("SomeInt32"), TEST_INT32);
EXPECT_EQUAL(all_cols_record->getPropertyInt64("SomeInt64"), TEST_INT64);
EXPECT_EQUAL(all_cols_record->getPropertyUInt32("SomeUInt32"), static_cast<uint32_t>(TEST_INT32));
EXPECT_EQUAL(all_cols_record->getPropertyUInt64("SomeUInt64"), static_cast<uint64_t>(TEST_INT64));

// Verify that we cannot write to an internal table.
EXPECT_TRUE(db_mgr.getSchema().hasTable("internal$SchemaTables"));
EXPECT_THROW(db_mgr.INSERT(SQL_TABLE("internal$SchemaTables")));
EXPECT_THROW(db_mgr.INSERT(SQL_TABLE("internal$SchemaTables"), SQL_COLUMNS("TableName"), SQL_VALUES("blah")));
EXPECT_THROW(db_mgr.INSERT(SQL_TABLE("internal$SchemaTables"), SQL_VALUES("x", "y", "z")));
EXPECT_THROW(db_mgr.prepareINSERT(SQL_TABLE("internal$SchemaTables"), SQL_COLUMNS("TableName")));
EXPECT_THROW(db_mgr.prepareINSERT(SQL_TABLE("internal$SchemaTables")));
EXPECT_THROW(db_mgr.EXECUTE("INSERT INTO internal$SchemaTables (TableName) VALUES (\"blah\")"));

REPORT_ERROR;
Expand Down