os/persistentdata/persistentstorage/sqlite3api/TEST/SRC/test2.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
** 2001 September 15
sl@0
     3
**
sl@0
     4
** The author disclaims copyright to this source code.  In place of
sl@0
     5
** a legal notice, here is a blessing:
sl@0
     6
**
sl@0
     7
**    May you do good and not evil.
sl@0
     8
**    May you find forgiveness for yourself and forgive others.
sl@0
     9
**    May you share freely, never taking more than you give.
sl@0
    10
**
sl@0
    11
*************************************************************************
sl@0
    12
** Code for testing the pager.c module in SQLite.  This code
sl@0
    13
** is not included in the SQLite library.  It is used for automated
sl@0
    14
** testing of the SQLite library.
sl@0
    15
**
sl@0
    16
** $Id: test2.c,v 1.62 2008/09/29 11:49:48 danielk1977 Exp $
sl@0
    17
*/
sl@0
    18
#include "sqliteInt.h"
sl@0
    19
#include "tcl.h"
sl@0
    20
#include <stdlib.h>
sl@0
    21
#include <string.h>
sl@0
    22
#include <ctype.h>
sl@0
    23
sl@0
    24
/*
sl@0
    25
** Interpret an SQLite error number
sl@0
    26
*/
sl@0
    27
static char *errorName(int rc){
sl@0
    28
  char *zName;
sl@0
    29
  switch( rc ){
sl@0
    30
    case SQLITE_OK:         zName = "SQLITE_OK";          break;
sl@0
    31
    case SQLITE_ERROR:      zName = "SQLITE_ERROR";       break;
sl@0
    32
    case SQLITE_PERM:       zName = "SQLITE_PERM";        break;
sl@0
    33
    case SQLITE_ABORT:      zName = "SQLITE_ABORT";       break;
sl@0
    34
    case SQLITE_BUSY:       zName = "SQLITE_BUSY";        break;
sl@0
    35
    case SQLITE_NOMEM:      zName = "SQLITE_NOMEM";       break;
sl@0
    36
    case SQLITE_READONLY:   zName = "SQLITE_READONLY";    break;
sl@0
    37
    case SQLITE_INTERRUPT:  zName = "SQLITE_INTERRUPT";   break;
sl@0
    38
    case SQLITE_IOERR:      zName = "SQLITE_IOERR";       break;
sl@0
    39
    case SQLITE_CORRUPT:    zName = "SQLITE_CORRUPT";     break;
sl@0
    40
    case SQLITE_FULL:       zName = "SQLITE_FULL";        break;
sl@0
    41
    case SQLITE_CANTOPEN:   zName = "SQLITE_CANTOPEN";    break;
sl@0
    42
    case SQLITE_PROTOCOL:   zName = "SQLITE_PROTOCOL";    break;
sl@0
    43
    case SQLITE_EMPTY:      zName = "SQLITE_EMPTY";       break;
sl@0
    44
    case SQLITE_SCHEMA:     zName = "SQLITE_SCHEMA";      break;
sl@0
    45
    case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT";  break;
sl@0
    46
    case SQLITE_MISMATCH:   zName = "SQLITE_MISMATCH";    break;
sl@0
    47
    case SQLITE_MISUSE:     zName = "SQLITE_MISUSE";      break;
sl@0
    48
    case SQLITE_NOLFS:      zName = "SQLITE_NOLFS";       break;
sl@0
    49
    default:                zName = "SQLITE_Unknown";     break;
sl@0
    50
  }
sl@0
    51
  return zName;
sl@0
    52
}
sl@0
    53
sl@0
    54
/*
sl@0
    55
** Page size and reserved size used for testing.
sl@0
    56
*/
sl@0
    57
static int test_pagesize = 1024;
sl@0
    58
sl@0
    59
/*
sl@0
    60
** Usage:   pager_open FILENAME N-PAGE
sl@0
    61
**
sl@0
    62
** Open a new pager
sl@0
    63
*/
sl@0
    64
