Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes

mysqlpl::SqlStatement Class Reference

a MySQL SQL statement More...

Collaboration diagram for mysqlpl::SqlStatement:
Collaboration graph
[legend]

List of all members.

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

Detailed Description

a MySQL SQL statement

Definition at line 1237 of file mysqlpl.d.


Member Enumeration Documentation

enum mysqlpl::SqlStatement::STATE [protected]
Enumerator:
UNINITIALIZED 
PREPARED 
RESULT_BOUND 
EXECUTED 
FETCHED 

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
    };

Constructor & Destructor Documentation

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);
      }
    }

Here is the call graph for this function:

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);
    }

Here is the call graph for this function:

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);
    }

Here is the call graph for this function:

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);
      }
    }

Here is the call graph for this function:


Member Function Documentation

void mysqlpl::SqlStatement::bind_param ( int  idx,
BindPar  par 
) [inline]

Definition at line 1473 of file mysqlpl.d.

    {
        if (_param_bind_vector.length() <= idx)
            _param_bind_vector.resize(idx+1);

        _param_bind_vector.bind(idx, par);
    }
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]

Definition at line 1534 of file mysqlpl.d.

    {
        _param_bind_vector = params;
    }
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;
    }

Here is the call graph for this function:

void mysqlpl::SqlStatement::execute ( in char[]  sql ) [inline]

Definition at line 1647 of file mysqlpl.d.

    {
        prepare(sql);
        execute();
    }
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]

Definition at line 1660 of file mysqlpl.d.

    {
        execute();

        return fetch();
    }
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;
    }

Here is the call graph for this function:

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);
      }
    }

Here is the call graph for this function:

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);
    }

Here is the call graph for this function:

char [] mysqlpl::SqlStatement::get_last_sql (  ) [inline]

Definition at line 1436 of file mysqlpl.d.

    {
        return _last_sql;
    }
MYSQL_RES* mysqlpl::SqlStatement::get_meta_res (  ) [inline]

Definition at line 1698 of file mysqlpl.d.

References mysqlpl::version().

    {
      version(MYSQLPL_PREP_STMT) {
        if (!_result) {
            _result = mysqlpl_stmt_result_metadata(_stmt);

            if (!_result)
                throw new MSqlException("no result set metadata");
        }
      }

        return _result;
    }

Here is the call graph for this function:

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;
    }

Here is the call graph for this function:

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;
  }
    }

Here is the call graph for this function:

SqlResult mysqlpl::SqlStatement::opCall (  ) [inline]

Definition at line 1783 of file mysqlpl.d.

    {
        if (!_res)
            fetch();

        return _res;
    }
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();
    }

Here is the call graph for this function:

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]

Definition at line 1755 of file mysqlpl.d.

    {
        if (!(_state & STATE.EXECUTED))
            execute();

        if (header)
            _res.print_columns(o);

        int row_cnt = 0;

        while(fetch()) {
            ++row_cnt;

            o.writeString(format("row %d:\n", row_cnt));

            _res.print_values(o);

            o.write('\n');
        }
    }
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);
    }

  }

Here is the call graph for this function:

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]

Definition at line 1791 of file mysqlpl.d.

                      {//@@
    void dump_results(XMLStorage.XMLPos idx, in char[] element_name);

    void dump_results(in char[] sql, XMLStorage.XMLPos idx, in char[] element_name)
    {
        execute(sql);

        dump_results(idx, element_name);
    }
  }
mysqlpl::SqlStatement::version ( MYSQLPL_PREP_STMT   ) [inline, protected]

Definition at line 1304 of file mysqlpl.d.

                             {
    MYSQL_STMT* _stmt;

    MYSQL_BIND* _result_bind_vector;
  } else {

Member Data Documentation

MYSQL* mysqlpl::SqlStatement::_db [protected]

Definition at line 1302 of file mysqlpl.d.

char [] mysqlpl::SqlStatement::_last_sql [protected]

Definition at line 1396 of file mysqlpl.d.

SqlResult mysqlpl::SqlStatement::_res [protected]

Definition at line 1398 of file mysqlpl.d.

MYSQL_RES* mysqlpl::SqlStatement::_result [protected]

Definition at line 1382 of file mysqlpl.d.

Definition at line 1393 of file mysqlpl.d.

Definition at line 1400 of file mysqlpl.d.


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