diff --git transactd.yield.php transactd.yield.php index 080159f..212ef62 100644 --- transactd.yield.php +++ transactd.yield.php @@ -1,4 +1,9 @@ _cPtr); - if (is_resource($r)) { - $c=substr(get_resource_type($r), (strpos(get_resource_type($r), '__') ? strpos(get_resource_type($r), '__') + 2 : 3)); - if (class_exists($c)) return new $c($r); - return new database($r); - } - return $r; + $this->__construct($r); } function create($uri,$type=0) { @@ -2619,10 +2619,111 @@ class btrTimeStamp { } } -class fielddefs { +abstract class RangeIterator implements \Iterator { + protected $_position = 0; + protected $_start = -1; + protected $_end = -1; + + function __construct($start, $end) { + $this->_position = 0; + $this->_start = $start; + $this->_end = $end; + } + + public function rewind() { + $this->_position = $this->_start; + } + + public function valid() { + return $this->_position <= $this->_end; + } + + abstract public function current(); + + public function key() { + return $this->_position; + } + + public function next() { + $this->_position++; + } +} + +class fielddefsIterator extends RangeIterator { + private $_fielddefs_ptr = NULL; + + function __construct($fielddefs_ptr, $start, $end) { + $this->_fielddefs_ptr = $fielddefs_ptr; + parent::__construct($start, $end); + } + + public function current() { + $r = fielddefs_getFielddef($this->_fielddefs_ptr,$this->_position); + if (is_resource($r)) + return new fielddef($r); + return $r; + } +} + +class fielddefs implements \ArrayAccess, \Countable, \IteratorAggregate { public $_cPtr=null; protected $_pData=array(); + // IteratorAggregate + public function getIterator() { + return new fielddefsIterator($this->_cPtr, 0, (fielddefs_size($this->_cPtr) - 1)); + } + + // ArrayAccess + public function offsetExists($offset) { + return (\gettype($offset) === "integer" && + $offset >= 0 && $offset < fielddefs_size($this->_cPtr)); + } + + public function offsetGet($offset) { + if (\gettype($offset) !== "integer" || + $offset < 0 || $offset >= fielddefs_size($this->_cPtr)) + throw new \OutOfRangeException(); + $r = fielddefs_getFielddef($this->_cPtr,$offset); + if (is_resource($r)) + return new fielddef($r); + return $r; + } + + public function offsetSet($offset, $value) { + throw new \BadMethodCallException(); + } + + public function offsetUnset($offset) { + throw new \BadMethodCallException(); + } + + // Countable + public function count() { + return fielddefs_size($this->_cPtr); + } + + // Generator + function range($start = null, $end = null) { + $count = fielddefs_size($this->_cPtr); + if (\gettype($start) !== 'integer' || $start < 0) { + $i = 0; + } else { + $i = $start; + } + if (\gettype($end) !== 'integer' || $end < 0 || $end >= $count) { + $end = $count - 1; + } + while ($i <= $end) { + $r = fielddefs_getFielddef($this->_cPtr, $i); + if (is_resource($r)) + yield new fielddef($r); + else + yield $r; + $i++; + } + } + function __set($var,$value) { if ($var === 'thisown') return swig_transactd_alter_newobject($this->_cPtr,$value); $this->_pData[$var] = $value; @@ -2640,12 +2741,7 @@ class fielddefs { function __clone() { $r=fielddefs___clone($this->_cPtr); - if (is_resource($r)) { - $c=substr(get_resource_type($r), (strpos(get_resource_type($r), '__') ? strpos(get_resource_type($r), '__') + 2 : 3)); - if (class_exists($c)) return new $c($r); - return new fielddefs($r); - } - return $r; + $this->__construct($r); } function indexByName($name) { @@ -2680,6 +2776,51 @@ class fielddefs { } class field { + public function getFV() { + switch ($this->type()) { + case transactd::ft_integer: + case transactd::ft_uinteger: + case transactd::ft_autoinc: + case transactd::ft_autoIncUnsigned: + case transactd::ft_logical: + case transactd::ft_bit: + return $this->i64(); + case transactd::ft_float: + case transactd::ft_decimal: + case transactd::ft_money: + case transactd::ft_numeric: + case transactd::ft_bfloat: + case transactd::ft_numericsts: + case transactd::ft_numericsa: + case transactd::ft_currency: + return $this->d(); + case transactd::ft_mychar: + case transactd::ft_myvarchar: + case transactd::ft_mywchar: + case transactd::ft_mywvarchar: + case transactd::ft_mytext: + case transactd::ft_mydate: + case transactd::ft_mytime: + case transactd::ft_mydatetime: + case transactd::ft_mytimestamp: + case transactd::ft_date: + case transactd::ft_time: + case transactd::ft_datetime: + case transactd::ft_timestamp: + case transactd::ft_note: + case transactd::ft_zstring: + return $this->c_str(); + case transactd::ft_string: + case transactd::ft_myvarbinary: + case transactd::ft_mywvarbinary: + case transactd::ft_myblob: + return $this->getBin(); + default: + return $this->c_str(); + } + return null; + } + public $_cPtr=null; protected $_pData=array(); @@ -2766,7 +2907,141 @@ class field { } } -abstract class Record { +class RecordIterator implements \Iterator { + private $_record_cPtr = null; + private $_position = 0; + private $_count = -1; + private $_field = null; + private $_fielddefs = null; + + function __construct($record_cPtr, $fielddefs) { + $this->_record_cPtr = $record_cPtr; + $this->_position = 0; + $this->_count = Record_size($record_cPtr); + $this->_fielddefs = $fielddefs; + $this->_field = new field(); + } + + public function rewind() { + $this->_position = 0; + } + + public function valid() { + return $this->_position < $this->_count; + } + + public function current() { + Record_getFieldByIndexRef($this->_record_cPtr, $this->_position, $this->_field); + return $this->_field->getFV(); + } + + public function key() { + return $this->_fielddefs->getFielddef($this->_position)->name(); + } + + public function next() { + $this->_position++; + } +} + +class Record implements \ArrayAccess, \Countable, \IteratorAggregate { + protected $_field = null; + protected $_fielddefs = null; + + function __clone() { + $this->_field = new field(); + } + + // IteratorAggregate + public function getIterator() { + return new RecordIterator($this->_cPtr, $this->_fielddefs); + } + + // ArrayAccess + public function offsetExists($offset) { + switch (\gettype($offset)) { + case "integer": + return $offset >= 0 && $offset < $this->count(); + case "string": + return Record_indexByName($this->_cPtr, $offset) >= 0; + default: + return false; + } + } + + public function offsetGet($offset) { + switch (\gettype($offset)) { + case "integer": + Record_getFieldByIndexRef($this->_cPtr, $offset, $this->_field); + break; + case "string": + Record_getFieldByNameRef($this->_cPtr, $offset, $this->_field); + break; + default: + throw new \OutOfRangeException(); + } + return $this->_field->getFV(); + } + + public function offsetSet($offset, $value) { + throw new \BadMethodCallException(); + } + + public function offsetUnset($offset) { + throw new \BadMethodCallException(); + } + + // Countable + public function count() { + return Record_size($this->_cPtr); + } + + // Generator + function keys() { + $count = Record_size($this->_cPtr); + for ($i = 0; $i < $count; $i++) { + yield $this->_fielddefs->getFielddef($i)->name(); + } + } + + function values() { + $count = Record_size($this->_cPtr); + for ($i = 0; $i < $count; $i++) { + Record_getFieldByIndexRef($this->_cPtr, $i, $this->_field); + yield $this->_field->getFV(); + } + } + + // toArray + function keysArray() { + $ret = array(); + $count = Record_size($this->_cPtr); + for ($i = 0; $i < $count; $i++) { + $ret[] = $this->_fielddefs->getFielddef($i)->name(); + } + return $ret; + } + + function valuesArray() { + $ret = array(); + $count = Record_size($this->_cPtr); + for ($i = 0; $i < $count; $i++) { + Record_getFieldByIndexRef($this->_cPtr, $i, $this->_field); + $ret[] = $this->_field->getFV(); + } + return $ret; + } + + function toArray() { + $ret = array(); + $count = Record_size($this->_cPtr); + for ($i = 0; $i < $count; $i++) { + Record_getFieldByIndexRef($this->_cPtr, $i, $this->_field); + $ret[$this->_fielddefs->getFielddef($i)->name()] = $this->_field->getFV(); + } + return $ret; + } + public $_cPtr=null; protected $_pData=array(); @@ -2786,6 +3061,8 @@ abstract class Record { } function __construct($h) { $this->_cPtr=$h; + $this->_fielddefs = $this->fieldDefs(); + $this->_field = new field(); } function isInvalidRecord() { @@ -2863,14 +3140,31 @@ class memoryRecord extends Record { function __construct($fds) { if (is_resource($fds) && get_resource_type($fds) === '_p_bzs__db__protocol__tdap__client__memoryRecord') { + parent::__construct($fds); $this->_cPtr=$fds; return; } $this->_cPtr=new_memoryRecord($fds); + parent::__construct($this->_cPtr); } } class writableRecord extends memoryRecord { + // override ArrayAccess method (set value). + public function offsetSet($offset, $value) { + $this->offsetGet($offset); + switch ($this->_field->type()) { + case transactd::ft_string: + case transactd::ft_myvarbinary: + case transactd::ft_mywvarbinary: + case transactd::ft_myblob: + $this->_field->setFV($value, strlen($value)); + break; + default: + $this->_field->setFV($value); + } + } + public $_cPtr=null; function __set($var,$value) { @@ -2889,6 +3183,8 @@ class writableRecord extends memoryRecord { } function __construct($h) { $this->_cPtr=$h; + $this->_fielddefs = $this->fieldDefs(); + $this->_field = new field(); } function read($KeysetAlrady=false) { @@ -3225,6 +3521,7 @@ class recordsetQuery { abstract class groupFuncBase extends recordsetQuery { public $_cPtr=null; + protected $resultName = ''; function __set($var,$value) { if ($var === 'thisown') return swig_transactd_alter_newobject($this->_cPtr,$value); @@ -3259,7 +3556,8 @@ abstract class groupFuncBase extends recordsetQuery { } function setResultName($v) { - groupFuncBase_setResultName($this->_cPtr,$v); + $this->resultName = $v; + groupFuncBase_setResultName($this->_cPtr,$this->resultName); } function resultKey() { @@ -3372,6 +3670,7 @@ class groupQuery { class sum extends groupFuncBase { public $_cPtr=null; + protected $targetNames = null; function __set($var,$value) { if ($var === 'thisown') return swig_transactd_alter_newobject($this->_cPtr,$value); @@ -3399,9 +3698,11 @@ class sum extends groupFuncBase { $this->_cPtr=$targetNames; return; } + $this->targetNames = $targetNames; + $this->resultName = $resultName; switch (func_num_args()) { - case 1: $this->_cPtr=new_sum($targetNames); break; - default: $this->_cPtr=new_sum($targetNames,$resultName); + case 1: $this->_cPtr=new_sum($this->targetNames); break; + default: $this->_cPtr=new_sum($this->targetNames,$this->resultName); } } } @@ -3435,7 +3736,8 @@ class count extends groupFuncBase { $this->_cPtr=$resultName; return; } - $this->_cPtr=new_count($resultName); + $this->resultName = $resultName; + $this->_cPtr=new_count($this->resultName); } } @@ -3468,9 +3770,11 @@ class avg extends sum { $this->_cPtr=$targetNames; return; } + $this->targetNames = $targetNames; + $this->resultName = $resultName; switch (func_num_args()) { - case 1: $this->_cPtr=new_avg($targetNames); break; - default: $this->_cPtr=new_avg($targetNames,$resultName); + case 1: $this->_cPtr=new_avg($this->targetNames); break; + default: $this->_cPtr=new_avg($this->targetNames,$this->resultName); } } } @@ -3504,9 +3808,11 @@ class min extends sum { $this->_cPtr=$targetNames; return; } + $this->targetNames = $targetNames; + $this->resultName = $resultName; switch (func_num_args()) { - case 1: $this->_cPtr=new_min($targetNames); break; - default: $this->_cPtr=new_min($targetNames,$resultName); + case 1: $this->_cPtr=new_min($this->targetNames); break; + default: $this->_cPtr=new_min($this->targetNames,$this->resultName); } } } @@ -3540,14 +3846,130 @@ class max extends sum { $this->_cPtr=$targetNames; return; } + $this->targetNames = $targetNames; + $this->resultName = $resultName; switch (func_num_args()) { - case 1: $this->_cPtr=new_max($targetNames); break; - default: $this->_cPtr=new_max($targetNames,$resultName); + case 1: $this->_cPtr=new_max($this->targetNames); break; + default: $this->_cPtr=new_max($this->targetNames,$this->resultName); + } + } +} + +class RecordsetIterator implements \SeekableIterator { + private $_recordset_cPtr = null; + private $_position = 0; + private $_fieldsBase_p_p = null; + private $_record = null; + private $_count = -1; + + function __construct($recordset_cPtr, $fielddefs) { + $this->_recordset_cPtr = $recordset_cPtr; + $this->_position = 0; + $this->_fieldsBase_p_p = new_fieldsBase_p_p(); + $this->_count = Recordset_count($recordset_cPtr); + $this->_record = new Record(memoryRecord::createRecord($fielddefs)); + } + + function __destruct() { + delete_fieldsBase_p_p($this->_fieldsBase_p_p); + } + + public function rewind() { + $this->_position = 0; + } + + public function valid() { + return $this->_position < $this->_count; + } + + public function current() { + Recordset_getRow($this->_recordset_cPtr, $this->_position, $this->_fieldsBase_p_p); + $this->_record->_cPtr = fieldsBase_p_p_value($this->_fieldsBase_p_p); + return $this->_record; + } + + public function key() { + return $this->_position; + } + + public function next() { + $this->_position++; + } + + public function seek($position) { + if ($position < 0 || $position >= $this->_count) { + throw new \OutOfBoundsException("invalid seek position ($position)"); } + $this->_position = $position; } } -class Recordset { +class Recordset implements \ArrayAccess, \Countable, \IteratorAggregate { + private $_fieldsBase_p_p = null; + private $_record = null; + + function __destruct() { + transactd::delete_fieldsBase_p_p($this->_fieldsBase_p_p); + } + + // IteratorAggregate + public function getIterator() { + return new RecordsetIterator($this->_cPtr, $this->fieldDefs()); + } + + // ArrayAccess + public function offsetExists($offset) { + return \gettype($offset) !== 'integer' && $offset >= 0 && $offset < $this->count(); + } + + public function offsetGet($offset) { + Recordset_getRow($this->_cPtr, $offset, $this->_fieldsBase_p_p); + $this->_record->_cPtr = fieldsBase_p_p_value($this->_fieldsBase_p_p); + return $this->_record; + } + + public function offsetSet($offset, $value) { + throw new \BadMethodCallException(); + } + + public function offsetUnset($offset) { + throw new \BadMethodCallException(); + } + + // random access + function first() { + if ($this->count() <= 0) { + throw new \OutOfBoundsException('no records in recordset'); + } + return $this->offsetGet(0); + } + + function last() { + if ($this->count() <= 0) { + throw new \OutOfBoundsException('no records in recordset'); + } + return $this->offsetGet($this->count() - 1); + } + + // Generator + function range($start = null, $end = null) { + $count = $this->count(); + if (\gettype($start) !== 'integer' || $start < 0) { + $i = 0; + } else { + $i = $start; + } + if (\gettype($end) !== 'integer' || $end < 0 || $end >= $count) { + $end = $count - 1; + } + while ($i <= $end) { + Recordset_getRow($this->_cPtr, $i, $this->_fieldsBase_p_p); + $this->_record->_cPtr = fieldsBase_p_p_value($this->_fieldsBase_p_p); + yield $this->_record; + $i++; + } + } + public $_cPtr=null; protected $_pData=array(); @@ -3569,11 +3991,12 @@ class Recordset { function __clone() { $r=Recordset___clone($this->_cPtr); if (is_resource($r)) { - $c=substr(get_resource_type($r), (strpos(get_resource_type($r), '__') ? strpos(get_resource_type($r), '__') + 2 : 3)); - if (class_exists($c)) return new $c($r); - return new Recordset($r); + $this->_cPtr = $r; + } else { + $this->_cPtr = $r->_cPtr; } - return $r; + $this->_fieldsBase_p_p = new_fieldsBase_p_p(); + $this->_record = new Record(memoryRecord::createRecord($this->fieldDefs())); } function size() { @@ -3697,9 +4120,13 @@ class Recordset { function __construct($res=null) { if (is_resource($res) && get_resource_type($res) === '_p_bzs__db__protocol__tdap__client__recordset') { $this->_cPtr=$res; + $this->_fieldsBase_p_p = new_fieldsBase_p_p(); + $this->_record = new Record(memoryRecord::createRecord($this->fieldDefs())); return; } $this->_cPtr=new_Recordset(); + $this->_fieldsBase_p_p = new_fieldsBase_p_p(); + $this->_record = new Record(memoryRecord::createRecord($this->fieldDefs())); } } @@ -3831,7 +4258,7 @@ class activeTable { } } - function __construct($mgr_or_db,$tableName) { + function __construct($mgr_or_db,$tableName=null) { if (is_resource($mgr_or_db) && get_resource_type($mgr_or_db) === '_p_bzs__db__protocol__tdap__client__activeTable') { $this->_cPtr=$mgr_or_db; return;