<filename>simo1209-DB-Engine/src/db_column.cpp<gh_stars>0
#include "db_column.h"
#include "db_index.h"
#include "db_exceptions.h"
#include "db_bptree.h"

DBColumn::DBColumn(const std::u16string name, DBColumnType type) : columnName(name), columnType(type)
{
    columnOffset = 0;
}

DBColumn::DBColumn(const std::u16string name, DBColumnType type, DBcolumnIndexType indexType) : columnName(name), columnType(type), columnIndexType(indexType)
{

    if (type != INT)
    {
        throw WrongIndexTypeException();
    }

    columnOffset = 0;
    indexed = true;
}

DBColumn::DBColumn(const std::u16string name, DBColumnType type, uint64_t offset) : columnName(name), columnType(type), columnOffset(offset)
{}

DBColumn::DBColumn(const std::u16string name, DBColumnType type, uint64_t offset, DBcolumnIndexType indexType) : columnName(name), columnType(type), columnOffset(offset), columnIndexType(indexType)
{
    indexed = true;
}

std::u16string DBColumn::getColumnName()
{
    return columnName;
}

uint64_t DBColumn::getColumnOffset()
{
    return columnOffset;
}

void DBColumn::setColumnOffset(uint64_t offset)
{
    columnOffset = offset;
}

DBColumnType DBColumn::getColumnType()
{
    return columnType;
}

bool DBColumn::hasIndex()
{
    return indexed;
}

uint64_t DBColumn::getColumnTypeBytes(DBColumnType type)
{
    switch (type)
    {
    case INT:
        return 4;
    case TEXT:
        return 16;
    }

    throw InvalidColumnTypeException();
}

uint64_t DBColumn::getColumnBytes()
{
    return DBColumn::getColumnTypeBytes(columnType);
}

uint64_t DBColumn::getColumnIndexTypeNumber(){
    return columnIndexType;
}

DBcolumnIndexType DBColumn::getColumnIndexType(){
    return columnIndexType;
}

DBcolumnIndexType DBColumn::castIndexType(uint64_t indexType){
    switch (indexType)
    {
    case 1:
        return BPTREE;
        break;
    default:
        throw std::invalid_argument("Index tyoe unrecognized");
        break;
    }
}

DBIntegerIndex* DBColumn::createIntegerIndex(std::filesystem::path columnPath){
    
    DBIntegerIndex *index;

    switch (columnIndexType)
    {
    case BPTREE:
        index = new DBBPTreeIndex(columnPath);
        break;
    default:
        throw std::invalid_argument("Index not supported");
        break;
    }

    return index;    
}

DBIntegerIndex* DBColumn::getIntegerIndex(std::filesystem::path tablePath){
    
    DBIntegerIndex *index;

    // std::cerr << "Table path = " << tablePath << std::endl;

    std::filesystem::path columnPath(tablePath);
    columnPath.append(columnName);

    // std::cerr << "BeforeColumnPath = " << columnPath << std::endl;

    switch (columnIndexType)
    {
    case BPTREE:
        index = new DBBPTreeIndex();
        break;
    default:
        throw std::invalid_argument("Index not supported");
        break;
    }

    index->load(columnPath);

    return index;
}