⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

postgresql and grocery crud



olivetech

olivetech
  • profile picture
  • Member

Posted 21 December 2011 - 03:44 AM

hi

thank you for the grocery crud,
but i wonder does it compatible with postgresql since the model uses lot of mysql command

regards

web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 21 December 2011 - 07:20 AM

[quote name='olivetech' timestamp='1324439052' post='147']
hi

thank you for the grocery crud,
but i wonder does it compatible with postgresql since the model uses lot of mysql command

regards
[/quote]

Actually the first thought it was to use only the active record to have all the queries. After that I realized that the codeigniter drivers are really weak. So it was difficult for me to stay only with the active record. Now it works only with mySQL and mySQLi .

If you like you can have a try with the model. You can use the set_model ( http://www.grocerycrud.com/crud/function_name/set_model ), so you don't have to change the core of grocery CRUD , see what the error will be and just create a model for postgresql. For now it will be easy because you will have to change only ONE function, the

function get_field_types_basic_table(){}
at application/models/grocery_model.php

Nicholas

Nicholas
  • profile picture
  • Member

Posted 23 December 2011 - 18:57 PM

Note that the same technique can be used for MSSQL/SQLServer, The get_field_types and get_field_types_basic_table methods must be managed to populate the primary key. Alternatively you can modify the core field_data method at system/database/drivers/mssql/mssql_result.php. This is an ugly hack to use the first field as a primary key. Hope it helps someone :)


function field_data()
{
$retval = array();
$counter=1;
while ($field = mssql_fetch_field($this->result_id))
{
$F = new stdClass();
$F->name = $field->name;
$F->type = $field->type;
$F->max_length = $field->max_length;

// hack for primary key
if ($counter == 1) {
$F->primary_key = 1;
$counter++;
} else {
$F->primary_key = 0;
}

$F->default = '';
$retval[] = $F;
}
return $retval;
}

Heden

Heden
  • profile picture
  • Member

Posted 31 January 2012 - 04:33 AM

On SQL 2000

if you have problem like this below :


A Database Error Occurred
Error Number:
Incorrect syntax near the keyword 'FROM'.
SHOW COLUMNS FROM {table_name}
Filename: D:\xampp\htdocs\NAVY\system\database\DB_driver.php
Line Number: 330





The solution is :

update function get_field_types_basic_table in grocery_model.php



function get_field_types_basic_table()
{
$sql = "select column_name as Field, data_type+'('+convert(varchar(255), character_maximum_length)+')' as Type, is_nullable as [Null], case when ordinal_position = 1 then 'PRI' else '' end as [Key], column_default as [Default], '' as Extra from information_schema.columns where table_name = '{$this->table_name}'";
$db_field_types = array();
//foreach($this->db->query("SHOW COLUMNS FROM {$this->table_name}")->result() as $db_field_type)
foreach($this->db->query($sql)->result() as $db_field_type)
{
$type = explode("(",$db_field_type->Type);
$db_type = $type[0];

if(isset($type[1]))
{
$length = substr($type[1],0,-1);
}
else
{
$length = '';
}
$db_field_types[$db_field_type->Field]['db_max_length'] = $length;
$db_field_types[$db_field_type->Field]['db_type'] = $db_type;
$db_field_types[$db_field_type->Field]['db_null'] = $db_field_type->Null == 'YES' ? true : false;
$db_field_types[$db_field_type->Field]['db_extra'] = $db_field_type->Extra;
}

$results = $this->db->field_data($this->table_name);
foreach($results as $num => $row)
{
$row = (array)$row;
$results[$num] = (object)( array_merge($row, $db_field_types[$row['name']]) );
}

return $results;
}

reynierpm

reynierpm
  • profile picture
  • Member

Posted 13 April 2012 - 01:13 AM

I have the same problem and try your solution but get this error instead:
[quote]
Error Number:
ERROR: syntax error at or near "," LINE 1: ..._name as Field, data_type+'('+convert(varchar(255), characte... ^
select column_name as Field, data_type+'('+convert(varchar(255), character_maximum_length)+')' as Type, is_nullable as [Null], case when ordinal_position = 1 then 'PRI' else '' end as [Key], column_default as [Default], '' as Extra FROM information_schema.columns where table_name = 'tr007_usuarios'
Filename: /var/www/html/sic-auto-gas/models/grocery_crud_model.php
Line Number: 265
[/quote]
Any help?

Juliette F

Juliette F
  • profile picture
  • Member

Posted 22 May 2013 - 14:50 PM

I have the same problem and try your solution but get this error instead:

Any help?

 

I tried to find the correct query with PostgreSQL but it's really more complicated because :