Vai al contenuto


Foto

SQL (PostgreSQL)


Questa discussione e' stata archiviata Questo significa che non e' possibile rispondere
96 risposte a questa discussione

#61 Guest_ally_*

Guest_ally_*
  • Ospiti

Inviato 13 aprile 2012 - 13:44

... :mother: tutte le tabelle con nomi identici...id,name,type...chi cazzo l'ha disegnato il db?...

#62 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 13 aprile 2012 - 13:46

... :mother: tutte le tabelle con nomi identici...id,name,type...chi cazzo l'ha disegnato il db?...


Sei scemo ?
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#63 Guest_ally_*

Guest_ally_*
  • Ospiti

Inviato 13 aprile 2012 - 14:02

Sei scemo ?


...no...non puoi usare come chiave relazionale sempre il nome id...infatti non sai piu' a chi cazzo appartiene cosa...idiota...

#64 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 13 aprile 2012 - 14:10

Ma che cazzo dici ?

Il campo "Id" è "Table.Id".

Es:


  CONSTRAINT outfrm_prj_fkey FOREIGN KEY ("ProjectId")

	  REFERENCES "Project" ("Id") MATCH SIMPLE

	  ON UPDATE CASCADE ON DELETE CASCADE,

  ...



  CONSTRAINT gra_typ_fkey FOREIGN KEY ("Type")

	  REFERENCES "GrammarType" ("Id") MATCH SIMPLE

	  ON UPDATE CASCADE ON DELETE CASCADE



  ...



  CONSTRAINT inpfrm_gra_fkey FOREIGN KEY ("GrammarId")

	  REFERENCES "Grammar" ("Id") MATCH SIMPLE

	  ON UPDATE CASCADE ON DELETE CASCADE



   etc...



E poi:


SELECT

   goofy."Id",

   mickey."Id"

FROM

   "goofy"  goofy,

   "mickey" mickey


>>>


SELECT

   "goofyId",

   "mickeyId"

FROM

   "goofy",

   "mickey"


e


SELECT

   "goofyId"

FROM

   "goofy"


è obviously redundant


ergo STFU
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#65 Guest_ally_*

Guest_ally_*
  • Ospiti

Inviato 13 aprile 2012 - 14:12

... :pff: ...

#66 Ciccio17

Ciccio17

    Schiavo

  • Membri
  • StellettaStellettaStellettaStelletta
  • 982 Messaggi:

Inviato 13 aprile 2012 - 16:59

Non puoi spostare questo controllo a livello applicativo?

ciccio17.gif


#67 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 13 aprile 2012 - 19:49

Si che potrei ma preferisco che sia il DB a controllare il tutto per far fare all'applicativo il meno possibile.
L'applicativo in caso di modifiche va ricompilato, fosse anche solo il SQL_Plugin, il DB no.
Il DB gestisce il DB, l'applicativo si interfaccia, così la vedo io.

Comunque sto chiedendo l'impossibile, dai. So che oltre al trigger non c'è altro, ma c'ho provato, magari qualcuno conosceva qualche trick.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#68 Ciccio17

Ciccio17

    Schiavo

  • Membri
  • StellettaStellettaStellettaStelletta
  • 982 Messaggi:

Inviato 14 aprile 2012 - 19:05

Il DB gestisce il DB, l'applicativo si interfaccia, così la vedo io.


Se la metti così, giustamente, il trigger è il trick.

ciccio17.gif


#69 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 17 aprile 2012 - 13:48

Altro problema postgres:

Ho una funzione sql che controlla se un oggetto esiste:


CREATE OR REPLACE FUNCTION dmc_object_exists(object varchar(64), field varchar(64), val varchar(64))

RETURNS boolean AS $$

DECLARE retval boolean;

