GithubHelp home page GithubHelp logo

maciejtrybilo / postgresql Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vapor-community/postgresql

0.0 2.0 0.0 39 KB

Robust PostgreSQL interface for Swift

License: MIT License

Swift 100.00%

postgresql's Introduction

PostgreSQL for Swift

A Swift wrapper for PostgreSQL.

  • Thread-Safe
  • Prepared Statements
  • Tested

This wrapper uses the latest PostgreSQL fetch API to enable performant prepared statements and output bindings.

The Swift wrappers around the PostgreSQL's C structs and pointers automatically manage closing connections and deallocating memeory. Additionally, the PostgreSQL library API is used to perform thread safe, performant queries to the database.

Examples

Connecting to the Database

import PostgreSQL

let postgreSQL =  PostgreSQL.Database(
    dbname: "test",
    user: "root",
    password: ""
)

Select

let version = try postgreSQL.execute("SELECT version()")

Prepared Statement

The second parameter to execute() is an array of PostgreSQL.Values.

let results = try postgreSQL.execute("SELECT * FROM users WHERE age >= $1", [.int(21)])
public enum Value {
    case string(String)
    case int(Int)
    case bool(Int)
    case double(Double)
    case null
}

Connection

Each call to execute() creates a new connection to the PostgreSQL database. This ensures thread safety since a single connection cannot be used on more than one thread.

If you would like to re-use a connection between calls to execute, create a reusable connection and pass it as the third parameter to execute().

let connection = postgreSQL.makeConnection()
let result = try postgreSQL.execute("SELECT LAST_INSERTED_ID() as id", [], connection)

No need to worry about closing the connection.

Building

macOS

Install PostgreSQL

brew install postgresql
brew link postgresql
brew services start postgresql

// to stop 
brew services stop postgresql

Linux

Install PostgreSQL

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib
psql -h dbhost -U username dbname

swift build should work normally.

Fluent

This wrapper was created to power Fluent, an ORM for Swift.

๐Ÿ‘ฅ Authors

Made by Prince Ugwuh a member of Qutheory community.

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.