Artifact
0008e59eecedf3c723191d1a499dd58108153edf:
Wiki page
[The infamous DQS misfeature] by
sandro
2019-08-01 10:32:11.
0000: 44 20 32 30 31 39 2d 30 38 2d 30 31 54 31 30 3a D 2019-08-01T10:
0010: 33 32 3a 31 31 2e 33 34 36 0a 4c 20 54 68 65 5c 32:11.346.L The\
0020: 73 69 6e 66 61 6d 6f 75 73 5c 73 44 51 53 5c 73 sinfamous\sDQS\s
0030: 6d 69 73 66 65 61 74 75 72 65 0a 55 20 73 61 6e misfeature.U san
0040: 64 72 6f 0a 57 20 33 31 36 30 0a 3c 61 20 68 72 dro.W 3160.<a hr
0050: 65 66 3d 22 68 74 74 70 73 3a 2f 2f 77 77 77 2e ef="https://www.
0060: 67 61 69 61 2d 67 69 73 2e 69 74 2f 66 6f 73 73 gaia-gis.it/foss
0070: 69 6c 2f 6c 69 62 73 70 61 74 69 61 6c 69 74 65 il/libspatialite
0080: 2f 77 69 6b 69 3f 6e 61 6d 65 3d 34 2e 33 2e 30 /wiki?name=4.3.0
0090: 2d 64 6f 63 22 3e 62 61 63 6b 3c 2f 61 3e 3c 68 -doc">back</a><h
00a0: 72 3e 3c 62 72 3e 0d 0a 3c 68 31 3e 49 6e 74 72 r><br>..<h1>Intr
00b0: 6f 64 75 63 74 69 6f 6e 3c 2f 68 31 3e 0d 0a 3c oduction</h1>..<
00c0: 62 3e 44 51 53 3c 2f 62 3e 20 73 69 6d 70 6c 79 b>DQS</b> simply
00d0: 20 69 73 20 74 68 65 20 61 63 68 72 6f 6e 79 6d is the achronym
00e0: 20 66 6f 72 20 3c 62 3e 44 6f 75 62 6c 65 2d 51 for <b>Double-Q
00f0: 75 6f 74 65 64 20 53 74 72 69 6e 67 73 3c 2f 62 uoted Strings</b
0100: 3e 2e 3c 62 72 3e 0d 0a 41 6c 6c 20 70 72 65 76 >.<br>..All prev
0110: 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 ious versions of
0120: 20 3c 62 3e 53 51 4c 69 74 65 33 3c 2f 62 3e 20 <b>SQLite3</b>
0130: 77 65 72 65 20 61 66 66 65 63 74 65 64 20 62 79 were affected by
0140: 20 61 6e 20 6f 62 6e 6f 78 69 75 73 20 3c 69 3e an obnoxius <i>
0150: 3c 62 3e 44 51 53 20 6d 69 73 66 65 61 74 75 72 <b>DQS misfeatur
0160: 65 3c 2f 62 3e 3c 2f 69 3e 20 74 68 61 74 20 68 e</b></i> that h
0170: 61 73 20 62 65 65 6e 20 66 69 6e 61 6c 6c 79 20 as been finally
0180: 72 65 73 6f 6c 76 65 64 20 73 74 61 72 74 69 6e resolved startin
0190: 67 20 66 72 6f 6d 20 76 65 72 73 69 6f 6e 20 3c g from version <
01a0: 62 3e 33 2e 32 39 2e 30 3c 2f 62 3e 20 28 72 65 b>3.29.0</b> (re
01b0: 6c 65 61 73 65 64 20 6f 6e 20 3c 62 3e 32 30 31 leased on <b>201
01c0: 39 2d 30 37 2d 31 30 3c 2f 62 3e 29 2e 0d 0a 3c 9-07-10</b>)...<
01d0: 68 33 3e 57 68 61 74 20 74 68 65 20 49 53 4f 20 h3>What the ISO
01e0: 53 51 4c 20 73 74 61 6e 64 61 72 64 73 20 64 69 SQL standards di
01f0: 63 74 61 74 65 73 3c 2f 68 33 3e 0d 0a 54 68 65 ctates</h3>..The
0200: 20 49 53 4f 20 53 51 4c 20 73 74 61 6e 64 61 72 ISO SQL standar
0210: 64 20 68 61 73 20 74 77 6f 20 64 69 66 66 65 72 d has two differ
0220: 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74 65 78 ent kinds of tex
0230: 74 20 73 74 72 69 6e 67 73 3a 0d 0a 3c 6f 6c 3e t strings:..<ol>
0240: 0d 0a 3c 6c 69 3e 3c 62 3e 53 74 72 69 6e 67 20 ..<li><b>String
0250: 4c 69 74 65 72 61 6c 73 3c 2f 62 3e 20 28 61 6b Literals</b> (ak
0260: 61 20 3c 69 3e 3c 62 3e 54 65 78 74 20 43 6f 6e a <i><b>Text Con
0270: 73 74 61 6e 74 73 3c 2f 62 3e 3c 2f 69 3e 29 3c stants</b></i>)<
0280: 62 72 3e 0d 0a 20 20 20 20 54 68 65 79 20 61 72 br>.. They ar
0290: 65 20 61 6c 77 61 79 73 20 69 6e 74 65 72 70 72 e always interpr
02a0: 65 74 65 64 20 61 73 20 3c 62 3e 76 61 6c 75 65 eted as <b>value
02b0: 73 3c 2f 62 3e 2c 20 61 6e 64 20 3c 62 3e 6d 75 s</b>, and <b>mu
02c0: 73 74 3c 2f 62 3e 0d 0a 20 20 20 20 6e 65 63 65 st</b>.. nece
02d0: 73 73 61 72 69 6c 79 20 62 65 20 65 6e 63 6c 6f ssarily be enclo
02e0: 73 65 64 20 77 69 74 68 69 6e 20 61 20 70 61 69 sed within a pai
02f0: 72 20 6f 66 20 3c 62 3e 53 69 6e 67 6c 65 20 51 r of <b>Single Q
0300: 75 6f 74 65 73 3c 62 3e 2e 3c 62 72 3e 0d 0a 20 uotes<b>.<br>..
0310: 20 20 20 3c 62 3e 45 78 61 6d 70 6c 65 3c 2f 62 <b>Example</b
0320: 3e 3a 20 3c 69 3e 3c 62 3e 53 45 4c 45 43 54 20 >: <i><b>SELECT
0330: 2a 20 46 52 4f 4d 20 78 20 57 48 45 52 45 20 6e * FROM x WHERE n
0340: 61 6d 65 20 3d 20 27 73 6f 6d 65 74 68 69 6e 67 ame = 'something
0350: 27 3b 3c 2f 62 3e 3c 2f 69 3e 3c 2f 6c 69 3e 0d ';</b></i></li>.
0360: 0a 3c 6c 69 3e 3c 62 3e 53 51 4c 20 6e 61 6d 65 .<li><b>SQL name
0370: 73 3c 2f 62 3e 20 28 61 6b 61 20 3c 69 3e 3c 62 s</b> (aka <i><b
0380: 3e 53 51 4c 20 69 64 65 6e 74 69 66 69 65 72 73 >SQL identifiers
0390: 3c 2f 62 3e 3c 2f 69 3e 29 3c 62 72 3e 0d 0a 20 </b></i>)<br>..
03a0: 20 20 20 54 68 65 79 20 61 72 65 20 69 6e 74 65 They are inte
03b0: 6e 64 65 64 20 74 6f 20 69 64 65 6e 74 69 66 79 nded to identify
03c0: 20 3c 62 3e 54 61 62 6c 65 73 3c 2f 62 3e 2c 20 <b>Tables</b>,
03d0: 3c 62 3e 43 6f 6c 75 6d 6e 73 3c 2f 62 3e 2c 20 <b>Columns</b>,
03e0: 20 20 3c 62 3e 54 72 69 67 67 65 72 73 3c 2f 62 <b>Triggers</b
03f0: 3e 20 61 6e 64 20 61 6c 69 6b 65 2c 20 61 6e 64 > and alike, and
0400: 20 3c 62 3e 6d 75 73 74 3c 2f 62 3e 20 6f 70 74 <b>must</b> opt
0410: 69 6f 6e 61 6c 6c 79 20 65 6e 63 6c 6f 73 65 64 ionally enclosed
0420: 20 77 69 74 68 69 6e 20 61 20 70 61 69 72 20 6f within a pair o
0430: 72 20 3c 62 3e 44 6f 75 62 6c 65 20 51 75 6f 74 r <b>Double Quot
0440: 65 73 3c 2f 62 3e 2e 3c 62 72 3e 0d 0a 20 20 20 es</b>.<br>..
0450: 20 4d 6f 72 65 20 70 72 65 63 69 73 65 6c 79 3a More precisely:
0460: 3c 75 6c 3e 0d 0a 20 20 20 20 3c 6c 69 3e 53 51 <ul>.. <li>SQ
0470: 4c 20 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69 L names containi
0480: 6e 67 20 61 6e 79 20 3c 62 3e 3c 69 3e 66 6f 72 ng any <b><i>for
0490: 62 69 64 64 65 6e 20 63 68 61 72 61 63 74 65 72 bidden character
04a0: 3c 2f 69 3e 3c 2f 62 3e 20 6d 75 73 74 20 62 65 </i></b> must be
04b0: 20 61 6c 77 61 79 73 20 6d 61 73 71 75 65 64 20 always masqued
04c0: 62 79 20 65 6e 63 6c 6f 73 69 6e 67 20 74 68 65 by enclosing the
04d0: 20 69 6e 74 65 6e 64 65 64 20 6e 61 6d 65 20 77 intended name w
04e0: 69 74 68 69 6e 20 44 6f 75 62 6c 65 20 51 75 6f ithin Double Quo
04f0: 74 65 73 2e 3c 2f 6c 69 3e 0d 0a 20 20 20 20 3c tes.</li>.. <
0500: 6c 69 3e 61 6e 79 20 6f 74 68 65 72 20 53 51 4c li>any other SQL
0510: 20 6e 61 6d 65 20 28 6e 6f 74 20 63 6f 6e 74 61 name (not conta
0520: 69 6e 69 6e 67 20 66 6f 72 62 69 64 64 65 6e 20 ining forbidden
0530: 63 68 61 72 61 63 74 65 72 73 29 20 64 6f 65 73 characters) does
0540: 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 72 65 not strictly re
0550: 71 75 69 72 65 73 20 44 6f 75 62 6c 65 2d 71 75 quires Double-qu
0560: 6f 74 69 6e 67 2c 20 62 75 74 20 63 61 6e 20 62 oting, but can b
0570: 65 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 44 6f 75 e optionally Dou
0580: 62 6c 65 2d 71 75 6f 74 65 64 2e 3c 2f 6c 69 3e ble-quoted.</li>
0590: 3c 2f 75 6c 3e 0d 0a 20 20 20 20 3c 62 3e 45 78 </ul>.. <b>Ex
05a0: 61 6d 70 6c 65 3c 2f 62 3e 3a 20 3c 69 3e 3c 62 ample</b>: <i><b
05b0: 3e 53 45 4c 45 43 54 20 22 61 22 2c 20 22 62 22 >SELECT "a", "b"
05c0: 2c 20 22 63 22 20 46 52 4f 4d 20 22 78 22 3b 3c , "c" FROM "x";<
05d0: 2f 62 3e 3c 2f 69 3e 3c 2f 6c 69 3e 0d 0a 20 20 /b></i></li>..
05e0: 20 20 3c 2f 6c 69 3e 0d 0a 3c 2f 6f 6c 3e 0d 0a </li>..</ol>..
05f0: 3c 68 33 3e 57 68 61 74 20 77 61 73 20 65 66 66 <h3>What was eff
0600: 65 63 74 69 76 65 6c 79 20 73 75 70 70 6f 72 74 ectively support
0610: 65 64 20 62 79 20 53 51 4c 69 74 65 3c 2f 68 33 ed by SQLite</h3
0620: 3e 0d 0a 49 6e 20 61 6e 20 61 74 74 65 6d 70 74 >..In an attempt
0630: 20 74 6f 20 6d 69 6d 69 63 20 74 68 65 20 6e 6f to mimic the no
0640: 6e 2d 73 74 61 6e 64 61 72 64 20 62 65 68 61 76 n-standard behav
0650: 69 6f 72 20 6f 66 20 3c 62 3e 4d 79 53 51 4c 20 ior of <b>MySQL
0660: 33 2e 78 3c 2f 62 3e 20 53 51 4c 69 74 65 20 77 3.x</b> SQLite w
0670: 61 73 20 61 6c 77 61 79 73 20 76 65 72 79 20 74 as always very t
0680: 6f 6c 65 72 61 6e 74 20 61 62 6f 75 74 20 53 69 olerant about Si
0690: 6e 67 6c 65 2d 20 61 6e 64 20 44 6f 75 62 6c 65 ngle- and Double
06a0: 2d 51 75 6f 74 69 6e 67 2e 3c 62 72 3e 0d 0a 45 -Quoting.<br>..E
06b0: 76 65 6e 20 69 6e 20 74 68 65 20 63 61 73 65 20 ven in the case
06c0: 6f 66 20 69 6e 63 6f 72 72 65 63 74 20 71 75 6f of incorrect quo
06d0: 74 69 6e 67 20 53 51 4c 69 74 65 20 77 61 73 20 ting SQLite was
06e0: 6f 66 74 65 6e 20 61 62 6c 65 20 74 6f 20 67 75 often able to gu
06f0: 65 73 73 20 74 68 65 20 69 6e 74 65 6e 64 65 64 ess the intended
0700: 20 73 63 6f 70 65 20 6f 66 20 61 6e 79 20 73 74 scope of any st
0710: 72 69 6e 67 20 62 79 20 61 6e 61 6c 79 7a 69 6e ring by analyzin
0720: 67 20 69 74 73 20 63 6f 6e 74 65 78 74 2e 3c 62 g its context.<b
0730: 72 3e 0d 0a 49 74 20 63 6c 65 61 72 6c 79 20 77 r>..It clearly w
0740: 61 73 20 61 20 70 6f 74 65 6e 74 69 61 6c 6c 79 as a potentially
0750: 20 65 72 72 6f 72 20 70 72 6f 6e 65 20 61 70 70 error prone app
0760: 72 6f 61 63 68 2c 20 62 75 74 20 69 74 20 68 65 roach, but it he
0770: 6c 70 65 64 20 6d 61 6e 79 20 73 6c 6f 70 70 79 lped many sloppy
0780: 20 6f 72 20 6d 65 73 73 79 20 75 73 65 72 73 20 or messy users
0790: 74 6f 20 77 72 69 74 65 20 74 68 65 69 72 20 53 to write their S
07a0: 51 4c 20 71 75 65 72 69 65 73 20 77 69 74 68 20 QL queries with
07b0: 66 65 77 20 63 6f 6d 70 6c 69 63 61 74 69 6f 6e few complication
07c0: 73 2e 3c 62 72 3e 0d 0a 56 65 72 79 20 73 69 6d s.<br>..Very sim
07d0: 70 6c 79 20 73 61 69 64 2c 20 69 74 20 77 61 73 ply said, it was
07e0: 20 61 20 3c 62 3e 6d 69 73 66 65 61 74 75 72 65 a <b>misfeature
07f0: 3c 2f 62 3e 20 62 61 64 6c 79 20 65 6e 63 6f 75 </b> badly encou
0800: 72 61 67 69 6e 67 20 6d 61 6e 79 20 64 65 76 65 raging many deve
0810: 6c 6f 70 65 72 73 20 69 6e 74 6f 20 74 68 65 20 lopers into the
0820: 62 61 64 20 68 61 62 69 74 20 6f 66 20 6d 69 73 bad habit of mis
0830: 75 73 69 6e 67 20 64 6f 75 62 6c 65 2d 20 61 6e using double- an
0840: 64 20 73 69 6e 67 6c 65 2d 71 75 6f 74 69 6e 67 d single-quoting
0850: 2e 3c 62 72 3e 0d 0a 3c 68 33 3e 54 68 65 20 6d .<br>..<h3>The m
0860: 6f 73 74 20 72 65 63 65 6e 74 20 61 70 70 72 6f ost recent appro
0870: 61 63 68 20 6f 66 20 53 51 4c 69 74 65 20 74 6f ach of SQLite to
0880: 20 49 53 4f 20 53 51 4c 20 71 75 6f 74 69 6e 67 ISO SQL quoting
0890: 3c 2f 68 33 3e 20 0d 0a 49 6e 20 72 65 63 65 6e </h3> ..In recen
08a0: 74 20 74 69 6d 65 73 20 74 68 65 20 64 65 76 65 t times the deve
08b0: 6c 6f 70 65 72 73 20 6f 66 20 53 51 4c 69 74 65 lopers of SQLite
08c0: 20 66 69 6e 61 6c 6c 79 20 72 65 63 6f 67 6e 69 finally recogni
08d0: 7a 65 64 20 74 68 61 74 20 64 65 70 61 72 74 69 zed that departi
08e0: 6e 67 20 66 72 6f 6d 20 74 68 65 20 49 53 4f 20 ng from the ISO
08f0: 53 51 4c 20 73 74 61 6e 64 61 72 64 20 77 61 73 SQL standard was
0900: 6e 27 74 20 67 6f 6f 64 20 61 74 20 61 6c 6c 2e n't good at all.
0910: 3c 62 72 3e 0d 0a 41 6e 64 20 63 6f 6e 73 65 71 <br>..And conseq
0920: 75 65 6e 74 6c 79 20 74 68 65 79 20 64 65 63 69 uently they deci
0930: 64 65 64 20 74 68 61 74 20 73 74 61 72 74 69 6e ded that startin
0940: 67 20 66 72 6f 6d 20 76 65 72 73 69 6f 6e 20 3c g from version <
0950: 62 3e 33 2e 32 39 2e 30 3c 2f 62 3e 20 53 51 4c b>3.29.0</b> SQL
0960: 69 74 65 20 73 68 6f 75 6c 64 20 66 69 6e 61 6c ite should final
0970: 6c 79 20 62 65 20 61 62 6c 65 20 74 6f 20 63 6f ly be able to co
0980: 72 72 65 63 74 6c 79 20 65 6e 66 6f 72 63 65 20 rrectly enforce
0990: 73 74 72 69 63 74 20 49 53 4f 20 53 51 4c 20 71 strict ISO SQL q
09a0: 75 6f 74 69 6e 67 2e 3c 62 72 3e 0d 0a 55 6e 68 uoting.<br>..Unh
09b0: 61 70 70 69 6c 79 20 73 75 63 68 20 61 20 72 61 appily such a ra
09c0: 64 69 63 61 6c 20 63 68 61 6e 67 65 20 63 61 6e dical change can
09d0: 20 68 61 76 65 20 61 20 76 65 72 79 20 68 65 61 have a very hea
09e0: 76 79 20 69 6d 70 61 63 74 20 6f 6e 20 61 6c 72 vy impact on alr
09f0: 65 61 64 79 20 65 78 69 73 74 69 6e 67 20 61 70 eady existing ap
0a00: 70 6c 69 63 61 74 69 6f 6e 73 2c 20 73 6f 20 61 plications, so a
0a10: 20 6d 6f 64 65 72 61 74 65 20 61 70 70 72 6f 61 moderate approa
0a20: 63 68 20 77 61 73 20 61 64 6f 70 74 65 64 3a 0d ch was adopted:.
0a30: 0a 3c 75 6c 3e 0d 0a 3c 6c 69 3e 61 20 6e 65 77 .<ul>..<li>a new
0a40: 20 62 75 69 6c 64 2d 74 69 6d 65 20 6f 70 74 69 build-time opti
0a50: 6f 6e 20 68 61 73 20 62 65 65 6e 20 69 6e 74 72 on has been intr
0a60: 6f 64 75 63 65 2c 20 3c 62 3e 2d 44 53 51 4c 49 oduce, <b>-DSQLI
0a70: 54 45 5f 44 51 53 3d 3c 69 3e 6e 3c 2f 69 3e 3c TE_DQS=<i>n</i><
0a80: 2f 62 3e 3c 2f 6c 69 3e 0d 0a 3c 6c 69 3e 54 68 /b></li>..<li>Th
0a90: 65 20 64 65 66 61 75 6c 74 20 61 73 73 75 6d 70 e default assump
0aa0: 74 69 6f 6e 20 69 73 20 3c 62 3e 2d 44 53 51 4c tion is <b>-DSQL
0ab0: 49 54 45 5f 44 51 53 3d 33 3c 2f 62 3e 3c 62 72 ITE_DQS=3</b><br
0ac0: 3e 0d 0a 20 20 20 20 74 68 69 73 20 70 72 61 63 >.. this prac
0ad0: 74 69 63 61 6c 6c 79 20 63 6f 72 72 65 73 70 6f tically correspo
0ae0: 6e 64 73 20 74 6f 20 74 68 65 20 6f 6c 64 20 74 nds to the old t
0af0: 72 61 64 69 74 69 6f 6e 61 6c 20 62 65 68 61 76 raditional behav
0b00: 69 6f 72 20 28 69 2e 65 2e 20 73 75 70 70 6f 72 ior (i.e. suppor
0b10: 74 69 6e 67 20 61 20 76 65 72 79 20 70 65 72 6d ting a very perm
0b20: 69 73 73 69 76 65 20 61 6e 64 20 74 6f 6c 65 72 issive and toler
0b30: 61 6e 74 20 71 75 6f 74 69 6e 67 20 69 6e 74 65 ant quoting inte
0b40: 72 70 72 65 74 61 74 69 6f 6e 29 2e 3c 2f 6c 69 rpretation).</li
0b50: 3e 0d 0a 3c 6c 69 3e 42 75 74 20 74 68 65 20 64 >..<li>But the d
0b60: 65 76 65 6c 6f 70 65 72 73 20 6f 66 20 53 51 4c evelopers of SQL
0b70: 69 74 65 20 73 74 6f 6e 67 6c 79 20 72 65 63 63 ite stongly recc
0b80: 6f 6d 6d 65 64 20 74 6f 20 61 6c 77 61 79 73 20 ommed to always
0b90: 65 78 70 6c 69 63 69 74 6c 79 20 73 65 74 20 3c explicitly set <
0ba0: 62 3e 2d 44 53 51 4c 49 54 45 5f 44 51 53 3d 33 b>-DSQLITE_DQS=3
0bb0: 3c 2f 62 3e 3c 62 72 3e 0d 0a 20 20 20 20 74 68 </b><br>.. th
0bc0: 69 73 20 77 69 6c 6c 20 65 6e 66 6f 72 63 65 20 is will enforce
0bd0: 73 74 72 69 63 74 20 49 53 4f 20 53 51 4c 20 71 strict ISO SQL q
0be0: 75 6f 74 69 6e 67 2c 20 61 6e 64 20 77 69 6c 6c uoting, and will
0bf0: 20 64 65 66 69 6e 69 74 65 6c 79 20 65 72 61 64 definitely erad
0c00: 69 63 61 74 65 20 74 68 65 20 65 78 74 61 76 61 icate the extava
0c10: 67 61 6e 74 20 3c 62 3e 44 51 53 20 6d 69 73 66 gant <b>DQS misf
0c20: 65 61 75 72 65 3c 2f 62 3e 2e 3c 2f 6c 69 3e 0d eaure</b>.</li>.
0c30: 0a 3c 2f 75 6c 3e 0d 0a 0d 0a 0d 0a 3c 62 72 3e .</ul>......<br>
0c40: 3c 62 72 3e 0d 0a 3c 68 72 3e 3c 62 72 3e 0d 0a <br>..<hr><br>..
0c50: 3c 61 20 68 72 65 66 3d 22 68 74 74 70 73 3a 2f <a href="https:/
0c60: 2f 77 77 77 2e 67 61 69 61 2d 67 69 73 2e 69 74 /www.gaia-gis.it
0c70: 2f 66 6f 73 73 69 6c 2f 6c 69 62 73 70 61 74 69 /fossil/libspati
0c80: 61 6c 69 74 65 2f 77 69 6b 69 3f 6e 61 6d 65 3d alite/wiki?name=
0c90: 34 2e 33 2e 30 2d 64 6f 63 22 3e 62 61 63 6b 3c 4.3.0-doc">back<
0ca0: 2f 61 3e 0a 5a 20 35 62 61 66 32 31 63 34 32 63 /a>.Z 5baf21c42c
0cb0: 39 66 39 65 33 31 31 34 34 66 61 36 63 39 34 62 9f9e31144fa6c94b
0cc0: 35 33 33 64 30 38 0a 533d08.