# 2008 June 9
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    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.
#
#***********************************************************************
#
# Test that it is possible to have two open blob handles on a single
# blob object.
#
# $Id: incrblob2.test,v 1.3 2008/06/12 14:42:07 danielk1977 Exp $
#

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

ifcapable {!autovacuum || !pragma || !incrblob} {
  finish_test
  return
}

do_test incrblob2-1.0 {
  execsql {
    CREATE TABLE blobs(id INTEGER PRIMARY KEY, data BLOB);
    INSERT INTO blobs VALUES(NULL, zeroblob(5000));
    INSERT INTO blobs VALUES(NULL, zeroblob(5000));
    INSERT INTO blobs VALUES(NULL, zeroblob(5000));
    INSERT INTO blobs VALUES(NULL, zeroblob(5000));
  }
} {}

foreach iOffset [list 0 256 4094] {
  do_test incrblob2-1.$iOffset.1 {
    set fd [db incrblob blobs data 1]
    puts $fd "[string repeat x $iOffset]SQLite version 3.6.0"
    close $fd
  } {}
  
  do_test incrblob2-1.$iOffset.2 {
    set fd1 [db incrblob blobs data 1]
    set fd2 [db incrblob blobs data 1]
    fconfigure $fd1 -buffering none
    fconfigure $fd2 -buffering none
    if {$iOffset != 0} {
      seek $fd2 $iOffset start
      seek $fd1 $iOffset start
    }
    read $fd1 6
  } {SQLite}
  
  do_test incrblob2-1.$iOffset.3 {
    read $fd2 6
  } {SQLite}
  
  do_test incrblob2-1.$iOffset.4 {
    seek $fd2 $iOffset start
    seek $fd1 $iOffset start
    puts -nonewline $fd2 "etiLQS"
  } {}

  
  do_test incrblob2-1.$iOffset.5 {
    seek $fd1 $iOffset start
    read $fd1 6
  } {etiLQS}
  
  do_test incrblob2-1.$iOffset.6 {
    seek $fd2 $iOffset start
    read $fd2 6
  } {etiLQS}
  
  do_test incrblob2-1.$iOffset.7 {
    seek $fd1 $iOffset start
    read $fd1 6
  } {etiLQS}
  
  do_test incrblob2-1.$iOffset.8 {
    close $fd1
    close $fd2
  } {}
}

#--------------------------------------------------------------------------

foreach iOffset [list 0 256 4094] {

  do_test incrblob2-2.$iOffset.1 {
    set fd1 [db incrblob blobs data 1]
    seek $fd1 [expr $iOffset - 5000] end
    fconfigure $fd1 -buffering none

    set fd2 [db incrblob blobs data 1]
    seek $fd2 [expr $iOffset - 5000] end
    fconfigure $fd2 -buffering none

    puts -nonewline $fd1 "123456"
  } {}
  
  do_test incrblob2-2.$iOffset.2 {
    read $fd2 6
  } {123456}

  do_test incrblob2-2.$iOffset.3 {
    close $fd1
    close $fd2
  } {}
}

do_test incrblob2-3.1 {
  set fd1 [db incrblob blobs data 1]
  fconfigure $fd1 -buffering none
} {}
do_test incrblob2-3.2 {
  execsql {
    INSERT INTO blobs VALUES(5, zeroblob(10240));
  }
} {}
do_test incrblob2-3.3 {
  set rc [catch { read $fd1 6 } msg]
  list $rc $msg
} {0 123456}
do_test incrblob2-3.4 {
  close $fd1
} {}

#--------------------------------------------------------------------------
# The following tests - incrblob2-4.* - test that blob handles are 
# invalidated at the correct times.
#
do_test incrblob2-4.1 {
  db eval BEGIN
  db eval { CREATE TABLE t1(id INTEGER PRIMARY KEY, data BLOB); }
  for {set ii 1} {$ii < 100} {incr ii} {
    set data [string repeat "blob$ii" 500]
    db eval { INSERT INTO t1 VALUES($ii, $data) }
  }
  db eval COMMIT
} {}

proc aborted_handles {} {
  global handles

  set aborted {}
  for {set ii 1} {$ii < 100} {incr ii} {
    set str "blob$ii"
    set nByte [string length $str]
    set iOffset [expr $nByte * $ii * 2]

    set rc [catch {sqlite3_blob_read $handles($ii) $iOffset $nByte} msg]
    if {$rc && $msg eq "SQLITE_ABORT"} {
      lappend aborted $ii
    } else {
      if {$rc || $msg ne $str} {
        error "blob $ii: $msg"
      }
    }
  }
  set aborted
}

do_test incrblob2-4.2 {
  for {set ii 1} {$ii < 100} {incr ii} {
    set handles($ii) [db incrblob t1 data $ii]
  }
  aborted_handles
} {}

