18e0b8e06Sdan /* 28e0b8e06Sdan ** 2017 April 07 38e0b8e06Sdan ** 48e0b8e06Sdan ** The author disclaims copyright to this source code. In place of 58e0b8e06Sdan ** a legal notice, here is a blessing: 68e0b8e06Sdan ** 78e0b8e06Sdan ** May you do good and not evil. 88e0b8e06Sdan ** May you find forgiveness for yourself and forgive others. 98e0b8e06Sdan ** May you share freely, never taking more than you give. 108e0b8e06Sdan ** 118e0b8e06Sdan ************************************************************************* 128e0b8e06Sdan */ 13*b88eaf16Smistachkin #if !defined(SQLITEEXPERT_H) 14*b88eaf16Smistachkin #define SQLITEEXPERT_H 1 158e0b8e06Sdan #include "sqlite3.h" 168e0b8e06Sdan 178e0b8e06Sdan typedef struct sqlite3expert sqlite3expert; 188e0b8e06Sdan 198e0b8e06Sdan /* 208e0b8e06Sdan ** Create a new sqlite3expert object. 21a4e61024Sdan ** 22a4e61024Sdan ** If successful, a pointer to the new object is returned and (*pzErr) set 23a4e61024Sdan ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to 24a4e61024Sdan ** an English-language error message. In this case it is the responsibility 25a4e61024Sdan ** of the caller to eventually free the error message buffer using 26a4e61024Sdan ** sqlite3_free(). 278e0b8e06Sdan */ 288e0b8e06Sdan sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr); 298e0b8e06Sdan 308e0b8e06Sdan /* 31e53b4f97Sdan ** Configure an sqlite3expert object. 32e53b4f97Sdan ** 33e53b4f97Sdan ** EXPERT_CONFIG_SAMPLE: 34e53b4f97Sdan ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for 35e53b4f97Sdan ** each candidate index. This involves scanning and sorting the entire 36e53b4f97Sdan ** contents of each user database table once for each candidate index 37e53b4f97Sdan ** associated with the table. For large databases, this can be 38e53b4f97Sdan ** prohibitively slow. This option allows the sqlite3expert object to 39e53b4f97Sdan ** be configured so that sqlite_stat1 data is instead generated based on a 40e53b4f97Sdan ** subset of each table, or so that no sqlite_stat1 data is used at all. 41e53b4f97Sdan ** 42e53b4f97Sdan ** A single integer argument is passed to this option. If the value is less 43e53b4f97Sdan ** than or equal to zero, then no sqlite_stat1 data is generated or used by 44e53b4f97Sdan ** the analysis - indexes are recommended based on the database schema only. 45e53b4f97Sdan ** Or, if the value is 100 or greater, complete sqlite_stat1 data is 46e53b4f97Sdan ** generated for each candidate index (this is the default). Finally, if the 47e53b4f97Sdan ** value falls between 0 and 100, then it represents the percentage of user 48e53b4f97Sdan ** table rows that should be considered when generating sqlite_stat1 data. 49e53b4f97Sdan ** 50e53b4f97Sdan ** Examples: 51e53b4f97Sdan ** 52e53b4f97Sdan ** // Do not generate any sqlite_stat1 data 53e53b4f97Sdan ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0); 54e53b4f97Sdan ** 55e53b4f97Sdan ** // Generate sqlite_stat1 data based on 10% of the rows in each table. 56e53b4f97Sdan ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10); 57e53b4f97Sdan */ 58e53b4f97Sdan int sqlite3_expert_config(sqlite3expert *p, int op, ...); 59e53b4f97Sdan 60e53b4f97Sdan #define EXPERT_CONFIG_SAMPLE 1 /* int */ 61e53b4f97Sdan 62e53b4f97Sdan /* 63a4e61024Sdan ** Specify zero or more SQL statements to be included in the analysis. 64a4e61024Sdan ** 65a4e61024Sdan ** Buffer zSql must contain zero or more complete SQL statements. This 66a4e61024Sdan ** function parses all statements contained in the buffer and adds them 67a4e61024Sdan ** to the internal list of statements to analyze. If successful, SQLITE_OK 68a4e61024Sdan ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example 69a4e61024Sdan ** due to a error in the SQL - an SQLite error code is returned and (*pzErr) 70a4e61024Sdan ** may be set to point to an English language error message. In this case 71a4e61024Sdan ** the caller is responsible for eventually freeing the error message buffer 72a4e61024Sdan ** using sqlite3_free(). 73a4e61024Sdan ** 74a4e61024Sdan ** If an error does occur while processing one of the statements in the 75a4e61024Sdan ** buffer passed as the second argument, none of the statements in the 76a4e61024Sdan ** buffer are added to the analysis. 77a4e61024Sdan ** 78a4e61024Sdan ** This function must be called before sqlite3_expert_analyze(). If a call 79a4e61024Sdan ** to this function is made on an sqlite3expert object that has already 80a4e61024Sdan ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned 81a4e61024Sdan ** immediately and no statements are added to the analysis. 828e0b8e06Sdan */ 838e0b8e06Sdan int sqlite3_expert_sql( 84a4e61024Sdan sqlite3expert *p, /* From a successful sqlite3_expert_new() */ 85a4e61024Sdan const char *zSql, /* SQL statement(s) to add */ 868e0b8e06Sdan char **pzErr /* OUT: Error message (if any) */ 878e0b8e06Sdan ); 888e0b8e06Sdan 89e53b4f97Sdan 90a4e61024Sdan /* 91a4e61024Sdan ** This function is called after the sqlite3expert object has been configured 92a4e61024Sdan ** with all SQL statements using sqlite3_expert_sql() to actually perform 93a4e61024Sdan ** the analysis. Once this function has been called, it is not possible to 94a4e61024Sdan ** add further SQL statements to the analysis. 95a4e61024Sdan ** 96a4e61024Sdan ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if 97a4e61024Sdan ** an error occurs, an SQLite error code is returned and (*pzErr) set to 98a4e61024Sdan ** point to a buffer containing an English language error message. In this 99a4e61024Sdan ** case it is the responsibility of the caller to eventually free the buffer 100a4e61024Sdan ** using sqlite3_free(). 101a4e61024Sdan ** 102a4e61024Sdan ** If an error does occur within this function, the sqlite3expert object 103a4e61024Sdan ** is no longer useful for any purpose. At that point it is no longer 104a4e61024Sdan ** possible to add further SQL statements to the object or to re-attempt 105a4e61024Sdan ** the analysis. The sqlite3expert object must still be freed using a call 106a4e61024Sdan ** sqlite3_expert_destroy(). 107a4e61024Sdan */ 1088e0b8e06Sdan int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr); 1098e0b8e06Sdan 1108e0b8e06Sdan /* 111a4e61024Sdan ** Return the total number of statements loaded using sqlite3_expert_sql(). 112a4e61024Sdan ** The total number of SQL statements may be different from the total number 113a4e61024Sdan ** to calls to sqlite3_expert_sql(). 1148e0b8e06Sdan */ 1158e0b8e06Sdan int sqlite3_expert_count(sqlite3expert*); 1168e0b8e06Sdan 1178e0b8e06Sdan /* 1188e0b8e06Sdan ** Return a component of the report. 119a4e61024Sdan ** 120a4e61024Sdan ** This function is called after sqlite3_expert_analyze() to extract the 121a4e61024Sdan ** results of the analysis. Each call to this function returns either a 122a4e61024Sdan ** NULL pointer or a pointer to a buffer containing a nul-terminated string. 123a4e61024Sdan ** The value passed as the third argument must be one of the EXPERT_REPORT_* 124a4e61024Sdan ** #define constants defined below. 125a4e61024Sdan ** 126a4e61024Sdan ** For some EXPERT_REPORT_* parameters, the buffer returned contains 127a4e61024Sdan ** information relating to a specific SQL statement. In these cases that 128a4e61024Sdan ** SQL statement is identified by the value passed as the second argument. 129a4e61024Sdan ** SQL statements are numbered from 0 in the order in which they are parsed. 130a4e61024Sdan ** If an out-of-range value (less than zero or equal to or greater than the 131a4e61024Sdan ** value returned by sqlite3_expert_count()) is passed as the second argument 132a4e61024Sdan ** along with such an EXPERT_REPORT_* parameter, NULL is always returned. 133a4e61024Sdan ** 134a4e61024Sdan ** EXPERT_REPORT_SQL: 135a4e61024Sdan ** Return the text of SQL statement iStmt. 136a4e61024Sdan ** 137a4e61024Sdan ** EXPERT_REPORT_INDEXES: 138a4e61024Sdan ** Return a buffer containing the CREATE INDEX statements for all recommended 139a4e61024Sdan ** indexes for statement iStmt. If there are no new recommeded indexes, NULL 140a4e61024Sdan ** is returned. 141a4e61024Sdan ** 142a4e61024Sdan ** EXPERT_REPORT_PLAN: 143a4e61024Sdan ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query 144a4e61024Sdan ** iStmt after the proposed indexes have been added to the database schema. 145a4e61024Sdan ** 146a4e61024Sdan ** EXPERT_REPORT_CANDIDATES: 147a4e61024Sdan ** Return a pointer to a buffer containing the CREATE INDEX statements 148a4e61024Sdan ** for all indexes that were tested (for all SQL statements). The iStmt 149a4e61024Sdan ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls. 1508e0b8e06Sdan */ 1518e0b8e06Sdan const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport); 1528e0b8e06Sdan 1538e0b8e06Sdan /* 1548e0b8e06Sdan ** Values for the third argument passed to sqlite3_expert_report(). 1558e0b8e06Sdan */ 1568e0b8e06Sdan #define EXPERT_REPORT_SQL 1 1578e0b8e06Sdan #define EXPERT_REPORT_INDEXES 2 1588e0b8e06Sdan #define EXPERT_REPORT_PLAN 3 159a4e61024Sdan #define EXPERT_REPORT_CANDIDATES 4 1608e0b8e06Sdan 1618e0b8e06Sdan /* 162a4e61024Sdan ** Free an (sqlite3expert*) handle and all associated resources. There 163a4e61024Sdan ** should be one call to this function for each successful call to 164a4e61024Sdan ** sqlite3-expert_new(). 1658e0b8e06Sdan */ 1668e0b8e06Sdan void sqlite3_expert_destroy(sqlite3expert*); 1678e0b8e06Sdan 168*b88eaf16Smistachkin #endif /* !defined(SQLITEEXPERT_H) */ 169