28 import(
'Dataface/QueryBuilder.php');
29 import(
'Dataface/Table.php');
30 import(
'Dataface/Record.php');
31 import(
'Dataface/DB.php');
69 if ( !is_array($query) ) $query= array();
71 $db = DATAFACE_DB_HANDLE;
74 $this->_query = $query;
79 $this->_data = array();
80 if ( isset( $query[
'-cursor'] ) ){
81 $this->_data[
'cursor'] = $query[
'-cursor'];
83 $this->_data[
'cursor'] = 0;
86 if ( isset( $query[
'-skip'] ) ){
87 $this->_data[
'start'] = $query[
'-skip'];
89 $this->_data[
'start'] = 0;
92 if ( isset( $query[
'-limit'] ) ){
93 $this->_data[
'end'] = $this->_data[
'start'] + $query[
'-limit']-1;
94 $this->_data[
'limit'] = $query[
'-limit'];
96 $this->_data[
'end'] = $this->_data[
'start'] +
$GLOBALS[
'Dataface_QueryTool_limit']-1;
97 $this->_data[
'limit'] = $GLOBALS[
'Dataface_QueryTool_limit'];
101 $tableKeyNames = array_keys($this->_table->keys());
102 if ( count($tableKeyNames) <= 0 )
throw new Exception(
"The table '$tablename' has no primary key. Please add one.", E_USER_ERROR);
104 $firstKeyName = $tableKeyNames[0];
107 $sql =
"select count(`$firstKeyName`) from `$tablename`";
109 if ( isset($cache[$sql]) ) $this->_data[
'cardinality'] = $cache[$sql];
111 $res = $this->dbObj->query( $sql, $this->_db,null,
true );
112 if ( !$res and !is_array($res) )
throw new Exception(
"We had a problem with the query $sql.", E_USER_ERROR);
114 $this->_data[
'cardinality'] = reset($res[0]);
115 $cache[$sql] = $this->_data[
'cardinality'];
119 $builder->selectMetaData =
true;
120 $sql = $builder->select_num_rows();
121 if ( isset($cache[$sql]) ){
122 $this->_data[
'found'] = $cache[$sql];
125 $res = $this->dbObj->query( $sql, $this->_db,null,
true );
126 if ( !$res and !is_array($res) ){
127 throw new Exception(mysql_error($this->_db).$sql, E_USER_ERROR);
129 $this->_data[
'found'] = array_shift($res[0]);
130 $cache[$sql] = $this->_data[
'found'];
133 if ( $this->_data[
'end'] > $this->_data[
'found']-1 ){
134 $this->_data[
'end'] = $this->_data[
'found']-1;
136 if ( $this->_data[
'start'] > $this->_data[
'found'] ){
137 $this->_data[
'start'] = $this->_data[
'found'];
143 function getTitles($ordered=
true, $genericKeys =
false, $ignoreLimit=
false){
145 if ( !isset($this->_titles[$ordered][$genericKeys][$ignoreLimit]) ){
146 $titleColumn = $this->_table->titleColumn();
148 $keys = array_keys($this->_table->keys());
152 'No primary key defined',
153 'There is no primary key defined on table "'.$this->_table->tablename.
'". Please define a primary key.',
154 array(
'table'=>$this->_table->tablename,
'stack_trace'=>
'')
159 $len = strlen($titleColumn);
160 if ( $titleColumn{$len-1} !=
')' and $titleColumn{$len-1} !=
'`') $titleColumn =
'`'.$titleColumn.
'`';
163 $builder->action =
'select';
164 $from = $builder->
_from();
165 $sql =
"SELECT `".implode(
'`,`',
$keys).
"`,$titleColumn as `__titleColumn__` $from";
166 $where = $builder->_where();
167 $where = $builder->_secure($where);
168 $limit = $builder->_limit();
169 if ( strlen($where)>0 ){
173 $sql .=
" ORDER BY `__titleColumn__`";
175 $sql .= $builder->_orderby();
177 if ( strlen($limit)>0 and !$ignoreLimit ){
179 }
else if ( !$ignoreLimit) {
180 $sql .=
" LIMIT 250";
182 $res = $this->dbObj->query($sql, $this->_table->db, null,
true );
183 if ( !$res and !is_array($res) ){
184 $app->refreshSchemas($this->_table->tablename);
187 $res = $this->dbObj->query($sql, $this->_table->db,null,
true );
188 if ( !$res and !is_array($res) )
191 'scripts.Dataface.QueryTool.getTitles.ERROR_ERROR_RETRIEVING_TITLES',
192 "Error retrieving title from database in Dataface_QueryTool::getTitles(): "
194 .$sql.mysql_error($this->_table->db), E_USER_ERROR);
198 foreach ( $res as $row ){
199 $title = array_pop($row);
200 if ( !$genericKeys) {
203 while (
sizeof($row)>0 ){
204 $keyvals[current(
$keys)] = array_shift($row);
209 foreach ($keyvals as $keykey=>$keyval){
210 $keystr .= urlencode($keykey).
"=".urlencode($keyval).
"&";
212 $keystr = substr($keystr, 0, strlen($keystr)-1);
213 $titles[$keystr] = $title;
221 $this->_titles[$ordered][$genericKeys][$ignoreLimit] =& $titles;
224 return $this->_titles[$ordered][$genericKeys][$ignoreLimit];
241 $fields = $this->_table->fields(
false,
true);
242 $fieldnames = array_keys(
$fields);
244 $builder->selectMetaData =
true;
250 if ( !isset( $this->_data[
'loaded'] ) ){
251 $this->_data[
'loaded'] = array();
256 $loaded =& $this->_data[
'loaded'];
263 foreach ($fieldnames as $col){
264 if ( $loaded[$col] )
continue;
269 if (
sizeof( $cols ) > 0 ){
271 $tablekeys = array_keys($this->_table->keys());
272 $select_cols = array_merge($cols, $tablekeys);
273 $sql = $builder->select($select_cols, array(),
false, null, $preview);
276 $res = $this->dbObj->query( $sql, $this->_db, null,
true);
277 if ( !$res and !is_array($res) ){
278 $app->refreshSchemas($this->_table->tablename);
279 $res = $this->dbObj->query( $sql, $this->_db, null,
true);
280 if ( !$res and !is_array($res) )
284 'scripts.Dataface.QueryTool.loadSet.ERROR_LOADING_RECORDS',
285 "Error loading records in Dataface_QueryTool::loadSet(): "
287 .mysql_error($this->_db).
"\n<br>".$sql, E_USER_ERROR);
289 if ( !isset( $this->_data[
'start'] ) )
290 $this->_data[
'start'] = $this->_query[
'-skip'];
291 if ( !isset( $this->_data[
'end'] ) )
292 $this->_data[
'end'] = $this->_query[
'-skip'] + count($res);
294 if ( !isset( $this->_data[
'data'] ) ){
295 $this->_data[
'data'] = array();
296 $this->_data[
'indexedData'] = array();
299 $fieldnames = array_keys( $this->_table->fields(
false,
true) );
301 foreach ( $res as $row){
303 foreach ($tablekeys as $name){
307 foreach ($row as $att=>$attval){
308 if ( !in_array($att, $fieldnames) and strpos($att,
'__')!== 0 ){
313 if ( !isset( $this->_data[
'data'][$key] ) ){
314 $this->_data[
'data'][$key] = $row;
315 $this->_data[
'indexedData'][] =& $this->_data[
'data'][$key];
317 foreach ($cols as $col){
318 $this->_data[
'data'][$key][$col] = $row[$col];
324 foreach ($cols as $col){
325 $loaded[$col] =
true;
331 if (!isset( $this->_data[
'found'] ) ){
333 $sql = $builder->select_num_rows();
335 if ( isset($cache[$sql]) ){
336 $this->_data[
'found'] = $cache[$sql];
338 $res = $this->dbObj->query( $sql, $this->_db,null,
true );
339 $this->_data[
'found'] = array_shift($res[0]);
340 $cache[$sql] = $this->_data[
'found'];
345 if ( !isset( $this->_data[
'cardinality'] ) ){
346 $tableKeyNames = array_keys($this->_table->keys());
347 if ( count($tableKeyNames) <= 0 )
throw new Exception(
"The table '$tablename' has no primary key. Please add one.", E_USER_ERROR);
349 $firstKeyName = $tableKeyNames[0];
350 $sql =
"select count(`$firstKeyName`) from `".$this->_tablename.
'`';
352 if ( isset($cache[$sql]) ) $this->_data[
'cardinality'] = $cache[$sql];
354 $res = $this->dbObj->query( $sql, $this->_db,null,
true );
355 $this->_data[
'cardinality'] = array_shift($res[0]);
356 $cache[$sql] = $this->_data[
'cardinality'];
372 if ( $this->_currentRecord === null ){
376 $this->_currentRecord =
new Dataface_Record($this->_table->tablename, array());
384 $fields =& $this->_table->fields(
false,
true);
391 foreach ($names as $name){
392 if ( !$this->_currentRecord->isLoaded($name) ){
393 if ( !$loadText and $this->_table->isText($name) )
continue;
394 if ( !$loadBlobs and $this->_table->isBlob($name) )
continue;
395 if ( !$loadPasswords and $this->_table->isPassword($name) )
continue;
400 if (
sizeof( $unloaded ) > 0 ){
402 $query = array_merge( $this->_query, array(
'-skip'=>$this->_data[
'cursor'],
'-limit'=>1) );
404 $builder->selectMetaData =
true;
405 $builder->_omitBlobs =
false;
407 $sql = $builder->select($unloaded);
410 throw new Exception($sql->toString(), E_USER_ERROR);
414 $res = $this->dbObj->query($sql, $this->_db,null,
true );
415 if ( !$res and !is_array($res) ){
416 $app->refreshSchemas($this->_table->tablename);
417 $res = $this->dbObj->query($sql, $this->_db, null,
true );
418 if ( !$res and !is_array($res) ){
419 error_log(df_translate(
'scripts.Dataface.QueryTool.loadCurrent.ERROR_COULD_NOT_LOAD_CURRENT_RECORD',
"Error: Could not load current record: ").mysql_error( $this->_db).
"\n$sql");
420 throw new Exception(
"Failed to load current record due to an SQL error");
424 if (count($res) <= 0 ){
429 $this->_currentRecord =
new Dataface_Record($this->_table->tablename, $row);
442 if (!isset( $this->_data[
'found'] ) ){
445 $sql = $builder->select_num_rows();
446 if ( isset($cache[$sql]) ) $this->_data[
'found'] = $cache[$sql];
448 $res = $this->dbObj->query( $sql, $this->_db, null,
true );
449 $this->_data[
'found'] = array_shift($res[0]);
450 $cache[$sql] = $this->_data[
'found'];
454 return $this->_data[
'found'];
460 if ( !isset( $this->_data[
'cardinality'] ) ){
462 $tableKeyNames = array_keys($this->_table->keys());
463 if ( count($tableKeyNames) <= 0 )
throw new Exception(
"The table '$tablename' has no primary key. Please add one.", E_USER_ERROR);
465 $firstKeyName = $tableKeyNames[0];
466 $sql =
"select count(`$firstKeyName`) from ".$this->_tablename;
467 if ( isset($cache[$sql]) ) $this->_data[
'cardinality'] = $cache[$sql];
469 $res = $this->dbObj->query( $sql, $this->_db,null,
true );
470 $this->_data[
'cardinality'] = array_shift($res[0]);
471 $cache[$sql] = $this->_data[
'cardinality'];
476 return $this->_data[
'cardinality'];
480 return $this->_data[
'start'];
483 return $this->_data[
'end'];
487 return $this->_data[
'data'];
500 while ($it->hasNext()){
501 $records[] = $it->next();
507 return $this->_data[
'limit'];
512 return $this->_data[
'cursor'];
516 return $this->_data[
'indexedData'];
520 if ( $db === null and defined(
'DATAFACE_DB_HANDLE') ) $db = DATAFACE_DB_HANDLE;
521 if ( !isset( $resultDescriptors ) ){
522 static $resultDescriptors = array();
525 if ( is_array($query) and @$query[
'--no-query'] ){
530 if ( !isset( $resultDescriptors[
$tablename] ) ){
562 function getTitles($ordered=
true, $genericKeys =
false, $ignoreLimit=
false){