⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 build.c

📁 最新的sqlite3.6.2源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
    pParse->rc = SQLITE_NOMEM;    pParse->nErr++;    goto begin_table_error;  }  pTable->zName = zName;  pTable->iPKey = -1;  pTable->pSchema = db->aDb[iDb].pSchema;  pTable->nRef = 1;  pTable->db = db;  if( pParse->pNewTable ) sqlite3DeleteTable(pParse->pNewTable);  pParse->pNewTable = pTable;  /* If this is the magic sqlite_sequence table used by autoincrement,  ** then record a pointer to this table in the main database structure  ** so that INSERT can find the table easily.  */#ifndef SQLITE_OMIT_AUTOINCREMENT  if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){    pTable->pSchema->pSeqTab = pTable;  }#endif  /* Begin generating the code that will insert the table record into  ** the SQLITE_MASTER table.  Note in particular that we must go ahead  ** and allocate the record number for the table entry now.  Before any  ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause  ** indices to be created and the table record must come before the   ** indices.  Hence, the record number for the table must be allocated  ** now.  */  if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){    int j1;    int fileFormat;    int reg1, reg2, reg3;    sqlite3BeginWriteOperation(pParse, 0, iDb);#ifndef SQLITE_OMIT_VIRTUALTABLE    if( isVirtual ){      sqlite3VdbeAddOp0(v, OP_VBegin);    }#endif    /* If the file format and encoding in the database have not been set,     ** set them now.    */    reg1 = pParse->regRowid = ++pParse->nMem;    reg2 = pParse->regRoot = ++pParse->nMem;    reg3 = ++pParse->nMem;    sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, 1);   /* file_format */    sqlite3VdbeUsesBtree(v, iDb);    j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);    fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?                  1 : SQLITE_MAX_FILE_FORMAT;    sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 1, reg3);    sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);    sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, 4, reg3);    sqlite3VdbeJumpHere(v, j1);    /* This just creates a place-holder record in the sqlite_master table.    ** The record created does not contain anything yet.  It will be replaced    ** by the real entry in code generated at sqlite3EndTable().    **    ** The rowid for the new entry is left on the top of the stack.    ** The rowid value is needed by the code that sqlite3EndTable will    ** generate.    */#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)    if( isView || isVirtual ){      sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);    }else#endif    {      sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);    }    sqlite3OpenMasterTable(pParse, iDb);    sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);    sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);    sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);    sqlite3VdbeAddOp0(v, OP_Close);  }  /* Normal (non-error) return. */  return;  /* If an error occurs, we jump here */begin_table_error:  sqlite3DbFree(db, zName);  return;}/*** This macro is used to compare two strings in a case-insensitive manner.** It is slightly faster than calling sqlite3StrICmp() directly, but** produces larger code.**** WARNING: This macro is not compatible with the strcmp() family. It** returns true if the two strings are equal, otherwise false.*/#define STRICMP(x, y) (\sqlite3UpperToLower[*(unsigned char *)(x)]==   \sqlite3UpperToLower[*(unsigned char *)(y)]     \&& sqlite3StrICmp((x)+1,(y)+1)==0 )/*** Add a new column to the table currently being constructed.**** The parser calls this routine once for each column declaration** in a CREATE TABLE statement.  sqlite3StartTable() gets called** first to get things going.  Then this routine is called for each** column.*/void sqlite3AddColumn(Parse *pParse, Token *pName){  Table *p;  int i;  char *z;  Column *pCol;  sqlite3 *db = pParse->db;  if( (p = pParse->pNewTable)==0 ) return;#if SQLITE_MAX_COLUMN  if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){    sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);    return;  }#endif  z = sqlite3NameFromToken(pParse->db, pName);  if( z==0 ) return;  for(i=0; i<p->nCol; i++){    if( STRICMP(z, p->aCol[i].zName) ){      sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);      sqlite3DbFree(db, z);      return;    }  }  if( (p->nCol & 0x7)==0 ){    Column *aNew;    aNew = sqlite3DbRealloc(pParse->db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));    if( aNew==0 ){      sqlite3DbFree(db, z);      return;    }    p->aCol = aNew;  }  pCol = &p->aCol[p->nCol];  memset(pCol, 0, sizeof(p->aCol[0]));  pCol->zName = z;   /* If there is no type specified, columns have the default affinity  ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will  ** be called next to set pCol->affinity correctly.  */  pCol->affinity = SQLITE_AFF_NONE;  p->nCol++;}/*** This routine is called by the parser while in the middle of** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has** been seen on a column.  This routine sets the notNull flag on** the column currently under construction.*/void sqlite3AddNotNull(Parse *pParse, int onError){  Table *p;  int i;  if( (p = pParse->pNewTable)==0 ) return;  i = p->nCol-1;  if( i>=0 ) p->aCol[i].notNull = onError;}/*** Scan the column type name zType (length nType) and return the** associated affinity type.**** This routine does a case-independent search of zType for the ** substrings in the following table. If one of the substrings is** found, the corresponding affinity is returned. If zType contains** more than one of the substrings, entries toward the top of ** the table take priority. For example, if zType is 'BLOBINT', ** SQLITE_AFF_INTEGER is returned.**** Substring     | Affinity** --------------------------------** 'INT'         | SQLITE_AFF_INTEGER** 'CHAR'        | SQLITE_AFF_TEXT** 'CLOB'        | SQLITE_AFF_TEXT** 'TEXT'        | SQLITE_AFF_TEXT** 'BLOB'        | SQLITE_AFF_NONE** 'REAL'        | SQLITE_AFF_REAL** 'FLOA'        | SQLITE_AFF_REAL** 'DOUB'        | SQLITE_AFF_REAL**** If none of the substrings in the above table are found,** SQLITE_AFF_NUMERIC is returned.*/char sqlite3AffinityType(const Token *pType){  u32 h = 0;  char aff = SQLITE_AFF_NUMERIC;  const unsigned char *zIn = pType->z;  const unsigned char *zEnd = &pType->z[pType->n];  while( zIn!=zEnd ){    h = (h<<8) + sqlite3UpperToLower[*zIn];    zIn++;    if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */      aff = SQLITE_AFF_TEXT;     }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */      aff = SQLITE_AFF_TEXT;    }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */      aff = SQLITE_AFF_TEXT;    }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */        && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){      aff = SQLITE_AFF_NONE;#ifndef SQLITE_OMIT_FLOATING_POINT    }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */        && aff==SQLITE_AFF_NUMERIC ){      aff = SQLITE_AFF_REAL;    }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */        && aff==SQLITE_AFF_NUMERIC ){      aff = SQLITE_AFF_REAL;    }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */        && aff==SQLITE_AFF_NUMERIC ){      aff = SQLITE_AFF_REAL;#endif    }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */      aff = SQLITE_AFF_INTEGER;      break;    }  }  return aff;}/*** This routine is called by the parser while in the middle of** parsing a CREATE TABLE statement.  The pFirst token is the first** token in the sequence of tokens that describe the type of the** column currently under construction.   pLast is the last token** in the sequence.  Use this information to construct a string** that contains the typename of the column and store that string** in zType.*/ void sqlite3AddColumnType(Parse *pParse, Token *pType){  Table *p;  int i;  Column *pCol;  sqlite3 *db;  if( (p = pParse->pNewTable)==0 ) return;  i = p->nCol-1;  if( i<0 ) return;  pCol = &p->aCol[i];  db = pParse->db;  sqlite3DbFree(db, pCol->zType);  pCol->zType = sqlite3NameFromToken(db, pType);  pCol->affinity = sqlite3AffinityType(pType);}/*** The expression is the default value for the most recently added column** of the table currently under construction.**** Default value expressions must be constant.  Raise an exception if this** is not the case.**** This routine is called by the parser while in the middle of** parsing a CREATE TABLE statement.*/void sqlite3AddDefaultValue(Parse *pParse, Expr *pExpr){  Table *p;  Column *pCol;  sqlite3 *db = pParse->db;  if( (p = pParse->pNewTable)!=0 ){    pCol = &(p->aCol[p->nCol-1]);    if( !sqlite3ExprIsConstantOrFunction(pExpr) ){      sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",          pCol->zName);    }else{      Expr *pCopy;      sqlite3ExprDelete(db, pCol->pDflt);      pCol->pDflt = pCopy = sqlite3ExprDup(db, pExpr);      if( pCopy ){        sqlite3TokenCopy(db, &pCopy->span, &pExpr->span);      }    }  }  sqlite3ExprDelete(db, pExpr);}/*** Designate the PRIMARY KEY for the table.  pList is a list of names ** of columns that form the primary key.  If pList is NULL, then the** most recently added column of the table is the primary key.**** A table can have at most one primary key.  If the table already has** a primary key (and this is the second primary key) then create an** error.**** If the PRIMARY KEY is on a single column whose datatype is INTEGER,** then we will try to use that column as the rowid.  Set the Table.iPKey** field of the table under construction to be the index of the** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is** no INTEGER PRIMARY KEY.**** If the key is not an INTEGER PRIMARY KEY, then create a unique** index for the key.  No index is created for INTEGER PRIMARY KEYs.*/void sqlite3AddPrimaryKey(  Parse *pParse,    /* Parsing context */  ExprList *pList,  /* List of field names to be indexed */  int onError,      /* What to do with a uniqueness conflict */  int autoInc,      /* True if the AUTOINCREMENT keyword is present */  int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */){  Table *pTab = pParse->pNewTable;  char *zType = 0;  int iCol = -1, i;  if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;  if( pTab->tabFlags & TF_HasPrimaryKey ){    sqlite3ErrorMsg(pParse,       "table \"%s\" has more than one primary key", pTab->zName);    goto primary_key_exit;  }  pTab->tabFlags |= TF_HasPrimaryKey;  if( pList==0 ){    iCol = pTab->nCol - 1;    pTab->aCol[iCol].isPrimKey = 1;  }else{    for(i=0; i<pList->nExpr; i++){      for(iCol=0; iCol<pTab->nCol; iCol++){        if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){          break;        }      }      if( iCol<pTab->nCol ){        pTab->aCol[iCol].isPrimKey = 1;      }    }    if( pList->nExpr>1 ) iCol = -1;  }  if( iCol>=0 && iCol<pTab->nCol ){    zType = pTab->aCol[iCol].zType;  }  if( zType && sqlite3StrICmp(zType, "INTEGER")==0        && sortOrder==SQLITE_SO_ASC ){    pTab->iPKey = iCol;    pTab->keyConf = onError;    assert( autoInc==0 || autoInc==1 );    pTab->tabFlags |= autoInc*TF_Autoincrement;  }else if( autoInc ){#ifndef SQLITE_OMIT_AUTOINCREMENT    sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "       "INTEGER PRIMARY KEY");#endif  }else{    sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0);    pList = 0;  }primary_key_exit:  sqlite3ExprListDelete(pParse->db, pList);  return;}/*** Add a new CHECK constraint to the table currently under construction.*/void sqlite3AddCheckConstraint(  Parse *pParse,    /* Parsing context */  Expr *pCheckExpr  /* The check expression */){  sqlite3 *db = pParse->db;#ifndef SQLITE_OMIT_CHECK  Table *pTab = pParse->pNewTable;  if( pTab && !IN_DECLARE_VTAB ){    /* The CHECK expression must be duplicated so that tokens refer    ** to malloced space and not the (ephemeral) text of the CREATE TABLE    ** statement */    pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck,                                   sqlite3ExprDup(db, pCheckExpr));  }#endif  sqlite3ExprDelete(db, pCheckExpr);}/*** Set the collation function of the most recently parsed table column** to the CollSeq given.*/void sqlite3AddCollateType(Parse *pParse, Token *pToken){  Table *p;  int i;  char *zColl;              /* Dequoted name of collation sequence */  sqlite3 *db;  if( (p = pParse->pNewTable)==0 ) return;  i = p->nCol-1;  db = pParse->db;  zColl = sqlite3NameFromToken(db, pToken);  if( !zColl ) return;  if( sqlite3LocateCollSeq(pParse, zColl, -1) ){    Index *pIdx;    p->aCol[i].zColl = zColl;      /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",    ** then an index may have been created on this column before the    ** collation type was added. Correct this if it is the case.    */    for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){      assert( pIdx->nColumn==1 );      if( pIdx->aiColumn[0]==i ){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -