a MySQL SQL statement More...
Public Member Functions | |
this (MSqlConnection conn) | |
this (MSqlConnection conn, in char[] sql) | |
this (MSqlConnection conn, in SqlQuery query) | |
~this () | |
void | prepare (in char[] sql) |
char[] | get_last_sql () |
my_ulonglong | get_row_count () |
void | bind_param (int idx, BindPar par) |
void | bind_param (int idx, in char[] str, my_bool *pind=null) |
void | bind_param (int idx, in int *var, my_bool *pind=null) |
void | bind_param (int idx, in double *var, my_bool *pind=null) |
version (MYSQLPL_PREP_STMT) | |
void | bind_params (in BindVector params) |
version (MYSQLPL_PREP_STMT) | |
void | execute () |
void | execute (in char[] sql) |
void | execute (in SqlQuery query) |
bool | execute_fetch () |
bool | execute_fetch (in char[] sql) |
bool | fetch () |
MYSQL_RES * | get_meta_res () |
bool | is_select () |
int | get_column_count () |
void | get_column_type (int idx, out ColumnType info) |
void | print_results (Stream o, bool header=true) |
void | print_results (in char[] sql, Stream o, bool header=true) |
SqlResult | opCall () |
version (XMLSTORAGE) | |
Protected Types | |
enum | STATE { UNINITIALIZED = 0, PREPARED = 1, RESULT_BOUND = 2, EXECUTED = 4, FETCHED = 8 } |
Protected Member Functions | |
version (MYSQLPL_PREP_STMT) | |
Protected Attributes | |
MYSQL * | _db |
MYSQL_RES * | _result |
int | _state |
char[] | _last_sql |
SqlResult | _res |
int | _stmt_type |
a MySQL SQL statement
Definition at line 1237 of file mysqlpl.d.
enum mysqlpl::SqlStatement::STATE [protected] |
Definition at line 1386 of file mysqlpl.d.
{ UNINITIALIZED=0, //version(MYSQLPL_PREP_STMT) { PREPARED=1, RESULT_BOUND=2, //} EXECUTED=4, FETCHED=8, //EOF_DATA=16, STMT_ERROR=32 };
mysqlpl::SqlStatement::this | ( | MSqlConnection | conn ) | [inline] |
Definition at line 1239 of file mysqlpl.d.
References mysqlpl::version().
{ _db = conn(); _state = STATE.UNINITIALIZED; // _res = null; _stmt_type = -1; // _result = null; _param_bind_vector = new BindVector; version(MYSQLPL_PREP_STMT) { _stmt = mysqlpl_stmt_init(_db); } }
mysqlpl::SqlStatement::this | ( | MSqlConnection | conn, |
in char[] | sql | ||
) | [inline] |
Definition at line 1253 of file mysqlpl.d.
References mysqlpl::version().
{ _db = conn(); _state = STATE.UNINITIALIZED; // _res = null; _stmt_type = -1; // _result = null; _param_bind_vector = new BindVector; version(MYSQLPL_PREP_STMT) { _stmt = mysqlpl_stmt_init(_db); } prepare(sql); }
mysqlpl::SqlStatement::this | ( | MSqlConnection | conn, |
in SqlQuery | query | ||
) | [inline] |
Definition at line 1269 of file mysqlpl.d.
References mysqlpl::version().
{ _db = conn(); _state = STATE.UNINITIALIZED; // _res = null; _stmt_type = -1; // _result = null; _param_bind_vector = new BindVector; version(MYSQLPL_PREP_STMT) { _stmt = mysqlpl_stmt_init(_db); } execute(query); }
mysqlpl::SqlStatement::~this | ( | ) | [inline] |
Definition at line 1285 of file mysqlpl.d.
References mysqlpl::version().
{ version(MYSQLPL_PREP_STMT) { if (_stmt) mysqlpl_stmt_close(_stmt); bindvector_free(_result_bind_vector); _result_bind_vector = null; } else { if (_result) mysqlpl_free_result(_result); } }
void mysqlpl::SqlStatement::bind_param | ( | int | idx, |
BindPar | par | ||
) | [inline] |
void mysqlpl::SqlStatement::bind_param | ( | int | idx, |
in double * | var, | ||
my_bool * | pind = null |
||
) | [inline] |
Definition at line 1492 of file mysqlpl.d.
{ bind_param(idx, new BindPar(var, pind)); }
void mysqlpl::SqlStatement::bind_param | ( | int | idx, |
in char[] | str, | ||
my_bool * | pind = null |
||
) | [inline] |
Definition at line 1482 of file mysqlpl.d.
{ bind_param(idx, new BindPar(str, pind)); }
void mysqlpl::SqlStatement::bind_param | ( | int | idx, |
in int * | var, | ||
my_bool * | pind = null |
||
) | [inline] |
Definition at line 1487 of file mysqlpl.d.
{ bind_param(idx, new BindPar(var, pind)); }
void mysqlpl::SqlStatement::bind_params | ( | in BindVector | params ) | [inline] |
void mysqlpl::SqlStatement::execute | ( | ) | [inline] |
Definition at line 1590 of file mysqlpl.d.
References mysqlpl::mysqlpl_check_error(), and mysqlpl::version().
{ version(MYSQLPL_PREP_STMT) { if (!(_state & STATE.RESULT_BOUND)) { // a SELECT statement without ready define variables? if (is_select()) { // a statement with result set? (=> SELECT) _result_bind_vector = bindvector_resize(_result_bind_vector, get_column_count()); // define resultset variables assert(!_res); _res = new SqlResult(this); } } MYSQL_BIND* param_mbind_vector; // bind parameter values if (!_param_bind_vector.empty()) { // Umwandlung der Bindparamater von BindPar in MYSQL_BIND-Strukturelemente param_mbind_vector = bindvector_resize(param_mbind_vector, _param_bind_vector.length); MYSQL_BIND* o = param_mbind_vector; foreach(i; _param_bind_vector) { mysqlpl_bind_param(i, o); o = bindvector_next(o); } my_bool res = mysqlpl_stmt_bind_param(_stmt, param_mbind_vector); mysqlpl_check_error(_stmt, res); } int res = mysqlpl_stmt_execute(_stmt); mysqlpl_check_error(_stmt, res); } else { int res = mysqlpl_query(_db, toStringz(create_bound_sql())); mysqlpl_check_error(_db, res); if (_result) mysqlpl_free_result(_result); _result = mysqlpl_store_result(_db); if (_result) { // is_select() // define resultset variables if (_res) delete _res; _res = new SqlResult(this); } } _state = (_state|STATE.EXECUTED) & ~STATE.FETCHED; }
void mysqlpl::SqlStatement::execute | ( | in char[] | sql ) | [inline] |
void mysqlpl::SqlStatement::execute | ( | in SqlQuery | query ) | [inline] |
Definition at line 1653 of file mysqlpl.d.
{ prepare(query.get_sql()); bind_params(query.get_params()); execute(); }
bool mysqlpl::SqlStatement::execute_fetch | ( | in char[] | sql ) | [inline] |
Definition at line 1667 of file mysqlpl.d.
{ prepare(sql); return execute_fetch(); }
bool mysqlpl::SqlStatement::execute_fetch | ( | ) | [inline] |
bool mysqlpl::SqlStatement::fetch | ( | ) | [inline] |
Definition at line 1674 of file mysqlpl.d.
References mysqlpl::mysqlpl_check_error(), and mysqlpl::version().
{ if (!(_state & STATE.EXECUTED)) execute(); version(MYSQLPL_PREP_STMT) { int res = mysqlpl_stmt_fetch(_stmt); if (res == MYSQL_NO_DATA) return false; mysqlpl_check_error(_stmt, res); } else { _res._row = mysqlpl_fetch_row(_result); if (!_res._row) return false; } _state |= STATE.FETCHED; return true; }
int mysqlpl::SqlStatement::get_column_count | ( | ) | [inline] |
Definition at line 1727 of file mysqlpl.d.
References mysqlpl::version().
{ version(MYSQLPL_PREP_STMT) { uint count = mysqlpl_num_fields(get_meta_res()); if (count == -1u) throw new MSqlException(_stmt); return count; } else { return mysqlpl_num_fields(_result); } }
void mysqlpl::SqlStatement::get_column_type | ( | int | idx, |
out ColumnType | info | ||
) | [inline] |
Definition at line 1741 of file mysqlpl.d.
References mysqlpl::version().
{ MYSQL_FIELD* field = mysqlpl_fetch_field_direct(get_meta_res(), idx); if (!field) version(MYSQLPL_PREP_STMT) { throw new MSqlException(_stmt); } else { throw new MSqlException(_db); } info.init(field); }
char [] mysqlpl::SqlStatement::get_last_sql | ( | ) | [inline] |
MYSQL_RES* mysqlpl::SqlStatement::get_meta_res | ( | ) | [inline] |
my_ulonglong mysqlpl::SqlStatement::get_row_count | ( | ) | [inline] |
Definition at line 1444 of file mysqlpl.d.
References mysqlpl::version().
{ version(MYSQLPL_PREP_STMT) { if (is_select()) { if (mysqlpl_stmt_store_result(_stmt)) throw new MSqlException(_stmt); } my_ulonglong res = mysqlpl_stmt_affected_rows(_stmt); // for DML statements /* if (res == (my_ulonglong)-1) res = mysqlpl_stmt_num_rows(_stmt); // only for SELECT statements */ if (res == cast(my_ulonglong)-1) throw new MSqlException(_stmt); } else { int res = mysqlpl_affected_rows(_db); if (res == -1) throw new MSqlException(_db); } return res; }
bool mysqlpl::SqlStatement::is_select | ( | ) | [inline] |
Definition at line 1712 of file mysqlpl.d.
References mysqlpl::version().
{ version(MYSQLPL_PREP_STMT) { if (!_result) _result = mysqlpl_stmt_result_metadata(_stmt); return _result!=null; } else { if (!(_state & STATE.EXECUTED)) throw new MSqlException("statement not yet executed"); return _result!=null; } }
SqlResult mysqlpl::SqlStatement::opCall | ( | ) | [inline] |
void mysqlpl::SqlStatement::prepare | ( | in char[] | sql ) | [inline] |
Definition at line 1410 of file mysqlpl.d.
References mysqlpl::mysqlpl_check_error(), and mysqlpl::version().
{ delete _res; _res = null; _last_sql = sql; version(MYSQLPL_PREP_STMT) { int res = mysqlpl_stmt_prepare(_stmt, sql.ptr, sql.length); mysqlpl_check_error(_stmt, res); _state = STATE.PREPARED; } else { _sql = sql; if (_result) mysqlpl_free_result(_result); } _stmt_type = -1; _result = null; // _result_bind_vector = null; _param_bind_vector.clear(); }
void mysqlpl::SqlStatement::print_results | ( | in char[] | sql, |
Stream | o, | ||
bool | header = true |
||
) | [inline] |
Definition at line 1776 of file mysqlpl.d.
{ execute(sql); print_results(o, header); }
void mysqlpl::SqlStatement::print_results | ( | Stream | o, |
bool | header = true |
||
) | [inline] |
mysqlpl::SqlStatement::version | ( | MYSQLPL_PREP_STMT | ) | [inline] |
Definition at line 1542 of file mysqlpl.d.
References mysqlpl::mysqlpl_check_error().
{ void bind_result_column(BindResultPar par, MYSQL_BIND* bind) { bindvector_bind(bind, par._ptr, par._type, par._len, par._pind, par._plen); /* ersetzt memset(&bind, 0, sizeof(bind)); bind.buffer = par._ptr; bind.buffer_type = par._type; bind.buffer_length = par._len; bind.is_null = par._pind; bind.length = par._plen; */ } void bind_result(MYSQL_BIND* bind_array) { my_bool res = mysqlpl_stmt_bind_result(_stmt, bind_array); mysqlpl_check_error(_stmt, res); if (_state & STATE.EXECUTED) { delete _res; _res = null; _state = (_state|STATE.RESULT_BOUND) & ~STATE.FETCHED; } else _state |= STATE.RESULT_BOUND; } // define all columns of a complete recordset void bind_result(ColumnDataVector columns) { MYSQL_BIND* p = _result_bind_vector; foreach(col; columns) { col._column.bind_result_column(this, p); p = bindvector_next(p); } bind_result(_result_bind_vector); } }
mysqlpl::SqlStatement::version | ( | MYSQLPL_PREP_STMT | ) | [inline] |
Definition at line 1497 of file mysqlpl.d.
{ void bind_param(int idx, SqlInt var) { bind_param(idx, new BindPar(var)); } void bind_param(int idx, SqlInteger var) { bind_param(idx, new BindPar(var)); } void bind_param(int idx, SqlString str) { bind_param(idx, new BindPar(str)); } /*@@ SqlString as output buffer void bind_param(int idx, SqlString str) { bind_param(idx, new BindPar(str)); } */ void bind_param(int idx, SqlDate date) { bind_param(idx, new BindPar(date)); } void bind_param(int idx, SqlNumber num) { bind_param(idx, new BindPar(num)); } void bind_param(int idx, MSqlBlob blob) { bind_param(idx, new BindPar(blob)); } } // version(MYSQLPL_PREP_STMT)
mysqlpl::SqlStatement::version | ( | XMLSTORAGE | ) | [inline] |
mysqlpl::SqlStatement::version | ( | MYSQLPL_PREP_STMT | ) | [inline, protected] |
MYSQL* mysqlpl::SqlStatement::_db [protected] |
char [] mysqlpl::SqlStatement::_last_sql [protected] |
SqlResult mysqlpl::SqlStatement::_res [protected] |
MYSQL_RES* mysqlpl::SqlStatement::_result [protected] |
int mysqlpl::SqlStatement::_state [protected] |
int mysqlpl::SqlStatement::_stmt_type [protected] |