monotone

monotone Mtn Source Tree

Root/sqlite/copy.c

1/*
2** 2003 April 6
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
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.
10**
11*************************************************************************
12** This file contains code used to implement the COPY command.
13**
14** $Id: copy.c,v 1.1 2003/08/05 23:03:07 graydon Exp $
15*/
16#include "sqliteInt.h"
17
18/*
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
21** follows:
22**
23** COPY table FROM file [USING DELIMITERS string]
24**
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.
28*/
29void sqliteCopy(
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 */
35){
36 Table *pTab;
37 int i;
38 Vdbe *v;
39 int addr, end;
40 Index *pIdx;
41 char *zFile = 0;
42 const char *zDb;
43 sqlite *db = pParse->db;
44
45
46 if( sqlite_malloc_failed ) goto copy_cleanup;
47 assert( pTableName->nSrc==1 );
48 pTab = sqliteSrcListLookup(pParse, pTableName);
49 if( pTab==0 || sqliteIsReadOnly(pParse, pTab, 0) ) goto copy_cleanup;
50 zFile = sqliteStrNDup(pFilename->z, pFilename->n);
51 sqliteDequote(zFile);
52 assert( pTab->iDb<db->nDb );
53 zDb = db->aDb[pTab->iDb].zName;
54 if( sqliteAuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb)
55 || sqliteAuthCheck(pParse, SQLITE_COPY, pTab->zName, zFile, zDb) ){
56 goto copy_cleanup;
57 }
58 v = sqliteGetVdbe(pParse);
59 if( v ){
60 sqliteBeginWriteOperation(pParse, 1, pTab->iDb);
61 addr = sqliteVdbeAddOp(v, OP_FileOpen, 0, 0);
62 sqliteVdbeChangeP3(v, addr, pFilename->z, pFilename->n);
63 sqliteVdbeDequoteP3(v, addr);
64 sqliteVdbeAddOp(v, OP_Integer, pTab->iDb, 0);
65 sqliteVdbeAddOp(v, OP_OpenWrite, 0, pTab->tnum);
66 sqliteVdbeChangeP3(v, -1, pTab->zName, P3_STATIC);
67 for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
68 assert( pIdx->iDb==1 || pIdx->iDb==pTab->iDb );
69 sqliteVdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
70 sqliteVdbeAddOp(v, OP_OpenWrite, i, pIdx->tnum);
71 sqliteVdbeChangeP3(v, -1, pIdx->zName, P3_STATIC);
72 }
73 if( db->flags & SQLITE_CountRows ){
74 sqliteVdbeAddOp(v, OP_Integer, 0, 0); /* Initialize the row count */
75 }
76 end = sqliteVdbeMakeLabel(v);
77 addr = sqliteVdbeAddOp(v, OP_FileRead, pTab->nCol, end);
78 if( pDelimiter ){
79 sqliteVdbeChangeP3(v, addr, pDelimiter->z, pDelimiter->n);
80 sqliteVdbeDequoteP3(v, addr);
81 }else{
82 sqliteVdbeChangeP3(v, addr, "\t", 1);
83 }
84 if( pTab->iPKey>=0 ){
85 sqliteVdbeAddOp(v, OP_FileColumn, pTab->iPKey, 0);
86 sqliteVdbeAddOp(v, OP_MustBeInt, 0, 0);
87 }else{
88 sqliteVdbeAddOp(v, OP_NewRecno, 0, 0);
89 }
90 for(i=0; i<pTab->nCol; i++){
91 if( i==pTab->iPKey ){
92 /* The integer primary key column is filled with NULL since its
93 ** value is always pulled from the record number */
94 sqliteVdbeAddOp(v, OP_String, 0, 0);
95 }else{
96 sqliteVdbeAddOp(v, OP_FileColumn, i, 0);
97 }
98 }
99 sqliteGenerateConstraintChecks(pParse, pTab, 0, 0, pTab->iPKey>=0,
100 0, onError, addr);
101 sqliteCompleteInsertion(pParse, pTab, 0, 0, 0, 0, -1);
102 if( (db->flags & SQLITE_CountRows)!=0 ){
103 sqliteVdbeAddOp(v, OP_AddImm, 1, 0); /* Increment row count */
104 }
105 sqliteVdbeAddOp(v, OP_Goto, 0, addr);
106 sqliteVdbeResolveLabel(v, end);
107 sqliteVdbeAddOp(v, OP_Noop, 0, 0);
108 sqliteEndWriteOperation(pParse);
109 if( db->flags & SQLITE_CountRows ){
110 sqliteVdbeAddOp(v, OP_ColumnName, 0, 0);
111 sqliteVdbeChangeP3(v, -1, "rows inserted", P3_STATIC);
112 sqliteVdbeAddOp(v, OP_Callback, 1, 0);
113 }
114 }
115
116copy_cleanup:
117 sqliteSrcListDelete(pTableName);
118 sqliteFree(zFile);
119 return;
120}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status