Difference between revisions of "Vim/sqlite"
From RonWareWiki
< Vim
(New page: This is the patch for vim 7.2, to add SQLite interfaces: <code diff> *** src/eval.c.orig Mon Dec 1 08:39:38 2008 --- src/eval.c Sun Nov 30 08:08:38 2008 *************** *** 31,34 **** --...) |
|||
(One intermediate revision by the same user not shown) | |||
Line 53: | Line 53: | ||
+ ); | + ); | ||
+ | + | ||
− | + f_sqlclose(argvars, rettv) | + | + static void f_sqlclose(argvars, rettv) |
+ typval_T *argvars; | + typval_T *argvars; | ||
+ typval_T *rettv; | + typval_T *rettv; | ||
Line 61: | Line 61: | ||
+ rettv->vval.v_number = retval; | + rettv->vval.v_number = retval; | ||
+ } | + } | ||
− | + f_sqlopen(argvars, rettv) | + | + static void f_sqlopen(argvars, rettv) |
+ typval_T *argvars; | + typval_T *argvars; | ||
+ typval_T *rettv; | + typval_T *rettv; | ||
Line 114: | Line 114: | ||
+ } | + } | ||
+ | + | ||
− | + f_sqlexec(argvars, rettv) | + | + static void f_sqlexec(argvars, rettv) |
+ typval_T *argvars; | + typval_T *argvars; | ||
+ typval_T *rettv; | + typval_T *rettv; | ||
Line 145: | Line 145: | ||
NULL | NULL | ||
}; | }; | ||
+ | |||
+ | </code> | ||
+ | |||
+ | Here is the associated help file (for a local 'doc' directory): | ||
+ | |||
+ | <code text> | ||
+ | *sqlite.txt* For the 'sqlite_interface' patch to Vim version 7.2. Last change: 2008 Dec 01 | ||
+ | |||
+ | This patch is maintained by Ron Aaron (ron@ronware.org) | ||
+ | |||
+ | After applying the sqlite_interface patch, the following three functions will be | ||
+ | available: | ||
+ | |||
+ | sql_close( {handle} ) *sql_close()* | ||
+ | Closes the open SQLite database, whose {handle} was returned | ||
+ | by the [sql_open] function. Returns the SQLite result. | ||
+ | |||
+ | sql_exec( {handle}, {sql} [, {separator}] ) *sql_exec()* | ||
+ | Execute the SQL (a string of valid SQL commands) on the opened | ||
+ | {handle}, which must have been returned from [sql_open]. The | ||
+ | return result is a List consisting of one String per row of | ||
+ | results. Columns within each row are separated by the "|" | ||
+ | character by default. Optionally, one may pass the | ||
+ | {separator} string to use a different separator. | ||
+ | |||
+ | sql_open( {filename} ) *sql_open()* | ||
+ | Open the SQLite database in {filename}. The return result is | ||
+ | a {handle} which is passed to [sql_exec] and [sql_close] | ||
+ | |||
+ | |||
+ | vim:tw=78:ts=8:ft=help:norl: | ||
</code> | </code> |
Latest revision as of 15:27, 13 March 2009
This is the patch for vim 7.2, to add SQLite interfaces:
- src/eval.c.orig Mon Dec 1 08:39:38 2008
--- src/eval.c Sun Nov 30 08:08:38 2008
- 31,34 ****
--- 31,35 ----
#endif
+ #define FEAT_SQLITE 1
#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
- 675,678 ****
--- 676,684 ----
static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
+ #ifdef FEAT_SQLITE
+ static void f_sqlclose __ARGS((typval_T *argvars, typval_T *rettv));
+ static void f_sqlopen __ARGS((typval_T *argvars, typval_T *rettv));
+ static void f_sqlexec __ARGS((typval_T *argvars, typval_T *rettv));
+ #endif
#ifdef FEAT_FLOAT
static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
- 7675,7678 ****
--- 7681,7689 ----
{"spellsuggest", 1, 3, f_spellsuggest},
{"split", 1, 3, f_split},
+ #ifdef FEAT_SQLITE
+ {"sql_close", 1, 1, f_sqlclose},
+ {"sql_exec", 2, 3, f_sqlexec},
+ {"sql_open", 1, 1, f_sqlopen},
+ #endif
#ifdef FEAT_FLOAT
{"sqrt", 1, 1, f_sqrt},
- 16207,16210 ****
--- 16218,16312 ----
}
+ #ifdef FEAT_SQLITE
+ // NOTE: these do not exactly match the sqlite3.h definitions but they will work
+ extern int sqlite3_close(int handle);
+ extern int sqlite3_open(char *fname, int *handle);
+ int sqlite3_exec(
+ int handle, /* An open database */
+ const char *sql, /* SQL to be evaluted */
+ int (*callback)(void*,int,char**,char**), /* Callback function */
+ void *, /* 1st argument to callback */
+ char **errmsg /* Error msg written here */
+ );
+
+ static void f_sqlclose(argvars, rettv)
+ typval_T *argvars;
+ typval_T *rettv;
+ {
+ int handle = get_tv_number(&argvars[0]);
+ int retval = sqlite3_close(handle);
+ rettv->vval.v_number = retval;
+ }
+ static void f_sqlopen(argvars, rettv)
+ typval_T *argvars;
+ typval_T *rettv;
+ {
+ char_u *fname = get_tv_string(&argvars[0]);
+ int handle = 0;
+ int retval = sqlite3_open(fname, &handle);
+ rettv->vval.v_number = handle;
+ }
+
+ static char *sep;
+ static int sqlite_callback ( list_T *list, int nCols, char **sCols, char **colNames)
+ {
+ int x;
+ char *temp;
+ char *temp2;
+ int len = 0;
+ int seplen = STRLEN(sep);
+ if (nCols > 1)
+ {
+ // jam the data together with a "|" separator:
+ // first count string length:
+ for (x=0; x<nCols; x++)
+ {
+ len += STRLEN(sCols[x]) + seplen ;
+ }
+
+ // allocate the space:
+ temp = (char *) alloc(len+1);
+
+ // now copy over the data:
+ temp2 = temp;
+
+ for (x=0; x<nCols-1; x++)
+ {
+ STRCPY(temp2, sCols[x]);
+ STRCAT(temp2, sep);
+ temp2 += seplen + STRLEN(sCols[x]);
+ }
+ STRCPY(temp2, sCols[x]);
+ }
+ else
+ {
+ // just copy the data over:
+ temp = (char *) alloc(1 + STRLEN(sCols[0]));
+ STRCPY(temp, sCols[0]);
+ }
+
+ list_append_string(list, temp, STRLEN(temp));
+ free(temp);
+ return 0;
+ }
+
+ static void f_sqlexec(argvars, rettv)
+ typval_T *argvars;
+ typval_T *rettv;
+ {
+ int handle = get_tv_number(&argvars[0]);
+ char_u *sql = get_tv_string(&argvars[1]);
+ rettv->vval.v_list = NULL;
+ rettv->v_type = VAR_LIST;
+ sep = "|";
+ if (argvars[2].v_type != VAR_UNKNOWN)
+ {
+ sep = get_tv_string(&argvars[2]);
+ }
+ if (rettv_list_alloc(rettv) == OK)
+ sqlite3_exec(handle, sql, sqlite_callback, rettv->vval.v_list, NULL);
+ }
+ #endif
+
#ifdef FEAT_FLOAT
/*
- src/version.c.orig Mon Dec 1 08:10:23 2008
--- src/version.c Mon Dec 1 08:38:39 2008
- 811,814 ****
--- 811,816 ----
{ /* Add your patch description below this line */
/**/
+ "SQLite interface",
+ /**/
NULL
};
Here is the associated help file (for a local 'doc' directory):
- sqlite.txt* For the 'sqlite_interface' patch to Vim version 7.2. Last change: 2008 Dec 01
This patch is maintained by Ron Aaron (ron@ronware.org)
After applying the sqlite_interface patch, the following three functions will be
available:
sql_close( {handle} ) *sql_close()*
Closes the open SQLite database, whose {handle} was returned
by the [sql_open] function. Returns the SQLite result.
sql_exec( {handle}, {sql} [, {separator}] ) *sql_exec()*
Execute the SQL (a string of valid SQL commands) on the opened
{handle}, which must have been returned from [sql_open]. The
return result is a List consisting of one String per row of
results. Columns within each row are separated by the "|"
character by default. Optionally, one may pass the
{separator} string to use a different separator.
sql_open( {filename} ) *sql_open()*
Open the SQLite database in {filename}. The return result is
a {handle} which is passed to [sql_exec] and [sql_close]
vim:tw=78:ts=8:ft=help:norl: