SQLite

Check-in [0bf4e7fefd]
Login

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

Overview
Comment:Add the RTRIM collating sequence. Only implemented for UTF8. Still considered experimental and may be removed if we find adverse impact elsewhere in the system. (CVS 4732)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0bf4e7fefdbbf7be4e32195473563158f22f1869
User & Date: drh 2008-01-20 23:19:57.000
References
2019-06-14
13:25 Fixed ticket [f1580ba1b5]: Built-in RTRIM collating sequence yields incorrect comparisons plus 8 other changes (artifact: 71c96e66f5 user: drh)
Context
2008-01-21
13:04
Remove some unused branches from internal function sqlite3PagerDontRollback(). (CVS 4733) (check-in: 3d4252b06b user: danielk1977 tags: trunk)
2008-01-20
23:19
Add the RTRIM collating sequence. Only implemented for UTF8. Still considered experimental and may be removed if we find adverse impact elsewhere in the system. (CVS 4732) (check-in: 0bf4e7fefd user: drh tags: trunk)
2008-01-19
23:50
Additional test coverage improvements. Test coverage now stands at 98.73%. (CVS 4731) (check-in: 010f7b780c user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/main.c.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.409 2007/12/07 18:55:28 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

/*
** The version of the library
*/







|







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.410 2008/01/20 23:19:57 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

/*
** The version of the library
*/
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
** name of a directory, then that directory will be used to store
** temporary files.
**
** See also the "PRAGMA temp_store_directory" SQL command.
*/
char *sqlite3_temp_directory = 0;










/*
** This is the default collating function named "BINARY" which is always
** available.



*/
static int binCollFunc(
  void *NotUsed,
  int nKey1, const void *pKey1,
  int nKey2, const void *pKey2
){
  int rc, n;
  n = nKey1<nKey2 ? nKey1 : nKey2;
  rc = memcmp(pKey1, pKey2, n);
  if( rc==0 ){






    rc = nKey1 - nKey2;

  }
  return rc;
}

/*
** Another built-in collating sequence: NOCASE. 
**







>
>
>
>
>
>
>
>




>
>
>


|







>
>
>
>
>
>
|
>







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
** name of a directory, then that directory will be used to store
** temporary files.
**
** See also the "PRAGMA temp_store_directory" SQL command.
*/
char *sqlite3_temp_directory = 0;


/*
** Return true if the buffer z[0..n-1] contains all spaces.
*/
static int allSpaces(const char *z, int n){
  while( n>0 && z[--n]==' ' ){}
  return n==0;
}

/*
** This is the default collating function named "BINARY" which is always
** available.
**
** If the padFlag argument is not NULL then space padding at the end
** of strings is ignored.  This implements the RTRIM collation.
*/
static int binCollFunc(
  void *padFlag,
  int nKey1, const void *pKey1,
  int nKey2, const void *pKey2
){
  int rc, n;
  n = nKey1<nKey2 ? nKey1 : nKey2;
  rc = memcmp(pKey1, pKey2, n);
  if( rc==0 ){
    if( padFlag
     && allSpaces(((char*)pKey1)+n, nKey1-n)
     && allSpaces(((char*)pKey2)+n, nKey2-n)
    ){
      /* Leave rc unchanged at 0 */
    }else{
      rc = nKey1 - nKey2;
    }
  }
  return rc;
}

/*
** Another built-in collating sequence: NOCASE. 
**
990
991
992
993
994
995
996

997
998
999
1000
1001
1002
1003
  /* Add the default collation sequence BINARY. BINARY works for both UTF-8
  ** and UTF-16, so add a version for each to avoid any unnecessary
  ** conversions. The only error that can occur here is a malloc() failure.
  */
  if( createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0) ||
      createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0) ||
      createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) ||

      (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 
  ){
    assert( db->mallocFailed );
    db->magic = SQLITE_MAGIC_CLOSED;
    goto opendb_out;
  }








>







1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
  /* Add the default collation sequence BINARY. BINARY works for both UTF-8
  ** and UTF-16, so add a version for each to avoid any unnecessary
  ** conversions. The only error that can occur here is a malloc() failure.
  */
  if( createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0) ||
      createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0) ||
      createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0) ||
      createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0) ||
      (db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 6, 0))==0 
  ){
    assert( db->mallocFailed );
    db->magic = SQLITE_MAGIC_CLOSED;
    goto opendb_out;
  }

Added test/collateA.test.


























































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
#
# 2008 January 20
#
# 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.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is the built-in RTRIM collating
# API.
#
# $Id: collateA.test,v 1.1 2008/01/20 23:19:58 drh Exp $

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