static int pager_open(
sl@0
    65
  void *NotUsed,
sl@0
    66
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
    67
  int argc,              /* Number of arguments */
sl@0
    68
  const char **argv      /* Text of each argument */
sl@0
    69
){
sl@0
    70
  u16 pageSize;
sl@0
    71
  Pager *pPager;
sl@0
    72
  int nPage;
sl@0
    73
  int rc;
sl@0
    74
  char zBuf[100];
sl@0
    75
  if( argc!=3 ){
sl@0
    76
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
    77
       " FILENAME N-PAGE\"", 0);
sl@0
    78
    return TCL_ERROR;
sl@0
    79
  }
sl@0
    80
  if( Tcl_GetInt(interp, argv[2], &nPage) ) return TCL_ERROR;
sl@0
    81
  rc = sqlite3PagerOpen(sqlite3_vfs_find(0), &pPager, argv[1], 0, 0,
sl@0
    82
      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MAIN_DB);
sl@0
    83
  if( rc!=SQLITE_OK ){
sl@0
    84
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
    85
    return TCL_ERROR;
sl@0
    86
  }
sl@0
    87
  sqlite3PagerSetCachesize(pPager, nPage);
sl@0
    88
  pageSize = test_pagesize;
sl@0
    89
  sqlite3PagerSetPagesize(pPager, &pageSize);
sl@0
    90
  sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPager);
sl@0
    91
  Tcl_AppendResult(interp, zBuf, 0);
sl@0
    92
  return TCL_OK;
sl@0
    93
}
sl@0
    94
sl@0
    95
/*
sl@0
    96
** Usage:   pager_close ID
sl@0
    97
**
sl@0
    98
** Close the given pager.
sl@0
    99
*/
sl@0
   100
static int pager_close(
sl@0
   101
  void *NotUsed,
sl@0
   102
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   103
  int argc,              /* Number of arguments */
sl@0
   104
  const char **argv      /* Text of each argument */
sl@0
   105
){
sl@0
   106
  Pager *pPager;
sl@0
   107
  int rc;
sl@0
   108
  if( argc!=2 ){
sl@0
   109
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   110
       " ID\"", 0);
sl@0
   111
    return TCL_ERROR;
sl@0
   112
  }
sl@0
   113
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   114
  rc = sqlite3PagerClose(pPager);
sl@0
   115
  if( rc!=SQLITE_OK ){
sl@0
   116
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   117
    return TCL_ERROR;
sl@0
   118
  }
sl@0
   119
  return TCL_OK;
sl@0
   120
}
sl@0
   121
sl@0
   122
/*
sl@0
   123
** Usage:   pager_rollback ID
sl@0
   124
**
sl@0
   125
** Rollback changes
sl@0
   126
*/
sl@0
   127
static int pager_rollback(
sl@0
   128
  void *NotUsed,
sl@0
   129
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   130
  int argc,              /* Number of arguments */
sl@0
   131
  const char **argv      /* Text of each argument */
sl@0
   132
){
sl@0
   133
  Pager *pPager;
sl@0
   134
  int rc;
sl@0
   135
  if( argc!=2 ){
sl@0
   136
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   137
       " ID\"", 0);
sl@0
   138
    return TCL_ERROR;
sl@0
   139
  }
sl@0
   140
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   141
  rc = sqlite3PagerRollback(pPager);
sl@0
   142
  if( rc!=SQLITE_OK ){
sl@0
   143
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   144
    return TCL_ERROR;
sl@0
   145
  }
sl@0
   146
  return TCL_OK;
sl@0
   147
}
sl@0
   148
sl@0
   149
/*
sl@0
   150
** Usage:   pager_commit ID
sl@0
   151
**
sl@0
   152
** Commit all changes
sl@0
   153
*/
sl@0
   154
