📄 insert.c
字号:
#include "eDbInit.h"
void eDbInsert(
Parser *pParse, /* Parser context */
SrcList *pTabList, /* Name of table into which we are inserting */
ExprList *pList, /* List of values to be inserted */
Select *pSelect, /* A SELECT statement to use as the data source */
IdList *pColumn, /* Column names corresponding to IDLIST. */
int onError /* How to handle constraint errors */
){
Table *pTab; /* The table to insert into */
char *zTab; /* Name of the table into which we are inserting */
int i, j; /* Loop counters */
int nColumn; /* Number of columns in the data */
eDb *db; /* The main database structure */
int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */
int isView; /* True if attempting to insert into a view */
ParseResult *pRes = eDbMalloc(sizeof(ParseResult));
if( pParse->nErr || eDb_malloc_failed ) goto insert_cleanup;
db = pParse->db;
/* Locate the table into which we will be inserting new information.
*/
zTab = pTabList->a[0].zName;
if( zTab==0 ) goto insert_cleanup;
pTab = eDbSrcListLookup(pParse, pTabList);
if( pTab==0 ){
goto insert_cleanup;
}
isView = pTab->pSelect!=0;
nColumn = pList->nExpr;
for(i=0; i<nColumn; i++){
if( eDbExprResolveIds(pParse, 0, 0, pList->a[i].pExpr) ){
goto insert_cleanup;
}
if( eDbExprCheck(pParse, pList->a[i].pExpr, 0, 0) ){
goto insert_cleanup;
}
}
/* Make sure the number of columns in the source data matches the number
** of columns to be inserted into the table.
*/
if( pColumn==0 && nColumn!=pTab->nCol ){
eDbErrorMsg(pParse,
"table %s has %d columns but %d values were supplied",
pTab->zName, 0, pTab->nCol, nColumn);
goto insert_cleanup;
}
if( pColumn!=0 && nColumn!=pColumn->nId ){
eDbErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
goto insert_cleanup;
}
/* If the INSERT statement included an IDLIST term, then make sure
** all elements of the IDLIST really are columns of the table and
** remember the column indices.
*/
if( pColumn ){
for(i=0; i<pColumn->nId; i++){
pColumn->a[i].idx = -1;
}
for(i=0; i<pColumn->nId; i++){
/*find the column name in table and set its indices */
for(j=0; j<pTab->nCol; j++){
if( eDbStrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
pColumn->a[i].idx = j;
if( j==pTab->iPKey ){
keyColumn = i;
}
break;
}
}
if( j>=pTab->nCol ){
eDbErrorMsg(pParse, "table %s has no column named %s",
pTab->zName, 0, pColumn->a[i].zName);
pParse->nErr++;
goto insert_cleanup;
}
} /* end for(i=0; i<pColumn->nId; i++)*/
}/* end if(pColumn)*/
/* If there is no IDLIST term but the table has an integer primary
** key, the set the keyColumn variable to the primary key column index
** in the original table definition.
*/
if( pColumn==0 ){
keyColumn = pTab->iPKey;
}
pRes->pTable = pTab;
pRes->pIdList = pColumn;
pRes->pEList = pList;
pRes->op = Op_Insert;
pParse->pResult = pRes;
eDbSrcListDelete(pTabList);
return;
insert_cleanup:
eDbSrcListDelete(pTabList);
if( pList ) eDbExprListDelete(pList);
if( pSelect ) eDbSelectDelete(pSelect);
eDbIdListDelete(pColumn);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -