From: <ibr...@us...> - 2009-09-25 20:22:30
|
Revision: 3316 http://tora.svn.sourceforge.net/tora/?rev=3316&view=rev Author: ibre5041 Date: 2009-09-25 20:22:13 +0000 (Fri, 25 Sep 2009) Log Message: ----------- New class trotlQuery - descendent of SqlStatement. Better way of reading data from the query trotlQuery::readValue Modified Paths: -------------- branches/tora-trotl/src/tooracleconnection.cpp Modified: branches/tora-trotl/src/tooracleconnection.cpp =================================================================== --- branches/tora-trotl/src/tooracleconnection.cpp 2009-09-25 20:16:09 UTC (rev 3315) +++ branches/tora-trotl/src/tooracleconnection.cpp 2009-09-25 20:22:13 UTC (rev 3316) @@ -235,7 +235,76 @@ bool SaveInPool; public: - ::trotl::SqlStatement * Query; + class trotlQuery : public ::trotl::SqlStatement + { + public: + typedef ::trotl::SqlStatement super; + trotlQuery(::trotl::OciConnection &conn, + const ::trotl::tstring &stmt, // TODO QString here + ub4 lang=OCI_NTV_SYNTAX, + int bulk_rows=::trotl::g_OCIPL_BULK_ROWS) + : ::trotl::SqlStatement(conn, stmt, lang, bulk_rows) + {}; + void readValue(toQValue &value) + { + super::BindPar const &BP(get_stmt_type() == STMT_SELECT ? + get_next_column() : + get_next_out_bindpar()); + + if((_state & EXECUTED) == 0) + execute_internal(::trotl::g_OCIPL_BULK_ROWS, OCI_DEFAULT); + + if(BP.is_null(_last_buff_row)) { + value = toQValue(); + get_log().ts( std::string(__HERE__)) << "Just read: NULL" << std::endl; + } else { + switch(BP.dty) { + case SQLT_NUM: + case SQLT_VNU: + { + OCINumber* vnu = (OCINumber*) &((char*)BP.valuep)[_last_buff_row * BP.value_sz ]; + sword res; + boolean isint; + res = OCINumberIsInt(_errh, vnu, &isint); + oci_check_error(__HERE__, _errh, res); + if(isint) + { + long long i; + res = OCINumberToInt(_errh, + vnu, + sizeof(long long), + OCI_NUMBER_SIGNED, + &i); + oci_check_error(__HERE__, _errh, res); + value = toQValue(i); + get_log().ts( std::string(__HERE__)) << "Just read: " << i << std::endl; + } else { + double d; + sword res = OCINumberToReal(_errh, + vnu, + sizeof(double), + &d); + oci_check_error(__HERE__, _errh, res); + value = toQValue(d); + get_log().ts( std::string(__HERE__)) << "Just read: " << d << std::endl; + } + break; + } + default: + std::string s(BP.get_string(_last_buff_row)); + value = toQValue(QString(s.c_str())); + get_log().ts( std::string(__HERE__)) << "Just read: \"" << s << "\"" << std::endl; + } + } + + if(_out_pos == _column_count && BP._bind_type == BP.DEFINE_SELECT) + ++_last_buff_row; + + if(_last_buff_row == fetched_rows() && ((_state & EOF_DATA) == 0) && get_stmt_type() == STMT_SELECT) + fetch(_buff_size); + } + }; + trotlQuery * Query; oracleQuery(toQuery *query, oracleSub *) : toQuery::queryImpl(query) @@ -258,7 +327,11 @@ virtual toQValue readValue(void) { - const ::trotl::BindPar& bp( + toQValue retval; + Query->readValue(retval); + return retval; + + const oracleQuery::trotlQuery::BindPar& bp( Query->_stmt_type == OCI_STMT_SELECT ? Query->get_curr_column() : Query->get_curr_out_bindpar()); try { @@ -497,7 +570,7 @@ { //int descriptionLen; //Query->describe_select(descriptionLen); - get_log().ts( std::string(__HERE__)) << "TODO: columns:" << std::endl; + get_log().ts( std::string(__HERE__)) << "TODO: columns:" << Query->get_column_count() << std::endl; return Query->get_column_count(); } @@ -513,6 +586,7 @@ // TODO trotl should return const iterator const std::vector<trotl::ColumnType> &col = Query->get_columns(); + if(col.empty()) return ret; std::vector<trotl::ColumnType>::const_iterator it = col.begin(); ++it; // starts with 1st //get_log().ts( std::string(__HERE__)) << "Columns: " << q1.get_column_count() << std::endl; for(; it != col.end(); ++it) @@ -673,7 +747,7 @@ return ret; } - }; + }; // class oracleQuery : public toQuery::queryImpl class oracleConnection : public toConnection::connectionImpl { @@ -934,7 +1008,6 @@ } catch (::trotl::OciException e) { get_log().ts( std::string(__HERE__)) << e.what() << std::endl; - exit(0); } catch (...) { @@ -949,6 +1022,11 @@ return new oracleQuery(query, oracleConv(sub)); } + + /* TODO I have no idea when this method is used. + instance of toQuery is created and then dropped - why ? + NOTE: there is one more execute method + */ virtual void execute(toConnectionSub *sub, const QString &sql, toQList ¶ms) { oracleSub *conn = oracleConv(sub); @@ -957,9 +1035,12 @@ { try { - //otl_cursor::direct_exec(*(conn->Connection), sql.toUtf8().constData()); - //TODO - //get_log().ts( std::string(__HERE__)) << "TODO: otl_cursor::direct_exec" << __HERE__ << std::endl; + std::string _sql(sql.toUtf8().constData()); + oracleQuery::trotlQuery query(*conn->_conn, _sql); + query.execute_internal(::trotl::g_OCIPL_BULK_ROWS, OCI_DEFAULT); + //otl_cursor::direct_exec(*(conn->Connection), sql.toUtf8().constData()); + //TODO + get_log().ts( std::string(__HERE__)) << "TODO: otl_cursor::direct_exec" << std::endl; } catch (const ::trotl::OciException &exc) { @@ -976,7 +1057,7 @@ try { //oracleQuery::oracleSqlStatement s1(*conn->_conn, sql.toUtf8().constData()); - ::trotl::SqlStatement s1(*conn->_conn, ::std::string(sql.toUtf8().constData())); + oracleQuery::trotlQuery s1(*conn->_conn, ::std::string(sql.toUtf8().constData())); //conn->Connection->reset_throw_count(); //conn->Connection->syntax_check(sql.toUtf8().constData()); @@ -1171,7 +1252,7 @@ throw QString::fromLatin1("Internal error, not oracle sub connection"); try { - delete Query; + if(Query) delete Query; Query = NULL; if (Cancel) @@ -1185,7 +1266,7 @@ get_log().ts( std::string(__HERE__)) << "SQL:" << ::std::string(sql.toUtf8().constData()) << std::endl; //Query = new oracleQuery::oracleSqlStatement(*conn->_conn, sql.toUtf8().constData()); - Query = new ::trotl::SqlStatement(*conn->_conn, ::std::string(sql.toUtf8().constData())); + Query = new oracleQuery::trotlQuery(*conn->_conn, ::std::string(sql.toUtf8().constData())); // TODO autocommit ?? // Query->set_commit(0); //if (toQValue::numberFormat() == 0) @@ -1208,9 +1289,9 @@ //otl_null null; for (toQList::iterator i = query()->params().begin();i != query()->params().end();i++) { - const ::trotl::BindPar& bp = (*Query).get_curr_in_bindpar(); + const trotlQuery::BindPar& bp = (*Query).get_curr_in_bindpar(); - if( (bp._bind_type & ( ::trotl::BindPar::BIND_IN | ::trotl::BindPar::BIND_OUT )) == 0) + if( (bp._bind_type & ( trotlQuery::BindPar::BIND_IN | trotlQuery::BindPar::BIND_OUT )) == 0) throw toConnection::exception(QString::fromLatin1("Fatal pruser error - invalid BindPar")); if( bp.bind_typename == "int" /*&& (*i).isInt()*/ ) { @@ -1468,7 +1549,7 @@ QString str = QString::fromLatin1("ALTER SESSION SET NLS_DATE_FORMAT = '"); str += toConfigurationSingle::Instance().dateFormat(); str += QString::fromLatin1("'"); - ::trotl::SqlStatement date(*conn, ::std::string(str.toAscii().constData())); + oracleQuery::trotlQuery date(*conn, ::std::string(str.toAscii().constData())); } catch (...) { @@ -1479,8 +1560,8 @@ try { - //oracleQuery::oracleSqlStatement info(*conn, - ::trotl::SqlStatement info(*conn, + //oracleQuery::oracleSqlStatement info(*conn, + oracleQuery::trotlQuery info(*conn, "BEGIN\n" " SYS.DBMS_APPLICATION_INFO.SET_CLIENT_INFO('" TOAPPNAME " (http://tora.sf.net)" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |