OSDN Git Service

...。
[ring-lang-081/ring.git] / docs / ja-jp / build / html / _sources / postgresql.txt
1 .. index:: 
2         single: PostgreSQL; はじめに
3
4 ===============
5 PostgreSQL 関数
6 ===============
7
8 PostgreSQL データベースの用法を学びます。
9
10 .. index:: 
11         pair: PostgreSQL; ライブラリの読み込み
12
13 ライブラリの読み込み
14 ====================
15
16 この関数の使用前に postgresqllib.ring ライブラリを読み込みます。
17
18 .. code-block:: ring
19
20         load "postgresqllib.ring"
21         # PostgreSQL 関数の使用
22
23 .. index:: 
24         pair: PostgreSQL; 用例
25
26 用例
27 ====
28
29 用例 (1):
30
31 .. code-block:: ring
32
33                 load "postgresqllib.ring"
34
35                 conninfo = "user=postgres password=sa dbname = postgres"
36
37                 exit_nicely = func conn {
38                         PQfinish(conn)
39                         shutdown(1)
40                 }
41
42                 conn = PQconnectdb(conninfo)
43
44                 if (PQstatus(conn) != CONNECTION_OK)
45                         fputs(stderr, "Connection to database failed: "+PQerrorMessage(conn))
46                                 call exit_nicely(conn)
47                 ok
48
49                 res = PQexec(conn, "select * from pg_database")
50                 if PQresultStatus(res) != PGRES_TUPLES_OK
51                         fputs(stderr, "Select failed: " + PQerrorMessage(conn))
52                         PQclear(res)
53                         exit_nicely(conn)
54                 ok
55
56                 nFields = PQnfields(res)
57                 for i = 1 to nFields
58                                 ? PQfname(res, i-1) 
59                 next
60
61                 ? copy("*",60)
62
63                 for i = 1 to PQntuples(res)
64                         for j=1 to nFields
65                                 see PQgetvalue(res, i-1, j-1) + " "
66                         next
67                         see nl
68                 next
69
70                 PQclear(res)
71
72                 PQfinish(conn)
73
74 実行結果:
75
76 .. code-block:: none 
77
78         datname
79         datdba
80         encoding
81         datcollate
82         datctype
83         datistemplate
84         datallowconn
85         datconnlimit
86         datlastsysoid
87         datfrozenxid
88         datminmxid
89         dattablespace
90         datacl
91         ************************************************************
92         postgres 10 6 English_United States.1252 
93                 English_United States.1252 f t -1 12937 549 1 1663
94         template1 10 6 English_United States.1252 English_United States.1252 
95                 t t -1 12937 549 1 1663 {=c/postgres,postgres=CTc/postgres}
96         template0 10 6 English_United States.1252 English_United States.1252 
97                 t f -1 12937 549 1 1663 {=c/postgres,postgres=CTc/postgres}
98         mahdb 10 6 English_United States.1252 English_United States.1252 
99                 f t -1 12937 549 1 1663
100                 
101 用例 (2):
102
103 .. code-block:: ring
104
105         load "postgresqllib.ring"
106
107         conninfo = "user=postgres password=sa dbname = mahdb"
108
109         exit_nicely = func conn {
110                 PQfinish(conn)
111                 shutdown(1)
112         }
113
114         conn = PQconnectdb(conninfo)
115
116         if (PQstatus(conn) != CONNECTION_OK)
117                 fputs(stderr, "Connection to database failed: "+PQerrorMessage(conn))
118                         call exit_nicely(conn)
119         ok
120
121         res = PQexec(conn, "
122                 DROP DATABASE mahdb;
123         ")
124         if PQresultStatus(res) != PGRES_TUPLES_OK
125                 fputs(stderr, "Remove failed: " + PQerrorMessage(conn))
126                 PQclear(res)
127         ok
128         PQclear(res)
129
130
131         res = PQexec(conn, "CREATE DATABASE mahdb;")
132         if PQresultStatus(res) != PGRES_TUPLES_OK
133                 fputs(stderr, "Create database failed: " + PQerrorMessage(conn))
134                 PQclear(res)
135         ok
136
137
138         res = PQexec(conn, "
139         CREATE TABLE COMPANY (
140                          ID INT PRIMARY KEY     NOT NULL,
141                          NAME           TEXT    NOT NULL,
142                          AGE            INT     NOT NULL,
143                          ADDRESS        CHAR(50),
144                          SALARY         REAL );
145         ")
146         if PQresultStatus(res) != PGRES_TUPLES_OK
147                 fputs(stderr, "Create Table failed: " + PQerrorMessage(conn))
148                 PQclear(res)
149         ok
150         PQclear(res)
151
152         res = PQexec(conn, "
153                         INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
154                         VALUES  (1, 'Mahmoud' , 31, 'Jeddah', 10.00 ),
155                                         (2, 'Ahmed'   , 27, 'Jeddah', 20.00 ),
156                                         (3, 'Mohammed', 33, 'Egypt' , 30.00 ),
157                                         (4, 'Ibrahim' , 24, 'Egypt ', 40.00 );
158         ")
159         if PQresultStatus(res) != PGRES_TUPLES_OK
160                 fputs(stderr, "Insert Table failed: " + PQerrorMessage(conn))
161                 PQclear(res)
162         ok
163         PQclear(res)
164
165         res = PQexec(conn, "
166                    select * from COMPANY
167         ")
168         if PQresultStatus(res) != PGRES_TUPLES_OK
169                 fputs(stderr, "Select failed: " + PQerrorMessage(conn))
170                 PQclear(res)
171                 call exit_nicely(conn)
172         ok
173
174
175         nFields = PQnfields(res)
176         for i = 1 to nFields
177                         ? PQfname(res, i-1) 
178         next
179
180         ? copy("*",60)
181
182         for i = 1 to PQntuples(res)
183                 for j=1 to nFields
184                         see PQgetvalue(res, i-1, j-1) + " "
185                 next
186                 see nl
187         next
188
189         PQclear(res)
190
191         PQfinish(conn)
192
193 実行結果:
194
195 .. code-block:: none
196
197         id
198         name
199         age
200         address
201         salary
202         ************************************************************
203         1 Mahmoud  31 Jeddah  10
204         2 Ahmed    27 Jeddah  20
205         3 Mohammed 31 Egypt   30
206         4 Ibrahim  24 Egypt   40        
207         
208
209 .. index:: 
210         pair: PostgreSQL; RingPostgreSQL 定数
211
212 RingPostgreSQL 定数
213 ===================
214
215 この定数は RingPostgreSQL ライブラリで定義済みです。
216
217 .. code-block:: none
218
219         CONNECTION_STARTED
220         CONNECTION_MADE
221         CONNECTION_AWAITING_RESPONSE
222         CONNECTION_AUTH_OK
223         CONNECTION_SSL_STARTUP
224         CONNECTION_SETENV
225         CONNECTION_OK
226
227         PQPING_OK
228         PQPING_REJECT
229         PQPING_NO_RESPONSE
230         PQPING_NO_ATTEMPT
231
232         PGRES_EMPTY_QUERY
233         PGRES_COMMAND_OK
234         PGRES_TUPLES_OK
235         PGRES_COPY_OUT
236         PGRES_COPY_IN
237         PGRES_BAD_RESPONSE
238         PGRES_NONFATAL_ERROR
239         PGRES_FATAL_ERROR
240         PGRES_COPY_BOTH
241         PGRES_SINGLE_TUPLE
242
243         PG_DIAG_SEVERITY
244         PG_DIAG_SQLSTATE
245         PG_DIAG_MESSAGE_PRIMARY
246         PG_DIAG_MESSAGE_DETAIL
247         PG_DIAG_MESSAGE_HINT
248         PG_DIAG_STATEMENT_POSITION
249         PG_DIAG_INTERNAL_POSITION
250         PG_DIAG_INTERNAL_QUERY
251         PG_DIAG_CONTEXT
252         PG_DIAG_SCHEMA_NAME
253         PG_DIAG_TABLE_NAME
254         PG_DIAG_COLUMN_NAME
255         PG_DIAG_DATATYPE_NAME
256         PG_DIAG_CONSTRAINT_NAME
257         PG_DIAG_SOURCE_FILE
258         PG_DIAG_SOURCE_LINE
259         PG_DIAG_SOURCE_FUNCTION
260
261
262 .. index:: 
263         pair: PostgreSQL; RingPostgreSQL 関数
264
265 RingPostgreSQL 関数
266 ===================
267
268 この関数は RingPostgreSQL ライブラリで定義されています。
269
270 リファレンス : https://www.postgresql.org/docs/9.1/static/libpq.html
271
272 .. code-block:: none
273
274         PGconn *PQconnectdbParams(const char **keywords,
275                 const char **values,int expand_dbname);
276         PGconn *PQconnectdb(const char *conninfo)
277         PGconn *PQsetdbLogin(const char *pghost,const char *pgport,
278                 const char *pgoptions,const char *pgtty,
279                 const char *dbName,const char *login,const char *pwd)
280         PGconn *PQsetdb(char *pghost,char *pgport,char *pgoptions,
281                 char *pgtty,char *dbName)
282         PGconn *PQconnectStartParams(const char **keywords,
283                 const char **values,int expand_dbname)
284         PGconn *PQconnectStart(const char *conninfo)
285         PostgresPollingStatusType PQconnectPoll(PGconn *conn)
286         PQconninfoOption *PQconndefaults(void)
287         PQconninfoOption *PQconninfo(PGconn *conn)
288         PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg)
289         void PQfinish(PGconn *conn)
290         void PQreset(PGconn *conn)
291         int PQresetStart(PGconn *conn)
292         PostgresPollingStatusType PQresetPoll(PGconn *conn)
293         PGPing PQpingParams(const char **keywords,const char **values,
294                 int expand_dbname)
295         PGPing PQping(const char *conninfo)
296         char *PQdb(const PGconn *conn)
297         char *PQuser(const PGconn *conn)
298         char *PQpass(const PGconn *conn)
299         char *PQhost(const PGconn *conn)
300         char *PQport(const PGconn *conn)
301         char *PQtty(const PGconn *conn)
302         char *PQoptions(const PGconn *conn)
303         ConnStatusType PQstatus(const PGconn *conn)
304         PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
305         const char *PQparameterStatus(const PGconn *conn, const char *paramName)
306         int PQprotocolVersion(const PGconn *conn)
307         int PQserverVersion(const PGconn *conn)
308         char *PQerrorMessage(const PGconn *conn)
309         int PQsocket(const PGconn *conn)
310         int PQbackendPID(const PGconn *conn)
311         int PQconnectionNeedsPassword(const PGconn *conn)
312         int PQconnectionUsedPassword(const PGconn *conn)
313         int PQsslInUse(const PGconn *conn)
314         const char *PQsslAttribute(const PGconn *conn, const char *attribute_name)
315         const char **PQsslAttributeNames(const PGconn *conn)
316         void *PQsslStruct(const PGconn *conn, const char *struct_name)
317         void *PQgetssl(const PGconn *conn)
318         PGresult *PQexec(PGconn *conn, const char *command);
319         PGresult *PQexecParams(PGconn *conn,const char *command,int nParams,
320                 const Oid *paramTypes,const char **paramValues,
321                 const int *paramLengths,const int *paramFormats,int resultFormat)
322         PGresult *PQprepare(PGconn *conn,const char *stmtName,
323                 const char *query,int nParams,const Oid *paramTypes)
324         PGresult *PQexecPrepared(PGconn *conn,const char *stmtName,
325                 int nParams,const char **paramValues,
326                 const int *paramLengths,const int *paramFormats,int resultFormat)
327         PGresult *PQdescribePrepared(PGconn *conn, const char *stmtName)
328         PGresult *PQdescribePortal(PGconn *conn, const char *portalName)
329         ExecStatusType PQresultStatus(const PGresult *res)
330         char *PQresStatus(ExecStatusType status)
331         char *PQresultErrorMessage(const PGresult *res)
332         char *PQresultErrorField(const PGresult *res, int fieldcode)
333         void PQclear(PGresult *res)
334         int PQntuples(const PGresult *res)
335         int PQnfields(const PGresult *res)
336         char *PQfname(const PGresult *res,int column_number)
337         int PQfnumber(const PGresult *res,const char *column_name)
338         Oid PQftable(const PGresult *res,int column_number)
339         int PQftablecol(const PGresult *res,int column_number)
340         int PQfformat(const PGresult *res,int column_number)
341         Oid PQftype(const PGresult *res,int column_number)
342         int PQfmod(const PGresult *res,int column_number)
343         int PQfsize(const PGresult *res,int column_number)
344         int PQbinaryTuples(const PGresult *res)
345         char *PQgetvalue(const PGresult *res,int row_number,int column_number)
346         int PQgetisnull(const PGresult *res,int row_number,int column_number)
347         int PQgetlength(const PGresult *res,int row_number,int column_number)
348         int PQnparams(const PGresult *res)
349         Oid PQparamtype(const PGresult *res, int param_number)
350         void PQprint(FILE *fout,const PGresult *res,const PQprintOpt *po)
351         char *PQcmdStatus(PGresult *res)
352         char *PQcmdTuples(PGresult *res)
353         Oid PQoidValue(const PGresult *res)
354         char *PQoidStatus(const PGresult *res)
355         char *PQescapeLiteral(PGconn *conn, const char *str, size_t length)
356         char *PQescapeIdentifier(PGconn *conn, const char *str, size_t length)
357         size_t PQescapeStringConn(PGconn *conn,char *to, 
358                 const char *from, size_t length,int *error)
359         size_t PQescapeString(char *to, const char *from, size_t length)
360         unsigned char *PQescapeByteaConn(PGconn *conn,
361                 const unsigned char *from,size_t from_length,size_t *to_length)
362         unsigned char *PQescapeBytea(const unsigned char *from,
363                 size_t from_length,size_t *to_length)
364         unsigned char *PQunescapeBytea(const unsigned char *from, size_t *to_length)
365         int PQsendQuery(PGconn *conn, const char *command)
366         int PQsendQueryParams(PGconn *conn,const char *command,
367                 int nParams,const Oid *paramTypes,const char **paramValues,
368                 const int *paramLengths,const int *paramFormats,int resultFormat)
369         int PQsendPrepare(PGconn *conn,const char *stmtName,
370                 const char *query,int nParams,const Oid *paramTypes)
371         int PQsendQueryPrepared(PGconn *conn,const char *stmtName,
372                 int nParams,const char **paramValues,
373                 const int *paramLengths,const int *paramFormats,int resultFormat)
374         int PQsendDescribePrepared(PGconn *conn, const char *stmtName)
375         int PQsendDescribePortal(PGconn *conn, const char *portalName)
376         PGresult *PQgetResult(PGconn *conn)
377         int PQconsumeInput(PGconn *conn)
378         int PQisBusy(PGconn *conn)
379         int PQsetnonblocking(PGconn *conn, int arg)
380         int PQisnonblocking(const PGconn *conn)
381         int PQflush(PGconn *conn)
382         int PQsetSingleRowMode(PGconn *conn)
383         PGcancel *PQgetCancel(PGconn *conn)
384         void PQfreeCancel(PGcancel *cancel)
385         int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
386         int PQrequestCancel(PGconn *conn)
387         PGresult *PQfn(PGconn *conn,int fnid,int *result_buf,
388                 int *result_len,int result_is_int,const PQArgBlock *args,int nargs)
389         PGnotify *PQnotifies(PGconn *conn)
390         int PQputCopyData(PGconn *conn,const char *buffer,int nbytes)
391         int PQputCopyEnd(PGconn *conn,const char *errormsg)
392         int PQgetCopyData(PGconn *conn,char **buffer,int async)
393         int PQgetline(PGconn *conn,char *buffer,int length)
394         int PQgetlineAsync(PGconn *conn,char *buffer,int bufsize)
395         int PQputline(PGconn *conn,const char *string)
396         int PQputnbytes(PGconn *conn,const char *buffer,int nbytes)
397         int PQendcopy(PGconn *conn)
398         int PQclientEncoding(const PGconn *conn)
399         char *pg_encoding_to_char(int encoding_id)
400         int PQsetClientEncoding(PGconn *conn, const char *encoding)
401         void PQtrace(PGconn *conn, FILE *stream)
402         void PQuntrace(PGconn *conn)
403         void PQfreemem(void *ptr)
404         void PQconninfoFree(PQconninfoOption *connOptions)
405         char *PQencryptPasswordConn(PGconn *conn, const char *passwd,
406                  const char *user, const char *algorithm)
407         char *PQencryptPassword(const char *passwd, const char *user)
408         PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status)
409         int PQfireResultCreateEvents(PGconn *conn, PGresult *res)
410         PGresult *PQcopyResult(const PGresult *src, int flags)
411         int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs)
412         int PQsetvalue(PGresult *res, int tup_num, int field_num, 
413                         char *value, int len)
414         void *PQresultAlloc(PGresult *res, size_t nBytes)
415         int PQlibVersion(void)
416         PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
417                         PQnoticeReceiver proc,void *arg)
418         PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
419                         PQnoticeProcessor proc,void *arg)
420         void PQinitOpenSSL(int do_ssl, int do_crypto)
421         void PQinitSSL(int do_ssl)
422         int PQisthreadsafe(void)