# Update row 3. This should abort handle 3 but leave all others untouched.
#
do_test incrblob2-4.3 {
  db eval {UPDATE t1 SET data = data || '' WHERE id = 3}
  aborted_handles
} {3}

# Test that a write to handle 3 also returns SQLITE_ABORT.
#
do_test incrblob2-4.3.1 {
  set rc [catch {sqlite3_blob_write $::handles(3) 10 HELLO} msg]
  list $rc $msg
} {1 SQLITE_ABORT}

# Delete row 14. This should abort handle 6 but leave all others untouched.
#
do_test incrblob2-4.4 {
  db eval {DELETE FROM t1 WHERE id = 14}
  aborted_handles
} {3 14}

# Change the rowid of row 15 to 102. Should abort handle 15.
#
do_test incrblob2-4.5 {
  db eval {UPDATE t1 SET id = 102 WHERE id = 15}
  aborted_handles
} {3 14 15}

# Clobber row 92 using INSERT OR REPLACE.
#
do_test incrblob2-4.6 {
  db eval {INSERT OR REPLACE INTO t1 VALUES(92, zeroblob(1000))}
  aborted_handles
} {3 14 15 92}

# Clobber row 65 using UPDATE OR REPLACE on row 35. This should abort 
# handles 35 and 65.
#
do_test incrblob2-4.7 {
  db eval {UPDATE OR REPLACE t1 SET id = 65 WHERE id = 35}
  aborted_handles
} {3 14 15 35 65 92}

# Insert a couple of new rows. This should not invalidate any handles.
#
do_test incrblob2-4.9 {
  db eval {INSERT INTO t1 SELECT NULL, data FROM t1}
  aborted_handles
} {3 14 15 35 65 92}

# Delete all rows from 1 to 25. This should abort all handles up to 25.
#
do_test incrblob2-4.9 {
  db eval {DELETE FROM t1 WHERE id >=1 AND id <= 25}
  aborted_handles
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 35 65 92}

# Delete the whole table (this will use sqlite3BtreeClearTable()). All handles
# should now be aborted.
#
do_test incrblob2-4.10 {
  db eval {DELETE FROM t1}
  aborted_handles
} {1 2 3 4 5 6 7 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}

do_test incrblob2-4.1.X {
  for {set ii 1} {$ii < 100} {incr ii} {
    close $handles($ii) 
  }
} {}

#--------------------------------------------------------------------------
# The following tests - incrblob2-5.* - test that in shared cache an open
# blob handle counts as a read-lock on its table.
#
ifcapable shared_cache {
  db close
  set ::enable_shared_cache [sqlite3_enable_shared_cache 1]

  do_test incrblob2-5.1 {
    sqlite3 db test.db
    sqlite3 db2 test.db

    execsql {
      INSERT INTO t1 VALUES(1, 'abcde');
    }
  } {}

  do_test incrblob2-5.2 {
    catchsql { INSERT INTO t1 VALUES(2, 'fghij') } db2
  } {0 {}}

  do_test incrblob2-5.3 {
    set blob [db incrblob t1 data 1]
    catchsql { INSERT INTO t1 VALUES(3, 'klmno') } db2
  } {1 {database is locked}}

  do_test incrblob2-5.4 {
    close $blob
    execsql BEGIN db2
    catchsql { INSERT INTO t1 VALUES(4, 'pqrst') } db2
  } {0 {}}

  do_test incrblob2-5.5 {
    set blob [db incrblob -readonly t1 data 1]
    catchsql { INSERT INTO t1 VALUES(5, 'uvwxy') } db2
  } {1 {database table is locked}}

  do_test incrblob2-5.6 {
    close $blob
    catchsql { INSERT INTO t1 VALUES(3, 'klmno') } db2
  } {0 {}}

  db2 close
  db close
  sqlite3_enable_shared_cache $::enable_shared_cache
}

#--------------------------------------------------------------------------
# The following tests - incrblob2-6.* - test a specific scenario that might
# be causing an error.
#
sqlite3 db test.db
do_test incrblob2-6.1 {
  execsql {
    DELETE FROM t1;
    INSERT INTO t1 VALUES(1, zeroblob(100));
  }
  
  set rdHandle [db incrblob -readonly t1 data 1]
  set wrHandle [db incrblob t1 data 1]

  sqlite3_blob_read $rdHandle 0 100

  sqlite3_blob_write $wrHandle 0 ABCDEF
  close $wrHandle
  close $rdHandle
} {}

do_test incrblob2-6.2 {
  set rdHandle [db incrblob -readonly t1 data 1]
  sqlite3_blob_read $rdHandle 0 2
} {AB}

do_test incrblob2-6.3 {
  set wrHandle [db incrblob t1 data 1]
  sqlite3_blob_write $wrHandle 0 ZZZZZZZZZZ
  sqlite3_blob_read $rdHandle 2 4
} {ZZZZ}

do_test incrblob2-6.4 {
  close $wrHandle
  close $rdHandle
} {}

finish_test