do_test collateA-1.1 {
  execsql {
    CREATE TABLE t1(
      a INTEGER PRIMARY KEY,
      b TEXT COLLATE BINARY,
      c TEXT COLLATE RTRIM
    );
    INSERT INTO t1 VALUES(1, 'hello','hello');
    INSERT INTO t1 VALUES(2, 'xyzzy ','xyzzy ');
    INSERT INTO t1 VALUES(3, 'xyzzy  ','xyzzy  ');
    INSERT INTO t1 VALUES(4, 'xyzzy   ','xyzzy   ');
    SELECT count(*) FROM t1;
  }
} {4}
do_test collateA-1.2 {
  execsql {SELECT a FROM t1 WHERE b='hello     '}
} {}
do_test collateA-1.3 {
  execsql {SELECT a FROM t1 WHERE c='hello     '}
} {1}
do_test collateA-1.4 {
  execsql {SELECT a FROM t1 WHERE b='xyzzy'}
} {}
do_test collateA-1.5 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy'}
} {2 3 4}
do_test collateA-1.6 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy '}
} {2 3 4}
do_test collateA-1.7 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy  '}
} {2 3 4}
do_test collateA-1.8 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy   '}
} {2 3 4}
do_test collateA-1.9 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy    '}
} {2 3 4}
do_test collateA-1.10 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy                                  '}
} {2 3 4}
do_test collateA-1.11 {
  execsql {SELECT 'abc123'='abc123                         ' COLLATE RTRIM;}
} {1}
do_test collateA-1.12 {
  execsql {SELECT 'abc123                         '='abc123' COLLATE RTRIM;}
} {1}

do_test collateA-2.1 {
  execsql {
    CREATE INDEX i1b ON t1(b);
    CREATE INDEX i1c ON t1(c);
    PRAGMA integrity_check;
  }
} {ok}
do_test collateA-2.2 {
  execsql {SELECT a FROM t1 WHERE b='hello     '}
} {}
do_test collateA-2.3 {
  execsql {SELECT a FROM t1 WHERE c='hello     '}
} {1}
do_test collateA-2.4 {
  execsql {SELECT a FROM t1 WHERE b='xyzzy'}
} {}
do_test collateA-2.5 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy'}
} {2 3 4}
do_test collateA-2.6 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy '}
} {2 3 4}
do_test collateA-2.7 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy  '}
} {2 3 4}
do_test collateA-2.8 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy   '}
} {2 3 4}
do_test collateA-2.9 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy    '}
} {2 3 4}
do_test collateA-2.10 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy                                  '}
} {2 3 4}

do_test collateA-3.1 {
  db close
  sqlite3 db test.db
  execsql {
    REINDEX;
    PRAGMA integrity_check;
  }
} {ok}
do_test collateA-3.2 {
  execsql {SELECT a FROM t1 WHERE b='hello     '}
} {}
do_test collateA-3.3 {
  execsql {SELECT a FROM t1 WHERE c='hello     '}
} {1}
do_test collateA-3.4 {
  execsql {SELECT a FROM t1 WHERE b='xyzzy'}
} {}
do_test collateA-3.5 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy'}
} {2 3 4}
do_test collateA-3.6 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy '}
} {2 3 4}
do_test collateA-3.7 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy  '}
} {2 3 4}
do_test collateA-3.8 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy   '}
} {2 3 4}
do_test collateA-3.9 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy    '}
} {2 3 4}
do_test collateA-3.10 {
  execsql {SELECT a FROM t1 WHERE c='xyzzy                                  '}
} {2 3 4}


finish_test
Changes to test/pragma.test.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# This file implements tests for the PRAGMA command.
#
# $Id: pragma.test,v 1.56 2007/12/29 13:39:21 danielk1977 Exp $

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

# Test organization:
#
# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db.







|







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# This file implements tests for the PRAGMA command.
#
# $Id: pragma.test,v 1.57 2008/01/20 23:19:58 drh Exp $

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

# Test organization:
#
# pragma-1.*: Test cache_size, default_cache_size and synchronous on main db.
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
} ;# ifcapable trigger

ifcapable schema_pragmas {
  do_test pragma-11.1 {
    execsql2 {
      pragma collation_list;
    }
  } {seq 0 name NOCASE seq 1 name BINARY}
  do_test pragma-11.2 {
    db collate New_Collation blah...
    execsql {
      pragma collation_list;
    }
  } {0 New_Collation 1 NOCASE 2 BINARY}
}

ifcapable schema_pragmas&&tempdb {
  do_test pragma-12.1 {
    sqlite3 db2 test.db
    execsql {
      PRAGMA temp.table_info('abc');







|





|







974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
} ;# ifcapable trigger

ifcapable schema_pragmas {
  do_test pragma-11.1 {
    execsql2 {
      pragma collation_list;
    }
  } {seq 0 name NOCASE seq 1 name RTRIM seq 2 name BINARY}
  do_test pragma-11.2 {
    db collate New_Collation blah...
    execsql {
      pragma collation_list;
    }
  } {0 New_Collation 1 NOCASE 2 RTRIM 3 BINARY}
}

ifcapable schema_pragmas&&tempdb {
  do_test pragma-12.1 {
    sqlite3 db2 test.db
    execsql {
      PRAGMA temp.table_info('abc');