static int pager_commit(
sl@0
   155
  void *NotUsed,
sl@0
   156
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   157
  int argc,              /* Number of arguments */
sl@0
   158
  const char **argv      /* Text of each argument */
sl@0
   159
){
sl@0
   160
  Pager *pPager;
sl@0
   161
  int rc;
sl@0
   162
  if( argc!=2 ){
sl@0
   163
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   164
       " ID\"", 0);
sl@0
   165
    return TCL_ERROR;
sl@0
   166
  }
sl@0
   167
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   168
  rc = sqlite3PagerCommitPhaseOne(pPager, 0, 0, 0);
sl@0
   169
  if( rc!=SQLITE_OK ){
sl@0
   170
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   171
    return TCL_ERROR;
sl@0
   172
  }
sl@0
   173
  rc = sqlite3PagerCommitPhaseTwo(pPager);
sl@0
   174
  if( rc!=SQLITE_OK ){
sl@0
   175
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   176
    return TCL_ERROR;
sl@0
   177
  }
sl@0
   178
  return TCL_OK;
sl@0
   179
}
sl@0
   180
sl@0
   181
/*
sl@0
   182
** Usage:   pager_stmt_begin ID
sl@0
   183
**
sl@0
   184
** Start a new checkpoint.
sl@0
   185
*/
sl@0
   186
static int pager_stmt_begin(
sl@0
   187
  void *NotUsed,
sl@0
   188
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   189
  int argc,              /* Number of arguments */
sl@0
   190
  const char **argv      /* Text of each argument */
sl@0
   191
){
sl@0
   192
  Pager *pPager;
sl@0
   193
  int rc;
sl@0
   194
  if( argc!=2 ){
sl@0
   195
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   196
       " ID\"", 0);
sl@0
   197
    return TCL_ERROR;
sl@0
   198
  }
sl@0
   199
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   200
  rc = sqlite3PagerStmtBegin(pPager);
sl@0
   201
  if( rc!=SQLITE_OK ){
sl@0
   202
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   203
    return TCL_ERROR;
sl@0
   204
  }
sl@0
   205
  return TCL_OK;
sl@0
   206
}
sl@0
   207
sl@0
   208
/*
sl@0
   209
** Usage:   pager_stmt_rollback ID
sl@0
   210
**
sl@0
   211
** Rollback changes to a checkpoint
sl@0
   212
*/
sl@0
   213
static int pager_stmt_rollback(
sl@0
   214
  void *NotUsed,
sl@0
   215
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   216
  int argc,              /* Number of arguments */
sl@0
   217
  const char **argv      /* Text of each argument */
sl@0
   218
){
sl@0
   219
  Pager *pPager;
sl@0
   220
  int rc;
sl@0
   221
  if( argc!=2 ){
sl@0
   222
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   223
       " ID\"", 0);
sl@0
   224
    return TCL_ERROR;
sl@0
   225
  }
sl@0
   226
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   227
  rc = sqlite3PagerStmtRollback(pPager);
sl@0
   228
  if( rc!=SQLITE_OK ){
sl@0
   229
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   230
    return TCL_ERROR;
sl@0
   231
  }
sl@0
   232
  return TCL_OK;
sl@0
   233
}
sl@0
   234
sl@0
   235
/*
sl@0
   236
** Usage:   pager_stmt_commit ID
sl@0
   237
**
sl@0
   238
** Commit changes to a checkpoint
sl@0
   239
*/
sl@0
   240
static int pager_stmt_commit(
sl@0
   241
  void *NotUsed,
sl@0
   242
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   243
  int argc,              /* Number of arguments */
sl@0
   244
  const char **argv      /* Text of each argument */
sl@0
   245
){
sl@0
   246
  Pager *pPager;
sl@0
   247
  int rc;
sl@0
   248
  if( argc!=2 ){
sl@0
   249
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   250
       " ID\"", 0);
sl@0
   251
    return TCL_ERROR;
sl@0
   252
  }
sl@0
   253
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   254
  rc = sqlite3PagerStmtCommit(pPager);
sl@0
   255
  if( rc!=SQLITE_OK ){
sl@0
   256
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   257
    return TCL_ERROR;
sl@0
   258
  }
sl@0
   259
  return TCL_OK;
