Coder Social home page Coder Social logo

easy_sqlite3's Introduction

Yet another SQLite wrapper for Nim

Features:

  1. Design for ARC/ORC, you don’t need to close the connection manually
  2. Use importdb macro to create helper function (see examples)
  3. Including a memfs implemention, may better than :memory: database since it support WAL mode (Experimental, see tests/test_thread)

Example

Basic usage:

import std/tables
import easy_sqlite3

# Bind function argument to sql statment
# The tuple return value indicate the query will got exactly 1 result
proc select_1(arg: int): tuple[value: int] {.importdb: "SELECT $arg".}

var db = initDatabase(":memory:")
# Use as a method (the statment will be cached, thats why `var` is required)
echo db.select_1(1).value
# Got 1

# You can bind create statment as well
proc create_table() {.importdb: """
  CREATE TABLE mydata(name TEXT PRIMARY KEY NOT NULL, value INT NOT NULL);
""".}

# Or insert
proc insert_data(name: string, value: int) {.importdb: """
  INSERT INTO mydata(name, value) VALUES ($name, $value);
""".}

# And you can create iterator by the same way (the `= discard` is required, since iterator must have body in nim)
iterator iterate_data(): tuple[name: string, value: int] {.importdb: """
  SELECT name, value FROM mydata;
""".} = discard

const dataset = {
  "A": 0,
  "B": 1,
  "C": 2,
  "D": 3,
}.toTable
db.create_table()
# Use transaction (commit by default)
db.transaction:
  for name, value in dataset:
    db.insert_data name, value
  commit() # optional
  # Never goes here

for name, value in db.iterate_data():
  assert name in dataset
  assert dataset[name] == value

easy_sqlite3's People

Contributors

codehz 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.