dba_tcadb.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Michael Maclean <mgdm@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php.h"
  20. #if DBA_TCADB
  21. #include "php_tcadb.h"
  22. #ifdef TCADB_INCLUDE_FILE
  23. #include TCADB_INCLUDE_FILE
  24. #endif
  25. #define TCADB_DATA dba_tcadb_data *dba = info->dbf
  26. typedef struct {
  27. TCADB *tcadb;
  28. } dba_tcadb_data;
  29. DBA_OPEN_FUNC(tcadb)
  30. {
  31. char *path_string;
  32. TCADB *tcadb = tcadbnew();
  33. if (tcadb) {
  34. switch(info->mode) {
  35. case DBA_READER:
  36. spprintf(&path_string, 0, "%s#mode=r", info->path);
  37. break;
  38. case DBA_WRITER:
  39. spprintf(&path_string, 0, "%s#mode=w", info->path);
  40. break;
  41. case DBA_CREAT:
  42. spprintf(&path_string, 0, "%s#mode=wc", info->path);
  43. break;
  44. case DBA_TRUNC:
  45. spprintf(&path_string, 0, "%s#mode=wct", info->path);
  46. break;
  47. default:
  48. tcadbdel(tcadb);
  49. return FAILURE;
  50. }
  51. if (!tcadbopen(tcadb, path_string)) {
  52. efree(path_string);
  53. tcadbdel(tcadb);
  54. return FAILURE;
  55. }
  56. efree(path_string);
  57. info->dbf = pemalloc(sizeof(dba_tcadb_data), info->flags & DBA_PERSISTENT);
  58. memset(info->dbf, 0, sizeof(dba_tcadb_data));
  59. ((dba_tcadb_data *) info->dbf)->tcadb = tcadb;
  60. return SUCCESS;
  61. }
  62. return FAILURE;
  63. }
  64. DBA_CLOSE_FUNC(tcadb)
  65. {
  66. TCADB_DATA;
  67. tcadbclose(dba->tcadb);
  68. tcadbdel(dba->tcadb);
  69. pefree(dba, info->flags & DBA_PERSISTENT);
  70. }
  71. DBA_FETCH_FUNC(tcadb)
  72. {
  73. TCADB_DATA;
  74. char *value, *new = NULL;
  75. int value_size;
  76. value = tcadbget(dba->tcadb, key, keylen, &value_size);
  77. if (value) {
  78. if (newlen) {
  79. *newlen = value_size;
  80. }
  81. new = estrndup(value, value_size);
  82. tcfree(value);
  83. }
  84. return new;
  85. }
  86. DBA_UPDATE_FUNC(tcadb)
  87. {
  88. TCADB_DATA;
  89. int result;
  90. if (mode == 1) {
  91. /* Insert */
  92. if (tcadbvsiz(dba->tcadb, key, keylen) > -1) {
  93. return FAILURE;
  94. }
  95. }
  96. result = tcadbput(dba->tcadb, key, keylen, val, vallen);
  97. if (result) {
  98. return SUCCESS;
  99. }
  100. php_error_docref2(NULL, key, val, E_WARNING, "Error updating data");
  101. return FAILURE;
  102. }
  103. DBA_EXISTS_FUNC(tcadb)
  104. {
  105. TCADB_DATA;
  106. char *value;
  107. int value_len;
  108. value = tcadbget(dba->tcadb, key, keylen, &value_len);
  109. if (value) {
  110. tcfree(value);
  111. return SUCCESS;
  112. }
  113. return FAILURE;
  114. }
  115. DBA_DELETE_FUNC(tcadb)
  116. {
  117. TCADB_DATA;
  118. return tcadbout(dba->tcadb, key, keylen) ? SUCCESS : FAILURE;
  119. }
  120. DBA_FIRSTKEY_FUNC(tcadb)
  121. {
  122. TCADB_DATA;
  123. int value_size;
  124. char *value, *new = NULL;
  125. tcadbiterinit(dba->tcadb);
  126. value = tcadbiternext(dba->tcadb, &value_size);
  127. if (value) {
  128. if (newlen) {
  129. *newlen = value_size;
  130. }
  131. new = estrndup(value, value_size);
  132. tcfree(value);
  133. }
  134. return new;
  135. }
  136. DBA_NEXTKEY_FUNC(tcadb)
  137. {
  138. TCADB_DATA;
  139. int value_size;
  140. char *value, *new = NULL;
  141. value = tcadbiternext(dba->tcadb, &value_size);
  142. if (value) {
  143. if (newlen) {
  144. *newlen = value_size;
  145. }
  146. new = estrndup(value, value_size);
  147. tcfree(value);
  148. }
  149. return new;
  150. }
  151. DBA_OPTIMIZE_FUNC(tcadb)
  152. {
  153. TCADB_DATA;
  154. #if _TC_LIBVER >= 811
  155. return tcadboptimize(dba->tcadb, NULL) ? SUCCESS : FAILURE;
  156. #else
  157. return FAILURE;
  158. #endif
  159. }
  160. DBA_SYNC_FUNC(tcadb)
  161. {
  162. TCADB_DATA;
  163. return tcadbsync(dba->tcadb) ? SUCCESS : FAILURE;
  164. }
  165. DBA_INFO_FUNC(tcadb)
  166. {
  167. return estrdup(tcversion);
  168. }
  169. #endif