sl@0
   260
}
sl@0
   261
sl@0
   262
/*
sl@0
   263
** Usage:   pager_stats ID
sl@0
   264
**
sl@0
   265
** Return pager statistics.
sl@0
   266
*/
sl@0
   267
static int pager_stats(
sl@0
   268
  void *NotUsed,
sl@0
   269
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   270
  int argc,              /* Number of arguments */
sl@0
   271
  const char **argv      /* Text of each argument */
sl@0
   272
){
sl@0
   273
  Pager *pPager;
sl@0
   274
  int i, *a;
sl@0
   275
  if( argc!=2 ){
sl@0
   276
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   277
       " ID\"", 0);
sl@0
   278
    return TCL_ERROR;
sl@0
   279
  }
sl@0
   280
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   281
  a = sqlite3PagerStats(pPager);
sl@0
   282
  for(i=0; i<9; i++){
sl@0
   283
    static char *zName[] = {
sl@0
   284
      "ref", "page", "max", "size", "state", "err",
sl@0
   285
      "hit", "miss", "ovfl",
sl@0
   286
    };
sl@0
   287
    char zBuf[100];
sl@0
   288
    Tcl_AppendElement(interp, zName[i]);
sl@0
   289
    sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",a[i]);
sl@0
   290
    Tcl_AppendElement(interp, zBuf);
sl@0
   291
  }
sl@0
   292
  return TCL_OK;
sl@0
   293
}
sl@0
   294
sl@0
   295
/*
sl@0
   296
** Usage:   pager_pagecount ID
sl@0
   297
**
sl@0
   298
** Return the size of the database file.
sl@0
   299
*/
sl@0
   300
static int pager_pagecount(
sl@0
   301
  void *NotUsed,
sl@0
   302
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   303
  int argc,              /* Number of arguments */
sl@0
   304
  const char **argv      /* Text of each argument */
sl@0
   305
){
sl@0
   306
  Pager *pPager;
sl@0
   307
  char zBuf[100];
sl@0
   308
  int nPage;
sl@0
   309
  if( argc!=2 ){
sl@0
   310
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   311
       " ID\"", 0);
sl@0
   312
    return TCL_ERROR;
sl@0
   313
  }
sl@0
   314
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   315
  sqlite3PagerPagecount(pPager, &nPage);
sl@0
   316
  sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", nPage);
sl@0
   317
  Tcl_AppendResult(interp, zBuf, 0);
sl@0
   318
  return TCL_OK;
sl@0
   319
}
sl@0
   320
sl@0
   321
/*
sl@0
   322
** Usage:   page_get ID PGNO
sl@0
   323
**
sl@0
   324
** Return a pointer to a page from the database.
sl@0
   325
*/
sl@0
   326
static int page_get(
sl@0
   327
  void *NotUsed,
sl@0
   328
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   329
  int argc,              /* Number of arguments */
sl@0
   330
  const char **argv      /* Text of each argument */
sl@0
   331
){
sl@0
   332
  Pager *pPager;
sl@0
   333
  char zBuf[100];
sl@0
   334
  DbPage *pPage;
sl@0
   335
  int pgno;
sl@0
   336
  int rc;
sl@0
   337
  if( argc!=3 ){
sl@0
   338
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   339
       " ID PGNO\"", 0);
sl@0
   340
    return TCL_ERROR;
sl@0
   341
  }
sl@0
   342
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   343
  if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR;
sl@0
   344
  rc = sqlite3PagerGet(pPager, pgno, &pPage);
sl@0
   345
  if( rc!=SQLITE_OK ){
sl@0
   346
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   347
    return TCL_ERROR;
sl@0
   348
  }
sl@0
   349
  sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPage);
sl@0
   350
  Tcl_AppendResult(interp, zBuf, 0);
sl@0
   351
  return TCL_OK;
sl@0
   352
}
sl@0
   353
sl@0
   354
