
org.rapidoidx.db.XDB Maven / Gradle / Ivy
The newest version!
package org.rapidoidx.db;
/*
* #%L
* rapidoid-x-db
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
* #L%
*/
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.cls.Cls;
import org.rapidoid.concurrent.Callback;
import org.rapidoid.config.Conf;
import org.rapidoid.lambda.Operation;
import org.rapidoid.lambda.Predicate;
import org.rapidoid.u.U;
@Authors("Nikolche Mihajlovski")
@Since("3.0.0")
public class XDB {
private static final String IMPL_NAME = "org.rapidoidx.db.impl.inmem.DbImpl";
static final Class DB_IMPL_CLASS;
private static Database db;
static {
String dbImplName = Conf.option("db", IMPL_NAME);
DB_IMPL_CLASS = Cls.getClassIfExists(IMPL_NAME);
U.must(DB_IMPL_CLASS != null, "Cannot find Db implementation (%s)!", dbImplName);
U.must(Database.class.isAssignableFrom(DB_IMPL_CLASS), "%s must implement %s!", dbImplName,
Database.class.getCanonicalName());
start();
}
public static String path() {
String path = Conf.option("db", "");
if (!path.isEmpty() && !path.endsWith(File.separator)) {
path += File.separator;
}
return path;
}
public static void start() {
db = (Database) Cls.customizable(XDB.DB_IMPL_CLASS, "default", path() + "default.db");
db.loadAndStart();
}
public static Database db() {
U.notNull(db, "Database not initialized!");
return db;
}
public static void setDb(Database db) {
XDB.db = db;
}
public static long insert(Object record) {
return db().insert(record);
}
public static void delete(long id) {
db().delete(id);
}
public static void delete(Object record) {
db().delete(record);
}
public static T get(long id) {
return db().get(id);
}
public static T getIfExists(long id) {
return db().getIfExists(id);
}
public static T get(long id, Class clazz) {
return db().get(id, clazz);
}
public static List getAll(Class clazz) {
return db().getAll(clazz);
}
public static List getAll(long... ids) {
return db().getAll(ids);
}
public static List getAll(Collection ids) {
return db().getAll(ids);
}
public static void refresh(E record) {
db().refresh(record);
}
public static void update(long id, Object record) {
db().update(id, record);
}
public static void update(Object record) {
db().update(record);
}
public static long persist(Object record) {
return db().persist(record);
}
public static long persistedIdOf(Object record) {
return db().persistedIdOf(record);
}
public static E readColumn(long id, String column) {
return db().readColumn(id, column);
}
public static List find(Predicate match) {
return db().find(match);
}
public static List find(Iterable ids) {
return db().find(ids);
}
public static List find(Class clazz, Predicate match, Comparator orderBy) {
return db().find(clazz, match, orderBy);
}
public static List find(String searchPhrase) {
return db().find(searchPhrase);
}
public static List find(Class clazz, String query, Object... args) {
return db().find(clazz, query, args);
}
public static void each(Operation lambda) {
db().each(lambda);
}
public static void transaction(Runnable transaction, boolean readOnly) {
db().transaction(transaction, readOnly);
}
public static void transaction(Runnable transaction, boolean readOnly, Callback callback) {
db().transaction(transaction, readOnly, callback);
}
public static void shutdown() {
if (db() != null) {
db().shutdown();
}
}
public static long size() {
return db().size();
}
public static void clear() {
db().clear();
}
public static boolean isActive() {
return db() != null && db().isActive();
}
public static void halt() {
if (db() != null) {
db().halt();
}
}
public static void destroy() {
if (db() != null) {
db().destroy();
}
}
public static long getIdOf(Object record) {
return db().getIdOf(record);
}
public static long getVersionOf(long id) {
return db().getVersionOf(id);
}
public static DbColumn column(Map map, String name, Class type) {
return db().column(map, name, type);
}
public static DbList list(Object holder, String relation) {
return db().list(holder, relation);
}
public static DbSet set(Object holder, String relation) {
return db().set(holder, relation);
}
public static DbRef ref(Object holder, String relation) {
return db().ref(holder, relation);
}
public static DbSchema schema() {
return db().schema();
}
@SuppressWarnings("unchecked")
public static E entity(Class entityType) {
return (E) schema().entity(entityType, Collections.EMPTY_MAP);
}
public static E entity(Class entityType, Map properties) {
return schema().entity(entityType, properties);
}
public static E entity(Class entityType, String prop, Object value) {
return schema().entity(entityType, U.map(prop, value));
}
public static E entity(Class entityType, String prop1, Object value1, String prop2, Object value2) {
return schema().entity(entityType, U.map(prop1, value1, prop2, value2));
}
public static E entity(Class entityType, String prop1, Object value1, String prop2, Object value2,
String prop3, Object value3) {
return schema().entity(entityType, U.map(prop1, value1, prop2, value2, prop3, value3));
}
public static E entity(Class entityType, String prop1, Object value1, String prop2, Object value2,
String prop3, Object value3, String prop4, Object value4) {
return schema().entity(entityType, U.map(prop1, value1, prop2, value2, prop3, value3, prop4, value4));
}
public static E entity(Class entityType, String prop1, Object value1, String prop2, Object value2,
String prop3, Object value3, String prop4, Object value4, String prop5, Object value5) {
return schema().entity(entityType,
U.map(prop1, value1, prop2, value2, prop3, value3, prop4, value4, prop5, value5));
}
public static DbDsl dsl(Class entityType) {
return schema().dsl(entityType);
}
public static Database as(String username) {
return db().as(username);
}
public static Database sudo() {
return db().sudo();
}
public static void init(String data, Object... args) {
db().init(data, args);
}
public static RESULT rql(String rql, Object... args) {
return db().rql(rql, args);
}
public static E entity(String rql, Object... args) {
return db().entity(rql, args);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy