Plugin Directory

Changeset 436145


Ignore:
Timestamp:
09/10/2011 11:38:01 PM (15 years ago)
Author:
hawk__
Message:

Moved all the rewriting code to a separate function (for easier testing)
Reworked 'ON DUPLICATE ...' handling, could fail in some cases

File:
1 edited

Legend:

Unmodified
Added
Removed
  • postgresql-for-wordpress/trunk/pg4wp/driver_pgsql.php

    r436142 r436145  
    122122            return true;
    123123        }
     124       
     125        $sql = pg4wp_rewrite( $sql);
     126       
     127        $GLOBALS['pg4wp_result'] = pg_query($sql);
     128        if( (PG4WP_DEBUG || PG4WP_LOG_ERRORS) && $GLOBALS['pg4wp_result'] === false && $err = pg_last_error())
     129            if( false === strpos($err, 'relation "'.$wpdb->options.'"'))
     130                error_log("Error running :\n$initial\n---- converted to ----\n$sql\n----\n$err\n---------------------\n", 3, PG4WP_LOG.'pg4wp_errors.log');
     131       
     132        if( $catchnumrows && $GLOBALS['pg4wp_result'] !== false)
     133        {
     134            $GLOBALS['pg4wp_numrows_query'] = $sql;
     135            if( PG4WP_DEBUG)
     136                error_log( "Number of rows required for :\n$sql\n---------------------\n", 3, PG4WP_LOG.'pg4wp_NUMROWS.log');
     137        }
     138        return $GLOBALS['pg4wp_result'];
     139    }
     140   
     141    function wpsql_insert_id($table)
     142    {
     143        global $wpdb;
     144        $ins_field = $GLOBALS['pg4wp_ins_field'];
     145       
     146        $tbls = split("\n", $GLOBALS['pg4wp_ins_table']); // Workaround for bad tablename
     147        $t = $tbls[0] . '_seq';
     148       
     149        if( in_array( $t, array( '_seq', $wpdb->prefix.'term_relationships_seq')))
     150            return 0;
     151       
     152        if( $ins_field == '"cat_ID"' || $ins_field == 'rel_id' || $ins_field == 'term_id')
     153            $sql = "SELECT MAX($ins_field) FROM ".$tbls[0];
     154        else
     155            $sql = "SELECT CURRVAL('$t')";
     156       
     157        $res = pg_query($sql);
     158        $data = pg_fetch_result($res, 0, 0);
     159        if( PG4WP_DEBUG && $sql)
     160            error_log("Getting inserted ID for '$t' : $sql => $data\n", 3, PG4WP_LOG.'pg4wp_insertid.log');
     161        return $data;
     162    }
     163   
     164    function pg4wp_rewrite( $sql)
     165    {
    124166        global $wpdb;
    125167       
     
    265307                // Remove 'ON DUPLICATE KEY UPDATE...' and following
    266308                $sql = substr( $sql, 0, $pos);
    267                 // Remove illegal characters
    268                 $sql = str_replace('`', '', $sql);
    269                 // Get the elements we need (table name, first field, value)
    270                 $pattern = '/INSERT INTO (\w+)\s+\(([^,]+).+VALUES\s+\(([^,]+)/';
     309                // Get the elements we need (table name, first field, corresponding value)
     310                $pattern = '/INSERT INTO\s+([^\(]+)\(([^,]+)[^\(]+VALUES\s*\(([^,]+)/';
    271311                preg_match($pattern, $sql, $matches);
    272312                $sql = 'DELETE FROM '.$matches[1].' WHERE '.$matches[2].' = '.$matches[3].';'.$sql;
     
    390430                error_log("$sql\n---------------------\n", 3, PG4WP_LOG.'pg4wp_unmodified.log');
    391431        }
    392         $GLOBALS['pg4wp_result'] = pg_query($sql);
    393         if( (PG4WP_DEBUG || PG4WP_LOG_ERRORS) && $GLOBALS['pg4wp_result'] === false && $err = pg_last_error())
    394             if( false === strpos($err, 'relation "'.$wpdb->options.'"'))
    395                 error_log("Error running :\n$initial\n---- converted to ----\n$sql\n----\n$err\n---------------------\n", 3, PG4WP_LOG.'pg4wp_errors.log');
    396        
    397         if( $catchnumrows && $GLOBALS['pg4wp_result'] !== false)
    398         {
    399             $GLOBALS['pg4wp_numrows_query'] = $sql;
    400             if( PG4WP_DEBUG)
    401                 error_log( "Number of rows required for :\n$sql\n---------------------\n", 3, PG4WP_LOG.'pg4wp_NUMROWS.log');
    402         }
    403         return $GLOBALS['pg4wp_result'];
    404     }
    405    
    406     function wpsql_insert_id($table)
    407     {
    408         global $wpdb;
    409         $ins_field = $GLOBALS['pg4wp_ins_field'];
    410        
    411         $tbls = split("\n", $GLOBALS['pg4wp_ins_table']); // Workaround for bad tablename
    412         $t = $tbls[0] . '_seq';
    413        
    414         if( in_array( $t, array( '_seq', $wpdb->prefix.'term_relationships_seq')))
    415             return 0;
    416        
    417         if( $ins_field == '"cat_ID"' || $ins_field == 'rel_id' || $ins_field == 'term_id')
    418             $sql = "SELECT MAX($ins_field) FROM ".$tbls[0];
    419         else
    420             $sql = "SELECT CURRVAL('$t')";
    421        
    422         $res = pg_query($sql);
    423         $data = pg_fetch_result($res, 0, 0);
    424         if( PG4WP_DEBUG && $sql)
    425             error_log("Getting inserted ID for '$t' : $sql => $data\n", 3, PG4WP_LOG.'pg4wp_insertid.log');
    426         return $data;
    427     }
     432        return $sql;
     433    }
Note: See TracChangeset for help on using the changeset viewer.