/*
sl@0
   355
** Usage:   page_lookup ID PGNO
sl@0
   356
**
sl@0
   357
** Return a pointer to a page if the page is already in cache.
sl@0
   358
** If not in cache, return an empty string.
sl@0
   359
*/
sl@0
   360
static int page_lookup(
sl@0
   361
  void *NotUsed,
sl@0
   362
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   363
  int argc,              /* Number of arguments */
sl@0
   364
  const char **argv      /* Text of each argument */
sl@0
   365
){
sl@0
   366
  Pager *pPager;
sl@0
   367
  char zBuf[100];
sl@0
   368
  DbPage *pPage;
sl@0
   369
  int pgno;
sl@0
   370
  if( argc!=3 ){
sl@0
   371
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   372
       " ID PGNO\"", 0);
sl@0
   373
    return TCL_ERROR;
sl@0
   374
  }
sl@0
   375
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   376
  if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR;
sl@0
   377
  pPage = sqlite3PagerLookup(pPager, pgno);
sl@0
   378
  if( pPage ){
sl@0
   379
    sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPage);
sl@0
   380
    Tcl_AppendResult(interp, zBuf, 0);
sl@0
   381
  }
sl@0
   382
  return TCL_OK;
sl@0
   383
}
sl@0
   384
sl@0
   385
/*
sl@0
   386
** Usage:   pager_truncate ID PGNO
sl@0
   387
*/
sl@0
   388
static int pager_truncate(
sl@0
   389
  void *NotUsed,
sl@0
   390
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   391
  int argc,              /* Number of arguments */
sl@0
   392
  const char **argv      /* Text of each argument */
sl@0
   393
){
sl@0
   394
  Pager *pPager;
sl@0
   395
  int rc;
sl@0
   396
  int pgno;
sl@0
   397
  if( argc!=3 ){
sl@0
   398
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   399
       " ID PGNO\"", 0);
sl@0
   400
    return TCL_ERROR;
sl@0
   401
  }
sl@0
   402
  pPager = sqlite3TestTextToPtr(argv[1]);
sl@0
   403
  if( Tcl_GetInt(interp, argv[2], &pgno) ) return TCL_ERROR;
sl@0
   404
  rc = sqlite3PagerTruncate(pPager, pgno);
sl@0
   405
  if( rc!=SQLITE_OK ){
sl@0
   406
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   407
    return TCL_ERROR;
sl@0
   408
  }
sl@0
   409
  return TCL_OK;
sl@0
   410
}
sl@0
   411
sl@0
   412
sl@0
   413
/*
sl@0
   414
** Usage:   page_unref PAGE
sl@0
   415
**
sl@0
   416
** Drop a pointer to a page.
sl@0
   417
*/
sl@0
   418
static int page_unref(
sl@0
   419
  void *NotUsed,
sl@0
   420
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   421
  int argc,              /* Number of arguments */
sl@0
   422
  const char **argv      /* Text of each argument */
sl@0
   423
){
sl@0
   424
  DbPage *pPage;
sl@0
   425
  int rc;
sl@0
   426
  if( argc!=2 ){
sl@0
   427
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   428
       " PAGE\"", 0);
sl@0
   429
    return TCL_ERROR;
sl@0
   430
  }
sl@0
   431
  pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]);
sl@0
   432
  rc = sqlite3PagerUnref(pPage);
sl@0
   433
  if( rc!=SQLITE_OK ){
sl@0
   434
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   435
    return TCL_ERROR;
sl@0
   436
  }
sl@0
   437
  return TCL_OK;
sl@0
   438
}
sl@0
   439
sl@0
   440
/*
sl@0
   441
** Usage:   page_read PAGE
sl@0
   442
**
sl@0
   443
** Return the content of a page
sl@0
   444
*/
sl@0
   445
static int page_read(
sl@0
   446
  void *NotUsed,
sl@0
   447
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   448
  int argc,              /* Number of arguments */
sl@0
   449
  const char **argv      /* Text of each argument */
sl@0
   450
){
sl@0
   451
  char zBuf[100];
sl@0
   452
  DbPage *pPage;
sl@0
   453
  if( argc!=2 ){
sl@0
   454
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   455
       " PAGE\"", 0);
sl@0
   456
    return TCL_ERROR;
sl@0
   457
  }