BEGIN

  

   EXECUTE 'SELECT true AS "Exists" FROM "'|| $1 || '" WHERE "'|| $2 || '" = ''' || $3 || ''''

   INTO retval USING object, field, val;

   IF retval IS NULL THEN

	  RETURN false;

   END IF;

  

   RETURN retval;

END;

$$  LANGUAGE plpgsql;


Il problema è che quando la chiamo da una funzione Qt, devo controllare il valore "dmc_object_exists" anche se la query è eseguita con AS "Exists"


DMC=# select dmc_object_exists('UserOptions', 'Option', 'LogGuiSize');
dmc_object_exists
-------------------
t
(1 row)


Praticamente la funzione Qt deve fare così:


   if (m_QueryModel.rowCount() == 1)

   {

	  return m_QueryModel.record(0).value("dmc_object_exists").toBool();

   }


che è orrendo.

Qualcuno sa come risolvere ?
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#70 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 14 maggio 2012 - 13:19


CREATE OR REPLACE FUNCTION get_test(OUT x text, OUT y text)

AS $$

BEGIN

   x := 1;

   y := 2;

END;

$$  LANGUAGE plpgsql;

----------------------------------------------------------------



CREATE OR REPLACE FUNCTION get_test_read()

RETURNS VOID AS $$

DECLARE

   xx text;

   yy text;

BEGIN

   SELECT get_test() INTO xx, yy;

  

   RAISE INFO 'x: <%>', xx;

   RAISE INFO 'y: <%>', yy;

END;

$$  LANGUAGE plpgsql;



select get_test_read();
INFO: x: <(1,2)>
INFO: y: <<NULL>>

get_test_read
---------------

(1 row)




:wat:

Come faccio a ficcare ogni valore nella variabile giusta ? vanno tutti nella prima...

Messaggio modificato da trallallero il 14 maggio 2012 - 13:24

Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#71 gugoXX

gugoXX

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStelletta
  • 5.440 Messaggi:

Inviato 14 maggio 2012 - 13:49

Ho un problema strano...

In una tabella (quella da cui partono le strisce rosse) il campo "ObjectId" dovrebbe essere la foreign key dell'Id di un'altra ma non si sa di quale, potrebbero (per ora) essere 2 le candidate, "Grammar" e "TemplateIds". Quale delle 2, lo decide il campo "ObjectType".

Ovviamente ciò non è possibile ma mi piacerebbe che il "ObjectId" fosse verificato dal DB quando inserito, come succede quando crei una foreign key.

Oltre ad un trigger, qualcuno ha idee buone ?


Immagine inserita


Potresti aggiungere una tabella entita' "Objects", con 2 soli campi: ObjectType e ObjectId
Poi sia la Grammar che la TemplateIDs sarebbero subclass di queste, ovvero ciascuna di essere potrebbe avere diversi campi, ma almeno anche ObjectType e ObjectId, e punterebbero in chiave straniera alla Objects.
Ovvero la Objects avrebbe tanti record quanti il totale di Grammar e TemplateIds
Bene spostare sulla Objects anche tutti gli altri eventuali campi comuni tra le 2.

Infine anche la tua tabella da cui partono le striscie rosse avrebbe un'unica foreign keys verso la Objects, e il gioco e' finito.
L'unico effetto positivo del comunismo e' quello di far produrre doppio a Civilization

The above post is my PERSONAL OPINION and DOES NOT REPRESENT the official position of any company or entity, and does not derive either from official or internal data of any company I am dealing with.

#72 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 14 maggio 2012 - 14:07

Potresti aggiungere una tabella entita' "Objects", con 2 soli campi: ObjectType e ObjectId
Poi sia la Grammar che la TemplateIDs sarebbero subclass di queste, ovvero ciascuna di essere potrebbe avere diversi campi, ma almeno anche ObjectType e ObjectId, e punterebbero in chiave straniera alla Objects.
Ovvero la Objects avrebbe tanti record quanti il totale di Grammar e TemplateIds
Bene spostare sulla Objects anche tutti gli altri eventuali campi comuni tra le 2.

Infine anche la tua tabella da cui partono le striscie rosse avrebbe un'unica foreign keys verso la Objects, e il gioco e' finito.


Ormai non mi serve più però me la segno, buona idea grazie.
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#73 MadJackal

MadJackal

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStelletta
  • 3.105 Messaggi:

Inviato 14 maggio 2012 - 14:42


CREATE OR REPLACE FUNCTION get_test(OUT x text, OUT y text)

AS $$

BEGIN

   x := 1;

   y := 2;

END;

$$  LANGUAGE plpgsql;

----------------------------------------------------------------



CREATE OR REPLACE FUNCTION get_test_read()

RETURNS VOID AS $$

DECLARE

   xx text;

   yy text;

BEGIN

   SELECT get_test() INTO xx, yy;

  

   RAISE INFO 'x: <%>', xx;

   RAISE INFO 'y: <%>', yy;

END;

$$  LANGUAGE plpgsql;







:wat:

Come faccio a ficcare ogni valore nella variabile giusta ? vanno tutti nella prima...



CREATE OR REPLACE FUNCTION get_test_read()

RETURNS VOID AS $$

DECLARE

   xx text;

   yy text;

BEGIN

   SELECT (get_test()).* INTO xx, yy;

  

   RAISE INFO 'x: <%>', xx;

   RAISE INFO 'y: <%>', yy;

END;

$$  LANGUAGE plpgsql;


Così dovrebbe funzionare.
Non so come però puoi limitare i parametri che ti interessano ad un sottoinsieme dei totali.
In Soviet Italy, the evil army owns you!

#74 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 14 maggio 2012 - 14:47

Ok, grazie 1000 :)
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#75 MadJackal

MadJackal

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStelletta
  • 3.105 Messaggi:

Inviato 14 maggio 2012 - 15:05

Ok, grazie 1000 :)


Prego.
Comunque mi correggo, basta usare:


SELECT x,y FROM get_test() INTO xx, yy;


Ed estrarre solo i campi che ti interessano.
Ovviamente funziona anche:


SELECT * FROM get_test() INTO xx, yy;


Invece di


SELECT (get_test()).* INTO xx,yy;


Messaggio modificato da MadJackal il 14 maggio 2012 - 15:06

In Soviet Italy, the evil army owns you!

#76 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 14 maggio 2012 - 15:06

Lo so perché contemporaneamente mi hanno risposto su stackoverflow :megusta:
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#77 MadJackal

MadJackal

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStelletta
  • 3.105 Messaggi:

Inviato 14 maggio 2012 - 15:20

Lo so perché contemporaneamente mi hanno risposto su stackoverflow :megusta:


:lnrg:


:okay:
In Soviet Italy, the evil army owns you!

#78 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 14 maggio 2012 - 15:22

:lnrg:


:okay:


Si ma tu sei più simpatico :biggrin:
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all 

 


#79 MadJackal

MadJackal

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStelletta
  • 3.105 Messaggi:

Inviato 14 maggio 2012 - 22:02

Si ma tu sei più simpatico :biggrin:


Ora sì che mi sento felice! :halone:

Messaggio modificato da MadJackal il 14 maggio 2012 - 22:02

In Soviet Italy, the evil army owns you!

#80 trallallero

trallallero

    Schiavo

  • Membri
  • StellettaStellettaStellettaStellettaStellettaStellettaStelletta
  • 16.188 Messaggi:

Inviato 15 maggio 2012 - 14:20

Oltre a felice sentiti utile :pff:

CREATE OR REPLACE FUNCTION dmc_on_after_delete_frame()

RETURNS trigger AS $retval$

BEGIN



   IF (SELECT COUNT(*) FROM "Frame") = 0 THEN

	  PERFORM reset_sequences();

   END IF;



   RETURN NEW;



END;

$retval$ LANGUAGE plpgsql;

INFO: Cannot reset as table Frame is not empty!


Se la tabella "Frame" è vuota, resetto le sequences.
Mi dice che non è vuota.

:lnrg:
Don't worry, faith will come soon, like a recall but,
if you can't wait, just stop thinking at all