noop.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. ** 2020-01-08
  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. **
  13. ** This SQLite extension implements a noop() function used for testing.
  14. **
  15. ** Variants:
  16. **
  17. ** noop(X) The default. Deterministic.
  18. ** noop_i(X) Deterministic and innocuous.
  19. ** noop_do(X) Deterministic and direct-only.
  20. ** noop_nd(X) Non-deterministic.
  21. */
  22. #include "sqlite3ext.h"
  23. SQLITE_EXTENSION_INIT1
  24. #include <assert.h>
  25. #include <string.h>
  26. /*
  27. ** Implementation of the noop() function.
  28. **
  29. ** The function returns its argument, unchanged.
  30. */
  31. static void noopfunc(
  32. sqlite3_context *context,
  33. int argc,
  34. sqlite3_value **argv
  35. ){
  36. assert( argc==1 );
  37. sqlite3_result_value(context, argv[0]);
  38. }
  39. /*
  40. ** Implementation of the multitype_text() function.
  41. **
  42. ** The function returns its argument. The result will always have a
  43. ** TEXT value. But if the original input is numeric, it will also
  44. ** have that numeric value.
  45. */
  46. static void multitypeTextFunc(
  47. sqlite3_context *context,
  48. int argc,
  49. sqlite3_value **argv
  50. ){
  51. assert( argc==1 );
  52. (void)argc;
  53. (void)sqlite3_value_text(argv[0]);
  54. sqlite3_result_value(context, argv[0]);
  55. }
  56. #ifdef _WIN32
  57. __declspec(dllexport)
  58. #endif
  59. int sqlite3_noop_init(
  60. sqlite3 *db,
  61. char **pzErrMsg,
  62. const sqlite3_api_routines *pApi
  63. ){
  64. int rc = SQLITE_OK;
  65. SQLITE_EXTENSION_INIT2(pApi);
  66. (void)pzErrMsg; /* Unused parameter */
  67. rc = sqlite3_create_function(db, "noop", 1,
  68. SQLITE_UTF8 | SQLITE_DETERMINISTIC,
  69. 0, noopfunc, 0, 0);
  70. if( rc ) return rc;
  71. rc = sqlite3_create_function(db, "noop_i", 1,
  72. SQLITE_UTF8 | SQLITE_DETERMINISTIC | SQLITE_INNOCUOUS,
  73. 0, noopfunc, 0, 0);
  74. if( rc ) return rc;
  75. rc = sqlite3_create_function(db, "noop_do", 1,
  76. SQLITE_UTF8 | SQLITE_DETERMINISTIC | SQLITE_DIRECTONLY,
  77. 0, noopfunc, 0, 0);
  78. if( rc ) return rc;
  79. rc = sqlite3_create_function(db, "noop_nd", 1,
  80. SQLITE_UTF8,
  81. 0, noopfunc, 0, 0);
  82. if( rc ) return rc;
  83. rc = sqlite3_create_function(db, "multitype_text", 1,
  84. SQLITE_UTF8,
  85. 0, multitypeTextFunc, 0, 0);
  86. return rc;
  87. }