KaliVeda
Toolkit for HIC analysis
KVSQLite::table Class Reference

Detailed Description

Examples
db_sqlite_examples.C.

Definition at line 296 of file SQLiteDB.h.

#include <SQLiteDB.h>

Public Member Functions

 table (const table &other)
 
 table (const TString &Name, const std::vector< KVSQLite::column > &cols)
 
 table (const TString &Name="")
 
virtual ~table ()
 
columnadd_column (const KVSQLite::column &c)
 
columnadd_column (const TString &name, const TString &type)
 
columnadd_column (const TString &name, KVSQLite_column_type type)
 
int check_columns (const KVNameValueList &)
 
void foreign_key (const TString &child_key, const TString &parent_table, const TString &parent_key)
 
KVSQLite::columnget_column (const TString &n)
 
KVSQLite::columnget_column (int i)
 
TString get_column_names (const TString &exclude="", const TString &delim=",") const
 
const char * get_insert_command () const
 
TString get_table_creation_command () const
 
bool has_column (const TString &name) const
 
bool has_foreign_keys (bool ignore_back_references=true) const
 
bool has_primary_key () const
 
void insert_single_row ()
 use to add single row data to table. see prepare_insert_single_row() for usage. More...
 
bool is_temporary () const
 
const char * name () const
 
int number_of_columns () const
 
KVSQLite::columnoperator[] (const TString &n)
 
const KVSQLite::columnoperator[] (const TString &n) const
 
KVSQLite::columnoperator[] (int i)
 
const KVSQLite::columnoperator[] (int i) const
 
void prepare_data (const KVNameValueList &, const KVNamedParameter *=nullptr)
 
void prepare_insert_single_row ()
 
void primary_key (const TString &cols)
 
void print () const
 
void set_all_columns_null ()
 set the value of all columns in the table to NULL More...
 
void set_insert_mode (KVSQLite_insert_mode i)
 
void set_name (const TString &name)
 
void set_temporary (bool temp=true)
 
void show_columns () const
 print list of columns More...
 

Private Member Functions

void init_type_map ()
 
void set_foreign_key_back_references ()
 

Private Attributes

std::unordered_map< std::string, int > fColMap
 
std::vector< KVSQLite::columnfColumns
 
databasefDB = nullptr
 
std::vector< foreign_key_constraintfForeignKeys
 
KVSQLite_insert_mode fInsert
 
TString fName
 
TString fPrimaryKey
 
bool fTemp
 

Static Private Attributes

static std::map< TString, KVSQLite::column_type::typestype_map
 

Friends

class database
 Table in an SQLite database. More...
 

Constructor & Destructor Documentation

◆ table() [1/3]

KVSQLite::table::table ( const TString Name,
const std::vector< KVSQLite::column > &  cols 
)
inline

Definition at line 318 of file SQLiteDB.h.

◆ table() [2/3]

KVSQLite::table::table ( const TString Name = "")
inline

Definition at line 324 of file SQLiteDB.h.

◆ table() [3/3]

KVSQLite::table::table ( const table other)
inline

Definition at line 327 of file SQLiteDB.h.

◆ ~table()

virtual KVSQLite::table::~table ( )
inlinevirtual

Definition at line 331 of file SQLiteDB.h.

Member Function Documentation

◆ add_column() [1/3]

column & KVSQLite::table::add_column ( const KVSQLite::column c)

add column to table

Returns
reference to added column.
Note
cannot be used for existing table in database: see database::add_column()
Examples
db_sqlite_examples.C.

Definition at line 1623 of file SQLiteDB.cpp.

◆ add_column() [2/3]

column & KVSQLite::table::add_column ( const TString name,
const TString type 
)

add column to table

Returns
reference to added column
Note
cannot be used for existing table in database: see database::add_column()

Definition at line 1641 of file SQLiteDB.cpp.

◆ add_column() [3/3]

column& KVSQLite::table::add_column ( const TString name,
KVSQLite_column_type  type 
)
inline

add column to table. return reference to added column.

Definition at line 374 of file SQLiteDB.h.

◆ check_columns()

int KVSQLite::table::check_columns ( const KVNameValueList l)

make sure that all parameters in the list have corresponding columns in the table

Returns
the number of columns to be added

Definition at line 1718 of file SQLiteDB.cpp.

◆ foreign_key()

void KVSQLite::table::foreign_key ( const TString child_key,
const TString parent_table,
const TString parent_key 
)

add a FOREIGN KEY constraint to the table.

The child key(s) should first be declared using add_column()

