1*c5c4113dSnw141292 /*
2*c5c4113dSnw141292 ** 2003 April 6
3*c5c4113dSnw141292 **
4*c5c4113dSnw141292 ** The author disclaims copyright to this source code. In place of
5*c5c4113dSnw141292 ** a legal notice, here is a blessing:
6*c5c4113dSnw141292 **
7*c5c4113dSnw141292 ** May you do good and not evil.
8*c5c4113dSnw141292 ** May you find forgiveness for yourself and forgive others.
9*c5c4113dSnw141292 ** May you share freely, never taking more than you give.
10*c5c4113dSnw141292 **
11*c5c4113dSnw141292 *************************************************************************
12*c5c4113dSnw141292 ** This file contains code used to implement the COPY command.
13*c5c4113dSnw141292 **
14*c5c4113dSnw141292 ** $Id: copy.c,v 1.9 2004/02/25 13:47:31 drh Exp $
15*c5c4113dSnw141292 */
16*c5c4113dSnw141292 #include "sqliteInt.h"
17*c5c4113dSnw141292
18*c5c4113dSnw141292 /*
19*c5c4113dSnw141292 ** The COPY command is for compatibility with PostgreSQL and specificially
20*c5c4113dSnw141292 ** for the ability to read the output of pg_dump. The format is as
21*c5c4113dSnw141292 ** follows:
22*c5c4113dSnw141292 **
23*c5c4113dSnw141292 ** COPY table FROM file [USING DELIMITERS string]
24*c5c4113dSnw141292 **
25*c5c4113dSnw141292 ** "table" is an existing table name. We will read lines of code from
26*c5c4113dSnw141292 ** file to fill this table with data. File might be "stdin". The optional
27*c5c4113dSnw141292 ** delimiter string identifies the field separators. The default is a tab.
28*c5c4113dSnw141292 */
sqliteCopy(Parse * pParse,SrcList * pTableName,Token * pFilename,Token * pDelimiter,int onError)29*c5c4113dSnw141292 void sqliteCopy(
30*c5c4113dSnw141292 Parse *pParse, /* The parser context */
31*c5c4113dSnw141292 SrcList *pTableName, /* The name of the table into which we will insert */
32*c5c4113dSnw141292 Token *pFilename, /* The file from which to obtain information */
33*c5c4113dSnw141292 Token *pDelimiter, /* Use this as the field delimiter */
34*c5c4113dSnw141292 int onError /* What to do if a constraint fails */
35*c5c4113dSnw141292 ){
36*c5c4113dSnw141292 Table *pTab;
37*c5c4113dSnw141292 int i;
38*c5c4113dSnw141292 Vdbe *v;
39*c5c4113dSnw141292 int addr, end;
40*c5c4113dSnw141292 char *zFile = 0;
41*c5c4113dSnw141292 const char *zDb;
42*c5c4113dSnw141292 sqlite *db = pParse->db;
43*c5c4113dSnw141292
44*c5c4113dSnw141292
45*c5c4113dSnw141292 if( sqlite_malloc_failed ) goto copy_cleanup;
46*c5c4113dSnw141292 assert( pTableName->nSrc==1 );
47*c5c4113dSnw141292 pTab = sqliteSrcListLookup(pParse, pTableName);
48*c5c4113dSnw141292 if( pTab==0 || sqliteIsReadOnly(pParse, pTab, 0) ) goto copy_cleanup;
49*c5c4113dSnw141292 zFile = sqliteStrNDup(pFilename->z, pFilename->n);
50*c5c4113dSnw141292 sqliteDequote(zFile);
51*c5c4113dSnw141292 assert( pTab->iDb<db->nDb );
52*c5c4113dSnw141292 zDb = db->aDb[pTab->iDb].zName;
53*c5c4113dSnw141292 if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb)
54*c5c4113dSnw141292 || sqliteAuthCheck(pParse, SQLITE_COPY, pTab->zName, zFile, zDb) ){
55*c5c4113dSnw141292 goto copy_cleanup;
56*c5c4113dSnw141292 }
57*c5c4113dSnw141292 v = sqliteGetVdbe(pParse);
58*c5c4113dSnw141292 if( v ){
59*c5c4113dSnw141292 sqliteBeginWriteOperation(pParse, 1, pTab->iDb);
60*c5c4113dSnw141292 addr = sqliteVdbeOp3(v, OP_FileOpen, 0, 0, pFilename->z, pFilename->n);
61*c5c4113dSnw141292 sqliteVdbeDequoteP3(v, addr);
62*c5c4113dSnw141292 sqliteOpenTableAndIndices(pParse, pTab, 0);
63*c5c4113dSnw141292 if( db->flags & SQLITE_CountRows ){
64*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
65*c5c4113dSnw141292 }
66*c5c4113dSnw141292 end = sqliteVdbeMakeLabel(v);
67*c5c4113dSnw141292 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
68*c5c4113dSnw141292 if( pDelimiter ){
69*c5c4113dSnw141292 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
70*c5c4113dSnw141292 sqliteVdbeDequoteP3(v, addr);
71*c5c4113dSnw141292 }else{
72*c5c4113dSnw141292 sqliteVdbeChangeP3(v, addr, "\t", 1);
73*c5c4113dSnw141292 }
74*c5c4113dSnw141292 if( pTab->iPKey>=0 ){
75*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
76*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
77*c5c4113dSnw141292 }else{
78*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
79*c5c4113dSnw141292 }
80*c5c4113dSnw141292 for(i=0; i<pTab->nCol; i++){
81*c5c4113dSnw141292 if( i==pTab->iPKey ){
82*c5c4113dSnw141292 /* The integer primary key column is filled with NULL since its
83*c5c4113dSnw141292 ** value is always pulled from the record number */
84*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_String, 0, 0);
85*c5c4113dSnw141292 }else{
86*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
87*c5c4113dSnw141292 }
88*c5c4113dSnw141292 }
89*c5c4113dSnw141292 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, pTab->iPKey>=0,
90*c5c4113dSnw141292 0, onError, addr);
91*c5c4113dSnw141292 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0, -1);
92*c5c4113dSnw141292 if( (db->flags & SQLITE_CountRows)!=0 ){
93*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
94*c5c4113dSnw141292 }
95*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
96*c5c4113dSnw141292 sqliteVdbeResolveLabel(v, end);
97*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
98*c5c4113dSnw141292 sqliteEndWriteOperation(pParse);
99*c5c4113dSnw141292 if( db->flags & SQLITE_CountRows ){
100*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_ColumnName, 0, 1);
101*c5c4113dSnw141292 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
102*c5c4113dSnw141292 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
103*c5c4113dSnw141292 }
104*c5c4113dSnw141292 }
105*c5c4113dSnw141292
106*c5c4113dSnw141292 copy_cleanup:
107*c5c4113dSnw141292 sqliteSrcListDelete(pTableName);
108*c5c4113dSnw141292 sqliteFree(zFile);
109*c5c4113dSnw141292 return;
110*c5c4113dSnw141292 }
111