1
McNaz
Criteria, SQL and using "is not null"
  • 2005/7/5 10:42

  • McNaz

  • Just can't stay away

  • Posts: 574

  • Since: 2003/4/21


Hi there.

I'm trying to use the Criteria class to construct a "is not null" condition in SQL but with little luck.

i.e

select from xoops_users where uid is not null


Doing the above using Criteria

$crit =& new Criteria('uid','0','','%s is not null');


produces

select from xoops_users where uid is not null 0


While

$crit =& new Criteria('uid','','','%s is not null');


produces

select from xoops_users where


because no value is specified in the Criteria constructor.

The only way I have managed to do this is by hacking the criteria.php XOOPS class to the following:

function render() {
        if ( 
is_numeric($this->value) || strtoupper($this->operator) == 'IN') {
            
$value $this->value;
        } else {
            if ( 
'' === ($value trim($this->value)) ) {
                
$value null;
            }
            if ( (
substr($value01) != '`') && (substr($value, -1) != '`') ) {
                
$value "'$value'";
            }
        }
        
$clause = (!empty($this->prefix) ? "{$this->prefix}." "") . $this->column;
        if ( !empty(
$this->function) ) {
            
$clause sprintf($this->function$clause);
        } else {
                
$clause .= {$this->operator} $value";
            }
            return 
$clause;
        }


Is this the only way of doing this? Am I missing something obvious? If not, can the above changes be incorporated into the core?

Cheers.

McNaz.

2
Mithrandir
Re: Criteria, SQL and using "is not null"

uid column in users table cannot be null...
"When you can flatten entire cities at a whim, a tendency towards quiet reflection and seeing-things-from-the-other-fellow's-point-of-view is seldom necessary."

Cusix Software

3
McNaz
Re: Criteria, SQL and using "is not null"
  • 2005/7/5 12:26

  • McNaz

  • Just can't stay away

  • Posts: 574

  • Since: 2003/4/21


Quote:
uid column in users table cannot be null...


Its just an example.

4
Mithrandir
Re: Criteria, SQL and using "is not null"

How do you build the criteria to use it?

Also, please mark your changes in these suggestions so it is easier to see, what you have done - especially when using an outdated version of a file.

I recall trying to work with this problem, but I didn't find a sure-fire method.
"When you can flatten entire cities at a whim, a tendency towards quiet reflection and seeing-things-from-the-other-fellow's-point-of-view is seldom necessary."

Cusix Software

5
McNaz
Re: Criteria, SQL and using "is not null"
  • 2005/7/5 14:13

  • McNaz

  • Just can't stay away

  • Posts: 574

  • Since: 2003/4/21


Thanks Mith.

I'll get the latest version of this file from the CVS and have a look at it. I'll post a marked up version after I've tested it.

Cheers.

McNaz.

6
McNaz
Re: Criteria, SQL and using "is not null"
  • 2005/7/7 9:00

  • McNaz

  • Just can't stay away

  • Posts: 574

  • Since: 2003/4/21


Hi Mith.

Here is the proposed modification to criteria.php class to support extar SQL syntax (ie

$crit =& new Criteria('column',null,'','','%s is not null');

should support "where column is not null" SQL clauses.


Original function render() in class Criteria (line 341) from the nightly 2.1 CVS
function render() {
        if ( 
strtoupper($this->operator) == 'IN' || strtoupper($this->operator) == "NOT IN") {
            
$value $this->value;
        } else {
            if ( 
'' === ($value trim($this->value)) ) {
                return 
'';
            }
            if ( (
substr($value01) != '`') && (substr($value, -1) != '`') ) {
                
$value "'".addslashes(stripslashes($value))."'";
                
//$value = "'".$value."'";
            
}
        }
        
$clause = (!empty($this->prefix) ? "{$this->prefix}." "") . $this->column;
        if ( !empty(
$this->function) ) {
            
$clause sprintf($this->function$clause);
        }
        
$clause .= {$this->operator} $value";
        return 
$clause;
    }


Modified

function render() {
        if ( 
strtoupper($this->operator) == 'IN' || strtoupper($this->operator) == "NOT IN") {
            
$value $this->value;
        } else {
            if ( 
'' === ($value trim($this->value)) ) { 
                
$value null
            }
            if ( (
substr($value01) != '`') && (substr($value, -1) != '`') && (!is_numeric($value)) ) {
                
$value "'".addslashes(stripslashes($value))."'";
                
//$value = "'".$value."'";
            
}
        }
        
$clause = (!empty($this->prefix) ? "{$this->prefix}." "") . $this->column;
        if ( !empty(
$this->function) ) {
            
$clause sprintf($this->function$clause);
        }
        if (isset(
$value)) {
          
$clause .= {$this->operator} $value";
        }
        return 
$clause;
}

7
Mithrandir
Re: Criteria, SQL and using "is not null"

numeric values are not allowed?
"When you can flatten entire cities at a whim, a tendency towards quiet reflection and seeing-things-from-the-other-fellow's-point-of-view is seldom necessary."

Cusix Software

8
McNaz
Re: Criteria, SQL and using "is not null"
  • 2005/7/7 9:44

  • McNaz

  • Just can't stay away

  • Posts: 574

  • Since: 2003/4/21


No... unless you want your numerics to be quoted. Numeric fields filter down (unquoted) to

$clause .= {$this->operator} $value";

Login

Who's Online

430 user(s) are online (348 user(s) are browsing Support Forums)


Members: 0


Guests: 430


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits