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