Welcome Guest, Not a member yet? Register   Sign In
ODBC: Database Class - Field Data
#1

[eluser]lsousa[/eluser]
Hi,

In a ODBC db, when using $this->db->field_data(), the primary key is not defined.

I've searched in odbc_result.php and I've found out that primary_key is always set to 0.

Code:
function field_data()
    {
        $retval = array();    
        for ($i = 1; $i <= $this->num_fields(); $i++)
        {    
            $F              = new stdClass();
            $F->name        = odbc_field_name($this->result_id, $i);
            $F->type        = odbc_field_type($this->result_id, $i);
            $F->max_length  = odbc_field_len($this->result_id, $i);
            $F->primary_key = 0;
            $F->default     = '';

            $retval[] = $F;
        }
        return $retval;
    }

But there is a method to find out which is the primary key using ODBC.

I've wrote the following function on odbc_driver.php
Code:
function primary_key($table = '')
    {
        if ($table == '')
        {
            return 0;
        }
        
        $result = @odbc_primarykeys($this->conn_id,$this->database,"dbo",$table);
        return (odbc_result($result,"COLUMN_NAME"));
    }

Using this, if I call $this->db->field_data('table_name') from the controller I get the correct primary_key name, so far so good. But now I've a problem, I don't know how to set the primary_key correctly when calling $this->db->field_data().

My idea was to do something such as this on odbc_result.php/field_data:

Code:
function field_data()
    {
        $retval = array();
        
        
        for ($i = 1; $i <= $this->num_fields(); $i++)
        {    
            $F                = new stdClass();
            $F->name          = odbc_field_name($this->result_id, $i);
            $F->type          = odbc_field_type($this->result_id, $i);
            $F->max_length    = odbc_field_len($this->result_id, $i);
            $F->primary_key   = odbc_field_name($this->result_id, $i) === $primary_key ? 1 : 0;
            $F->default       = '';

            $retval[] = $F;
        }

        return $retval;
    }

Where $primary_key is set using my primary_key() function.
Can anyone help?

I don't really need this, I'm just trying to improve field_data() when using ODBC so it could be added to codeigniter source.

PS: I'm sorry for any mistake, but english is not my primary language Smile

Thanks in advance,
LS
#2

[eluser]lsousa[/eluser]
Just to be clear, the problem I'm having is to call primary_key() from odbc_result.php, since in odbc_result.php I don't have access to connection resource, table name and database name that are needed to find out which is the primary key, only in odbc_driver.php :down:

Code:
@odbc_primarykeys($this->conn_id,$this->database,"dbo",$table);




Theme © iAndrew 2016 - Forum software by © MyBB