[转帖]游标fetch源码分析_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2062 | 回复: 0   主题: [转帖]游标fetch源码分析        下一篇 
kim
注册用户
等级:中校
经验:1729
发帖:222
精华:0
注册:2011-7-21
状态:离线
发送短消息息给kim 加好友    发送短消息息给kim 发消息
发表于: IP:您无权察看 2015-1-6 9:58:35 | [全部帖] [楼主帖] 楼主

    嵌入式sql采用游标编程,一般有几步曲

1、EXEC SQL PREPARE sSql FROM :sql ;

2、EXEC SQL DECLARE cSql CURSOR FOR sSql ; 一起jquery,17jquery

3、EXEC SQL OPEN cSql ;

4、EXEC SQL FETCH cSql INTO ...

while(sqlca.sqlcode....){ //判断结束条件
      EXEC SQL FETCH cSql INTO ...
}


那mysql中fetch是如何实现的呢,看一下fetch的主要流程 内容来自17jquery

先从sql_parse.cc中dispatch_command

case COM_STMT_FETCH: 一起jquery,17jquery

{ 17jquery.com
mysql_stmt_fetch(thd, packet, packet_length);
break;
}
sql_prepare.cc 17jquery.com
void mysql_stmt_fetch(THD *thd, char *packet, uint packet_length)
{
      ....
      if (!(stmt= find_prepared_statement(thd, stmt_id)))--先看执行了prepare没有
      {
            char llbuf[22];
            my_error(ER_UNKNOWN_STMT_HANDLER, MYF(0), sizeof(llbuf),
            llstr(stmt_id, llbuf), "mysql_stmt_fetch");
            DBUG_VOID_RETURN;
      } 17jquery.com
      .....
      cursor= stmt->cursor; --是否declare游标 一起jquery,17jquery
      if (!cursor)
      {


my_error(ER_STMT_HAS_NO_OPEN_CURSOR, MYF(0), stmt_id); 内容来自17jquery

DBUG_VOID_RETURN;
} 17jquery.com
cursor->fetch(num_rows); --执行具体的fetch操作
if (!cursor->is_open())--检查游标是否还打开


{ 一起jquery,17jquery

stmt->close_cursor();
thd->cursor= 0;
reset_stmt_params(stmt);
}
thd->restore_backup_statement(stmt, &stmt_backup);
thd->stmt_arena= thd;
DBUG_VOID_RETURN; 17jquery.com
}


cursor->fetch(num_rows)有好几种实现,找个简单的分析一下

sql_cursor.cc
void Materialized_cursor::fetch(ulong num_rows)
{


THD *thd= table->in_use; 内容来自17jquery

int res= 0;
result->begin_dataset();
for (fetch_limit+= num_rows; fetch_count < fetch_limit; fetch_count++)


{ 一起jquery,17jquery

if ((res= table->file->rnd_next(table->record[0]))) 17jquery.com
break;
/* Send data only if the read was successful. */
result->send_data(item_list); //直接从table里面读取,从临时表中读取
}
switch (res) { //根据不同结果,回复客户端不同信息
      case 0:
      thd->server_status|= SERVER_STATUS_CURSOR_EXISTS;
      result->send_eof();
      thd->server_status&= ~SERVER_STATUS_CURSOR_EXISTS;
      break;
      case HA_ERR_END_OF_FILE:
      thd->server_status|= SERVER_STATUS_LAST_ROW_SENT;
      result->send_eof(); 17jquery.com
      thd->server_status&= ~SERVER_STATUS_LAST_ROW_SENT; 17jquery.com
      close();
      break;
      default:
      table->file->print_error(res, MYF(0));
      close();
      break;
}
}


上面Materialized_cursor::fetch不满足事务隔离级别-游标稳定性,有时间看看

Sensitive_cursor::fetch能否满足事务隔离级别的要求

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论