Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/huntlabs/hunt-database
Database abstraction layer library using pure D programing language, support PostgreSQL and MySQL.
https://github.com/huntlabs/hunt-database
database dlang mysql native postgresql sqlite
Last synced: 3 months ago
JSON representation
Database abstraction layer library using pure D programing language, support PostgreSQL and MySQL.
- Host: GitHub
- URL: https://github.com/huntlabs/hunt-database
- Owner: huntlabs
- License: apache-2.0
- Created: 2017-06-22T10:11:45.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-11-15T08:15:53.000Z (about 1 year ago)
- Last Synced: 2024-09-28T15:41:37.631Z (3 months ago)
- Topics: database, dlang, mysql, native, postgresql, sqlite
- Language: D
- Homepage: https://www.huntlabs.net
- Size: 5.65 MB
- Stars: 48
- Watchers: 11
- Forks: 5
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-d - hunt-database - Hunt database abstraction layer for D programing language, support PostgreSQL / MySQL / SQLite. (Database clients / XML)
README
[![Build Status](https://travis-ci.org/huntlabs/hunt-database.svg?branch=master)](https://travis-ci.org/huntlabs/hunt-database)
## Database
Database abstraction layer for D programing language, support PostgreSQL / MySQL.## Example
```Dimport std.stdio;
import hunt.database;
void main()
{
writeln("run database MySQL demo.");auto db = new Database("mysql://root:123456@localhost:3306/test?charset=utf8mb4");
int result = db.execute(`INSERT INTO user(username) VALUES("test")`);
writeln(result);foreach(row; db.query("SELECT * FROM user LIMIT 10"))
{
writeln(row["username"]);
}db.close();
}```
## Use DatabaseOption to instantiate a Database object
```D
auto options = new DatabaseOption("mysql://root:123456@localhost:3306/test");
options.setMaximumConnection(5);auto db = new Database(options);
db.execute("SET NAMES utf8");
```## API
- int Database.execute(string sql) Return number of execute result.
```D
int result = db.execute('INSERT INTO user(username) VALUES("Brian")');
// if execute error ,db will throw an DatabaseException
```
- RowSet Database.query(sql) Return RowSet object for query(SELECT).
```D
RowSet rs = db.query("SELECT * FROM user LIMIT 10");
```
- Statement Database.prepare(sql) Create a prepared Statement object.
```D
Statement stmt = db.prepare("SELECT * FROM user where username = :username and age = :age LIMIT 10");
```
- Statement.setParameter(param, value) : bind param's value to :param for sql.
```D
stmt.setParameter("username", "viile");
stmt.setParameter("age", 18);
```
- RowSet Statement.query() Return RowSet
```D
RowSet rs = stmt.query();
foreach(row; rs)
{
writeln(row["username"]);
}
```
- Row Statement.fetch() Return Row
```D
Row row = stmt.fetch();
writeln(row["username"]);
```
- int Statement.execute() : return execute status for prepared Statement object.
```D
int result = stmt.execute();
```
- Statement.lastInsertId() : Statement.execute() for insert sql, return lastInsertId.### See also
- https://github.com/eclipse-vertx/vertx-sql-client