static int fetchExecuteResult(struct nuodb *db, Statement *stmt, int64_t *rows_affected, int64_t *last_insert_id) { ResultSet *resultSet = 0; try { resultSet = stmt->getGeneratedKeys(); // NuoDB uses -1 as a flag for zero-rows-affected *rows_affected = std::max(0, stmt->getUpdateCount()); if (*rows_affected > 0 && resultSet->getMetaData()->getColumnCount() > 0) { while (resultSet->next()) { // TODO find out how to read the last id first } switch (resultSet->getMetaData()->getColumnType(1)) { case NUOSQL_TINYINT: case NUOSQL_SMALLINT: case NUOSQL_INTEGER: case NUOSQL_BIGINT: case NUOSQL_FLOAT: case NUOSQL_DOUBLE: case NUOSQL_NUMERIC: case NUOSQL_DECIMAL: *last_insert_id = resultSet->getLong(1); break; default: // This is to avoid a failure when trying to call resultSet->getLong() when // the generated column has a string type and a default sequence. If the user // passes a string that cannot be converted to long, an exception is thrown. // // Since this only happens when the string is user-provided, we don't need to // worry about trying to parse the returned value to return to the user. // // See TestStringSequence for more details. *last_insert_id = 0; break; } } else { *last_insert_id = 0; } resultSet->close(); return 0; } catch (SQLException &e) { if (resultSet) { resultSet->close(); } return setError(db, e); } }
int nuodb_resultset_close(struct nuodb *db, struct nuodb_resultset **rs) { try { if (rs && *rs) { ResultSet *resultSet = reinterpret_cast<ResultSet *>(*rs); resultSet->close(); *rs = 0; } return 0; } catch (SQLException &e) { return setError(db, e); } }
static int fetchExecuteResult(struct nuodb *db, Statement *stmt, int64_t *rows_affected, int64_t *last_insert_id) { ResultSet *resultSet = 0; try { resultSet = stmt->getGeneratedKeys(); // NuoDB uses -1 as a flag for zero-rows-affected *rows_affected = std::max(0, stmt->getUpdateCount()); if (*rows_affected > 0 && resultSet->getMetaData()->getColumnCount() > 0) { while (resultSet->next()) { // TODO find out how to read the last id first } *last_insert_id = resultSet->getLong(1); } else { *last_insert_id = 0; } resultSet->close(); return 0; } catch (SQLException &e) { if (resultSet) { resultSet->close(); } return setError(db, e); } }
int nuodb_statement_query(struct nuodb *db, struct nuodb_statement *st, struct nuodb_resultset **rs, int *column_count) { ResultSet *resultSet = 0; PreparedStatement *stmt = reinterpret_cast<PreparedStatement *>(st); try { bool hasResults = stmt->execute(); if (hasResults) { resultSet = stmt->getResultSet(); } else { resultSet = stmt->getGeneratedKeys(); } *column_count = resultSet->getMetaData()->getColumnCount(); *rs = reinterpret_cast<struct nuodb_resultset *>(resultSet); return 0; } catch (SQLException &e) { if (resultSet) { resultSet->close(); } return setError(db, e); } }
Collection *JDBCTemplate::getByCondition(std::string sql, object *qm) { Collection *col = std::vector(); Connection *conn = 0; try { //调用钩子方法 conn = this->getConnection(); //调用原语操作 sql = this->prepareQuerySql(sql, qm); PreparedStatement *pstmt = conn->prepareStatement(sql); //调用原语操作 this->setQuerySqlValue(pstmt, qm); ResultSet *rs = pstmt->executeQuery(); while(rs->next()) { //调用原语操作 col->add(this->rs2Object(rs)); } rs->close(); pstmt->close(); } catch(Exception *err) { err->printStackTrace(); } //JAVA TO C++ CONVERTER TODO TASK: There is no native C++ equivalent to the exception 'finally' clause: finally { try { conn->close(); } catch (SQLException *e) { e->printStackTrace(); } } return col; }
vector<vector< vector<SkeletonSequence> > > getKFoldsVector(Connection *con,list<SkeletonSequence> *validationData){ vector<vector< vector<SkeletonSequence> > > kFoldsVector(Num_Actions, vector< vector<SkeletonSequence> >( K_Fold)); ResultSet *resBehaviours; PreparedStatement *prep_stmt; for(int idAction=0;idAction<Num_Actions;idAction++){ vector<SkeletonSequence> result; prep_stmt = con -> prepareStatement("SELECT id, user FROM `Behaviour` " "WHERE name=? "); //begin get the resultset // prep_stmt->setInt(1, compressionRate); // prep_stmt->setInt(2, numClusters); prep_stmt->setString(1, listActions[idAction]); resBehaviours = prep_stmt -> executeQuery(); /*For each action we get the ids of the behaviours*/ while (resBehaviours->next()) { SkeletonSequence beh; beh.idAction=idAction; beh.idBehaviour=resBehaviours->getInt("id"); beh.idUser=resBehaviours->getInt("user"); addSkeletons(con, &beh); result.push_back(beh); } //split train and validation float trainRate=result.size()*((float)TrainingRate/100); int validRate=result.size()- (int)(trainRate); for(int j=0;j<validRate;j++){ int k = rand()%result.size(); vector<SkeletonSequence>::iterator it=result.begin(); it+=k; validationData->push_back(result[k]); result.erase(it); } //shuffle array int n = result.size(); while (n > 1) { // 0 <= k < n. int k = rand()%n; // n is now the last pertinent index; n--; // swap array[n] with array[k] SkeletonSequence temp = result[n]; result[n] = result[k]; result[k] = temp; } int k=0; for(int i=0;i<result.size();i++){ kFoldsVector[idAction][k%K_Fold].push_back(result[i]); k++; } resBehaviours->close(); prep_stmt->clearParameters(); } delete resBehaviours; delete prep_stmt; return kFoldsVector; }
vector<vector< vector<BehaviourSequence> > > getKFoldsVector(Connection *con, list<BehaviourSequence> *validationData){ vector<vector< vector<BehaviourSequence> > > kFoldsVector(Num_Actions, vector< vector<BehaviourSequence> >( K_Fold)); ResultSet *resBehaviours; PreparedStatement *prep_stmt; for(int idAction=0;idAction<Num_Actions;idAction++){ vector<BehaviourSequence> result; prep_stmt = con -> prepareStatement("SELECT b.id, b.user, bc.sequence FROM `Behaviour` b, `BehaviourClustered` bc " "WHERE `compression_rate`=? AND `num_clusters`=? AND b.id=bc.id AND b.name=?");// AND b.user=?"); //begin get the resultset prep_stmt->setInt(1, compressionRate); prep_stmt->setInt(2, numClusters); prep_stmt->setString(3, listActions[idAction]); // prep_stmt->setInt(4, actualIdUser); resBehaviours = prep_stmt -> executeQuery(); /*For each action we get the ids of the behaviours*/ while (resBehaviours->next()) { BehaviourSequence beh; beh.idAction=idAction; beh.idBehaviour=resBehaviours->getInt("id"); beh.idUser=resBehaviours->getInt("user"); beh.compressionRate=compressionRate; // beh.numClusters=numClusters; beh.sequence=getSequence(resBehaviours->getString("sequence")); addSkeletonsFromIdBehaviour(con,beh.idBehaviour, &(beh.skeletons)); setIdCluster2Skeletons(&beh); //we add it to the result list result.push_back(beh); } //split train and validation float trainRate=result.size()*((float)TrainingRate/100); int validRate=result.size()-abs(trainRate); for(int j=0;j<validRate;j++){ int k = rand()%result.size(); vector<BehaviourSequence>::iterator it=result.begin(); it+=k; validationData->push_back(result[k]); result.erase(it); } //shuffle array int n = result.size(); while (n > 1) { // 0 <= k < n. int k = rand()%n; // n is now the last pertinent index; n--; // swap array[n] with array[k] BehaviourSequence temp = result[n]; result[n] = result[k]; result[k] = temp; } int k=0; for(int i=0;i<result.size();i++){ kFoldsVector[idAction][k%K_Fold].push_back(result[i]); k++; } resBehaviours->close(); prep_stmt->clearParameters(); } delete resBehaviours; delete prep_stmt; return kFoldsVector; }