dba_db2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Sascha Schumann <sascha@schumann.cx> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #if DBA_DB2
  24. #include "php_db2.h"
  25. #include <sys/stat.h>
  26. #include <string.h>
  27. #ifdef DB2_INCLUDE_FILE
  28. #include DB2_INCLUDE_FILE
  29. #endif
  30. #define DB2_DATA dba_db2_data *dba = info->dbf
  31. #define DB2_GKEY \
  32. DBT gkey = {0}; \
  33. gkey.data = (char *) key; \
  34. gkey.size = keylen
  35. typedef struct {
  36. DB *dbp;
  37. DBC *cursor;
  38. } dba_db2_data;
  39. DBA_OPEN_FUNC(db2)
  40. {
  41. DB *dbp;
  42. DBTYPE type;
  43. int gmode = 0;
  44. int filemode = 0644;
  45. struct stat check_stat;
  46. int s = VCWD_STAT(info->path, &check_stat);
  47. if (!s && !check_stat.st_size) {
  48. info->mode = DBA_TRUNC; /* force truncate */
  49. }
  50. type = info->mode == DBA_READER ? DB_UNKNOWN :
  51. info->mode == DBA_TRUNC ? DB_BTREE :
  52. s ? DB_BTREE : DB_UNKNOWN;
  53. gmode = info->mode == DBA_READER ? DB_RDONLY :
  54. (info->mode == DBA_CREAT && s) ? DB_CREATE :
  55. (info->mode == DBA_CREAT && !s) ? 0 :
  56. info->mode == DBA_WRITER ? 0 :
  57. info->mode == DBA_TRUNC ? DB_CREATE | DB_TRUNCATE : -1;
  58. if (gmode == -1) {
  59. return FAILURE;/* not possible */
  60. }
  61. if (info->argc > 0) {
  62. convert_to_long_ex(info->argv[0]);
  63. filemode = Z_LVAL_PP(info->argv[0]);
  64. }
  65. if (db_open(info->path, type, gmode, filemode, NULL, NULL, &dbp)) {
  66. return FAILURE;
  67. }
  68. info->dbf = pemalloc(sizeof(dba_db2_data), info->flags&DBA_PERSISTENT);
  69. memset(info->dbf, 0, sizeof(dba_db2_data));
  70. ((dba_db2_data *) info->dbf)->dbp = dbp;
  71. return SUCCESS;
  72. }
  73. DBA_CLOSE_FUNC(db2)
  74. {
  75. DB2_DATA;
  76. if (dba->cursor)
  77. dba->cursor->c_close(dba->cursor);
  78. dba->dbp->close(dba->dbp, 0);
  79. pefree(dba, info->flags&DBA_PERSISTENT);
  80. }
  81. DBA_FETCH_FUNC(db2)
  82. {
  83. DBT gval = {0};
  84. DB2_DATA;
  85. DB2_GKEY;
  86. if (dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
  87. return NULL;
  88. }
  89. if (newlen) *newlen = gval.size;
  90. return estrndup(gval.data, gval.size);
  91. }
  92. DBA_UPDATE_FUNC(db2)
  93. {
  94. DBT gval = {0};
  95. DB2_DATA;
  96. DB2_GKEY;
  97. gval.data = (char *) val;
  98. gval.size = vallen;
  99. if (dba->dbp->put(dba->dbp, NULL, &gkey, &gval,
  100. mode == 1 ? DB_NOOVERWRITE : 0)) {
  101. return FAILURE;
  102. }
  103. return SUCCESS;
  104. }
  105. DBA_EXISTS_FUNC(db2)
  106. {
  107. DBT gval = {0};
  108. DB2_DATA;
  109. DB2_GKEY;
  110. if (dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
  111. return FAILURE;
  112. }
  113. return SUCCESS;
  114. }
  115. DBA_DELETE_FUNC(db2)
  116. {
  117. DB2_DATA;
  118. DB2_GKEY;
  119. return dba->dbp->del(dba->dbp, NULL, &gkey, 0) ? FAILURE : SUCCESS;
  120. }
  121. DBA_FIRSTKEY_FUNC(db2)
  122. {
  123. DB2_DATA;
  124. if (dba->cursor) {
  125. dba->cursor->c_close(dba->cursor);
  126. dba->cursor = NULL;
  127. }
  128. #if (DB_VERSION_MAJOR > 2) || (DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR > 6) || (DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR == 6 && DB_VERSION_PATCH >= 4)
  129. if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor, 0)) {
  130. #else
  131. if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor)) {
  132. #endif
  133. return NULL;
  134. }
  135. /* we should introduce something like PARAM_PASSTHRU... */
  136. return dba_nextkey_db2(info, newlen TSRMLS_CC);
  137. }
  138. DBA_NEXTKEY_FUNC(db2)
  139. {
  140. DB2_DATA;
  141. DBT gkey = {0}, gval = {0};
  142. if (dba->cursor->c_get(dba->cursor, &gkey, &gval, DB_NEXT)
  143. || !gkey.data)
  144. return NULL;
  145. if (newlen) *newlen = gkey.size;
  146. return estrndup(gkey.data, gkey.size);
  147. }
  148. DBA_OPTIMIZE_FUNC(db2)
  149. {
  150. return SUCCESS;
  151. }
  152. DBA_SYNC_FUNC(db2)
  153. {
  154. DB2_DATA;
  155. return dba->dbp->sync(dba->dbp, 0) ? FAILURE : SUCCESS;
  156. }
  157. DBA_INFO_FUNC(db2)
  158. {
  159. return estrdup(DB_VERSION_STRING);
  160. }
  161. #endif
  162. /*
  163. * Local variables:
  164. * tab-width: 4
  165. * c-basic-offset: 4
  166. * End:
  167. * vim600: sw=4 ts=4 fdm=marker
  168. * vim<600: sw=4 ts=4
  169. */