MCPcopy Create free account
hub / github.com/GaijinEntertainment/daScript / zAutoColumn

Function zAutoColumn

modules/dasSQLITE/sqlite/shell.c:22792–22965  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22790#endif
22791
22792static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
22793 /* Queries and D{D,M}L used here */
22794 static const char * const zTabMake = "\
22795CREATE TABLE ColNames(\
22796 cpos INTEGER PRIMARY KEY,\
22797 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
22798CREATE VIEW RepeatedNames AS \
22799SELECT DISTINCT t.name FROM ColNames t \
22800WHERE t.name COLLATE NOCASE IN (\
22801 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
22802);\
22803";
22804 static const char * const zTabFill = "\
22805INSERT INTO ColNames(name,nlen,chop,reps,suff)\
22806 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
22807";
22808 static const char * const zHasDupes = "\
22809SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
22810 <count(name) FROM ColNames\
22811";
22812#ifdef SHELL_COLUMN_RENAME_CLEAN
22813 static const char * const zDedoctor = "\
22814UPDATE ColNames SET chop=iif(\
22815 (substring(name,nlen,1) BETWEEN '0' AND '9')\
22816 AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
22817 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
22818 0\
22819)\
22820";
22821#endif
22822 static const char * const zSetReps = "\
22823UPDATE ColNames AS t SET reps=\
22824(SELECT count(*) FROM ColNames d \
22825 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
22826 COLLATE NOCASE\
22827)\
22828";
22829#ifdef SQLITE_ENABLE_MATH_FUNCTIONS
22830 static const char * const zColDigits = "\
22831SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
22832";
22833#else
22834 /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
22835 static const char * const zColDigits = "\
22836SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
22837 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
22838 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
22839";
22840#endif
22841 static const char * const zRenameRank =
22842#ifdef SHELL_COLUMN_RENAME_CLEAN
22843 "UPDATE ColNames AS t SET suff="
22844 "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
22845#else /* ...RENAME_MINIMAL_ONE_PASS */
22846"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
22847" SELECT 0 AS nlz"
22848" UNION"
22849" SELECT nlz+1 AS nlz FROM Lzn"

Callers 1

do_meta_commandFunction · 0.85

Calls 4

assertFunction · 0.85
sqlite3_execFunction · 0.85
rc_err_oom_dieFunction · 0.85
db_intFunction · 0.85

Tested by

no test coverage detected