| Documentation PostgreSQL 7.4.29 | ||||
|---|---|---|---|---|
| Précédent | Arrière rapide | Chapitre 31. Interface JDBC | Avance rapide | Suivant |
PostgreSQL est un système de bases de données extensible. Vous pouvez ajouter vos propres fonctions au serveur, qui seront ensuite appelées à partir des requêtes, voire même ajouter vos propres types de données. Comme nous les supportons à partir de Java, avec un ensemble d'extensions de l'API, certaines fonctionnalités à l'intérieur du cœur du pilote standard utilisent en fait ces extensions pour implémenter les gros objets, etc.
Pour accéder à certaines des extensions, il faut utiliser
quelques méthodes supplémentaires dans la classe
org.postgresql.PGConnection. Dans ce cas, vous aurez
besoin de convertir la valeur de retour de
Driver.getConnection(). Par exemple :
Connection db = Driver.getConnection(url, username, password); // ... // plus tard Fastpath fp = ((org.postgresql.PGConnection)db).getFastpathAPI();
org.postgresql.PGConnectionpublic class PGConnection
Quelques méthodes supplémentaires sont utilisées pour accéder aux extensions de PostgreSQL.
public Fastpath getFastpathAPI() throws SQLException
Ceci renvoie le << fast-path >> de l'API pour la connexion en cours. Il est principalement utilisé par l'API des gros objets.
La meilleure façon de l'utiliser est la suivante :
import org.postgresql.fastpath.*; .. Fastpath fp = ((org.postgresql.PGConnection)myconn).getFastpathAPI();
où myconn est une Connection ouverte sur
le serveur PostgreSQL.
Valeur retournée : Un objet Fastpath donnant accès à des fonctions sur le
serveur PostgreSQL.
Erreur : SQLException par Fastpath lors de sa
première initialisation.
public LargeObjectManager getLargeObjectAPI() throws SQLException
Ceci renvoie l'API des gros objets pour la connexion en cours.
La meilleure façon de l'utiliser est la suivante :
import org.postgresql.largeobject.*; .. LargeObjectManager lo = ((org.postgresql.PGConnection)myconn).getLargeObjectAPI();
où myconn est une Connection ouverte sur
PostgreSQL.
Valeur retournée : Un objet LargeObject implémentant
l'API
Erreur : SQLException par
LargeObject lors de sa première initialisation
public void addDataType(String type, String name)
Ceci permet au code client d'ajouter un gestionnaire pour un des types
de données uniques de PostgreSQL.
Normalement, un type de données inconnu par le pilote est renvoyé par
ResultSet.getObject() comme une instance
PGobject. Cette méthode vous permet d'écrire la classe
qui étend PGobject et indique au pilote le nom du type et
le nom de la classe à utiliser. Le mauvais côté de ceci est que vous
devez appeler cette méthode à chaque fois qu'une connexion est faite.
La meilleure façon de l'utiliser est la suivante :
...
((org.postgresql.PGConnection)maconn).addDataType("montype","nom.de.ma.classe");
...
où maconn est une Connection ouverte sur
PostgreSQL. La classe gérante doit étendre
org.postgresql.util.PGobject.
org.postgresql.Fastpathpublic class Fastpath extends Object java.lang.Object | +----org.postgresql.fastpath.Fastpath
Fastpath est une API issue de
l'interface C de libpq. Elle
permet à une machine cliente d'exécuter une fonction sur le serveur de bases
de données. La plupart des programmes clients n'auront pas besoin
d'utiliser cette méthode mais elle est fournie car
l'API des gros objets l'utilise.
Pour l'utiliser, vous avez besoin d'importer le paquet
org.postgresql.fastpath en utilisant la ligne :
import org.postgresql.fastpath.*;
Puis, dans votre code, vous avez besoin d'un objet
FastPath :
Fastpath fp = ((org.postgresql.PGConnection)conn).getFastpathAPI();
Ceci renverra une instance associée avec la connexion de la base de données
que vous utilisez pour lancer des commandes. La conversion de
Connection en
org.postgresql.PGConnection est requise car la fonction
getFastpathAPI() est une méthode d'extension, et ne fait
pas partie de JDBC. Une fois que vous avez une
instance de Fastpath, vous pouvez utiliser les
méthodes fastpath() pour exécuter une fonction
serveur.
Voir aussi : FastpathFastpathArg,
LargeObject
public Object fastpath(int fnid,
boolean resulttype,
FastpathArg args[]) throws SQLExceptionAppelle une fonction sur le serveur PostgreSQL.
Paramètres : fnid - identifiant de fonction
resulttype - vrai si le résultat est un entier,
faux sinon
args - FastpathArguments à
passer lors d'un appel à << fast-path >>
Valeur retournée : NULL si aucune donnée, Integer si le résultat est un entier ou byte[] sinon
public Object fastpath(String name,
boolean resulttype,
FastpathArg args[]) throws SQLExceptionAppelle une fonction sur le serveur PostgreSQL par son nom.
Note : Cette correspondance entre le nom de la procédure et son identifiant est nécessaire. Elle est habituellement faite dans un appel antérieur à
addfunction(). C'est la meilleure méthode d'appel car l'identifiant de fonction peut/pourrait changer entre les versions du serveur. Comme exemple de ce fonctionnement, référez-vous à org.postgresql.LargeObject
Paramètres : name - Nom de fonction
resulttype - Vrai si le résultat est un entier,
faux sinon
args - FastpathArguments à
passer lors d'un appel à << fast-path >>
Valeur retournée : NULL si aucune donnée, Integer si le résultat est un entier ou byte[] sinon
Voir aussi : LargeObject
public int getInteger(String name,
FastpathArg args[]) throws SQLExceptionCette méthode suppose que la valeur de retour est un Integer.
Paramètres : name - Nom de la fonction args - Arguments de la fonction
Valeur retournée : résultat entier
Erreur : SQLException si une erreur d'accès à la base
de données survient ou s'il n'y a pas de résultat.
public byte[] getData(String name,
FastpathArg args[]) throws SQLExceptionCette méthode suppose que la valeur de retour est une donnée binaire.
Paramètres : name - Nom de la fonction args - Arguments de la fonction
Valeur retournée : tableau byte[] contenant le résultat
Erreur : SQLException si une erreur d'accès à la base de
données survient ou s'il n'y a pas de résultat.
public void addFunction(String name,
int fnid) Ceci ajoute une fonction dans notre table de recherche. Le code
utilisateur devrait utiliser la méthode
addFunctions, qui est basée sur une requête,
plutôt que sur le codage en dur de l'OID. L'OID d'une fonction n'est
pas forcément statique, même sur différents serveur utilisant la même
version.
public void addFunctions(ResultSet rs) throws SQLException
Ceci prend un ResultSet contenant deux colonnes.
La première contient le nom de la fonction, la deuxième l'OID. Il lit
le ResultSet complet en chargeant les valeurs
dans la table des fonctions.
Important : N'oubliez pas d'utiliser
close()sur leResultSetaprès avoir appelé ceci !
Notes d'implémentation sur les recherches de noms de fonction : PostgreSQL stocke l'identifiant de la fonction et le nom correspondant dans la table
pg_proc. Pour accélérer localement les choses, au lieu d'envoyer une requête pour chaque fonction à partir de cette table lorsque nécessaire, unHashtableest utilisé. De plus, seuls les requis de la fonction sont entrés dans cette table, ceci permettant des temps de connexion aussi rapides que possible.La classe
org.postgresql.LargeObjectexécute une requête au lancement et passe leResultSetrenvoyé à la méthodeaddFunctions(). Une fois ceci fait, l'API des gros objets se réfère aux fonctions par nom.Ne pensez pas qu'une conversion manuelle en OID fonctionnerait. Pour l'instant, ce sera certainement bon mais cela peut changer lors du développement (il y a déjà eu quelques discussions là-dessus pour la version 7.0), donc ceci est implémenté pour empêcher tout problème dans le futur.
Voir aussi : LargeObjectManager
public int getID(String name) throws SQLException
Ceci renvoie l'identifiant de la fonction associée à ce nom. Si
addFunction() ou
addFunctions() n'ont pas été appelées pour ce nom,
alors une erreur SQLException est renvoyée.
org.postgresql.fastpath.FastpathArgpublic class FastpathArg extends Object java.lang.Object | +----org.postgresql.fastpath.FastpathArg
Chaque appel << fast-path >> requiert un tableau d'arguments, dont le nombre et le type dépendent de la fonction appelée. Cette classe implémente aussi les méthodes nécessaire pour fournir cette fonctionnalité.
Pour un exemple sur la façon d'utiliser ceci, référez-vous au paquet
org.postgresql.LargeObject.
Voir aussi : Fastpath,
LargeObjectManager, LargeObject
public FastpathArg(int value)
Construit un argument consistant en une valeur entière
Paramètres : value - valeur entière à initialiser
public FastpathArg(byte bytes[])
Construit un argument consistant en un tableau d'octets
Paramètres : bytes - tableau à stocker
public FastpathArg(byte buf[],
int off,
int len)Construit un argument consistant en une partie du tableau d'octets
Paramètres :
tableau source
décalage dans le tableau
longueur des données à inclure
public FastpathArg(String s)
Construit un argument consistant en une chaîne.
PostgreSQL dispose d'un ensemble de données pouvant stocker des données géométriques dans une table. Ceci inclut des points, de lignes et des polygones. Nous supportons ces types en Java avec le paquet org.postgresql.geometric. Il contient des classes qui étendent la classe org.postgresql.util.PGobject. Référez-vous à cette classe pour plus de détails sur l'implémentation de vos propres gestionnaires de types de données.
Class org.postgresql.geometric.PGbox
java.lang.Object
|
+----org.postgresql.util.PGobject
|
+----org.postgresql.geometric.PGbox
public class PGbox extends PGobject implements Serializable,
Cloneable
This represents the box data type within
PostgreSQL.
Variables
public PGpoint point[]
These are the two corner points of the box.
Constructors
public PGbox(double x1,
double y1,
double x2,
double y2)
Parameters:
x1 - first x coordinate
y1 - first y coordinate
x2 - second x coordinate
y2 - second y coordinate
public PGbox(PGpoint p1,
PGpoint p2)
Parameters:
p1 - first point
p2 - second point
public PGbox(String s) throws SQLException
Parameters:
s - Box definition in PostgreSQL
syntax
Throws: SQLException
if definition is invalid
public PGbox()
Required constructor
Methods
public void setValue(String value) throws SQLException
This method sets the value of this object. It should be
overridden, but still called by subclasses.
Parameters:
value - a string representation of the value of the
object
Throws: SQLException
thrown if value is invalid for this type
Overrides:
setValue in class PGobject
public boolean equals(Object obj)
Parameters:
obj - Object to compare with
Returns:
true if the two boxes are identical
Overrides:
equals in class PGobject
public Object clone()
This must be overridden to allow the object to be cloned
Overrides:
clone in class PGobject
public String getValue()
Returns:
the PGbox in the syntax expected by
PostgreSQL
Overrides:
getValue in class PGobject
Class org.postgresql.geometric.PGcircle
java.lang.Object
|
+----org.postgresql.util.PGobject
|
+----org.postgresql.geometric.PGcircle
public class PGcircle extends PGobject implements Serializable,
Cloneable
This represents PostgreSQL's circle data type,
consisting of a point
and a radius
Variables
public PGpoint center
This is the center point
double radius
This is the radius
Constructors
public PGcircle(double x,
double y,
double r)
Parameters:
x - coordinate of center
y - coordinate of center
r - radius of circle
public PGcircle(PGpoint c,
double r)
Parameters:
c - PGpoint describing the circle's center
r - radius of circle
public PGcircle(String s) throws SQLException
Parameters:
s - definition of the circle in
PostgreSQL's syntax.
Throws: SQLException
on conversion failure
public PGcircle()
This constructor is used by the driver.
Methods
public void setValue(String s) throws SQLException
Parameters:
s - definition of the circle in
PostgreSQL's syntax.
Throws: SQLException
on conversion failure
Overrides:
setValue in class PGobject
public boolean equals(Object obj)
Parameters:
obj - Object to compare with
Returns:
true if the two circles are identical
Overrides:
equals in class PGobject
public Object clone()
This must be overridden to allow the object to be cloned
Overrides:
clone in class PGobject
public String getValue()
Returns:
the PGcircle in the syntax expected by
PostgreSQL
Overrides:
getValue in class PGobject
Class org.postgresql.geometric.PGline
java.lang.Object
|
+----org.postgresql.util.PGobject
|
+----org.postgresql.geometric.PGline
public class PGline extends PGobject implements Serializable,
Cloneable
This implements a line consisting of two points. Currently line is
not yet implemented in the server, but this class ensures that when
it's done were ready for it.
Variables
public PGpoint point[]
These are the two points.
Constructors
public PGline(double x1,
double y1,
double x2,
double y2)
Parameters:
x1 - coordinate for first point
y1 - coordinate for first point
x2 - coordinate for second point
y2 - coordinate for second point
public PGline(PGpoint p1,
PGpoint p2)
Parameters:
p1 - first point
p2 - second point
public PGline(String s) throws SQLException
Parameters:
s - definition of the line in
PostgreSQL's syntax.
Throws: SQLException
on conversion failure
public PGline()
required by the driver
Methods
public void setValue(String s) throws SQLException
Parameters:
s - Definition of the line segment in
PostgreSQL's
syntax
Throws: SQLException
on conversion failure
Overrides:
setValue in class PGobject
public boolean equals(Object obj)
Parameters:
obj - Object to compare with
Returns:
true if the two lines are identical
Overrides:
equals in class PGobject
public Object clone()
This must be overridden to allow the object to be cloned
Overrides:
clone in class PGobject
public String getValue()
Returns:
the PGline in the syntax expected by
PostgreSQL
Overrides:
getValue in class PGobject
Class org.postgresql.geometric.PGlseg
java.lang.Object
|
+----org.postgresql.util.PGobject
|
+----org.postgresql.geometric.PGlseg
public class PGlseg extends PGobject implements Serializable,
Cloneable
This implements a lseg (line segment) consisting of two points
Variables
public PGpoint point[]
These are the two points.
Constructors
public PGlseg(double x1,
double y1,
double x2,
double y2)
Parameters:
x1 - coordinate for first point
y1 - coordinate for first point
x2 - coordinate for second point
y2 - coordinate for second point
public PGlseg(PGpoint p1,
PGpoint p2)
Parameters:
p1 - first point
p2 - second point
public PGlseg(String s) throws SQLException
Parameters:
s - Definition of the line segment in
PostgreSQL's syntax.
Throws: SQLException
on conversion failure
public PGlseg()
required by the driver
Methods
public void setValue(String s) throws SQLException
Parameters:
s - Definition of the line segment in
PostgreSQL's
syntax
Throws: SQLException
on conversion failure
Overrides:
setValue in class PGobject
public boolean equals(Object obj)
Parameters:
obj - Object to compare with
Returns:
true if the two line segments are identical
Overrides:
equals in class PGobject
public Object clone()
This must be overridden to allow the object to be cloned
Overrides:
clone in class PGobject
public String getValue()
Returns:
the PGlseg in the syntax expected by
PostgreSQL
Overrides:
getValue in class PGobject
Class org.postgresql.geometric.PGpath
java.lang.Object
|
+----org.postgresql.util.PGobject
|
+----org.postgresql.geometric.PGpath
public class PGpath extends PGobject implements Serializable,
Cloneable
This implements a path (a multiply segmented line, which may be
closed)
Variables
public boolean open
True if the path is open, false if closed
public PGpoint points[]
The points defining this path
Constructors
public PGpath(PGpoint points[],
boolean open)
Parameters:
points - the PGpoints that define the path
open - True if the path is open, false if closed
public PGpath()
Required by the driver
public PGpath(String s) throws SQLException
Parameters:
s - definition of the path in
PostgreSQL's syntax.
Throws: SQLException
on conversion failure
Methods
public void setValue(String s) throws SQLException
Parameters:
s - Definition of the path in
PostgreSQL's syntax
Throws: SQLException
on conversion failure
Overrides:
setValue in class PGobject
public boolean equals(Object obj)
Parameters:
obj - Object to compare with
Returns:
true if the two pathes are identical
Overrides:
equals in class PGobject
public Object clone()
This must be overridden to allow the object to be cloned
Overrides:
clone in class PGobject
public String getValue()
This returns the path in the syntax expected by
PostgreSQL
Overrides:
getValue in class PGobject
public boolean isOpen()
This returns true if the path is open
public boolean isClosed()
This returns true if the path is closed
public void closePath()
Marks the path as closed
public void openPath()
Marks the path as open
Class org.postgresql.geometric.PGpoint
java.lang.Object
|
+----org.postgresql.util.PGobject
|
+----org.postgresql.geometric.PGpoint
public class PGpoint extends PGobject implements Serializable,
Cloneable
This implements a version of java.awt.Point, except it uses double
to represent the coordinates.
It maps to the point data type in PostgreSQL.
Variables
public double x
The X coordinate of the point
public double y
The Y coordinate of the point
Constructors
public PGpoint(double x,
double y)
Parameters:
x - coordinate
y - coordinate
public PGpoint(String value) throws SQLException
This is called mainly from the other geometric types, when a
point is embedded within their definition.
Parameters:
value - Definition of this point in
PostgreSQL's
syntax
public PGpoint()
Required by the driver
Methods
public void setValue(String s) throws SQLException
Parameters:
s - Definition of this point in
PostgreSQL's syntax
Throws: SQLException
on conversion failure
Overrides:
setValue in class PGobject
public boolean equals(Object obj)
Parameters:
obj - Object to compare with
Returns:
true if the two points are identical
Overrides:
equals in class PGobject
public Object clone()
This must be overridden to allow the object to be cloned
Overrides:
clone in class PGobject
public String getValue()
Returns:
the PGpoint in the syntax expected by
PostgreSQL
Overrides:
getValue in class PGobject
public void translate(int x,
int y)
Translate the point with the supplied amount.
Parameters:
x - integer amount to add on the x axis
y - integer amount to add on the y axis
public void translate(double x,
double y)
Translate the point with the supplied amount.
Parameters:
x - double amount to add on the x axis
y - double amount to add on the y axis
public void move(int x,
int y)
Moves the point to the supplied coordinates.
Parameters:
x - integer coordinate
y - integer coordinate
public void move(double x,
double y)
Moves the point to the supplied coordinates.
Parameters:
x - double coordinate
y - double coordinate
public void setLocation(int x,
int y)
Moves the point to the supplied coordinates. refer to
java.awt.Point for description of this
Parameters:
x - integer coordinate
y - integer coordinate
See Also:
Point
public void setLocation(Point p)
Moves the point to the supplied java.awt.Point refer to
java.awt.Point for description of this
Parameters:
p - Point to move to
See Also:
Point
Class org.postgresql.geometric.PGpolygon
java.lang.Object
|
+----org.postgresql.util.PGobject
|
+----org.postgresql.geometric.PGpolygon
public class PGpolygon extends PGobject implements Serializable,
Cloneable
This implements the polygon data type within
PostgreSQL.
Variables
public PGpoint points[]
The points defining the polygon
Constructors
public PGpolygon(PGpoint points[])
Creates a polygon using an array of PGpoints
Parameters:
points - the points defining the polygon
public PGpolygon(String s) throws SQLException
Parameters:
s - definition of the polygon in
PostgreSQL's syntax.
Throws: SQLException
on conversion failure
public PGpolygon()
Required by the driver
Methods
public void setValue(String s) throws SQLException
Parameters:
s - Definition of the polygon in
PostgreSQL's syntax
Throws: SQLException
on conversion failure
Overrides:
setValue in class PGobject
public boolean equals(Object obj)
Parameters:
obj - Object to compare with
Returns:
true if the two polygons are identical
Overrides:
equals in class PGobject
public Object clone()
This must be overridden to allow the object to be cloned
Overrides:
clone in class PGobject
public String getValue()
Returns:
the PGpolygon in the syntax expected by
PostgreSQL
Overrides:
getValue in class PGobjectLes gros objets sont supportés dans la spécification JDBC standard. Néanmoins, cette interface est limitée et l'API fournie par PostgreSQL permet des accès directs au contenu des objets comme s'il s'agissait d'un fichier local.
Le paquet org.postgresql.largeobject fournit à Java l'API
des gros objets de l'interface C libpq. Il
consiste en deux classes, LargeObjectManager, gérant
la création, l'ouverture et la suppression de gros objets, et
LargeObject gérant un objet individuel.
org.postgresql.largeobject.LargeObjectpublic class LargeObject extends Object java.lang.Object | +----org.postgresql.largeobject.LargeObject
Cette classe implémente l'interface des gros objets dans PostgreSQL.
Elle fournit les méthodes de base pour lancer l'interface, plus une paire
de méthodes fournissant les classes InputStream et
OutputStream pour cet objet.
Normalement, un code client pourrait utiliser les méthodes dans
BLOB pour accéder aux gros objets.
Néanmoins, quelque fois, un accès bas niveau vers les gros objets est requis, ce qui n'est pas supporté par la spécification JDBC.
Référez-vous à org.postgresql.largeobject.LargeObjectManager sur la façon d'obtenir un accès à un gros objet ou sur la façon d'en créer un.
Voir aussi : LargeObjectManager
Indique un déplacement relatif au début d'un fichier
Donne un déplacement à partir de la position actuelle.
Indique un déplacement à partir de la fin du fichier
public int getOID()
Renvoie l'OID de ce LargeObject
public void close() throws SQLException
Cette méthode ferme l'objet. Vous ne devez pas appeler les méthodes de cet objet après avoir appelé cette fonction.
public byte[] read(int len) throws SQLException
Lit une partie des données de l'objet et renvoie un tableau de byte[]
public int read(byte buf[],
int off,
int len) throws SQLExceptionLit une partie des données de l'objet et les place dans un tableau existant
Paramètres :
tableau de destination
décalage dans le tableau
nombre d'octets à lire
public void write(byte buf[]) throws SQLException
Écrit un tableau dans l'objet
public void write(byte buf[],
int off,
int len) throws SQLExceptionÉcrit une partie des données du tableau dans l'objet
Paramètres :
tableau de destination
décalage à l'intérieur du tableau
nombre d'octets à écrire
org.postgresql.largeobject.LargeObjectManagerpublic class LargeObjectManager extends Object java.lang.Object | +----org.postgresql.largeobject.LargeObjectManager
Cette classe implémente l'interface des gros objets dans
PostgreSQL. Elle fournit les méthodes qui
permettent au code client de créer, ouvrir et supprimer des gros objets à
partir de la base de données. Lors de l'ouverture d'un objet, une instance
de org.postgresql.largeobject.LargeObject est
renvoyée et ses méthodes sont ensuite utilisées pour obtenir l'accès à
l'objet.
Cette classe peut seulement être créée par org.postgresql.PGConnection. Pour accéder à cette classe, utilisez la portion de code suivant :
import org.postgresql.largeobject.*; Connection conn; LargeObjectManager lobj; // ... code ouvrant une connexion ... lobj = ((org.postgresql.PGConnection)myconn).getLargeObjectAPI();
Normalement, on utilise les méthodes
BLOB pour accéder aux gros objets. Néanmoins,
quelque fois, des accès de bas niveau aux gros objets sont nécessaires
bien qu'ils ne soient pas supportés par la spécification
JDBC.
Référez-vous à org.postgresql.largeobject.LargeObject pour savoir comment manipuler le contenu d'un gros objet.
Ce mode indique que nous voulons écrire dans un objet.
Ce mode indique que nous voulons lire un objet.
Ce mode est le mode par défaut. Il indique que nous voulons lire et écrire dans un gros objet.
public LargeObject open(int oid) throws SQLException
Ceci ouvre un gros objet existant, suivant son OID. Cette méthode suppose que les accès READ et WRITE sont requis (la valeur par défaut).
public LargeObject open(int oid,
int mode) throws SQLExceptionCeci ouvre un gros objet, suivant son OID, et permet l'initialisation du mode d'accès.
public int create() throws SQLException
Ceci crée un gros objet, renvoie son OID. Il est par défaut à READWRITE pour les attributs du nouvel objet.
public int create(int mode) throws SQLException
Ceci crée un gros objet, renvoie son OID, et initialise le mode d'accès.
public void delete(int oid) throws SQLException
Ceci supprime un gros objet.
public void unlink(int oid) throws SQLException
Ceci supprime un gros objet. Cette méthode identique à la méthode delete et est fournie car l'API C utilise << unlink >>.
| Précédent | Sommaire | Suivant |
| Stocker des données binaires | Niveau supérieur | Utiliser le pilote dans un environnement multithread ou de servlet |