** This function is called as part of sqlite3_expert_analyze(). Candidate ** indexes have already been created in database sqlite3expert.dbm, this ** function populates sqlite_stat1 table in the same database. ** ** The stat1 data is generated by querying the */
| 11922 | ** The stat1 data is generated by querying the |
| 11923 | */ |
| 11924 | static int idxPopulateStat1(sqlite3expert *p, char **pzErr){ |
| 11925 | int rc = SQLITE_OK; |
| 11926 | int nMax =0; |
| 11927 | struct IdxRemCtx *pCtx = 0; |
| 11928 | struct IdxSampleCtx samplectx; |
| 11929 | int i; |
| 11930 | i64 iPrev = -100000; |
| 11931 | sqlite3_stmt *pAllIndex = 0; |
| 11932 | sqlite3_stmt *pIndexXInfo = 0; |
| 11933 | sqlite3_stmt *pWrite = 0; |
| 11934 | |
| 11935 | const char *zAllIndex = |
| 11936 | "SELECT s.rowid, s.name, l.name FROM " |
| 11937 | " sqlite_schema AS s, " |
| 11938 | " pragma_index_list(s.name) AS l " |
| 11939 | "WHERE s.type = 'table'"; |
| 11940 | const char *zIndexXInfo = |
| 11941 | "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key"; |
| 11942 | const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)"; |
| 11943 | |
| 11944 | /* If iSample==0, no sqlite_stat1 data is required. */ |
| 11945 | if( p->iSample==0 ) return SQLITE_OK; |
| 11946 | |
| 11947 | rc = idxLargestIndex(p->dbm, &nMax, pzErr); |
| 11948 | if( nMax<=0 || rc!=SQLITE_OK ) return rc; |
| 11949 | |
| 11950 | rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0); |
| 11951 | |
| 11952 | if( rc==SQLITE_OK ){ |
| 11953 | int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax); |
| 11954 | pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte); |
| 11955 | } |
| 11956 | |
| 11957 | if( rc==SQLITE_OK ){ |
| 11958 | sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); |
| 11959 | rc = sqlite3_create_function( |
| 11960 | dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0 |
| 11961 | ); |
| 11962 | } |
| 11963 | if( rc==SQLITE_OK ){ |
| 11964 | rc = sqlite3_create_function( |
| 11965 | p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0 |
| 11966 | ); |
| 11967 | } |
| 11968 | |
| 11969 | if( rc==SQLITE_OK ){ |
| 11970 | pCtx->nSlot = nMax+1; |
| 11971 | rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex); |
| 11972 | } |
| 11973 | if( rc==SQLITE_OK ){ |
| 11974 | rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo); |
| 11975 | } |
| 11976 | if( rc==SQLITE_OK ){ |
| 11977 | rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite); |
| 11978 | } |
| 11979 | |
| 11980 | while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){ |
| 11981 | i64 iRowid = sqlite3_column_int64(pAllIndex, 0); |
no test coverage detected