so i harcoded my primary key in ows_storage.c <br>function ows_storage_fill_pkey() look like this :<br><br>static void ows_storage_fill_pkey(ows * o, ows_layer * l)<br>{<br> buffer *sql;<br> PGresult *res;<br><br> assert(o);<br>
assert(l);<br> assert(l->storage);<br><br> sql = buffer_init();<br><br> buffer_add_str(sql, "SELECT c.column_name FROM information_schema.constraint_column_usage c, pg_namespace n ");<br> buffer_add_str(sql, "WHERE n.nspname = '");<br>
buffer_copy(sql, l->storage->schema);<br> buffer_add_str(sql, "' AND c.table_name = '");<br> buffer_copy(sql, l->storage->table);<br> buffer_add_str(sql, "' AND c.constraint_name = (");<br>
<br> buffer_add_str(sql, "SELECT c.conname FROM pg_class r, pg_constraint c, pg_namespace n ");<br> buffer_add_str(sql, "WHERE r.oid = c.conrelid AND relname = '");<br> buffer_copy(sql, l->storage->table);<br>
buffer_add_str(sql, "' AND r.relnamespace = n.oid AND n.nspname = '");<br> buffer_copy(sql, l->storage->schema);<br> buffer_add_str(sql, "' AND c.contype = 'p')");<br>
<br> res = ows_psql_exec(o, sql->buf);<br> /*----------------------ENLEVER------------------*/<br> /* if (PQresultStatus(res) != PGRES_TUPLES_OK) {<br> PQclear(res);<br> buffer_free(sql);<br> ows_error(o, OWS_ERROR_REQUEST_SQL_FAILED, "Unable to access pg_* tables.", "pkey column");<br>
return;<br> }*/<br><br> /* Layer could have no Pkey indeed... (An SQL view for example) */<br> /* if (PQntuples(res) == 1) {*/<br> l->storage->pkey = buffer_init();<br> /*----------ENLEVER-------------*/<br>
/* buffer_add_str(l->storage->pkey, PQgetvalue(res, 0, 0));*/<br> buffer_add_str(l->storage->pkey, "ogc_fid");<br> buffer_empty(sql);<br> PQclear(res);<br><br> /* Retrieve the Pkey column number */<br>
buffer_add_str(sql, "SELECT a.attnum FROM pg_class c, pg_attribute a, pg_type t, pg_namespace n");<br> buffer_add_str(sql, " WHERE a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid AND n.nspname='");<br>
buffer_copy(sql, l->storage->schema);<br> buffer_add_str(sql, "' AND c.relname='");<br><br> buffer_copy(sql, l->storage->table);<br> buffer_add_str(sql, "' AND a.attname='ogc_fid'");<br>
<br> /* buffer_copy(sql, l->storage->pkey);<br> buffer_add_str(sql, "'");*/<br> res = ows_psql_exec(o, sql->buf);<br> if (PQresultStatus(res) != PGRES_TUPLES_OK) {<br> PQclear(res);<br>
buffer_free(sql);<br> ows_error(o, OWS_ERROR_REQUEST_SQL_FAILED, "Unable to find pkey column number.", "pkey_column number");<br> return;<br> }<br><br> /* -1 because column number start at 1 */<br>
l->storage->pkey_column_number = atoi(PQgetvalue(res, 0, 0)) - 1;<br> buffer_empty(sql);<br> PQclear(res);<br><br> /* Now try to find a sequence related to this Pkey<br> buffer_add_str(sql, "SELECT pg_get_serial_sequence('");<br>
buffer_copy(sql, l->storage->schema);<br> buffer_add_str(sql, ".\"");<br> buffer_copy(sql, l->storage->table);<br> buffer_add_str(sql, "\"', '");<br>
buffer_copy(sql, l->storage->pkey);<br> buffer_add_str(sql, "');");<br> */<br><br> /*NEW function in PG*/<br> buffer_add_str(sql, "SELECT get_sequence_view('");<br>
buffer_copy(sql, l->storage->table);<br> buffer_add_str(sql, "');");<br><br> res = ows_psql_exec(o, sql->buf);<br> if (PQresultStatus(res) != PGRES_TUPLES_OK) {<br> PQclear(res);<br>
buffer_free(sql);<br> ows_error(o, OWS_ERROR_REQUEST_SQL_FAILED,<br> "Unable to use pg_get_serial_sequence.", "pkey_sequence retrieve");<br> return;<br>
}<br><br> /* Even if no sequence found, this function return an empty row<br> * so we must check that result string returned > 0 char<br> */<br> if (PQntuples(res) == 1 && strlen((char *) PQgetvalue(res, 0, 0)) > 0) {<br>
l->storage->pkey_sequence = buffer_init();<br> buffer_add_str(l->storage->pkey_sequence, PQgetvalue(res, 0, 0));<br> }<br>/* }*/<br><br> PQclear(res);<br> buffer_free(sql);<br>
}<br><br><br>and get_sequence_view() looks like :<br><br>CREATE OR REPLACE FUNCTION get_sequence_view(name_table character varying)<br> RETURNS text AS<br>$BODY$<br><br>DECLARE<br> <br> viewn varchar;<br> resultat varchar := '';<br>
nom_seq varchar;<br> current_seq integer;<br> <br>BEGIN<br><br> viewn := name_table;<br> <br> SELECT table_name FROM information_schema.view_table_usage where view_name = viewn into resultat;<br> <br>
/* if resultat <> "" so view else it's a table */<br><br> if (resultat <> '') then<br> SELECT pg_get_serial_sequence(resultat,'ogc_fid')::text into nom_seq;<br> --SELECT currval(nom_seq) into current_seq;<br>
ELSE<br> SELECT pg_get_serial_sequence(viewn,'ogc_fid')::text into nom_seq;<br> --SELECT currval(nom_seq) into current_seq;<br> END IF;<br><br> RETURN nom_seq;<br> <br>END<br>$BODY$<br> LANGUAGE plpgsql VOLATILE<br>
COST 100;<br><br><br>very personalized but it works when <br>1- table got primary key : ogc_fid<br>2- view content ogc_fid<br>3- view got just one table<br>4- view with rules on INSERT, DELETE, UPDATE<br>