storage_db.module

Tracking 6.x-4.x branch
  1. drupal
    1. 6 contributions/storage_api/services/db/storage_db.module

Functions & methods

NameDescription
storage_db_storage_container_create
storage_db_storage_container_destroy
storage_db_storage_container_info
storage_db_storage_container_settings_form
storage_db_storage_container_validate
storage_db_storage_object_instance_create
storage_db_storage_object_instance_destroy
storage_db_storage_object_instance_get_filepath
storage_db_storage_object_instance_serve
storage_db_storage_service_info

File

View source
  1. <?php
  2. // Copyright 2008-2009 Jonathan Brown
  3. function storage_db_storage_service_info() {
  4. $info = array(
  5. 'name' => t('Database'),
  6. 'dynamic' => TRUE,
  7. 'local' => FALSE,
  8. 'direct' => TRUE
  9. );
  10. return $info;
  11. }
  12. function storage_db_storage_container_settings_form() {
  13. global $db_url;
  14. if(is_array($db_url)) {
  15. foreach($db_url as $key => $url)
  16. $options[$key] = $key;
  17. $items['db'] = array(
  18. '#type' => 'select',
  19. '#title' => t('Database'),
  20. '#options' => $options,
  21. '#required' => TRUE,
  22. '#validated' => TRUE
  23. );
  24. }
  25. else {
  26. $items['#db'] = 'default';
  27. }
  28. $items['table'] = array(
  29. '#type' => 'textfield',
  30. '#title' => t('Table'),
  31. '#description' => t('Without prefix or {}, e.g. \'storage_contents\'.'),
  32. '#required' => TRUE,
  33. '#validated' => TRUE
  34. );
  35. return $items;
  36. }
  37. function storage_db_storage_container_validate($container) {
  38. if(!$container['settings']['table']) {
  39. form_set_error('settings][path', t('Database table field is required.'));
  40. return;
  41. }
  42. }
  43. function storage_db_storage_container_create($container) {
  44. db_set_active($container['settings']['db']);
  45. $success = db_query("
  46. CREATE TABLE {%s} (
  47. object_id INT UNSIGNED NOT NULL,
  48. contents LONGBLOB NOT NULL,
  49. PRIMARY KEY (object_id)
  50. )
  51. ",
  52. $container['settings']['table']
  53. );
  54. db_set_active();
  55. return $success;
  56. }
  57. function storage_db_storage_container_info($container) {
  58. $info[t('Database')] = $container['settings']['db'];
  59. $info[t('Table')] = $container['settings']['table'];
  60. return $info;
  61. }
  62. function storage_db_storage_container_destroy($container) {
  63. db_set_active($container['settings']['db']);
  64. $success = db_query("
  65. DROP TABLE {%s}
  66. ",
  67. $container['settings']['table']
  68. );
  69. db_set_active();
  70. return $success;
  71. }
  72. function storage_db_storage_object_instance_create($container, $file) {
  73. db_set_active($container['settings']['db']);
  74. $success = db_query("
  75. INSERT INTO {%s}
  76. SET object_id = %d,
  77. contents = 0x%s
  78. ",
  79. $container['settings']['table'],
  80. $file['object_id'],
  81. bin2hex(file_get_contents($file['filepath']))
  82. );
  83. db_set_active();
  84. return $success;
  85. }
  86. function storage_db_storage_object_instance_destroy($container, $file) {
  87. db_set_active($container['settings']['db']);
  88. $success = db_query("
  89. DELETE FROM {%s}
  90. WHERE object_id = %d
  91. ",
  92. $container['settings']['table'],
  93. $file['object_id']
  94. );
  95. db_set_active();
  96. return $success;
  97. }
  98. function storage_db_storage_object_instance_get_filepath($container, $file) {
  99. $filepath = tempnam(file_directory_temp(), '');
  100. db_set_active($container['settings']['db']);
  101. $success = file_put_contents($filepath,
  102. db_result(db_query("
  103. SELECT contents
  104. FROM {%s}
  105. WHERE object_id = %d
  106. ",
  107. $container['settings']['table'],
  108. $file['object_id']
  109. ))
  110. );
  111. db_set_active();
  112. if(!$success) {
  113. @unlink($filepath);
  114. return FALSE;
  115. }
  116. return $filepath;
  117. }
  118. function storage_db_storage_object_instance_serve($container, $file) {
  119. storage_api_set_http_headers($file);
  120. db_set_active($container['settings']['db']);
  121. echo db_result(db_query("
  122. SELECT contents
  123. FROM {%s}
  124. WHERE object_id = %d
  125. ",
  126. $container['settings']['table'],
  127. $file['object_id']
  128. ));
  129. exit();
  130. }