Fossil SCM

Add the "helptext" virtual table. This is a stepping-stone to adding help-text search.

drh 2020-06-20 00:15 trunk
Commit b2dacfcd735d4b1c4b4e1f1c1efe8b50ce3410b78c3c5cc72e6f93055c4f4814
2 files changed +230 +1
--- src/dispatch.c
+++ src/dispatch.c
@@ -804,5 +804,235 @@
804804
*/
805805
const Setting *setting_info(int *pnCount){
806806
if( pnCount ) *pnCount = (int)(sizeof(aSetting)/sizeof(aSetting[0])) - 1;
807807
return aSetting;
808808
}
809
+
810
+/*****************************************************************************
811
+** A virtual table for accessing the information in aCommand[], and
812
+** especially the help-text
813
+*/
814
+
815
+/* helptextVtab_vtab is a subclass of sqlite3_vtab which is
816
+** underlying representation of the virtual table
817
+*/
818
+typedef struct helptextVtab_vtab helptextVtab_vtab;
819
+struct helptextVtab_vtab {
820
+ sqlite3_vtab base; /* Base class - must be first */
821
+ /* Add new fields here, as necessary */
822
+};
823
+
824
+/* helptextVtab_cursor is a subclass of sqlite3_vtab_cursor which will
825
+** serve as the underlying representation of a cursor that scans
826
+** over rows of the result
827
+*/
828
+typedef struct helptextVtab_cursor helptextVtab_cursor;
829
+struct helptextVtab_cursor {
830
+ sqlite3_vtab_cursor base; /* Base class - must be first */
831
+ /* Insert new fields here. For this helptextVtab we only keep track
832
+ ** of the rowid */
833
+ sqlite3_int64 iRowid; /* The rowid */
834
+};
835
+
836
+/*
837
+** The helptextVtabConnect() method is invoked to create a new
838
+** helptext virtual table.
839
+**
840
+** Think of this routine as the constructor for helptextVtab_vtab objects.
841
+**
842
+** All this routine needs to do is:
843
+**
844
+** (1) Allocate the helptextVtab_vtab object and initialize all fields.
845
+**
846
+** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
847
+** result set of queries against the virtual table will look like.
848
+*/
849
+static int helptextVtabConnect(
850
+ sqlite3 *db,
851
+ void *pAux,
852
+ int argc, const char *const*argv,
853
+ sqlite3_vtab **ppVtab,
854
+ char **pzErr
855
+){
856
+ helptextVtab_vtab *pNew;
857
+ int rc;
858
+
859
+ rc = sqlite3_declare_vtab(db,
860
+ "CREATE TABLE x(name,type,flags,helptext)"
861
+ );
862
+ if( rc==SQLITE_OK ){
863
+ pNew = sqlite3_malloc( sizeof(*pNew) );
864
+ *ppVtab = (sqlite3_vtab*)pNew;
865
+ if( pNew==0 ) return SQLITE_NOMEM;
866
+ memset(pNew, 0, sizeof(*pNew));
867
+ }
868
+ return rc;
869
+}
870
+
871
+/*
872
+** This method is the destructor for helptextVtab_vtab objects.
873
+*/
874
+static int helptextVtabDisconnect(sqlite3_vtab *pVtab){
875
+ helptextVtab_vtab *p = (helptextVtab_vtab*)pVtab;
876
+ sqlite3_free(p);
877
+ return SQLITE_OK;
878
+}
879
+
880
+/*
881
+** Constructor for a new helptextVtab_cursor object.
882
+*/
883
+static int helptextVtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
884
+ helptextVtab_cursor *pCur;
885
+ pCur = sqlite3_malloc( sizeof(*pCur) );
886
+ if( pCur==0 ) return SQLITE_NOMEM;
887
+ memset(pCur, 0, sizeof(*pCur));
888
+ *ppCursor = &pCur->base;
889
+ return SQLITE_OK;
890
+}
891
+
892
+/*
893
+** Destructor for a helptextVtab_cursor.
894
+*/
895
+static int helptextVtabClose(sqlite3_vtab_cursor *cur){
896
+ helptextVtab_cursor *pCur = (helptextVtab_cursor*)cur;
897
+ sqlite3_free(pCur);
898
+ return SQLITE_OK;
899
+}
900
+
901
+
902
+/*
903
+** Advance a helptextVtab_cursor to its next row of output.
904
+*/
905
+static int helptextVtabNext(sqlite3_vtab_cursor *cur){
906
+ helptextVtab_cursor *pCur = (helptextVtab_cursor*)cur;
907
+ pCur->iRowid++;
908
+ return SQLITE_OK;
909
+}
910
+
911
+/*
912
+** Return values of columns for the row at which the helptextVtab_cursor
913
+** is currently pointing.
914
+*/
915
+static int helptextVtabColumn(
916
+ sqlite3_vtab_cursor *cur, /* The cursor */
917
+ sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
918
+ int i /* Which column to return */
919
+){
920
+ helptextVtab_cursor *pCur = (helptextVtab_cursor*)cur;
921
+ const CmdOrPage *pPage = aCommand + pCur->iRowid;
922
+ switch( i ){
923
+ case 0: /* name */
924
+ sqlite3_result_text(ctx, pPage->zName, -1, SQLITE_STATIC);
925
+ break;
926
+ case 1: { /* type */
927
+ const char *zType = 0;
928
+ if( pPage->eCmdFlags & CMDFLAG_COMMAND ){
929
+ zType = "command";
930
+ }else if( pPage->eCmdFlags & CMDFLAG_WEBPAGE ){
931
+ zType = "webpage";
932
+ }else if( pPage->eCmdFlags & CMDFLAG_SETTING ){
933
+ zType = "setting";
934
+ }
935
+ sqlite3_result_text(ctx, zType, -1, SQLITE_STATIC);
936
+ break;
937
+ }
938
+ case 2: /* flags */
939
+ sqlite3_result_int(ctx, pPage->eCmdFlags);
940
+ break;
941
+ case 3: /* helptext */
942
+ sqlite3_result_text(ctx, pPage->zHelp, -1, SQLITE_STATIC);
943
+ break;
944
+ }
945
+ return SQLITE_OK;
946
+}
947
+
948
+/*
949
+** Return the rowid for the current row. In this implementation, the
950
+** rowid is the same as the output value.
951
+*/
952
+static int helptextVtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
953
+ helptextVtab_cursor *pCur = (helptextVtab_cursor*)cur;
954
+ *pRowid = pCur->iRowid;
955
+ return SQLITE_OK;
956
+}
957
+
958
+/*
959
+** Return TRUE if the cursor has been moved off of the last
960
+** row of output.
961
+*/
962
+static int helptextVtabEof(sqlite3_vtab_cursor *cur){
963
+ helptextVtab_cursor *pCur = (helptextVtab_cursor*)cur;
964
+ return pCur->iRowid>=MX_COMMAND;
965
+}
966
+
967
+/*
968
+** This method is called to "rewind" the helptextVtab_cursor object back
969
+** to the first row of output. This method is always called at least
970
+** once prior to any call to helptextVtabColumn() or helptextVtabRowid() or
971
+** helptextVtabEof().
972
+*/
973
+static int helptextVtabFilter(
974
+ sqlite3_vtab_cursor *pVtabCursor,
975
+ int idxNum, const char *idxStr,
976
+ int argc, sqlite3_value **argv
977
+){
978
+ helptextVtab_cursor *pCur = (helptextVtab_cursor *)pVtabCursor;
979
+ pCur->iRowid = 1;
980
+ return SQLITE_OK;
981
+}
982
+
983
+/*
984
+** SQLite will invoke this method one or more times while planning a query
985
+** that uses the virtual table. This routine needs to create
986
+** a query plan for each invocation and compute an estimated cost for that
987
+** plan.
988
+*/
989
+static int helptextVtabBestIndex(
990
+ sqlite3_vtab *tab,
991
+ sqlite3_index_info *pIdxInfo
992
+){
993
+ pIdxInfo->estimatedCost = (double)MX_COMMAND;
994
+ pIdxInfo->estimatedRows = MX_COMMAND;
995
+ return SQLITE_OK;
996
+}
997
+
998
+/*
999
+** This following structure defines all the methods for the
1000
+** virtual table.
1001
+*/
1002
+static sqlite3_module helptextVtabModule = {
1003
+ /* iVersion */ 0,
1004
+ /* xCreate */ 0, /* Helptext is eponymous and read-only */
1005
+ /* xConnect */ helptextVtabConnect,
1006
+ /* xBestIndex */ helptextVtabBestIndex,
1007
+ /* xDisconnect */ helptextVtabDisconnect,
1008
+ /* xDestroy */ 0,
1009
+ /* xOpen */ helptextVtabOpen,
1010
+ /* xClose */ helptextVtabClose,
1011
+ /* xFilter */ helptextVtabFilter,
1012
+ /* xNext */ helptextVtabNext,
1013
+ /* xEof */ helptextVtabEof,
1014
+ /* xColumn */ helptextVtabColumn,
1015
+ /* xRowid */ helptextVtabRowid,
1016
+ /* xUpdate */ 0,
1017
+ /* xBegin */ 0,
1018
+ /* xSync */ 0,
1019
+ /* xCommit */ 0,
1020
+ /* xRollback */ 0,
1021
+ /* xFindMethod */ 0,
1022
+ /* xRename */ 0,
1023
+ /* xSavepoint */ 0,
1024
+ /* xRelease */ 0,
1025
+ /* xRollbackTo */ 0,
1026
+ /* xShadowName */ 0
1027
+};
1028
+
1029
+
1030
+/*
1031
+** Register the helptext virtual table
1032
+*/
1033
+int helptext_vtab_register(sqlite3 *db){
1034
+ int rc = sqlite3_create_module(db, "helptext", &helptextVtabModule, 0);
1035
+ return rc;
1036
+}
1037
+/* End of the helptext virtual table
1038
+******************************************************************************/
8091039
--- src/dispatch.c
+++ src/dispatch.c
@@ -804,5 +804,235 @@
804 */
805 const Setting *setting_info(int *pnCount){
806 if( pnCount ) *pnCount = (int)(sizeof(aSetting)/sizeof(aSetting[0])) - 1;
807 return aSetting;
808 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
809
--- src/dispatch.c
+++ src/dispatch.c
@@ -804,5 +804,235 @@
804 */
805 const Setting *setting_info(int *pnCount){
806 if( pnCount ) *pnCount = (int)(sizeof(aSetting)/sizeof(aSetting[0])) - 1;
807 return aSetting;
808 }
809
810 /*****************************************************************************
811 ** A virtual table for accessing the information in aCommand[], and
812 ** especially the help-text
813 */
814
815 /* helptextVtab_vtab is a subclass of sqlite3_vtab which is
816 ** underlying representation of the virtual table
817 */
818 typedef struct helptextVtab_vtab helptextVtab_vtab;
819 struct helptextVtab_vtab {
820 sqlite3_vtab base; /* Base class - must be first */
821 /* Add new fields here, as necessary */
822 };
823
824 /* helptextVtab_cursor is a subclass of sqlite3_vtab_cursor which will
825 ** serve as the underlying representation of a cursor that scans
826 ** over rows of the result
827 */
828 typedef struct helptextVtab_cursor helptextVtab_cursor;
829 struct helptextVtab_cursor {
830 sqlite3_vtab_cursor base; /* Base class - must be first */
831 /* Insert new fields here. For this helptextVtab we only keep track
832 ** of the rowid */
833 sqlite3_int64 iRowid; /* The rowid */
834 };
835
836 /*
837 ** The helptextVtabConnect() method is invoked to create a new
838 ** helptext virtual table.
839 **
840 ** Think of this routine as the constructor for helptextVtab_vtab objects.
841 **
842 ** All this routine needs to do is:
843 **
844 ** (1) Allocate the helptextVtab_vtab object and initialize all fields.
845 **
846 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
847 ** result set of queries against the virtual table will look like.
848 */
849 static int helptextVtabConnect(
850 sqlite3 *db,
851 void *pAux,
852 int argc, const char *const*argv,
853 sqlite3_vtab **ppVtab,
854 char **pzErr
855 ){
856 helptextVtab_vtab *pNew;
857 int rc;
858
859 rc = sqlite3_declare_vtab(db,
860 "CREATE TABLE x(name,type,flags,helptext)"
861 );
862 if( rc==SQLITE_OK ){
863 pNew = sqlite3_malloc( sizeof(*pNew) );
864 *ppVtab = (sqlite3_vtab*)pNew;
865 if( pNew==0 ) return SQLITE_NOMEM;
866 memset(pNew, 0, sizeof(*pNew));
867 }
868 return rc;
869 }
870
871 /*
872 ** This method is the destructor for helptextVtab_vtab objects.
873 */
874 static int helptextVtabDisconnect(sqlite3_vtab *pVtab){
875 helptextVtab_vtab *p = (helptextVtab_vtab*)pVtab;
876 sqlite3_free(p);
877 return SQLITE_OK;
878 }
879
880 /*
881 ** Constructor for a new helptextVtab_cursor object.
882 */
883 static int helptextVtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
884 helptextVtab_cursor *pCur;
885 pCur = sqlite3_malloc( sizeof(*pCur) );
886 if( pCur==0 ) return SQLITE_NOMEM;
887 memset(pCur, 0, sizeof(*pCur));
888 *ppCursor = &pCur->base;
889 return SQLITE_OK;
890 }
891
892 /*
893 ** Destructor for a helptextVtab_cursor.
894 */
895 static int helptextVtabClose(sqlite3_vtab_cursor *cur){
896 helptextVtab_cursor *pCur = (helptextVtab_cursor*)cur;
897 sqlite3_free(pCur);
898 return SQLITE_OK;
899 }
900
901
902 /*
903 ** Advance a helptextVtab_cursor to its next row of output.
904 */
905 static int helptextVtabNext(sqlite3_vtab_cursor *cur){
906 helptextVtab_cursor *pCur = (helptextVtab_cursor*)cur;
907 pCur->iRowid++;
908 return SQLITE_OK;
909 }
910
911 /*
912 ** Return values of columns for the row at which the helptextVtab_cursor
913 ** is currently pointing.
914 */
915 static int helptextVtabColumn(
916 sqlite3_vtab_cursor *cur, /* The cursor */
917 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
918 int i /* Which column to return */
919 ){
920 helptextVtab_cursor *pCur = (helptextVtab_cursor*)cur;
921 const CmdOrPage *pPage = aCommand + pCur->iRowid;
922 switch( i ){
923 case 0: /* name */
924 sqlite3_result_text(ctx, pPage->zName, -1, SQLITE_STATIC);
925 break;
926 case 1: { /* type */
927 const char *zType = 0;
928 if( pPage->eCmdFlags & CMDFLAG_COMMAND ){
929 zType = "command";
930 }else if( pPage->eCmdFlags & CMDFLAG_WEBPAGE ){
931 zType = "webpage";
932 }else if( pPage->eCmdFlags & CMDFLAG_SETTING ){
933 zType = "setting";
934 }
935 sqlite3_result_text(ctx, zType, -1, SQLITE_STATIC);
936 break;
937 }
938 case 2: /* flags */
939 sqlite3_result_int(ctx, pPage->eCmdFlags);
940 break;
941 case 3: /* helptext */
942 sqlite3_result_text(ctx, pPage->zHelp, -1, SQLITE_STATIC);
943 break;
944 }
945 return SQLITE_OK;
946 }
947
948 /*
949 ** Return the rowid for the current row. In this implementation, the
950 ** rowid is the same as the output value.
951 */
952 static int helptextVtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
953 helptextVtab_cursor *pCur = (helptextVtab_cursor*)cur;
954 *pRowid = pCur->iRowid;
955 return SQLITE_OK;
956 }
957
958 /*
959 ** Return TRUE if the cursor has been moved off of the last
960 ** row of output.
961 */
962 static int helptextVtabEof(sqlite3_vtab_cursor *cur){
963 helptextVtab_cursor *pCur = (helptextVtab_cursor*)cur;
964 return pCur->iRowid>=MX_COMMAND;
965 }
966
967 /*
968 ** This method is called to "rewind" the helptextVtab_cursor object back
969 ** to the first row of output. This method is always called at least
970 ** once prior to any call to helptextVtabColumn() or helptextVtabRowid() or
971 ** helptextVtabEof().
972 */
973 static int helptextVtabFilter(
974 sqlite3_vtab_cursor *pVtabCursor,
975 int idxNum, const char *idxStr,
976 int argc, sqlite3_value **argv
977 ){
978 helptextVtab_cursor *pCur = (helptextVtab_cursor *)pVtabCursor;
979 pCur->iRowid = 1;
980 return SQLITE_OK;
981 }
982
983 /*
984 ** SQLite will invoke this method one or more times while planning a query
985 ** that uses the virtual table. This routine needs to create
986 ** a query plan for each invocation and compute an estimated cost for that
987 ** plan.
988 */
989 static int helptextVtabBestIndex(
990 sqlite3_vtab *tab,
991 sqlite3_index_info *pIdxInfo
992 ){
993 pIdxInfo->estimatedCost = (double)MX_COMMAND;
994 pIdxInfo->estimatedRows = MX_COMMAND;
995 return SQLITE_OK;
996 }
997
998 /*
999 ** This following structure defines all the methods for the
1000 ** virtual table.
1001 */
1002 static sqlite3_module helptextVtabModule = {
1003 /* iVersion */ 0,
1004 /* xCreate */ 0, /* Helptext is eponymous and read-only */
1005 /* xConnect */ helptextVtabConnect,
1006 /* xBestIndex */ helptextVtabBestIndex,
1007 /* xDisconnect */ helptextVtabDisconnect,
1008 /* xDestroy */ 0,
1009 /* xOpen */ helptextVtabOpen,
1010 /* xClose */ helptextVtabClose,
1011 /* xFilter */ helptextVtabFilter,
1012 /* xNext */ helptextVtabNext,
1013 /* xEof */ helptextVtabEof,
1014 /* xColumn */ helptextVtabColumn,
1015 /* xRowid */ helptextVtabRowid,
1016 /* xUpdate */ 0,
1017 /* xBegin */ 0,
1018 /* xSync */ 0,
1019 /* xCommit */ 0,
1020 /* xRollback */ 0,
1021 /* xFindMethod */ 0,
1022 /* xRename */ 0,
1023 /* xSavepoint */ 0,
1024 /* xRelease */ 0,
1025 /* xRollbackTo */ 0,
1026 /* xShadowName */ 0
1027 };
1028
1029
1030 /*
1031 ** Register the helptext virtual table
1032 */
1033 int helptext_vtab_register(sqlite3 *db){
1034 int rc = sqlite3_create_module(db, "helptext", &helptextVtabModule, 0);
1035 return rc;
1036 }
1037 /* End of the helptext virtual table
1038 ******************************************************************************/
1039
--- src/sqlcmd.c
+++ src/sqlcmd.c
@@ -171,10 +171,11 @@
171171
db_add_aux_functions(db);
172172
re_add_sql_func(db);
173173
search_sql_setup(db);
174174
foci_register(db);
175175
deltafunc_init(db);
176
+ helptext_vtab_register(db);
176177
g.repositoryOpen = 1;
177178
g.db = db;
178179
sqlite3_db_config(db, SQLITE_DBCONFIG_MAINDBNAME, "repository");
179180
db_maybe_set_encryption_key(db, g.zRepositoryName);
180181
if( g.zLocalDbName ){
181182
--- src/sqlcmd.c
+++ src/sqlcmd.c
@@ -171,10 +171,11 @@
171 db_add_aux_functions(db);
172 re_add_sql_func(db);
173 search_sql_setup(db);
174 foci_register(db);
175 deltafunc_init(db);
 
176 g.repositoryOpen = 1;
177 g.db = db;
178 sqlite3_db_config(db, SQLITE_DBCONFIG_MAINDBNAME, "repository");
179 db_maybe_set_encryption_key(db, g.zRepositoryName);
180 if( g.zLocalDbName ){
181
--- src/sqlcmd.c
+++ src/sqlcmd.c
@@ -171,10 +171,11 @@
171 db_add_aux_functions(db);
172 re_add_sql_func(db);
173 search_sql_setup(db);
174 foci_register(db);
175 deltafunc_init(db);
176 helptext_vtab_register(db);
177 g.repositoryOpen = 1;
178 g.db = db;
179 sqlite3_db_config(db, SQLITE_DBCONFIG_MAINDBNAME, "repository");
180 db_maybe_set_encryption_key(db, g.zRepositoryName);
181 if( g.zLocalDbName ){
182

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button