foreign_key("item_id", "items", "id")
=> FOREIGN KEY(item_id) REFERENCES items(id)
foreign_key("item_id", "items")
=> FOREIGN KEY(item_id) REFERENCES items
[case where 'items' has an INTEGER PRIMARY KEY, no need to specify]
foreign_key("songartist,songalbum", "album", "albumartist,albumname")
=> FOREIGN KEY(songartist, songalbum) REFERENCES album(albumartist, albumname)
[case where both parent and child tables have composite primary keys]
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t child
void foreign_key(const TString &child_key, const TString &parent_table, const TString &parent_key)
Definition: SQLiteDB.cpp:1674
Examples
db_sqlite_examples.C.

Definition at line 1674 of file SQLiteDB.cpp.

◆ get_column() [1/2]

KVSQLite::column& KVSQLite::table::get_column ( const TString n)
inline

Definition at line 407 of file SQLiteDB.h.

◆ get_column() [2/2]

KVSQLite::column& KVSQLite::table::get_column ( int  i)
inline

Definition at line 389 of file SQLiteDB.h.

◆ get_column_names()

TString KVSQLite::table::get_column_names ( const TString exclude = "",
const TString delim = "," 
) const

Return a comma-separated list of the colum names

Parameters
[in]delimseparator to use in list (default: ",")
[in]excludelist of column names to exclude from list

Definition at line 1779 of file SQLiteDB.cpp.

◆ get_insert_command()

const char * KVSQLite::table::get_insert_command ( ) const

Definition at line 476 of file SQLiteDB.cpp.

◆ get_table_creation_command()

TString KVSQLite::table::get_table_creation_command ( ) const

Create and fill SQLite command to create this table, i.e.

CREATE TABLE name (
col1 TYPE_1 [CONSTRAINT_1]
col2 TYPE_2 [CONSTRAINT_2]
...
[PRIMARY KEY(col1,...)]
[FOREIGN KEY(col1,...) REFERENCES other_table(other_column) ...]
);
const char * name() const
Definition: SQLiteDB.h:333

Definition at line 1551 of file SQLiteDB.cpp.

◆ has_column()

bool KVSQLite::table::has_column ( const TString name) const
inline

return true if column with given name exists

Definition at line 393 of file SQLiteDB.h.

◆ has_foreign_keys()

bool KVSQLite::table::has_foreign_keys ( bool  ignore_back_references = true) const
inline

Definition at line 462 of file SQLiteDB.h.

◆ has_primary_key()

bool KVSQLite::table::has_primary_key ( ) const
inline

Definition at line 458 of file SQLiteDB.h.

◆ init_type_map()

void KVSQLite::table::init_type_map ( )
private

Definition at line 1508 of file SQLiteDB.cpp.

◆ insert_single_row()

void KVSQLite::table::insert_single_row ( )

use to add single row data to table. see prepare_insert_single_row() for usage.

Definition at line 1829 of file SQLiteDB.cpp.

◆ is_temporary()

bool KVSQLite::table::is_temporary ( ) const
inline

Definition at line 368 of file SQLiteDB.h.

◆ name()

const char* KVSQLite::table::name ( ) const
inline

Definition at line 333 of file SQLiteDB.h.

◆ number_of_columns()

int KVSQLite::table::number_of_columns ( ) const
inline

Definition at line 422 of file SQLiteDB.h.

◆ operator[]() [1/4]

KVSQLite::column& KVSQLite::table::operator[] ( const TString n)
inline

Definition at line 399 of file SQLiteDB.h.

◆ operator[]() [2/4]

const KVSQLite::column& KVSQLite::table::operator[] ( const TString n) const
inline

Definition at line 403 of file SQLiteDB.h.

◆ operator[]() [3/4]

KVSQLite::column& KVSQLite::table::operator[] ( int  i)
inline

Definition at line 385 of file SQLiteDB.h.

◆ operator[]() [4/4]

const KVSQLite::column& KVSQLite::table::operator[] ( int  i) const
inline

Definition at line 381 of file SQLiteDB.h.

◆ prepare_data()

void KVSQLite::table::prepare_data ( const KVNameValueList l,
const KVNamedParameter null_value = nullptr 
)

fill all columns in table with data contained in KVNameValueList parameters having the same name.

any columns which do not appear in the KVNameValueList (except for PRIMARY KEY) will be set to 'null'

if required, any parameters with the same type&value as "null_value" will be set to 'null' too

Definition at line 1741 of file SQLiteDB.cpp.

◆ prepare_insert_single_row()

void KVSQLite::table::prepare_insert_single_row ( )

call before setting data in individual columns and then calling insert_single_row()