sl@0
   458
  pPage = sqlite3TestTextToPtr(argv[1]);
sl@0
   459
  memcpy(zBuf, sqlite3PagerGetData(pPage), sizeof(zBuf));
sl@0
   460
  Tcl_AppendResult(interp, zBuf, 0);
sl@0
   461
  return TCL_OK;
sl@0
   462
}
sl@0
   463
sl@0
   464
/*
sl@0
   465
** Usage:   page_number PAGE
sl@0
   466
**
sl@0
   467
** Return the page number for a page.
sl@0
   468
*/
sl@0
   469
static int page_number(
sl@0
   470
  void *NotUsed,
sl@0
   471
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   472
  int argc,              /* Number of arguments */
sl@0
   473
  const char **argv      /* Text of each argument */
sl@0
   474
){
sl@0
   475
  char zBuf[100];
sl@0
   476
  DbPage *pPage;
sl@0
   477
  if( argc!=2 ){
sl@0
   478
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   479
       " PAGE\"", 0);
sl@0
   480
    return TCL_ERROR;
sl@0
   481
  }
sl@0
   482
  pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]);
sl@0
   483
  sqlite3_snprintf(sizeof(zBuf), zBuf, "%d", sqlite3PagerPagenumber(pPage));
sl@0
   484
  Tcl_AppendResult(interp, zBuf, 0);
sl@0
   485
  return TCL_OK;
sl@0
   486
}
sl@0
   487
sl@0
   488
/*
sl@0
   489
** Usage:   page_write PAGE DATA
sl@0
   490
**
sl@0
   491
** Write something into a page.
sl@0
   492
*/
sl@0
   493
static int page_write(
sl@0
   494
  void *NotUsed,
sl@0
   495
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   496
  int argc,              /* Number of arguments */
sl@0
   497
  const char **argv      /* Text of each argument */
sl@0
   498
){
sl@0
   499
  DbPage *pPage;
sl@0
   500
  char *pData;
sl@0
   501
  int rc;
sl@0
   502
  if( argc!=3 ){
sl@0
   503
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   504
       " PAGE DATA\"", 0);
sl@0
   505
    return TCL_ERROR;
sl@0
   506
  }
sl@0
   507
  pPage = (DbPage *)sqlite3TestTextToPtr(argv[1]);
sl@0
   508
  rc = sqlite3PagerWrite(pPage);
sl@0
   509
  if( rc!=SQLITE_OK ){
sl@0
   510
    Tcl_AppendResult(interp, errorName(rc), 0);
sl@0
   511
    return TCL_ERROR;
sl@0
   512
  }
sl@0
   513
  pData = sqlite3PagerGetData(pPage);
sl@0
   514
  strncpy(pData, argv[2], test_pagesize-1);
sl@0
   515
  pData[test_pagesize-1] = 0;
sl@0
   516
  return TCL_OK;
sl@0
   517
}
sl@0
   518
sl@0
   519
#ifndef SQLITE_OMIT_DISKIO
sl@0
   520
/*
sl@0
   521
** Usage:   fake_big_file  N  FILENAME
sl@0
   522
**
sl@0
   523
** Write a few bytes at the N megabyte point of FILENAME.  This will
sl@0
   524
** create a large file.  If the file was a valid SQLite database, then
sl@0
   525
** the next time the database is opened, SQLite will begin allocating
sl@0
   526
** new pages after N.  If N is 2096 or bigger, this will test the
sl@0
   527
** ability of SQLite to write to large files.
sl@0
   528
*/
sl@0
   529
