SQLite源代码分析----------代码生成器③

2021SC@SDUSC

OP_Goto

OP_Goto的内容:

case OP_Goto: {
    
    
    pc = pOp->p2 - 1;
}

OP_Goto操作符就是跳转到p2所指向的操作码,这里是pc为0,经过for循环后pc为1。下一个要执行的是 aOp[1] OP_ReadCookie。

OP_ReadCookie

OP_ReadCookie的内容:

case OP_ReadCookie: {
    
    
    int iMeta;
    int iDb = pOp->p1;
     int iCookie = pOp->p3;
sqlite3BtreeGetMeta(db->aDb[iDb].pBt,iCookie,(u32*)&iMeta);
}

sqlite3BtreeGetMeta函数是根据p3的值从第iDb号数据库中读取相应的元信息,这里p3为2,所以读取的是数据库格式信息。

OP_If

OP_If的内容:

case OP_If: {
    
    
    int c;
      pIn1 = &aMem[pOp->p1];
    c = sqlite3VdbeRealValue(pIn1)!=0.0;//c为1
     if( c ){
    
    
            pc = pOp->p2-1;
      }
}

OP_If操作符在&aMem[pOp->p1]不为0的情况下会返回p2的值给pc,让下一个操作符由p2决定,但是由于 &aMem[pOp->p1]的值为0,经过sqlite3VdbeRealValue函数转化为浮点数后还是0.0,所以if判断为假不执行。pc在for循环后加1,为3,下一个要执行的是aOp[3] OP_Integer。

OP_Integer

OP_Integer的内容:

 case OP_Integer: {
    
             /* out2-prerelease */
  pOut->u.i = pOp->p1;
  break;
}

和上面一样,OP_Integer操作符就是把p1的值赋值给pOut,也就是p2。pc加1为6,下一个要执行的是aOp[6] OP_SetCookie。

OP_SetCookie

OP_SetCookie的内容:

case OP_SetCookie : {
    
    
    Db *pDb = &db->aDb[pOp->p1];
    pIn3 = &aMem[pOp->p3];
    sqlite3VdbeMemIntegerify(pIn3);
    sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
    pDb->pSchema->schema_cookie = (int)pIn3->u.i;
}

OP_SetCookie操作符会根据p1值确定数据库,p1为0说明是对main数据库进行操作,根据p2值确定缓存位,这里p2为5,所以系统建议的页缓存大小为5,经过sqlite3BtreeUpdateMeta函数操作,p3里的内容就写入了main数据库中的页缓存的内存中。pc加1为7,下一个要执行aOp[7]OP_CreateTable。

OP_Integer

OP_Integer的内容:

 case OP_Integer: {
    
             /* out2-prerelease */
  pOut->u.i = pOp->p1;
  break;
}

和上面一样,OP_Integer操作符就是把p1的值赋值给pOut,也就是p2。pc加1为6,下一个要执行的是aOp[6] OP_SetCookie。

OP_CreateTable

OP_CreateTable的内容:

case OP_CreateTable: {
    
    
    int pgno=0,
    int flags = BTREE_INTKEY;//1
    Db* pDb = &db->aDb[pOp->p1];
      sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
    pOut->u.i = pgno;//pgno是新表的根页号
}

OP_CreateTable操作符根据p1的值0确定是在main数据库中添加新表,sqlite3BtreeCreateTable函数就 是在后端(BTree和Pager)进行表的创建,并返回新表所在的根页号给p2。pc加1为8,下一个要执行的是aOp[8] OP_OpenWrite。

OP_OpenWrite

OP_OpenWrite的内容:

case OP_OpenWrite : {
    
    
    int nField = pOp->p4.i;
      int p2 = pOp->p2, iDb = pOp->p3; ;
    Db pDb = &db->aDb[iDb];
      Btree* pX = pDb->pBt;
    int wrFlag = 1;
    VdbeCursor* pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
    pCur->pgnoRoot = p2;
    sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
    pCur->isTable = pOp->p4type!=P4_KEYINFO;
}

在OP_OpenWrite操作符中,allocateCursor函数根据p3的值确定操作的是main数据库,根据p4确定分配 的字段为5,根据p1的值确定这是游标的索引,为VDBE实例v创建一个指向BTree游标,然后sqlite3BtreeCursor函数根据p2确定刚刚创建的表创建一个写游标,这个游标和为VDBE实例创建的游标是有联系的。pc加1为9,下一个要执行的是aOp[9] OP_NewRowid。

附源代码

vdbe.c

Guess you like

Origin blog.csdn.net/wy_csdn_sdu/article/details/121894611