Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
The Ring programming language version 1.9 book - Part 37 of 210
1. CHAPTER
FORTYTWO
POSTGRESQL FUNCTIONS
In this chapter we will learn about using the PostgreSQL database in the Ring programming language.
42.1 Loading the library
Before using the next function load the postgresqllib.ring library
load "postgresqllib.ring"
# Use PostgreSQL functions
42.2 Examples
Example (1):
load "postgresqllib.ring"
conninfo = "user=postgres password=sa dbname = postgres"
exit_nicely = func conn {
PQfinish(conn)
shutdown(1)
}
conn = PQconnectdb(conninfo)
if (PQstatus(conn) != CONNECTION_OK)
fputs(stderr, "Connection to database failed: "+PQerrorMessage(conn))
call exit_nicely(conn)
ok
res = PQexec(conn, "select * from pg_database")
if PQresultStatus(res) != PGRES_TUPLES_OK
fputs(stderr, "Select failed: " + PQerrorMessage(conn))
PQclear(res)
exit_nicely(conn)
ok
nFields = PQnfields(res)
for i = 1 to nFields
? PQfname(res, i-1)
next
329
2. Ring Documentation, Release 1.9
? copy("*",60)
for i = 1 to PQntuples(res)
for j=1 to nFields
see PQgetvalue(res, i-1, j-1) + " "
next
see nl
next
PQclear(res)
PQfinish(conn)
Output:
datname
datdba
encoding
datcollate
datctype
datistemplate
datallowconn
datconnlimit
datlastsysoid
datfrozenxid
datminmxid
dattablespace
datacl
************************************************************
postgres 10 6 English_United States.1252
English_United States.1252 f t -1 12937 549 1 1663
template1 10 6 English_United States.1252 English_United States.1252
t t -1 12937 549 1 1663 {=c/postgres,postgres=CTc/postgres}
template0 10 6 English_United States.1252 English_United States.1252
t f -1 12937 549 1 1663 {=c/postgres,postgres=CTc/postgres}
mahdb 10 6 English_United States.1252 English_United States.1252
f t -1 12937 549 1 1663
Example(2):
load "postgresqllib.ring"
conninfo = "user=postgres password=sa dbname = mahdb"
exit_nicely = func conn {
PQfinish(conn)
shutdown(1)
}
conn = PQconnectdb(conninfo)
if (PQstatus(conn) != CONNECTION_OK)
fputs(stderr, "Connection to database failed: "+PQerrorMessage(conn))
call exit_nicely(conn)
ok
res = PQexec(conn, "
DROP DATABASE mahdb;
42.2. Examples 330
3. Ring Documentation, Release 1.9
")
if PQresultStatus(res) != PGRES_TUPLES_OK
fputs(stderr, "Remove failed: " + PQerrorMessage(conn))
PQclear(res)
ok
PQclear(res)
res = PQexec(conn, "CREATE DATABASE mahdb;")
if PQresultStatus(res) != PGRES_TUPLES_OK
fputs(stderr, "Create database failed: " + PQerrorMessage(conn))
PQclear(res)
ok
res = PQexec(conn, "
CREATE TABLE COMPANY (
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL );
")
if PQresultStatus(res) != PGRES_TUPLES_OK
fputs(stderr, "Create Table failed: " + PQerrorMessage(conn))
PQclear(res)
ok
PQclear(res)
res = PQexec(conn, "
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Mahmoud' , 31, 'Jeddah', 10.00 ),
(2, 'Ahmed' , 27, 'Jeddah', 20.00 ),
(3, 'Mohammed', 33, 'Egypt' , 30.00 ),
(4, 'Ibrahim' , 24, 'Egypt ', 40.00 );
")
if PQresultStatus(res) != PGRES_TUPLES_OK
fputs(stderr, "Insert Table failed: " + PQerrorMessage(conn))
PQclear(res)
ok
PQclear(res)
res = PQexec(conn, "
select * from COMPANY
")
if PQresultStatus(res) != PGRES_TUPLES_OK
fputs(stderr, "Select failed: " + PQerrorMessage(conn))
PQclear(res)
call exit_nicely(conn)
ok
nFields = PQnfields(res)
for i = 1 to nFields
? PQfname(res, i-1)
next
? copy("*",60)
42.2. Examples 331
4. Ring Documentation, Release 1.9
for i = 1 to PQntuples(res)
for j=1 to nFields
see PQgetvalue(res, i-1, j-1) + " "
next
see nl
next
PQclear(res)
PQfinish(conn)
Output:
id
name
age
address
salary
************************************************************
1 Mahmoud 31 Jeddah 10
2 Ahmed 27 Jeddah 20
3 Mohammed 31 Egypt 30
4 Ibrahim 24 Egypt 40
42.3 RingPostgreSQL Constants
The next constants are define by the RingPostgreSQL Library
CONNECTION_STARTED
CONNECTION_MADE
CONNECTION_AWAITING_RESPONSE
CONNECTION_AUTH_OK
CONNECTION_SSL_STARTUP
CONNECTION_SETENV
CONNECTION_OK
PQPING_OK
PQPING_REJECT
PQPING_NO_RESPONSE
PQPING_NO_ATTEMPT
PGRES_EMPTY_QUERY
PGRES_COMMAND_OK
PGRES_TUPLES_OK
PGRES_COPY_OUT
PGRES_COPY_IN
PGRES_BAD_RESPONSE
PGRES_NONFATAL_ERROR
PGRES_FATAL_ERROR
PGRES_COPY_BOTH
PGRES_SINGLE_TUPLE
PG_DIAG_SEVERITY
PG_DIAG_SQLSTATE
PG_DIAG_MESSAGE_PRIMARY
PG_DIAG_MESSAGE_DETAIL
42.3. RingPostgreSQL Constants 332
6. Ring Documentation, Release 1.9
int PQbackendPID(const PGconn *conn)
int PQconnectionNeedsPassword(const PGconn *conn)
int PQconnectionUsedPassword(const PGconn *conn)
int PQsslInUse(const PGconn *conn)
const char *PQsslAttribute(const PGconn *conn, const char *attribute_name)
const char **PQsslAttributeNames(const PGconn *conn)
void *PQsslStruct(const PGconn *conn, const char *struct_name)
void *PQgetssl(const PGconn *conn)
PGresult *PQexec(PGconn *conn, const char *command);
PGresult *PQexecParams(PGconn *conn,const char *command,int nParams,
const Oid *paramTypes,const char **paramValues,
const int *paramLengths,const int *paramFormats,int resultFormat)
PGresult *PQprepare(PGconn *conn,const char *stmtName,
const char *query,int nParams,const Oid *paramTypes)
PGresult *PQexecPrepared(PGconn *conn,const char *stmtName,
int nParams,const char **paramValues,
const int *paramLengths,const int *paramFormats,int resultFormat)
PGresult *PQdescribePrepared(PGconn *conn, const char *stmtName)
PGresult *PQdescribePortal(PGconn *conn, const char *portalName)
ExecStatusType PQresultStatus(const PGresult *res)
char *PQresStatus(ExecStatusType status)
char *PQresultErrorMessage(const PGresult *res)
char *PQresultErrorField(const PGresult *res, int fieldcode)
void PQclear(PGresult *res)
int PQntuples(const PGresult *res)
int PQnfields(const PGresult *res)
char *PQfname(const PGresult *res,int column_number)
int PQfnumber(const PGresult *res,const char *column_name)
Oid PQftable(const PGresult *res,int column_number)
int PQftablecol(const PGresult *res,int column_number)
int PQfformat(const PGresult *res,int column_number)
Oid PQftype(const PGresult *res,int column_number)
int PQfmod(const PGresult *res,int column_number)
int PQfsize(const PGresult *res,int column_number)
int PQbinaryTuples(const PGresult *res)
char *PQgetvalue(const PGresult *res,int row_number,int column_number)
int PQgetisnull(const PGresult *res,int row_number,int column_number)
int PQgetlength(const PGresult *res,int row_number,int column_number)
int PQnparams(const PGresult *res)
Oid PQparamtype(const PGresult *res, int param_number)
void PQprint(FILE *fout,const PGresult *res,const PQprintOpt *po)
char *PQcmdStatus(PGresult *res)
char *PQcmdTuples(PGresult *res)
Oid PQoidValue(const PGresult *res)
char *PQoidStatus(const PGresult *res)
char *PQescapeLiteral(PGconn *conn, const char *str, size_t length)
char *PQescapeIdentifier(PGconn *conn, const char *str, size_t length)
size_t PQescapeStringConn(PGconn *conn,char *to,
const char *from, size_t length,int *error)
size_t PQescapeString(char *to, const char *from, size_t length)
unsigned char *PQescapeByteaConn(PGconn *conn,
const unsigned char *from,size_t from_length,size_t *to_length)
unsigned char *PQescapeBytea(const unsigned char *from,
size_t from_length,size_t *to_length)
unsigned char *PQunescapeBytea(const unsigned char *from, size_t *to_length)
int PQsendQuery(PGconn *conn, const char *command)
int PQsendQueryParams(PGconn *conn,const char *command,
int nParams,const Oid *paramTypes,const char **paramValues,
42.4. RingPostgreSQL Functions 334
7. Ring Documentation, Release 1.9
const int *paramLengths,const int *paramFormats,int resultFormat)
int PQsendPrepare(PGconn *conn,const char *stmtName,
const char *query,int nParams,const Oid *paramTypes)
int PQsendQueryPrepared(PGconn *conn,const char *stmtName,
int nParams,const char **paramValues,
const int *paramLengths,const int *paramFormats,int resultFormat)
int PQsendDescribePrepared(PGconn *conn, const char *stmtName)
int PQsendDescribePortal(PGconn *conn, const char *portalName)
PGresult *PQgetResult(PGconn *conn)
int PQconsumeInput(PGconn *conn)
int PQisBusy(PGconn *conn)
int PQsetnonblocking(PGconn *conn, int arg)
int PQisnonblocking(const PGconn *conn)
int PQflush(PGconn *conn)
int PQsetSingleRowMode(PGconn *conn)
PGcancel *PQgetCancel(PGconn *conn)
void PQfreeCancel(PGcancel *cancel)
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
int PQrequestCancel(PGconn *conn)
PGresult *PQfn(PGconn *conn,int fnid,int *result_buf,
int *result_len,int result_is_int,const PQArgBlock *args,int nargs)
PGnotify *PQnotifies(PGconn *conn)
int PQputCopyData(PGconn *conn,const char *buffer,int nbytes)
int PQputCopyEnd(PGconn *conn,const char *errormsg)
int PQgetCopyData(PGconn *conn,char **buffer,int async)
int PQgetline(PGconn *conn,char *buffer,int length)
int PQgetlineAsync(PGconn *conn,char *buffer,int bufsize)
int PQputline(PGconn *conn,const char *string)
int PQputnbytes(PGconn *conn,const char *buffer,int nbytes)
int PQendcopy(PGconn *conn)
int PQclientEncoding(const PGconn *conn)
char *pg_encoding_to_char(int encoding_id)
int PQsetClientEncoding(PGconn *conn, const char *encoding)
void PQtrace(PGconn *conn, FILE *stream)
void PQuntrace(PGconn *conn)
void PQfreemem(void *ptr)
void PQconninfoFree(PQconninfoOption *connOptions)
char *PQencryptPasswordConn(PGconn *conn, const char *passwd,
const char *user, const char *algorithm)
char *PQencryptPassword(const char *passwd, const char *user)
PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status)
int PQfireResultCreateEvents(PGconn *conn, PGresult *res)
PGresult *PQcopyResult(const PGresult *src, int flags)
int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs)
int PQsetvalue(PGresult *res, int tup_num, int field_num,
char *value, int len)
void *PQresultAlloc(PGresult *res, size_t nBytes)
int PQlibVersion(void)
PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
PQnoticeReceiver proc,void *arg)
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
PQnoticeProcessor proc,void *arg)
void PQinitOpenSSL(int do_ssl, int do_crypto)
void PQinitSSL(int do_ssl)
int PQisthreadsafe(void)
42.4. RingPostgreSQL Functions 335
8. CHAPTER
FORTYTHREE
SECURITY AND INTERNET FUNCTIONS
This chapter contains the security and internet functions provided by the Ring programming language for Hashing,
Encryption & Decryption.
Before using the next function load the openssllib.ring library
load "openssllib.ring"
# Use OpenSSL functions
• MD5()
• SHA1()
• SHA256()
• SHA512()
• SHA384()
• SHA224()
• Encrypt()
• Decrypt()
• Randbytes()
Before using the next function load the internetlib.ring library
load "internetlib.ring"
# Use the Internet functions
• Download()
• SendEmail()
43.1 MD5() Function
We can calculate the MD5 hash using the MD5() Function
Syntax:
MD5(cString) ---> String contains the MD5 hash of the string cString
Example:
see "md5('happy') = " + md5("happy") + nl +
"md5('Hello') = " + md5("Hello") + nl
336
9. Ring Documentation, Release 1.9
Output:
md5('happy') = 56ab24c15b72a457069c5ea42fcfc640
md5('Hello') = 8b1a9953c4611296a827abf8c47804d7
43.2 SHA1() Function
We can calculate the SHA1 hash using the SHA1() Function
Syntax:
SHA1(cString) ---> String contains the SHA1 hash of the string cString
Example:
see "sha1('hello') : " + sha1("hello") + nl +
"sha1('apple') : " + sha1("apple") + nl
Output:
sha1('hello') : aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
sha1('apple') : d0be2dc421be4fcd0172e5afceea3970e2f3d940
43.3 SHA256() Function
We can calculate the SHA256 hash using the SHA256() Function
Syntax:
SHA256(cString) ---> String contains the SHA256 hash of the string cString
Example:
see "sha256('hello') : " + sha256("hello") + nl +
"sha256('apple') : " + sha256("apple") + nl
Output:
sha256('hello') : 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
sha256('apple') : 3a7bd3e2360a3d29eea436fcfb7e44c735d117c42d1c1835420b6b9942dd4f1b
43.4 SHA512() Function
We can calculate the SHA512 hash using the SHA512() Function
Syntax:
SHA512(cString) ---> String contains the SHA512 hash of the string cString
Example:
see "sha512('hello') : " + sha512("hello") + nl +
"sha512('apple') : " + sha512("apple") + nl +
"sha512('hello world') : " + sha512("hello world") + nl
43.2. SHA1() Function 337
10. Ring Documentation, Release 1.9
Output:
sha512('hello') : 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673c
a72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
sha512('apple') : 844d8779103b94c18f4aa4cc0c3b4474058580a991fba85d3ca698a0bc9e52
c5940feb7a65a3a290e17e6b23ee943ecc4f73e7490327245b4fe5d5efb590feb2
sha512('hello world') : 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca8
6d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
43.5 SHA384() Function
We can calculate the SHA384 hash using the SHA384() Function
Syntax:
SHA384(cString) ---> String contains the SHA384 hash of the string cString
Example:
see "sha384('hello') : " + sha384("hello") + nl +
"sha384('apple') : " + sha384("apple") + nl +
"sha384('hello world') : " + sha384("hello world") + nl
Output:
sha384('hello') : 59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa
90125a3c79f90397bdf5f6a13de828684f
sha384('apple') : 3d8786fcb588c93348756c6429717dc6c374a14f7029362281a3b21dc10250
ddf0d0578052749822eb08bc0dc1e68b0f
sha384('hello world') : fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcb
b83578b3e417cb71ce646efd0819dd8c088de1bd
43.6 SHA224() Function
We can calculate the SHA224 hash using the SHA224() Function
Syntax:
SHA224(cString) ---> String contains the SHA224 hash of the string cString
Example:
see "sha224('hello') : " + sha224("hello") + nl +
"sha224('apple') : " + sha224("apple") + nl +
"sha224('hello world') : " + sha224("hello world") + nl
Output:
sha224('hello') : ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193
sha224('apple') : b7bbfdf1a1012999b3c466fdeb906a629caa5e3e022428d1eb702281
sha224('hello world') : 2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b
43.7 Encrypt() Function
We can use the Encrypt() function to encrypts the data using the Blowfish algorithm.
43.5. SHA384() Function 338