static int fake_big_file(
sl@0
   530
  void *NotUsed,
sl@0
   531
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   532
  int argc,              /* Number of arguments */
sl@0
   533
  const char **argv      /* Text of each argument */
sl@0
   534
){
sl@0
   535
  sqlite3_vfs *pVfs;
sl@0
   536
  sqlite3_file *fd = 0;
sl@0
   537
  int rc;
sl@0
   538
  int n;
sl@0
   539
  i64 offset;
sl@0
   540
  if( argc!=3 ){
sl@0
   541
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   542
       " N-MEGABYTES FILE\"", 0);
sl@0
   543
    return TCL_ERROR;
sl@0
   544
  }
sl@0
   545
  if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR;
sl@0
   546
sl@0
   547
  pVfs = sqlite3_vfs_find(0);
sl@0
   548
  rc = sqlite3OsOpenMalloc(pVfs, argv[2], &fd, 
sl@0
   549
      (SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_DB), 0
sl@0
   550
  );
sl@0
   551
  if( rc ){
sl@0
   552
    Tcl_AppendResult(interp, "open failed: ", errorName(rc), 0);
sl@0
   553
    return TCL_ERROR;
sl@0
   554
  }
sl@0
   555
  offset = n;
sl@0
   556
  offset *= 1024*1024;
sl@0
   557
  rc = sqlite3OsWrite(fd, "Hello, World!", 14, offset);
sl@0
   558
  sqlite3OsCloseFree(fd);
sl@0
   559
  if( rc ){
sl@0
   560
    Tcl_AppendResult(interp, "write failed: ", errorName(rc), 0);
sl@0
   561
    return TCL_ERROR;
sl@0
   562
  }
sl@0
   563
  return TCL_OK;
sl@0
   564
}
sl@0
   565
#endif
sl@0
   566
sl@0
   567
sl@0
   568
/*
sl@0
   569
** sqlite3BitvecBuiltinTest SIZE PROGRAM
sl@0
   570
**
sl@0
   571
** Invoke the SQLITE_TESTCTRL_BITVEC_TEST operator on test_control.
sl@0
   572
** See comments on sqlite3BitvecBuiltinTest() for additional information.
sl@0
   573
*/
sl@0
   574
static int testBitvecBuiltinTest(
sl@0
   575
  void *NotUsed,
sl@0
   576
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
sl@0
   577
  int argc,              /* Number of arguments */
sl@0
   578
  const char **argv      /* Text of each argument */
sl@0
   579
){
sl@0
   580
  int sz, rc;
sl@0
   581
  int nProg = 0;
sl@0
   582
  int aProg[100];
sl@0
   583
  const char *z;
sl@0
   584
  if( argc!=3 ){
sl@0
   585
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
sl@0
   586
                     " SIZE PROGRAM\"", (void*)0);
sl@0
   587
  }
sl@0
   588
  if( Tcl_GetInt(interp, argv[1], &sz) ) return TCL_ERROR;
sl@0
   589
  z = argv[2];
sl@0
   590
  while( nProg<99 && *z ){
sl@0
   591
    while( *z && !isdigit(*z) ){ z++; }
sl@0
   592
    if( *z==0 ) break;
sl@0
   593
    aProg[nProg++] = atoi(z);
sl@0
   594
    while( isdigit(*z) ){ z++; }
sl@0
   595
  }
sl@0
   596
  aProg[nProg] = 0;
sl@0
   597
  rc = sqlite3_test_control(SQLITE_TESTCTRL_BITVEC_TEST, sz, aProg);
sl@0
   598
  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
sl@0
   599
  return TCL_OK;
sl@0
   600
}  
sl@0
   601
sl@0
   602
/*
sl@0
   603
** Register commands with the TCL interpreter.
sl@0
   604
*/
sl@0
   605
