GithubHelp home page GithubHelp logo

postgresql-connector's Introduction

postgresql-connector

This module provides the functionality required to access and manipulate data stored in a PostgreSQL database.

Client

To access a database, you must first create a postgresql:Client object. To create a client the jdbc connection string for the database is mandatory.

Following is a sample usecase to use this connector.

import ballerina/io;
import ballerina/config;
import ballerina/sql;
import ballerinax/postgresql;

public function main() {
    string fname = "James";
    string lname = "Bond";
    string country = "US";
    
    postgresql:Client postgresqlClient = checkpanic new (config:getAsString("CONNECTION_STRING"));
    
    // Insert Users into db
    sql:ExecutionResult insertResult = insertUser(postgresqlClient, fname, lname, country);
    io:println(insertResult);
    //Get All users from db
    getAllUser(postgresqlClient);
    checkpanic postgresqlClient.close();
}

function insertUser(postgresql:Client postgresqlClient, string fname, string lname, string country) returns sql:ExecutionResult{
    sql:ParameterizedQuery insertQuery = `INSERT INTO userdetail(firstname, lastname, country) 
            VALUES ( ${fname}, ${lname}, ${country})`;
    sql:ExecutionResult result = checkpanic postgresqlClient->execute(insertQuery);
    return result;
}
function getAllUser(postgresql:Client postgresqlClient){
    stream<record{}, error> resultStream = postgresqlClient->query("Select * from userdetail");

    error? e = resultStream.forEach(function(record {} result) {
        io:println("Full User details: ", result);
        io:println("User first name: ", result["firstname"]);
        io:println("User last name: ", result["lastname"]);
        io:println("User country: ", result["country"]);
    });

    if (e is error) {
        io:println("ForEach operation on the stream failed!");
        io:println(e);
    }
}

Need to setup the CONNECTION_STRING for the db (with database name) in the ballerina.config file.

postgresql-connector's People

Contributors

kasthuriraajan avatar

Watchers

James Cloos avatar  avatar

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.