/ Check-in [ac428c8d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fail an ATTACH if the auxiliary database is locked. Ticket #514. (CVS 1127)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:ac428c8d4a731678cc26cf198689814a8a56d141
User & Date: drh 2003-12-06 22:22:36
Context
2003-12-07
00:24
Make the VACUUM command run out of the VDBE like all other commands. (Ticket #464). Make the VACUUM command work even if there are VIEWs in the SQLITE_MASTER table that come before tables they reference. (Ticket #515) (CVS 1128) check-in: 614cbbaf user: drh tags: trunk
2003-12-06
22:22
Fail an ATTACH if the auxiliary database is locked. Ticket #514. (CVS 1127) check-in: ac428c8d user: drh tags: trunk
21:43
Always use "(char*)0" to terminate the argument list of sqliteSetString(). This is needed for 64-bit systems that use a 32-bit integer by default. (CVS 1126) check-in: 656c9038 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/attach.c.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..
87
88
89
90
91
92
93

94

95






96
97
98
99
100
101
102
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code used to implement the ATTACH and DETACH commands.
**
** $Id: attach.c,v 1.7 2003/06/14 12:04:08 drh Exp $
*/
#include "sqliteInt.h"

/*
** This routine is called by the parser to process an ATTACH statement:
**
**     ATTACH DATABASE filename AS dbname
................................................................................
  rc = sqliteBtreeFactory(db, zFile, 0, MAX_PAGES, &aNew->pBt);
  if( rc ){
    sqliteErrorMsg(pParse, "unable to open database: %s", zFile);
  }
  sqliteFree(zFile);
  db->flags &= ~SQLITE_Initialized;
  if( pParse->nErr ) return;

  rc = sqliteInit(pParse->db, &pParse->zErrMsg);

  if( rc ){






    sqliteResetInternalSchema(db, 0);
    pParse->nErr++;
    pParse->rc = SQLITE_ERROR;
  }
}

/*







|







 







>
|
>

>
>
>
>
>
>







7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains code used to implement the ATTACH and DETACH commands.
**
** $Id: attach.c,v 1.8 2003/12/06 22:22:36 drh Exp $
*/
#include "sqliteInt.h"

/*
** This routine is called by the parser to process an ATTACH statement:
**
**     ATTACH DATABASE filename AS dbname
................................................................................
  rc = sqliteBtreeFactory(db, zFile, 0, MAX_PAGES, &aNew->pBt);
  if( rc ){
    sqliteErrorMsg(pParse, "unable to open database: %s", zFile);
  }
  sqliteFree(zFile);
  db->flags &= ~SQLITE_Initialized;
  if( pParse->nErr ) return;
  if( rc==SQLITE_OK ){
    rc = sqliteInit(pParse->db, &pParse->zErrMsg);
  }
  if( rc ){
    int i = db->nDb - 1;
    assert( i>=2 );
    if( db->aDb[i].pBt ){
      sqliteBtreeClose(db->aDb[i].pBt);
      db->aDb[i].pBt = 0;
    }
    sqliteResetInternalSchema(db, 0);
    pParse->nErr++;
    pParse->rc = SQLITE_ERROR;
  }
}

/*

Changes to test/attach2.test.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
23
24
25
26
27
28
29

30
31
32
33
34
35
36
37
38
39
















































































40
41
42
43
44
45
46
47
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is testing the ATTACH and DETACH commands
# and related functionality.
#
# $Id: attach2.test,v 1.2 2003/07/18 01:25:35 drh Exp $
#


set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Ticket #354
................................................................................
#
do_test attach2-1.1 {
  db eval {
    CREATE TABLE t1(a,b);
    CREATE INDEX x1 ON t1(a);
  }
  file delete -force test2.db

  sqlite db2 test2.db
  db2 eval {
    CREATE TABLE t1(a,b);
    CREATE INDEX x1 ON t1(a);
  }
  catchsql {
    ATTACH 'test2.db' AS t2;
  }
} {0 {}}

















































































db close
for {set i 2} {$i<=15} {incr i} {
  catch {db$i close}
}
file delete -force test2.db


finish_test







|







 







>










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is testing the ATTACH and DETACH commands
# and related functionality.
#
# $Id: attach2.test,v 1.3 2003/12/06 22:22:37 drh Exp $
#


set testdir [file dirname $argv0]
source $testdir/tester.tcl

# Ticket #354
................................................................................
#
do_test attach2-1.1 {
  db eval {
    CREATE TABLE t1(a,b);
    CREATE INDEX x1 ON t1(a);
  }
  file delete -force test2.db
  file delete -force test2.db-journal
  sqlite db2 test2.db
  db2 eval {
    CREATE TABLE t1(a,b);
    CREATE INDEX x1 ON t1(a);
  }
  catchsql {
    ATTACH 'test2.db' AS t2;
  }
} {0 {}}

# Ticket #514
#
proc db_list {db} {
  set list {}
  foreach {idx name file} [execsql {PRAGMA database_list} $db] {
    lappend list $idx $name
  }
  return $list
}
db eval {DETACH t2}
do_test attach2-2.1 {
  # lock test2.db then try to attach it.  Should get an error.
  db2 eval {BEGIN}
  catchsql {
    ATTACH 'test2.db' AS t2;
  }
} {1 {database is locked}}
do_test attach2-2.2 {
  # make sure test2.db did not get attached.
  db_list db
} {0 main 1 temp}
do_test attach2-2.3 {
  # unlock test2.db and try to attach again.  should work this time.
  db2 eval {COMMIT}
  catchsql {
    ATTACH 'test2.db' AS t2;
  }
} {0 {}}
do_test attach2-2.4 {
  db_list db
} {0 main 1 temp 2 t2}
do_test attach2-2.5 {
  catchsql {
    SELECT name FROM t2.sqlite_master;
  }
} {0 {t1 x1}}
do_test attach2-2.6 {
  # lock test2.db and try to read from it.  should get an error.
  db2 eval BEGIN
  catchsql {
    SELECT name FROM t2.sqlite_master;
  }
} {1 {database is locked}}
do_test attach2-2.7 {
  # but we can still read from test1.db even though test2.db is locked.
  catchsql {
    SELECT name FROM main.sqlite_master;
  }
} {0 {t1 x1}}
do_test attach2-2.8 {
  # start a transaction on test.db even though test2.db is locked.
  catchsql {
    BEGIN;
    INSERT INTO t1 VALUES(8,9);
  }
} {0 {}}
do_test attach2-2.9 {
  execsql {
    SELECT * FROM t1
  }
} {8 9}
do_test attach2-2.10 {
  # now try to write to test2.db.  the write should fail
  catchsql {
    INSERT INTO t2.t1 VALUES(1,2);
  }
} {1 {database is locked}}
do_test attach2-2.11 {
  # when the write failed in the previous test, the transaction should
  # have rolled back.
  execsql {
    SELECT * FROM t1
  }
} {}
do_test attach2-2.12 {
  catchsql {
    COMMIT
  }
} {1 {cannot commit - no transaction is active}}

db close
for {set i 2} {$i<=15} {incr i} {
  catch {db$i close}
}
file delete -force test2.db


finish_test