test_autoext.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. ** 2006 August 23
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** Test extension for testing the sqlite3_auto_extension() function.
  13. */
  14. #include "tcl.h"
  15. #include "sqlite3ext.h"
  16. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  17. SQLITE_EXTENSION_INIT1
  18. /*
  19. ** The sqr() SQL function returns the square of its input value.
  20. */
  21. static void sqrFunc(
  22. sqlite3_context *context,
  23. int argc,
  24. sqlite3_value **argv
  25. ){
  26. double r = sqlite3_value_double(argv[0]);
  27. sqlite3_result_double(context, r*r);
  28. }
  29. /*
  30. ** This is the entry point to register the extension for the sqr() function.
  31. */
  32. static int sqr_init(
  33. sqlite3 *db,
  34. char **pzErrMsg,
  35. const sqlite3_api_routines *pApi
  36. ){
  37. SQLITE_EXTENSION_INIT2(pApi);
  38. sqlite3_create_function(db, "sqr", 1, SQLITE_ANY, 0, sqrFunc, 0, 0);
  39. return 0;
  40. }
  41. /*
  42. ** The cube() SQL function returns the cube of its input value.
  43. */
  44. static void cubeFunc(
  45. sqlite3_context *context,
  46. int argc,
  47. sqlite3_value **argv
  48. ){
  49. double r = sqlite3_value_double(argv[0]);
  50. sqlite3_result_double(context, r*r*r);
  51. }
  52. /*
  53. ** This is the entry point to register the extension for the cube() function.
  54. */
  55. static int cube_init(
  56. sqlite3 *db,
  57. char **pzErrMsg,
  58. const sqlite3_api_routines *pApi
  59. ){
  60. SQLITE_EXTENSION_INIT2(pApi);
  61. sqlite3_create_function(db, "cube", 1, SQLITE_ANY, 0, cubeFunc, 0, 0);
  62. return 0;
  63. }
  64. /*
  65. ** This is a broken extension entry point
  66. */
  67. static int broken_init(
  68. sqlite3 *db,
  69. char **pzErrMsg,
  70. const sqlite3_api_routines *pApi
  71. ){
  72. char *zErr;
  73. SQLITE_EXTENSION_INIT2(pApi);
  74. zErr = sqlite3_mprintf("broken autoext!");
  75. *pzErrMsg = zErr;
  76. return 1;
  77. }
  78. /*
  79. ** tclcmd: sqlite3_auto_extension_sqr
  80. **
  81. ** Register the "sqr" extension to be loaded automatically.
  82. */
  83. static int autoExtSqrObjCmd(
  84. void * clientData,
  85. Tcl_Interp *interp,
  86. int objc,
  87. Tcl_Obj *CONST objv[]
  88. ){
  89. int rc = sqlite3_auto_extension((void*)sqr_init);
  90. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  91. return SQLITE_OK;
  92. }
  93. /*
  94. ** tclcmd: sqlite3_cancel_auto_extension_sqr
  95. **
  96. ** Unregister the "sqr" extension.
  97. */
  98. static int cancelAutoExtSqrObjCmd(
  99. void * clientData,
  100. Tcl_Interp *interp,
  101. int objc,
  102. Tcl_Obj *CONST objv[]
  103. ){
  104. int rc = sqlite3_cancel_auto_extension((void*)sqr_init);
  105. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  106. return SQLITE_OK;
  107. }
  108. /*
  109. ** tclcmd: sqlite3_auto_extension_cube
  110. **
  111. ** Register the "cube" extension to be loaded automatically.
  112. */
  113. static int autoExtCubeObjCmd(
  114. void * clientData,
  115. Tcl_Interp *interp,
  116. int objc,
  117. Tcl_Obj *CONST objv[]
  118. ){
  119. int rc = sqlite3_auto_extension((void*)cube_init);
  120. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  121. return SQLITE_OK;
  122. }
  123. /*
  124. ** tclcmd: sqlite3_cancel_auto_extension_cube
  125. **
  126. ** Unregister the "cube" extension.
  127. */
  128. static int cancelAutoExtCubeObjCmd(
  129. void * clientData,
  130. Tcl_Interp *interp,
  131. int objc,
  132. Tcl_Obj *CONST objv[]
  133. ){
  134. int rc = sqlite3_cancel_auto_extension((void*)cube_init);
  135. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  136. return SQLITE_OK;
  137. }
  138. /*
  139. ** tclcmd: sqlite3_auto_extension_broken
  140. **
  141. ** Register the broken extension to be loaded automatically.
  142. */
  143. static int autoExtBrokenObjCmd(
  144. void * clientData,
  145. Tcl_Interp *interp,
  146. int objc,
  147. Tcl_Obj *CONST objv[]
  148. ){
  149. int rc = sqlite3_auto_extension((void*)broken_init);
  150. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  151. return SQLITE_OK;
  152. }
  153. /*
  154. ** tclcmd: sqlite3_cancel_auto_extension_broken
  155. **
  156. ** Unregister the broken extension.
  157. */
  158. static int cancelAutoExtBrokenObjCmd(
  159. void * clientData,
  160. Tcl_Interp *interp,
  161. int objc,
  162. Tcl_Obj *CONST objv[]
  163. ){
  164. int rc = sqlite3_cancel_auto_extension((void*)broken_init);
  165. Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  166. return SQLITE_OK;
  167. }
  168. #endif /* SQLITE_OMIT_LOAD_EXTENSION */
  169. /*
  170. ** tclcmd: sqlite3_reset_auto_extension
  171. **
  172. ** Reset all auto-extensions
  173. */
  174. static int resetAutoExtObjCmd(
  175. void * clientData,
  176. Tcl_Interp *interp,
  177. int objc,
  178. Tcl_Obj *CONST objv[]
  179. ){
  180. sqlite3_reset_auto_extension();
  181. return SQLITE_OK;
  182. }
  183. /*
  184. ** This procedure registers the TCL procs defined in this file.
  185. */
  186. int Sqlitetest_autoext_Init(Tcl_Interp *interp){
  187. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  188. Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_sqr",
  189. autoExtSqrObjCmd, 0, 0);
  190. Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_cube",
  191. autoExtCubeObjCmd, 0, 0);
  192. Tcl_CreateObjCommand(interp, "sqlite3_auto_extension_broken",
  193. autoExtBrokenObjCmd, 0, 0);
  194. Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_sqr",
  195. cancelAutoExtSqrObjCmd, 0, 0);
  196. Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_cube",
  197. cancelAutoExtCubeObjCmd, 0, 0);
  198. Tcl_CreateObjCommand(interp, "sqlite3_cancel_auto_extension_broken",
  199. cancelAutoExtBrokenObjCmd, 0, 0);
  200. #endif
  201. Tcl_CreateObjCommand(interp, "sqlite3_reset_auto_extension",
  202. resetAutoExtObjCmd, 0, 0);
  203. return TCL_OK;
  204. }