interbase.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. srand((double)microtime()*1000000);
  3. $user = 'SYSDBA';
  4. $password = 'masterkey';
  5. ini_set('ibase.default_user',$user);
  6. ini_set('ibase.default_password',$password);
  7. /* we need just the generated name, not the file itself */
  8. unlink($test_base = tempnam('/tmp',"php_ibase_test"));
  9. function init_db()
  10. {
  11. global $test_base, $user, $password;
  12. $test_db = ibase_query(IBASE_CREATE,
  13. sprintf("CREATE SCHEMA '%s' USER '%s' PASSWORD '%s' DEFAULT CHARACTER SET %s",$test_base,
  14. $user, $password, ($charset = ini_get('ibase.default_charset')) ? $charset : 'NONE'));
  15. $tr = ibase_trans($test_db);
  16. ibase_query($tr,"create table test1 (i integer, c varchar(100))");
  17. ibase_commit_ret($tr);
  18. ibase_query($tr,"insert into test1(i, c) values(1, 'test table not created with isql')");
  19. ibase_commit($tr);
  20. ibase_close($test_db);
  21. }
  22. function cleanup_db()
  23. {
  24. global $test_base;
  25. $r = ibase_connect($test_base);
  26. ibase_drop_db($r);
  27. }
  28. register_shutdown_function('cleanup_db');
  29. init_db();
  30. function out_table($table_name)
  31. {
  32. echo "--- $table_name ---\n";
  33. $res = ibase_query("select * from $table_name");
  34. while ($r = ibase_fetch_row($res)) {
  35. echo join("\t",$r)."\t\n";
  36. }
  37. ibase_free_result($res);
  38. echo "---\n";
  39. }
  40. function out_result($result, $table_name = "")
  41. {
  42. echo "--- $table_name ---\n";
  43. while ($r = ibase_fetch_row($result)) {
  44. echo join("\t",$r)."\t\n";
  45. }
  46. echo "---\n";
  47. }
  48. function out_result_trap_error($result, $table_name = "")
  49. {
  50. echo "--- $table_name ---\n";
  51. while ($r = @ibase_fetch_row($result)) {
  52. echo join("\t",$r)."\t\n";
  53. }
  54. echo "errmsg [" . ibase_errmsg() . "]\t\n";
  55. echo "---\n";
  56. }
  57. /* M/D/Y H:M:S */
  58. function rand_datetime()
  59. {
  60. return sprintf("%02d/%02d/%4d %02d:%02d:%02d",
  61. rand()%12+1, rand()%28+1, rand()%100+1910,
  62. rand()%24, rand()%60, rand()%60);
  63. }
  64. /* random binary string */
  65. function rand_binstr($max_len)
  66. {
  67. $len = rand() % $max_len;
  68. $s = "";
  69. while($len--) {
  70. $s .= sprintf("%c", rand() % 256);
  71. }
  72. return $s;
  73. }
  74. function rand_str($max_len)
  75. {
  76. $len = rand() % $max_len;
  77. $s = "";
  78. while ($len--) {
  79. $s .= sprintf("%c", rand() % 26 + 65);
  80. }
  81. return $s;
  82. }
  83. function rand_number($len , $prec = -1, $sign = 1)
  84. {
  85. if ($prec == -1) {
  86. $n = substr(rand() . rand(), 0, rand() % $len + 1);
  87. if (strlen($n) < $len) {
  88. $n .= "." . substr(rand(), 0, rand() % ($len - strlen($n)) + 1);
  89. }
  90. } else if ($prec == 0) {
  91. $n = substr(rand() . rand(), 0, rand() % $len + 1);
  92. } else if (($prec - $len) == 0) {
  93. $n = substr(rand() . rand(), 0, 1);
  94. $n .= "." . substr(rand(), 0, $prec);
  95. } else {
  96. $n = substr(rand() . rand(), 0, rand() % ($len - $prec) + 1);
  97. $n .= "." . substr(rand(), 0, $prec);
  98. }
  99. if ($sign && (rand() % 3 == 0)) {
  100. $n = "-" .$n;
  101. }
  102. return $n;
  103. }
  104. ?>