Plugin Directory

Changeset 546724


Ignore:
Timestamp:
05/20/2012 09:57:33 PM (14 years ago)
Author:
hawk__
Message:

Reworked wpsql_insert_id() for better report of errors

File:
1 edited

Legend:

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

    r546689 r546724  
    145145    }
    146146   
    147     function wpsql_insert_id($table)
     147    function wpsql_insert_id($lnk = NULL)
    148148    {
    149149        global $wpdb;
    150150        $ins_field = $GLOBALS['pg4wp_ins_field'];
    151        
    152         $tbls = split("\n", $GLOBALS['pg4wp_ins_table']); // Workaround for bad tablename
    153         $t = $tbls[0] . '_seq';
    154        
    155         if( in_array( $t, array( '_seq', $wpdb->prefix.'term_relationships_seq')))
    156             return 0;
    157        
    158         if( $ins_field == '"cat_ID"' || $ins_field == 'rel_id' || $ins_field == 'term_id')
    159             $sql = "SELECT MAX($ins_field) FROM ".$tbls[0];
     151        $table = $GLOBALS['pg4wp_ins_table'];
     152       
     153        $seq = $table . '_seq';
     154       
     155        // Table 'term_relationships' doesn't have a sequence
     156        if( $table == $wpdb->term_relationships || 'post_author' == $ins_field)
     157        {
     158            $sql = 'NO QUERY';
     159            $data = 0;
     160        }
    160161        else
    161             $sql = "SELECT CURRVAL('$t')";
    162        
    163         $res = pg_query($sql);
    164         $data = pg_fetch_result($res, 0, 0);
     162        {
     163            $sql = "SELECT CURRVAL('$seq')";
     164           
     165            $res = pg_query($sql);
     166            if( false !== $res)
     167                $data = pg_fetch_result($res, 0, 0);
     168            elseif( PG4WP_DEBUG || PG4WP_ERROR_LOG)
     169            {
     170                if( PG4WP_DEBUG)
     171                    $lastq = $GLOBALS['pg4wp_last_insert'];
     172                else
     173                    $lastq = 'UNKNOWN';
     174                $log = '['.microtime(true)."] wpsql_insert_id() was called with '$table' and '$ins_field'".
     175                        " and generated an error. The latest INSERT query was :\n'$lastq'\n";
     176                error_log( $log, 3, PG4WP_LOG.'pg4wp_errors.log');
     177            }
     178        }
    165179        if( PG4WP_DEBUG && $sql)
    166             error_log( '['.microtime(true)."] Getting inserted ID for '$t' : $sql => $data\n", 3, PG4WP_LOG.'pg4wp_insertid.log');
     180            error_log( '['.microtime(true)."] Getting inserted ID for '$table' ('$ins_field') : $sql => $data\n", 3, PG4WP_LOG.'pg4wp_insertid.log');
     181           
    167182        return $data;
    168183    }
     
    424439        $sql = str_replace( 'IN ()', 'IN (NULL)', $sql);
    425440       
     441        // Put back the end of the query if it was separated
     442        $sql .= $end;
     443       
    426444        // For insert ID catching
    427445        if( $logto == 'INSERT')
     
    437455                    $GLOBALS['pg4wp_ins_field'] = trim($match_list[4],' ()  ');
    438456            }
    439         }
    440        
    441         // Put back the end of the query if it was separated
    442         $sql .= $end;
     457            if( PG4WP_DEBUG)
     458                $GLOBALS['pg4wp_last_insert'] = $sql;
     459        }
    443460       
    444461        // Correct quoting for PostgreSQL 9.1+ compatibility
Note: See TracChangeset for help on using the changeset viewer.