int Sqlitetest2_Init(Tcl_Interp *interp){
sl@0
   606
  extern int sqlite3_io_error_persist;
sl@0
   607
  extern int sqlite3_io_error_pending;
sl@0
   608
  extern int sqlite3_io_error_hit;
sl@0
   609
  extern int sqlite3_io_error_hardhit;
sl@0
   610
  extern int sqlite3_diskfull_pending;
sl@0
   611
  extern int sqlite3_diskfull;
sl@0
   612
  extern int sqlite3_pager_n_sort_bucket;
sl@0
   613
  static struct {
sl@0
   614
    char *zName;
sl@0
   615
    Tcl_CmdProc *xProc;
sl@0
   616
  } aCmd[] = {
sl@0
   617
    { "pager_open",              (Tcl_CmdProc*)pager_open          },
sl@0
   618
    { "pager_close",             (Tcl_CmdProc*)pager_close         },
sl@0
   619
    { "pager_commit",            (Tcl_CmdProc*)pager_commit        },
sl@0
   620
    { "pager_rollback",          (Tcl_CmdProc*)pager_rollback      },
sl@0
   621
    { "pager_stmt_begin",        (Tcl_CmdProc*)pager_stmt_begin    },
sl@0
   622
    { "pager_stmt_commit",       (Tcl_CmdProc*)pager_stmt_commit   },
sl@0
   623
    { "pager_stmt_rollback",     (Tcl_CmdProc*)pager_stmt_rollback },
sl@0
   624
    { "pager_stats",             (Tcl_CmdProc*)pager_stats         },
sl@0
   625
    { "pager_pagecount",         (Tcl_CmdProc*)pager_pagecount     },
sl@0
   626
    { "page_get",                (Tcl_CmdProc*)page_get            },
sl@0
   627
    { "page_lookup",             (Tcl_CmdProc*)page_lookup         },
sl@0
   628
    { "page_unref",              (Tcl_CmdProc*)page_unref          },
sl@0
   629
    { "page_read",               (Tcl_CmdProc*)page_read           },
sl@0
   630
    { "page_write",              (Tcl_CmdProc*)page_write          },
sl@0
   631
    { "page_number",             (Tcl_CmdProc*)page_number         },
sl@0
   632
    { "pager_truncate",          (Tcl_CmdProc*)pager_truncate      },
sl@0
   633
#ifndef SQLITE_OMIT_DISKIO
sl@0
   634
    { "fake_big_file",           (Tcl_CmdProc*)fake_big_file       },
sl@0
   635
#endif
sl@0
   636
    { "sqlite3BitvecBuiltinTest",(Tcl_CmdProc*)testBitvecBuiltinTest},
sl@0
   637
  };
sl@0
   638
  int i;
sl@0
   639
  for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
sl@0
   640
    Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
sl@0
   641
  }
sl@0
   642
  Tcl_LinkVar(interp, "sqlite_io_error_pending",
sl@0
   643
     (char*)&sqlite3_io_error_pending, TCL_LINK_INT);
sl@0
   644
  Tcl_LinkVar(interp, "sqlite_io_error_persist",
sl@0
   645
     (char*)&sqlite3_io_error_persist, TCL_LINK_INT);
sl@0
   646
  Tcl_LinkVar(interp, "sqlite_io_error_hit",
sl@0
   647
     (char*)&sqlite3_io_error_hit, TCL_LINK_INT);
sl@0
   648
  Tcl_LinkVar(interp, "sqlite_io_error_hardhit",
sl@0
   649
     (char*)&sqlite3_io_error_hardhit, TCL_LINK_INT);
sl@0
   650
  Tcl_LinkVar(interp, "sqlite_diskfull_pending",
sl@0
   651
     (char*)&sqlite3_diskfull_pending, TCL_LINK_INT);
sl@0
   652
  Tcl_LinkVar(interp, "sqlite_diskfull",
sl@0
   653
     (char*)&sqlite3_diskfull, TCL_LINK_INT);
sl@0
   654
  Tcl_LinkVar(interp, "sqlite_pending_byte",
sl@0
   655
     (char*)&sqlite3_pending_byte, TCL_LINK_INT);
sl@0
   656
  Tcl_LinkVar(interp, "sqlite_pager_n_sort_bucket",
sl@0
   657
     (char*)&sqlite3_pager_n_sort_bucket, TCL_LINK_INT);
sl@0
   658
  return TCL_OK;
sl@0
   659
}