utils.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. class DatabaseUtils {
  3. /**
  4. * Summary of BindArguments
  5. * @param PDOStatement $stmt
  6. * @param array $values
  7. */
  8. public static function BindArguments(\PDOStatement $stmt, array &$values) {
  9. foreach ($values as $key => &$value) {
  10. $stmt->bindParam($key, $value, PDO::PARAM_STR);
  11. }
  12. }
  13. /**
  14. * Summary of BindExpressions
  15. * @param PDOStatement $stmt
  16. * @param array $values
  17. * @param array $remove_from
  18. */
  19. public static function BindExpressions(\PDOStatement $stmt, array &$values, array &$remove_from) {
  20. foreach ($values as $key => $value) {
  21. unset($remove_from[$key]);
  22. if (empty($value['arguments'])) {
  23. continue;
  24. }
  25. if (is_array($value['arguments'])) {
  26. foreach ($value['arguments'] as $placeholder => $argument) {
  27. // We assume that an expression will never happen on a BLOB field,
  28. // which is a fairly safe assumption to make since in most cases
  29. // it would be an invalid query anyway.
  30. $stmt->bindParam($placeholder, $value['arguments'][$placeholder]);
  31. }
  32. }
  33. else {
  34. $stmt->bindParam($key, $value['arguments'], PDO::PARAM_STR);
  35. }
  36. }
  37. }
  38. /**
  39. * Binds a set of values to a PDO Statement,
  40. * taking care of properly managing binary data.
  41. *
  42. * @param PDOStatement $stmt
  43. * PDOStatement to bind the values to
  44. *
  45. * @param array $values
  46. * Values to bind. It's an array where the keys are column
  47. * names and the values what is going to be inserted.
  48. *
  49. * @param array $blobs
  50. * When sending binary data to the PDO driver, we need to keep
  51. * track of the original references to data
  52. *
  53. * @param array $ref_prefix
  54. * The $ref_holder might be shared between statements, use this
  55. * prefix to prevent key colision.
  56. *
  57. * @param mixed $placeholder_prefix
  58. * Prefix to use for generating the query placeholders.
  59. *
  60. * @param mixed $max_placeholder
  61. * Placeholder count, if NULL will start with 0.
  62. *
  63. */
  64. public static function BindValues(\PDOStatement $stmt, array &$values, array &$blobs, $placeholder_prefix, $columnInformation, &$max_placeholder = NULL, $blob_suffix = NULL) {
  65. if (empty($max_placeholder)) {
  66. $max_placeholder = 0;
  67. }
  68. foreach ($values as $field_name => &$field_value) {
  69. $placeholder = $placeholder_prefix . $max_placeholder++;
  70. $blob_key = $placeholder . $blob_suffix;
  71. if (isset($columnInformation['blobs'][$field_name])) {
  72. $blobs[$blob_key] = fopen('php://memory', 'a');
  73. fwrite($blobs[$blob_key], $field_value);
  74. rewind($blobs[$blob_key]);
  75. $stmt->bindParam($placeholder, $blobs[$blob_key], PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
  76. }
  77. else {
  78. // Even though not a blob, make sure we retain a copy of these values.
  79. $blobs[$blob_key] = $field_value;
  80. $stmt->bindParam($placeholder, $blobs[$blob_key], PDO::PARAM_STR);
  81. }
  82. }
  83. }
  84. public static function GetConfigBoolean($name, $default = FALSE) {
  85. global $conf;
  86. // Isolation level.
  87. if (isset($conf[$name])) {
  88. $value = $conf[$name];
  89. if (is_bool($value)) {
  90. return $value;
  91. }
  92. }
  93. return FALSE;
  94. }
  95. /**
  96. * Get the value of a constant string from configuration.
  97. *
  98. * @param mixed $name
  99. * @param mixed $default
  100. * @return mixed
  101. */
  102. public static function GetConfigConstant($name, $default) {
  103. global $conf;
  104. // Isolation level.
  105. if (isset($conf[$name])) {
  106. $level = $conf[$name];
  107. if($l = @constant($level)) {
  108. return $l;
  109. }
  110. }
  111. return $default;
  112. }
  113. /**
  114. * Returns the spec for a MSSQL data type definition.
  115. *
  116. * @param mixed $type
  117. */
  118. public static function GetMSSQLType($type) {
  119. $matches = array();
  120. if(preg_match('/^[a-zA-Z]*/' , $type, $matches)) {
  121. return reset($matches);
  122. }
  123. return $type;
  124. }
  125. }