my_table["col1"] = 6;
my_table["col3"] = "hello";
void prepare_insert_single_row()
Definition: SQLiteDB.cpp:1810
void insert_single_row()
use to add single row data to table. see prepare_insert_single_row() for usage.
Definition: SQLiteDB.cpp:1829
Examples
db_sqlite_examples.C.

Definition at line 1810 of file SQLiteDB.cpp.

◆ primary_key()

void KVSQLite::table::primary_key ( const TString cols)
inline

Define PRIMARY KEY column(s) for table. If more than one column is used, separate names with commas.

Translates to SQLite statement:

PRIMARY KEY(col)
PRIMARY KEY(col1,col2,...)

We detect the case where a single INTEGER column is used as PRIMARY KEY: In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID. On an INSERT, if the ROWID or INTEGER PRIMARY KEY column is not explicitly given a value, then it will be filled automatically with an unused integer, usually one more than the largest ROWID currently in use. This is true regardless of whether or not the AUTOINCREMENT keyword is used. The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and disk I/O overhead and should be avoided if not strictly needed. It is usually not needed.

Examples
db_sqlite_examples.C.

Definition at line 432 of file SQLiteDB.h.

◆ print()

void KVSQLite::table::print ( ) const
inline

Definition at line 417 of file SQLiteDB.h.

◆ set_all_columns_null()

void KVSQLite::table::set_all_columns_null ( )

set the value of all columns in the table to NULL

Definition at line 1763 of file SQLiteDB.cpp.

◆ set_foreign_key_back_references()

void KVSQLite::table::set_foreign_key_back_references ( )
private

called by database::add_table

for any foreign key defined for this table, we set up a back reference in the parent table

Definition at line 1525 of file SQLiteDB.cpp.

◆ set_insert_mode()

void KVSQLite::table::set_insert_mode ( KVSQLite_insert_mode  i)
inline

The insert mode determines how to deal with errors caused by constraints The possible values of i are (with corresponding SQLite meaning):

KVSQLite::insert_mode::DEFAULT ("INSERT INTO [table] ...")
KVSQLite::insert_mode::FAIL ("INSERT OR FAIL INTO [table] ...")
- if any data in row being inserted fails a constraint on one or more
columns, insertion fails with error
KVSQLite::insert_mode::IGNORE ("INSERT OR IGNORE INTO [table] ...")
- if any data in row being inserted fails a constraint on one or more
columns, the row is silently ignored (existing row unchanged)
KVSQLite::insert_mode::REPLACE ("INSERT OR REPLACE INTO [table] ...")
- if any data in row being inserted fails a constraint on one or more
columns, we replace the old row with the new one
table(const TString &Name, const std::vector< KVSQLite::column > &cols)
Definition: SQLiteDB.h:318

Definition at line 344 of file SQLiteDB.h.

◆ set_name()

void KVSQLite::table::set_name ( const TString name)
inline

Definition at line 337 of file SQLiteDB.h.

◆ set_temporary()

void KVSQLite::table::set_temporary ( bool  temp = true)
inline

Create a temporary table

Definition at line 363 of file SQLiteDB.h.

◆ show_columns()

void KVSQLite::table::show_columns ( ) const

print list of columns

Definition at line 1603 of file SQLiteDB.cpp.

Member Data Documentation

◆ fColMap

std::unordered_map<std::string, int> KVSQLite::table::fColMap
mutableprivate

Definition at line 308 of file SQLiteDB.h.

◆ fColumns

std::vector<KVSQLite::column> KVSQLite::table::fColumns
mutableprivate

Definition at line 307 of file SQLiteDB.h.

◆ fDB

database* KVSQLite::table::fDB = nullptr
private

Definition at line 304 of file SQLiteDB.h.

◆ fForeignKeys

std::vector<foreign_key_constraint> KVSQLite::table::fForeignKeys
private

Definition at line 311 of file SQLiteDB.h.

◆ fInsert

KVSQLite_insert_mode KVSQLite::table::fInsert
private

Definition at line 306 of file SQLiteDB.h.

◆ fName

TString KVSQLite::table::fName
private

Definition at line 305 of file SQLiteDB.h.

◆ fPrimaryKey

TString KVSQLite::table::fPrimaryKey
private

Definition at line 310 of file SQLiteDB.h.

◆ fTemp

bool KVSQLite::table::fTemp
private

Definition at line 312 of file SQLiteDB.h.

◆ type_map

std::map< TString, KVSQLite::column_type::types > KVSQLite::table::type_map
staticprivate

Definition at line 309 of file SQLiteDB.h.