DBase.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package php.db;
  26. private class DBaseConnection {
  27. var c : Void;
  28. var fields : Array<Field>;
  29. public function new( file : String, mode : Int) {
  30. c = untyped __call__("dbase_open", file, mode);
  31. if(c == null) throw "Invalid dBase file: " + file;
  32. var infos : Array<Dynamic> = untyped __call__("dbase_get_header_info", c);
  33. fields = [];
  34. for(info in infos) {
  35. fields.push({
  36. name : info[untyped 'name'],
  37. type : getType(info)
  38. });
  39. }
  40. }
  41. public function close() {
  42. untyped __call__("dbase_close", c);
  43. untyped __call__("unset", c);
  44. }
  45. public function count() {
  46. return untyped __call__("dbase_numrecords", c);
  47. }
  48. public function insert(values : Array<Dynamic>) : Bool {
  49. return untyped __call__("dbase_add_record", c, values);
  50. }
  51. public function replace(index : Int, values : Array<Dynamic>) {
  52. return untyped __call__("dbase_replace_record", c, values, index);
  53. }
  54. public function delete(index : Int) : Bool {
  55. return untyped __call__("dbase_delete_record", c, index);
  56. }
  57. public function records() : Array<Dynamic> {
  58. var arr = [];
  59. for(i in 1...count()+1)
  60. arr.push(record(i));
  61. return arr;
  62. }
  63. public function rows() : Array<Array<Dynamic>> {
  64. var arr = [];
  65. for(i in 1...count()+1)
  66. arr.push(row(i));
  67. return arr;
  68. }
  69. public function row(index : Int) : Array<Dynamic> {
  70. var r = untyped __call__("dbase_get_record", c, index);
  71. if(untyped __php__("isset($r['deleted'])")) {
  72. untyped __php__("unset($r['deleted'])");
  73. }
  74. return r;
  75. }
  76. public function record(index : Int) : Dynamic {
  77. var row = row(index);
  78. var record = {};
  79. for(j in 0...fields.length)
  80. Reflect.setField(record, fields[j].name, row[j]);
  81. return record;
  82. }
  83. private function getType(info : Array<Dynamic>) {
  84. switch(info[untyped 'type']) {
  85. case 'D': return DateField;
  86. case 'N': return NumberField(info[untyped 'length'], info[untyped 'precision']);
  87. case 'C': return StringField(info[untyped 'length']);
  88. case 'L': return BooleanField;
  89. case 'F': return FloatField;
  90. }
  91. return null;
  92. }
  93. }
  94. class DBase {
  95. public static function open( file : String ) : DBaseConnection {
  96. return new DBaseConnection(file, 2);
  97. }
  98. public static function openReadOnly( file : String ) : DBaseConnection {
  99. return new DBaseConnection(file, 0);
  100. }
  101. public static function create(file : String, fields : Array<Field>) : Void {
  102. var flds : Array<Array<Dynamic>> = [];
  103. for(field in fields) {
  104. var fld : Array<Dynamic>= [];
  105. fld.push(field.name);
  106. switch(field.type) {
  107. case DateField:
  108. fld.push('D');
  109. case NumberField(len, precision):
  110. fld.push('N');
  111. fld.push(len);
  112. fld.push(precision);
  113. case StringField(len):
  114. fld.push('C');
  115. fld.push(len);
  116. case BooleanField:
  117. fld.push('L');
  118. case FloatField:
  119. fld.push('F');
  120. }
  121. flds.push(fld);
  122. }
  123. var h = untyped __call__("dbase_create", file, flds);
  124. if(h == false) throw "Error creating: " + file;
  125. untyped __call__("dbase_close", h);
  126. }
  127. }
  128. typedef Field = {
  129. name : String, // max 10 chars
  130. type : FieldType
  131. }
  132. enum FieldType {
  133. // MemoField;
  134. DateField;
  135. NumberField(len : Int, precision : Int);
  136. StringField(len : Int);
  137. BooleanField;
  138. FloatField;
  139. }