Samchon Framework for CPP  1.0.0
samchon::library::SQLStatement Class Reference

A sql statement. More...

#include <SQLStatement.hpp>

Collaboration diagram for samchon::library::SQLStatement:

Public Member Functions

void free ()
 Free the sql statement. More...
 
void refresh ()
 Refresh the sql statement. More...
 
template<typename T , typename... _Args>
void prepare (const std::string &sql, const T &val, const _Args &...args)
 Prepare a sql statement. More...
 
void execute ()
 Execute the prepared sql statement. More...
 
void executeDirectly (const std::string &)
 Execute sql-statement direclty. More...
 
auto fetch () const -> bool
 Fetch a record. More...
 
auto next () const -> bool
 Move cursor to the next sql-statement. More...
 
auto size () const -> size_t
 Get size of columns. More...
 
template<typename T >
auto at (size_t index) const -> T
 Get column's data by its index. More...
 
template<typename T >
auto get (const std::string &) const -> T
 Get a column data by its name. More...
 
virtual auto toXML () const -> std::shared_ptr< XML >
 Result sets to XML. More...
 

Protected Member Functions

 SQLStatement (SQLi *sqli)
 Protected Constructor. More...
 

Protected Attributes

SQLisqli
 SQLi who created the SQLStatement. More...
 
void * hstmt
 Handler of sql statement (OBDC) More...
 
size_t bindParameterCount
 Count of binded parameters
. More...
 
Map< size_t, SQL_SIZE_T > bindParameterBASizeMap
 A map for binary size. More...
 

Detailed Description

A sql statement.

A SQLStatement instance is used to executing a SQL statement and returning the results it produces against a SQL database that is opened through a SQLi instance.

Through the SQLi, it's the reason why a principle of DBMS, DBMS system doesn't allow simultaneous query from a session (connection). Only a query (process) is allowed at a time. If you try to simultaneous query from a SQLi, ODBC throws error. To avoid the error, an execute of query from SQLStatement will lock a mutex of SQLi to ensure exclusiveness.

library_sql.png
Note

To ensure the exclusiveness, you've to make SQLStatement from SQLi. Do not make SQLStatement by yourself. call SQLi::createSQLStatement() instead. Even you make a derived class from SQLStatement, don't make its constructor to have public accessor.

Becuase execution of a sql statement causes lock on mutex in SQLi, you've to destruct the SQLStatement or call SQLStatement::free() method(). If you don't, the mutex will not be unlocked, thus you can't do anything by the SQLi.

See also
samchon::library
Author
Jeongho Nam http://samchon.org

Definition at line 44 of file SQLStatement.hpp.

Constructor & Destructor Documentation

SQLStatement::SQLStatement ( SQLi sqli)
protected

Protected Constructor.

SQLStatement's constructor have to created by SQLi::createStatement().

Note

Don't create SQLStatement by yourself.

SQLStatement has to be created by SQLi::createStatement().

Parameters
sqliParent SQLi who created the SQLStatement

Definition at line 22 of file SQLStatement.cpp.

References samchon::library::SQLi::hdbc.

Member Function Documentation

void SQLStatement::free ( )

Free the sql statement.

Definition at line 41 of file SQLStatement.cpp.

References samchon::library::SQLi::stmt, and samchon::library::SQLi::stmtMutex.

void SQLStatement::refresh ( )

Refresh the sql statement.

Definition at line 53 of file SQLStatement.cpp.

References samchon::library::SQLi::stmt, and samchon::library::SQLi::stmtMutex.

template<typename T , typename... _Args>
void samchon::library::SQLStatement::prepare ( const std::string &  sql,
const T &  val,
const _Args &...  args 
)
inline

Prepare a sql statement.

Prepare a sql statement with parameters to bind for execution

Warning
Be careful for destructions of binded parameters
Parameters
sqlA sql-statement to prepare
...args The parameters to bind

Definition at line 126 of file SQLStatement.hpp.

void SQLStatement::execute ( )

Execute the prepared sql statement.

Exceptions
exceptionError message from DBMS

Definition at line 83 of file SQLStatement.cpp.

void SQLStatement::executeDirectly ( const std::string &  sql)

Execute sql-statement direclty.

Executes the given sql-statement without preparing or binding any parameter

Warning
  • Cannot use if prepare statement has already defined. Use execute instead.
  • Not recommended when the case of dynamic sql and the sql-statement is not for procedure but for direct access to table. Use prepare and execute instead.
Parameters
sqlsql-statement you want to execute
Exceptions
exceptionError message from DBMS
exceptionMethod prepare is already called

Definition at line 89 of file SQLStatement.cpp.

auto SQLStatement::fetch ( ) const -> bool

Fetch a record.

Returns

Whether succeded to fetch a record.

False means there's not any record or previous record was the last.

Definition at line 109 of file SQLStatement.cpp.

auto SQLStatement::next ( ) const -> bool

Move cursor to the next sql-statement.

Returns
Whether succeded to move cursor to the next statement.

Definition at line 105 of file SQLStatement.cpp.

auto SQLStatement::size ( ) const -> size_t

Get size of columns.

Returns the number of columns in a result set.

Warning
Not size of rows.

Definition at line 123 of file SQLStatement.cpp.

template<typename T >
auto samchon::library::SQLStatement::at ( size_t  index) const -> T
inline

Get column's data by its index.

Returns column's data from fetched-record by specified column index

Parameters
indexIndex number of a column wants to get
Returns
Data stored in the record at the position of specifield column

Definition at line 220 of file SQLStatement.hpp.

template<typename T >
auto samchon::library::SQLStatement::get ( const std::string &  ) const -> T
inline

Get a column data by its name.

Returns column's data from fetchched-recrod by specified column name

Parameters
nameName of a column wants to get
Returns
Data stored in the record at the position of specifield column

Definition at line 247 of file SQLStatement.hpp.

auto SQLStatement::toXML ( ) const -> std::shared_ptr<XML>
virtual

Result sets to XML.

Converts the records of current sql-statement to XML.

Recommends to override for each DBMS's domain XML rule.

Returns
XML representing records of the statement

Reimplemented in samchon::library::TSQLStatement.

Definition at line 134 of file SQLStatement.cpp.

Member Data Documentation

SQLi* samchon::library::SQLStatement::sqli
protected

SQLi who created the SQLStatement.

Definition at line 61 of file SQLStatement.hpp.

void* samchon::library::SQLStatement::hstmt
protected

Handler of sql statement (OBDC)

Definition at line 66 of file SQLStatement.hpp.

size_t samchon::library::SQLStatement::bindParameterCount
protected

Count of binded parameters
.

Definition at line 74 of file SQLStatement.hpp.

Map<size_t, SQL_SIZE_T> samchon::library::SQLStatement::bindParameterBASizeMap
protected

A map for binary size.

When calls ByteArray::size(), the returned size_t value can't be kept until

Definition at line 82 of file SQLStatement.hpp.


The documentation for this class was generated from the following files: