-C Create\snew\sbranch\snamed\s"auto-column"
-D 2022-02-10T02:09:43.272
+C A\sCLI\sfeature.\sauto\s.import\s(new\stable)\scolumns.\sWIP
+D 2022-02-10T03:21:48.442
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
F src/resolve.c ea935b87d6fb36c78b70cdc7b28561dc8f33f2ef37048389549c7b5ef9b0ba5e
F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
F src/select.c 3baa9dd8cf240654773c7974e2bcce398ac9dd24419c36684156963defe43b35
-F src/shell.c.in b800bf8e02d9b4fd97078b68ca4371048f7196fc63accaa99c3c5943f72c80a0
+F src/shell.c.in 90d01dff5c235191ee478436859026251e192b4312c8eb6b25f94c86c7915027
F src/sqlite.h.in 7047c4b60fa550264d6363bb1d983540e7828fb19d2d1e5aa43b52ca13144807
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
F src/sqlite3ext.h a95cb9ed106e3d39e2118e4dcc15a14faec3fa50d0093425083d340d9dfd96e6
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P e97c6ad4c915c82c2f0b347a0cdc8f80942c345194675a88174047ce0d0a43ad
-R 8f64a3cecc6fb60a0d94c857cb63d5b6
-T *branch * auto-column
-T *sym-auto-column *
-T -sym-trunk *
+P 066febe8931c5d90c009f05fe9ad0924ad35ec25a61ab42db63a9b9dbb1cecce
+R 49051b07ca9d50a8b27652a5a6f09819
U larrybr
-Z ac5c8b1c3b5d3e8ba3b923c1ff1fbc55
+Z c8e184e7bf6093d05c70b6f7f22f08a4
# Remove this line to create a well-formed Fossil manifest.
/*
** Run an SQL command and return the single integer result.
*/
-static int db_int(ShellState *p, const char *zSql){
+static int db_int(sqlite3 *db, const char *zSql){
sqlite3_stmt *pStmt;
int res = 0;
- sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
+ sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
res = sqlite3_column_int(pStmt,0);
}
}
for(i=0; i<ArraySize(aQuery); i++){
char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
- int val = db_int(p, zSql);
+ int val = db_int(p->db, zSql);
sqlite3_free(zSql);
utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
}
return z;
}
+
+/*
+ * zAutoColumn(zCol, &db) => Maybe init db, add column zCol to it.
+ * zAutoColumn(0, &db) => (db!=0) Form columns spec for CREATE TABLE,
+ * close db and set it to 0, and return the columns spec, to later
+ * be sqlite3_free()'ed by the caller.
+ * The return is 0 when either:
+ * (a) The db was not initialized and zCol==0 (There are no columns.)
+ * (b) zCol!=0 (Column was added, db initialized as needed.)
+ */
+#ifdef SHELL_DEBUG
+#define rc_err_oom_die(rc) \
+ if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
+ else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
+ fprintf(stderr,"E:%d\n",rc), assert(0)
+#else
+static void rc_err_oom_die(int rc){
+ if( rc==SQLITE_NOMEM ) shell_check_oom(0);
+ assert(rc==SQLITE_OK||rc==SQLITE_DONE);
+}
+#endif
+
+static const char *zCOL_DB = ":memory:";
+
+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)\
+";
+ static const char const *zTabFill = "\
+INSERT INTO ColNames(name,nlen,suff,reps) VALUES(?1,length(?1),'',0)\
+";
+ static const char const *zHasDupes = "\
+SELECT count(DISTINCT name||suff)<count(name) FROM ColNames\
+";
+ static const char const *zSetReps = "\
+UPDATE ColNames AS t SET reps=(SELECT count(*) FROM ColNames d \
+WHERE t.name=d.name)\
+";
+#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
+ static const char const *zColDigits = "\
+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\
+";
+ static const char const *zCollectVar = "\
+SELECT\
+ '('||\
+ group_concat(\
+ cname||' TEXT',\
+ ','||iif((rank-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\
+)";
+ int rc;
+ sqlite3_stmt *pStmt = 0;
+ assert(pDb!=0);
+ if( zColNew ){
+ /* Add initial or additional column. Init db if necessary. */
+ if( *pDb==0 ){
+ if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
+ if(*zCOL_DB!=':')
+ sqlite3_exec(*pDb,"drop table if exists ColNames",0,0,0);
+ rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
+ rc_err_oom_die(rc);
+ }
+ assert(*pDb!=0);
+ rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
+ rc_err_oom_die(rc);
+ rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
+ rc_err_oom_die(rc);
+ rc = sqlite3_step(pStmt);
+ rc_err_oom_die(rc);
+ sqlite3_finalize(pStmt);
+ return 0;
+ }else if( *pDb==0 ){
+ return 0;
+ }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 ){
+ rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
+ rc_err_oom_die(rc);
+ rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
+ rc_err_oom_die(rc);
+ rc = sqlite3_bind_int(pStmt, 1, nDigits);
+ rc_err_oom_die(rc);
+ rc = sqlite3_step(pStmt);
+ sqlite3_finalize(pStmt);
+ assert(rc==SQLITE_DONE);
+ hasDupes = db_int(*pDb, zHasDupes);
+ }
+ 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));
+ }else{
+ zColsSpec = 0;
+ }
+ sqlite3_finalize(pStmt);
+ sqlite3_close(*pDb);
+ *pDb = 0;
+ return zColsSpec;
+ }
+}
+
+
/*
** When running the ".recover" command, each output table, and the special
** orphaned row table if it is required, is represented by an instance
if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
zSchema, zTable);
- char cSep = '(';
+ sqlite3 *dbCols = 0;
+ char *zColDefs;
while( xRead(&sCtx) ){
- zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
- cSep = ',';
+ zAutoColumn(sCtx.z, &dbCols);
if( sCtx.cTerm!=sCtx.cColSep ) break;
- }
- if( cSep=='(' ){
+ }
+ zColDefs = zAutoColumn(0, &dbCols);
+ assert(dbCols==0);
+ if( zColDefs==0 ){
sqlite3_free(zCreate);
import_cleanup(&sCtx);
utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
rc = 1;
goto meta_command_exit;
}
- zCreate = sqlite3_mprintf("%z\n)", zCreate);
+ zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
if( eVerbose>=1 ){
utf8_printf(p->out, "%s\n", zCreate);
}