CLI auto-column rename revamped after forum discussion.
FossilOrigin-Name: 2da1f8e45eb0cb1ead6049df6a31a903bfad5a7abd27e92cd3dac25fe355aec3
This commit is contained in:
parent
58a53d6e7b
commit
4c5c6214b1
14
manifest
14
manifest
@ -1,5 +1,5 @@
|
||||
C A\sCLI\sfeature.\sauto\s.import\s(new\stable)\scolumns.\sWIP
|
||||
D 2022-02-10T03:21:48.442
|
||||
C CLI\sauto-column\srename\srevamped\safter\sforum\sdiscussion.
|
||||
D 2022-02-11T01:21:09.366
|
||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
|
||||
@ -553,7 +553,7 @@ F src/random.c 097dc8b31b8fba5a9aca1697aeb9fd82078ec91be734c16bffda620ced7ab83c
|
||||
F src/resolve.c ea935b87d6fb36c78b70cdc7b28561dc8f33f2ef37048389549c7b5ef9b0ba5e
|
||||
F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
|
||||
F src/select.c 3baa9dd8cf240654773c7974e2bcce398ac9dd24419c36684156963defe43b35
|
||||
F src/shell.c.in 90d01dff5c235191ee478436859026251e192b4312c8eb6b25f94c86c7915027
|
||||
F src/shell.c.in 14c6c05a25042873ebb011e78158d98ee360ca1564490e28c7295367c4b6dfac
|
||||
F src/sqlite.h.in 7047c4b60fa550264d6363bb1d983540e7828fb19d2d1e5aa43b52ca13144807
|
||||
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
|
||||
F src/sqlite3ext.h a95cb9ed106e3d39e2118e4dcc15a14faec3fa50d0093425083d340d9dfd96e6
|
||||
@ -1388,7 +1388,7 @@ F test/sharedA.test 49d87ec54ab640fbbc3786ee3c01de94aaa482a3a9f834ad3fe92770eb69
|
||||
F test/sharedB.test 16cc7178e20965d75278f410943109b77b2e645e
|
||||
F test/shared_err.test 32634e404a3317eeb94abc7a099c556a346fdb8fb3858dbe222a4cbb8926a939
|
||||
F test/sharedlock.test 5ede3c37439067c43b0198f580fd374ebf15d304
|
||||
F test/shell1.test ce2f66ba87b2f0dbd71fe55b97e189feb28767aab788c9f1fbfdfc5bce11fca2
|
||||
F test/shell1.test b224e0793c5f48aa3749e65d8c64b93a30731bd206f2e41e6c5f1bee1bdb16c6
|
||||
F test/shell2.test 89e4b2db062d52baed75022227b462d085cff495809de1699652779d8e0257d6
|
||||
F test/shell3.test a50628ab1d78d90889d9d3f32fb2c084ee15674771e96afe954aaa0accd1de3c
|
||||
F test/shell4.test 8f6c0fce4abed19a8a7f7262517149812a04caa905d01bdc8f5e92573504b759
|
||||
@ -1944,8 +1944,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
|
||||
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
||||
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
||||
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
||||
P 066febe8931c5d90c009f05fe9ad0924ad35ec25a61ab42db63a9b9dbb1cecce
|
||||
R 49051b07ca9d50a8b27652a5a6f09819
|
||||
P 7e3be36dfcb65c87a87344cf91c0c8f8d1aff0f2e4bcc23444a8f6b8ddcdae69
|
||||
R ea4a830aa3e422eb65f0e056f130ef8f
|
||||
U larrybr
|
||||
Z c8e184e7bf6093d05c70b6f7f22f08a4
|
||||
Z 218fbe1e74345194717208c07bd21226
|
||||
# Remove this line to create a well-formed Fossil manifest.
|
||||
|
@ -1 +1 @@
|
||||
7e3be36dfcb65c87a87344cf91c0c8f8d1aff0f2e4bcc23444a8f6b8ddcdae69
|
||||
2da1f8e45eb0cb1ead6049df6a31a903bfad5a7abd27e92cd3dac25fe355aec3
|
@ -7245,24 +7245,57 @@ static void rc_err_oom_die(int rc){
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
|
||||
static char zCOL_DB[] = SHELL_COLFIX_DB;
|
||||
#else /* Otherwise, memory is faster/better for the transient DB. */
|
||||
static const char *zCOL_DB = ":memory:";
|
||||
#endif
|
||||
|
||||
static char *zAutoColumn(const char *zColNew, sqlite3 **pDb){
|
||||
/* Queries and D{D,M}L used here */
|
||||
static const char const *zTabMake = "\
|
||||
CREATE TABLE ColNames(\
|
||||
rank INTEGER PRIMARY KEY,\
|
||||
name TEXT, nlen INT, suff TEXT, reps INT)\
|
||||
cpos INTEGER PRIMARY KEY,\
|
||||
name TEXT, nlen INT, chop INT, reps INT, suff TEXT)\
|
||||
";
|
||||
static const char const *zTabFill = "\
|
||||
INSERT INTO ColNames(name,nlen,suff,reps) VALUES(?1,length(?1),'',0)\
|
||||
INSERT INTO ColNames(name,nlen,chop,reps,suff)\
|
||||
VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
|
||||
";
|
||||
static const char const *zHasDupes = "\
|
||||
SELECT count(DISTINCT name||suff)<count(name) FROM ColNames\
|
||||
SELECT count(DISTINCT substring(name,1,nlen-chop)||suff)\
|
||||
<count(name) FROM ColNames\
|
||||
";
|
||||
static const char const *zDedoctor = "\
|
||||
WITH chopped AS ( \
|
||||
WITH RECURSIVE chopping(cpos, name, chop, nlen) AS ( \
|
||||
SELECT cpos, name, 0, nlen FROM ColNames \
|
||||
WHERE cpos IN ( \
|
||||
WITH RECURSIVE choppable(nc, name, cpos) AS \
|
||||
(SELECT nlen AS nc, name, cpos \
|
||||
FROM ColNames \
|
||||
UNION ALL \
|
||||
SELECT nc-1 AS nc, name, cpos \
|
||||
FROM choppable \
|
||||
WHERE substring(name, nc, 1) BETWEEN '0' AND '9'\
|
||||
) SELECT cpos /*name*/ FROM choppable \
|
||||
WHERE nc<length(name) and substring(name, nc, 1)='_'\
|
||||
)\
|
||||
UNION ALL\
|
||||
SELECT cpos, name, chop+1 AS chop, nlen \
|
||||
FROM chopping \
|
||||
WHERE instr('_0123456789', substring(name, nlen-chop, 1))>0 \
|
||||
)\
|
||||
SELECT cpos, name, max(chop) AS chop FROM chopping s \
|
||||
GROUP BY cpos \
|
||||
) UPDATE ColNames AS c \
|
||||
SET chop=n.chop FROM chopped n WHERE c.cpos=n.cpos \
|
||||
";
|
||||
static const char const *zSetReps = "\
|
||||
UPDATE ColNames AS t SET reps=(SELECT count(*) FROM ColNames d \
|
||||
WHERE t.name=d.name)\
|
||||
UPDATE ColNames AS t SET reps=\
|
||||
(SELECT count(*) FROM ColNames d \
|
||||
WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
|
||||
)\
|
||||
";
|
||||
#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
|
||||
static const char const *zColDigits = "\
|
||||
@ -7270,20 +7303,20 @@ SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
|
||||
";
|
||||
#endif
|
||||
static const char const *zRenameRank = "\
|
||||
UPDATE ColNames AS t SET suff=printf('_%0*d', $1, rank) WHERE reps>1\
|
||||
UPDATE ColNames AS t SET suff=iif(reps>1, printf('_%0*d', $1, cpos), '')\
|
||||
";
|
||||
static const char const *zCollectVar = "\
|
||||
SELECT\
|
||||
'('||\
|
||||
group_concat(\
|
||||
cname||' TEXT',\
|
||||
','||iif((rank-1)%4>0, ' ', x'0a'||' '))\
|
||||
','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
|
||||
||')' AS ColsSpec \
|
||||
FROM (\
|
||||
SELECT rank,\
|
||||
iif($1, printf('Col_%0*d', $2, rank), printf('\"%w\"', name||suff)) AS cname \
|
||||
FROM ColNames ORDER BY rank\
|
||||
SELECT cpos, printf('\"%w\"',printf('%.*s%s', nlen-chop,name,suff)) AS cname \
|
||||
FROM ColNames ORDER BY cpos\
|
||||
)";
|
||||
|
||||
int rc;
|
||||
sqlite3_stmt *pStmt = 0;
|
||||
assert(pDb!=0);
|
||||
@ -7291,8 +7324,10 @@ FROM (\
|
||||
/* Add initial or additional column. Init db if necessary. */
|
||||
if( *pDb==0 ){
|
||||
if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
|
||||
#ifdef SHELL_COLFIX_DB
|
||||
if(*zCOL_DB!=':')
|
||||
sqlite3_exec(*pDb,"drop table if exists ColNames",0,0,0);
|
||||
#endif
|
||||
rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
|
||||
rc_err_oom_die(rc);
|
||||
}
|
||||
@ -7310,14 +7345,15 @@ FROM (\
|
||||
}else{
|
||||
/* Formulate the columns spec, close the DB, zero *pDb. */
|
||||
char *zColsSpec = 0;
|
||||
enum { RK_None, RK_AppendColNum, RK_Replace } renameKind = RK_None;
|
||||
int hasDupes = db_int(*pDb, zHasDupes);
|
||||
#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
|
||||
int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
|
||||
#else
|
||||
# define nDigits 2
|
||||
#endif
|
||||
while( hasDupes && ++renameKind<RK_Replace ){
|
||||
if( hasDupes ){
|
||||
rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
|
||||
rc_err_oom_die(rc);
|
||||
rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
|
||||
rc_err_oom_die(rc);
|
||||
rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
|
||||
@ -7327,12 +7363,10 @@ FROM (\
|
||||
rc = sqlite3_step(pStmt);
|
||||
sqlite3_finalize(pStmt);
|
||||
assert(rc==SQLITE_DONE);
|
||||
hasDupes = db_int(*pDb, zHasDupes);
|
||||
}
|
||||
assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
|
||||
rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
|
||||
rc_err_oom_die(rc);
|
||||
sqlite3_bind_int(pStmt, 1, (renameKind==RK_Replace));
|
||||
sqlite3_bind_int(pStmt, 2, nDigits);
|
||||
rc = sqlite3_step(pStmt);
|
||||
if( rc==SQLITE_ROW ){
|
||||
zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
|
||||
@ -8896,7 +8930,7 @@ static int do_meta_command(char *zLine, ShellState *p){
|
||||
while( xRead(&sCtx) ){
|
||||
zAutoColumn(sCtx.z, &dbCols);
|
||||
if( sCtx.cTerm!=sCtx.cColSep ) break;
|
||||
}
|
||||
}
|
||||
zColDefs = zAutoColumn(0, &dbCols);
|
||||
assert(dbCols==0);
|
||||
if( zColDefs==0 ){
|
||||
|
@ -593,7 +593,8 @@ CREATE VIEW v2 AS SELECT x+1 AS y FROM t1
|
||||
/* v2(y) */;
|
||||
CREATE VIEW v1 AS SELECT y+1 FROM v2
|
||||
/* v1("y+1") */;}}
|
||||
db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}
|
||||
|
||||
catch {db eval {DROP VIEW v1; DROP VIEW v2; DROP TABLE t1;}}
|
||||
}
|
||||
|
||||
# .separator STRING Change column separator used by output and .import
|
||||
|
Loading…
Reference in New Issue
Block a user