arsd.database

Generic interface for RDBMS access. Use with one of the implementations in arsd.mysql, arsd.sqlite, arsd.postgres, or arsd.mssql. I'm sorry the docs are not good, but a little bit goes a long way:

	auto db = new Sqlite("file.db"); // see the implementations for constructors
	// then the interface, for any impl can be as simple as:

	foreach(row; db.query("SELECT id, name FROM people")) {
             string id = row[0];
	     string name = row[1];
	}

	db.query("INSERT INTO people (id, name) VALUES (?, ?)", 5, "Adam");

To convert to other types, just use std.conv.to since everything comes out of this as simple strings with the exception of binary data, which you'll want to cast to const(ubyte)[].

Public Imports

std.variant
public import std.variant;
Undocumented in source.
std.datetime
public import std.datetime;
Undocumented in source.

Members

Classes

InsertBuilder
class InsertBuilder
Undocumented in source.
PredefinedResultSet
class PredefinedResultSet

Unittest utility that returns a predefined set of values

SelectBuilder
class SelectBuilder

WARNING: this is as susceptible to SQL injections as you would be writing it out by hand

SimpleDataObject
class SimpleDataObject(string tableToUse, fieldsToUse)

This creates an editable data object out of a simple struct.

Functions

DataObjectField
string DataObjectField()

You can subclass DataObject if you want to get some compile time checks or better types.

escapedVariants
string escapedVariants(Database db, string sql, Variant[] t)

Note: ?n params are zero based!

Interfaces

Database
interface Database

Mixin templates

DatabaseRestObject
mixintemplate DatabaseRestObject(alias getDb)

Easy interop with arsd.cgi serveRestObject classes.

Structs

StructFromCreateTable
struct StructFromCreateTable(string sql, string tableName)

Given some SQL, it finds the CREATE TABLE instruction for the given tableName. (this is so it can find one entry from a file with several SQL commands. But it may break on a complex file, so try to only feed it simple sql files.)

Templates

DataObjectFromSqlCreateTable
template DataObjectFromSqlCreateTable(string sql, string tableName)

Combines StructFromCreateTable and SimpleDataObject into a one-stop template. alias DataObjectFromSqlCreateTable(import("file.sql"), "my_table") MyTable;

Meta

History

Originally written prior to 2011.

On August 2, 2022, the behavior of BLOB (or BYTEA in postgres) changed significantly. Before, it would convert to strings with to!string(bytes) on insert and platform specific on query. It didn't really work at all.

It now actually stores ubyte[] as a blob and retrieves it without modification. Note you need to cast it.

This is potentially breaking, but since it didn't work much before I doubt anyone was using it successfully but this might be a problem. I advise you to retest.

Be aware I don't like this string interface much anymore and want to change it significantly but idk how to work it in without breaking a decade of code.