Giter Club home page Giter Club logo

r7rs-postgresql's Introduction

PostgreSQL binding for R7RS Scheme

This is a PostgreSQL socket frontend interface library written in pure R7RS Scheme.

NOTE: it's still working state.

Supported implementations

  • Sagittarius 0.5.9 or later
  • Gauche 0.9.4
  • Chibi Scheme 0.7

This library should work in R7RS implementations which support the following SRFIs:

  • SRFI-60/SRFI-33 or R6RS library (rnrs)
  • SRFI-106
  • SRFI-19 (Optional)

High level APIs

Library

  • (postgresql) The library provides high level APIs to communicate with PostgreSQL.

Procedures

  • (postgresql-connection? obj)

    Returns #t if obj is an PostgreSQL connection object.

  • (make-postgresql-connection host port database username password)

    database can be #f.

    All arguments must be a string except database. Creates a PostgreSQL connection. At this moment, the connection to the server is not established.

  • (postgresql-open-connection! conn)

    Establishes a connection with specified conn object.

  • (postgresql-login! conn)

    Logging in to the PostgreSQL server.

  • (postgresql-terminate! conn)

    Terminates the session and disconnects from the server.

  • (postgresql-prepared-statement? obj)

    Return #t if obj is a PostgreSQL prepared statement.

  • (postgresql-prepared-statement conn sql)

    Creates a prepared statement object.

  • (postgresql-close-prepared-statement! prepared-statement)

    Closes the prepared statement prepared-statement.

  • (postgresql-bind-parameters! prepared-statement . params)

    Binds parameter params to given prepared-statement.

  • (postgresql-execute! prepared-statement)

    Executes the given prepared-statement and returns either PostgreSQL query object for SELECT statement or affected row count.

    To retrieve the result, use the postgresql-fetch-query! procedure.

  • (postgresql-query? obj)

    Returns #t if obj is a PostgreSQL query object.

  • (postgresql-execute-sql! conn sql)

    Executes the given sql statement. If sql is a select statement then the value returned is a PostgreSQL query object. Otherwise #t. This procedure retrieves all results in one go if sql is a SELECT statement. So it may cause memory explosion if the result set is too big.

  • (postgresql-fetch-query! query)

    Fetch a row as a vector. If no more data are available, then returns #f.

  • (postgresql-start-transaction! conn mode)

    Issue START TRANSACTION statement to start a transaction. mode specifies how the transation should be.

    The argument mode must be either a PostgreSQL transaction mode object or #f.

  • (postgresql-transaction-mode alist)

    Creates a PostgreSQL transaction mode object. The alist specifies how the transaction mode is created. It may have the following symbols as its key.

    • isolation-level
    • access-mode
    • deferrable

    Each key must have one of the followings:

    For isolation-level:

    • Variable: postgresql-isolation-level-serializable
    • Variable: postgresql-isolation-level-repeatable-read
    • Variable: postgresql-isolation-level-read-committed
    • Variable: postgresql-isolation-level-read-uncommitted

    For access-mode:

    • Variable: postgresql-access-mode-read-write
    • Variable: postgresql-access-mode-read-only

    For deferrable:

    • Variable: postgresql-deferrable-on
    • Variable: postgresql-deferrable-off
  • (postgresql-commit! conn)

    Issue COMMIT statement.

  • (postgresql-rollback! conn)

    Issue ROLLBACK statement.

Parameters

  • *postgresql-maximum-results*

    Configuration parameter for how many result it should fetch. Default value is 50.

  • *postgresql-copy-data-handler*

    Handler of COPY to stdout command. The value must be a procedure and takes 2 arguments, data type and data. The data type should be one of the the following symbols:

    • header
    • data
    • complete

    When the data type is header then the given data is a list of data information. It contains 3 elements, the format of overall COPY command, 0 is textual, 1 is binary.

    When the data type is data then the given data is a bytevector whose content is the result of COPY command.

    When the data type is complete then the given data is #f. This indicates the COPY command is done.

  • *postgresql-write-data-handler*

Handler of COPY from stdin command. The value must be a procedure and take 2 arguments, data type and data. The data type could be one of the following symbols;

  • header
  • data
  • complete

When the data type is header then the given data is a list of data information. It contains 3 elements, the format of overall COPY command, 0 is textual, 1 is binary.

When the data type is data then the given data is a #f. When there is no more data to send, then the handler must return #f otherwise it would go into inifinite loop.

When the data type is complete then the given data is #t. This indicates the COPY command is done.

These handlers are currently a thin wrapper of the COPY command. Using them, users need to know about how the data is sent. For more detail, please refer the PostgreSQL manual.

  • *postgresql-unknown-type-handler*

    Handler of unknown type, which is the library default couldn't handle converting the value according to the type identifier. The value must be aprocedure and take 2 arguments; type and value. The type is an integer which represents PostgreSQL internal type defined in catalog/pg_type.h header file of PostgreSQL source. The value is a raw value of SQL query, bytevector.

Low level APIs

TBD

Data conversion

Data conversion is done automatically by high level APIs. The following table describes how it's done.

PostgreSQL type Scheme type
Integers Number
Float Inexact number
Characters String
Date SRFI-19 date
Time SRFI-19 date
Timestamp SRFI-19 time
UUID String

Note: If the implementation doesn't support SRFI-19, the scheme type will be string.

Copyright and lincense

Copyright 2014-2015 Takashi Kato. Code released under the BSD-style license. See COPYING.

TODO

  • Data conversion is not done properly
    • need some document but couldn't find it...
  • Prepared statement
  • Maybe buffering
    • currently it can't execute second query unless it fetches all records.

r7rs-postgresql's People

Contributors

ktakashi avatar roklein avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

theschemer drewc

r7rs-postgresql's Issues

Creating an index with very long name hands

Executing the following SQL script hangs:

CREATE INDEX this_is_a_very_long_name_of_the_index_for_test_table_for_some_reason_this_would_hang 
ON test (id)

NOTE: If the name is not that much long, then it's ok. (didn't check how many bytes can be accepted)

Creating a prepared statement after failing creation of prepared statement hangs.

The following script hangs on second prepared statement creation.

(postgresql-open-connection! conn)
(postgresql-login! conn)

(guard (e (else (print e)))
  (let ((ps (postgresql-prepared-statement 
             conn "select * from foo where a = ?")))
    (print ps)
    (postgresql-close-prepared-statement! ps)))

(guard (e (else (print e)))
  (let ((ps (postgresql-prepared-statement 
             conn "select * from foo where a = ?")))
    (print ps)
    (postgresql-close-prepared-statement! ps)))

(postgresql-terminate! conn)

No license file

Software without an explicit license is effectively all rights reserved. Mind licensing this properly under some free software license?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.