dba_db2.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "php.h"
  22. #if DBA_DB2
  23. #include "php_db2.h"
  24. #include <sys/stat.h>
  25. #include <string.h>
  26. #ifdef DB2_INCLUDE_FILE
  27. #include DB2_INCLUDE_FILE
  28. #endif
  29. #define DB2_DATA dba_db2_data *dba = info->dbf
  30. #define DB2_GKEY \
  31. DBT gkey = {0}; \
  32. gkey.data = (char *) key; \
  33. gkey.size = keylen
  34. typedef struct {
  35. DB *dbp;
  36. DBC *cursor;
  37. } dba_db2_data;
  38. DBA_OPEN_FUNC(db2)
  39. {
  40. DB *dbp;
  41. DBTYPE type;
  42. int gmode = 0;
  43. int filemode = 0644;
  44. struct stat check_stat;
  45. int s = VCWD_STAT(info->path, &check_stat);
  46. if (!s && !check_stat.st_size) {
  47. info->mode = DBA_TRUNC; /* force truncate */
  48. }
  49. type = info->mode == DBA_READER ? DB_UNKNOWN :
  50. info->mode == DBA_TRUNC ? DB_BTREE :
  51. s ? DB_BTREE : DB_UNKNOWN;
  52. gmode = info->mode == DBA_READER ? DB_RDONLY :
  53. (info->mode == DBA_CREAT && s) ? DB_CREATE :
  54. (info->mode == DBA_CREAT && !s) ? 0 :
  55. info->mode == DBA_WRITER ? 0 :
  56. info->mode == DBA_TRUNC ? DB_CREATE | DB_TRUNCATE : -1;
  57. if (gmode == -1) {
  58. return FAILURE;/* not possible */
  59. }
  60. if (info->argc > 0) {
  61. filemode = zval_get_long(&info->argv[0]);
  62. }
  63. if (db_open(info->path, type, gmode, filemode, NULL, NULL, &dbp)) {
  64. return FAILURE;
  65. }
  66. info->dbf = pemalloc(sizeof(dba_db2_data), info->flags&DBA_PERSISTENT);
  67. memset(info->dbf, 0, sizeof(dba_db2_data));
  68. ((dba_db2_data *) info->dbf)->dbp = dbp;
  69. return SUCCESS;
  70. }
  71. DBA_CLOSE_FUNC(db2)
  72. {
  73. DB2_DATA;
  74. if (dba->cursor)
  75. dba->cursor->c_close(dba->cursor);
  76. dba->dbp->close(dba->dbp, 0);
  77. pefree(dba, info->flags&DBA_PERSISTENT);
  78. }
  79. DBA_FETCH_FUNC(db2)
  80. {
  81. DBT gval = {0};
  82. DB2_DATA;
  83. DB2_GKEY;
  84. if (dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
  85. return NULL;
  86. }
  87. if (newlen) *newlen = gval.size;
  88. return estrndup(gval.data, gval.size);
  89. }
  90. DBA_UPDATE_FUNC(db2)
  91. {
  92. DBT gval = {0};
  93. DB2_DATA;
  94. DB2_GKEY;
  95. gval.data = (char *) val;
  96. gval.size = vallen;
  97. if (dba->dbp->put(dba->dbp, NULL, &gkey, &gval,
  98. mode == 1 ? DB_NOOVERWRITE : 0)) {
  99. return FAILURE;
  100. }
  101. return SUCCESS;
  102. }
  103. DBA_EXISTS_FUNC(db2)
  104. {
  105. DBT gval = {0};
  106. DB2_DATA;
  107. DB2_GKEY;
  108. if (dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
  109. return FAILURE;
  110. }
  111. return SUCCESS;
  112. }
  113. DBA_DELETE_FUNC(db2)
  114. {
  115. DB2_DATA;
  116. DB2_GKEY;
  117. return dba->dbp->del(dba->dbp, NULL, &gkey, 0) ? FAILURE : SUCCESS;
  118. }
  119. DBA_FIRSTKEY_FUNC(db2)
  120. {
  121. DB2_DATA;
  122. if (dba->cursor) {
  123. dba->cursor->c_close(dba->cursor);
  124. dba->cursor = NULL;
  125. }
  126. #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)
  127. if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor, 0)) {
  128. #else
  129. if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor)) {
  130. #endif
  131. return NULL;
  132. }
  133. /* we should introduce something like PARAM_PASSTHRU... */
  134. return dba_nextkey_db2(info, newlen);
  135. }
  136. DBA_NEXTKEY_FUNC(db2)
  137. {
  138. DB2_DATA;
  139. DBT gkey = {0}, gval = {0};
  140. if (dba->cursor->c_get(dba->cursor, &gkey, &gval, DB_NEXT)
  141. || !gkey.data)
  142. return NULL;
  143. if (newlen) *newlen = gkey.size;
  144. return estrndup(gkey.data, gkey.size);
  145. }
  146. DBA_OPTIMIZE_FUNC(db2)
  147. {
  148. return SUCCESS;
  149. }
  150. DBA_SYNC_FUNC(db2)
  151. {
  152. DB2_DATA;
  153. return dba->dbp->sync(dba->dbp, 0) ? FAILURE : SUCCESS;
  154. }
  155. DBA_INFO_FUNC(db2)
  156. {
  157. return estrdup(DB_VERSION_STRING);
  158. }
  159. #endif
  160. /*
  161. * Local variables:
  162. * tab-width: 4
  163. * c-basic-offset: 4
  164. * End:
  165. * vim600: sw=4 ts=4 fdm=marker
  166. * vim<600: sw=4 ts=4
  167. */