qapi

package module
v0.0.0-...-448f349 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2020 License: MIT Imports: 4 Imported by: 0

README

Qapi

API query in Golang, query your api like SQL

Install

go get -u github.com/FerdinaKusumah/qapi

Query from JSON Object

package main

import (
	"encoding/json"
	"fmt"
	"github.com/FerdinaKusumah/qapi"
	"log"
)

func main() {
	var (
		err     error
		val     []byte
		records interface{}
		query1  = `
            select id from "https://jsonplaceholder.typicode.com/comments/1"
        `
		query2  = `
            select id, name, email from "https://jsonplaceholder.typicode.com/comments/1"
        `
		query3  = `
            select name, email from "https://jsonplaceholder.typicode.com/comments/1" 
            where email = 'Eliseo@gardner.biz'
        `
		query4  = `
            select name, email from "https://jsonplaceholder.typicode.com/comments" 
            where email like '.biz'
        `
		query5  = `
            select name, email from "https://jsonplaceholder.typicode.com/comments" 
            where email != 'Eliseo@gardner.biz'
        `
	)

	if records, err = qapi.Fetch(query1); err != nil {
		log.Fatalf(`unable to fetch sql: %s`, err)
	}
	val, _ = json.Marshal(records)
	fmt.Println(string(val)) // {"id":1}

	if records, err = qapi.Fetch(query2); err != nil {
		log.Fatalf(`unable to fetch sql: %s`, err)
	}
	val, _ = json.Marshal(records)
	fmt.Println(string(val)) // {"email":"eliseo@gardner.biz","id":1,"name":"id labore ex et quam laborum"}

	if records, err = qapi.Fetch(query3); err != nil {
		log.Fatalf(`unable to fetch sql: %s`, err)
	}
	val, _ = json.Marshal(records)
	fmt.Println(string(val)) // {"email":"eliseo@gardner.biz","name":"id labore ex et quam laborum"}

	if records, err = qapi.Fetch(query4); err != nil {
		log.Fatalf(`unable to fetch sql: %s`, err)
	}
	val, _ = json.Marshal(records)
	fmt.Println(string(val)) // {"email":"eliseo@gardner.biz","name":"id labore ex et quam laborum"}

	if records, err = qapi.Fetch(query5); err != nil {
		log.Fatalf(`unable to fetch sql: %s`, err)
	}
	val, _ = json.Marshal(records)
	fmt.Println(string(val)) // [{"name":"id labore ex et quam laborum"},{"email":"eliseo@gardner.biz"}, ... ]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fetch

func Fetch(sqlString string) (interface{}, error)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL