SQLDelight从SQL生成Java模型CREATE TABLE
语句。这些模型给你一个类型安全的API来读写你的表中的行。它可以帮助你保持你的SQL语句共同组织,并容易从Java访问。
要使用SQLDelight,把你的SQL语句在.sq
文件,就像 在src / main / sqldelight / COM /例子/ HockeyPlayer.sq
。通常情况下,第一条语句创建一个表。
CREATE TABLE hockey_player (
_id LONG NOT NULL PRIMARY KEY AUTOINCREMENT,
number INT NOT NULL,
name STRING NOT NULL
);
-进一步SQL语句由标识符进行。这将被用于命名恒定 -在生成的Java代码。
select_by_name:
SELECT *
FROM hockey_player
WHERE name = ;
从这个SQLDelight会生成一个HockeyPlayerModel
嵌套类的Java类阅读(映射器)和写作(元帅)表。
package com.example;
import android.content.ContentValues;
import android.database.Cursor;
import java.lang.String;
public interface HockeyPlayerModel {
String TABLE_NAME = "hockey_player";
String _ID = "_id";
String NUMBER = "number";
String NAME = "name";
String CREATE_TABLE = ""
+ "CREATE TABLE hockey_player (\n"
+ " _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n"
+ " number INTEGER NOT NULL,\n"
+ " name TEXT NOT NULL\n"
+ ")";
String SELECT_BY_NAME = ""
+ "SELECT *\n"
+ "FROM hockey_player\n"
+ "WHERE name = ";
long _id();
int number();
String name();
final class Mapper<T extends HockeyPlayerModel> {
private final Creator<T> creator;
protected Mapper(Creator<T> creator) {
this.creator = creator;
}
public T map(Cursor cursor) {
return creator.create(
cursor.getLong(cursor.getColumnIndex(_ID)),
cursor.getInt(cursor.getColumnIndex(NUMBER)),
cursor.getString(cursor.getColumnIndex(NAME))
);
}
public interface Creator<R extends HockeyPlayerModel> {
R create(long _id, int number, String name);
}
}
class HockeyPlayerMarshal<T extends HockeyPlayerMarshal<T>> {
protected ContentValues contentValues = new ContentValues();
public HockeyPlayerMarshal() {
}
public final ContentValues asContentValues() {
return contentValues;
}
public T _id(long _id) {
contentValues.put(_ID, _id);
return (T) this;
}
public T number(int number) {
contentValues.put(NUMBER, number);
return (T) this;
}
public T name(String name) {
contentValues.put(NAME, name);
return (T) this;
}
}
}
使用谷歌的AutoValue可以最低限度地使模型/元帅/映射器的实现:
@AutoValue
public abstract class HockeyPlayer implements HockeyPlayerModel {
public static final Mapper<HockeyPlayer> MAPPER = new Mapper<>(new Mapper.Creator() {
@Override public HockeyPlayer create(long _id, int number, String name) {
return new AutoValue_HockeyPlayer(_id, age, number, gender);
}
}
public static final class Marshal extends HockeyPlayerMarshal<Marshal> { }
}
如果您还使用Retrolambda匿名类可以通过方法引用来代替:
@AutoValue
public abstract class HockeyPlayer implements HockeyPlayerModel {
public static final Mapper<HockeyPlayer> MAPPER = new Mapper<>(AutoValue_HockeyPlayer::new);
public static final class Marshal extends HockeyPlayerMarshal<Marshal> { }
}
使用生成的常量引用表名和SQL语句。
public void insert(SqliteDatabase db, long _id, int number, String name) {
db.insert(HockeyPlayer.TABLE_NAME, null, new HockeyPlayer.Marshal()
._id(_id)
.number(number)
.name(name)
.asContentValues());
}
public List<HockeyPlayer> alecs(SqliteDatabase db) {
List<HockeyPlayer> result = new ArrayList<>();
try (Cursor cursor = db.rawQuery(HockeyPlayer.SELECT_BY_NAME, new String[] { "Alec" })) {
while (cursor.moveToNext()) {
result.add(HockeyPlayer.MAPPER.map(cursor));
}
}
return result;
}
SQLDelight支持同类型光标
和ContentValues
期望:
CREATE TABLE some_types {
some_int INT, -存储为INTEGER
some_short SHORT, -存储为INTEGER
some_long LONG, -存储为INTEGER
some_float FLOAT, -存储为REAL
some_double DOUBLE,-存储为REAL
some_string STRING,-存储为文本
some_blob BLOB -存储为BLOB
}
SQLDelight支持枚举,但需要在SQL完全限定Enum类型:
CREATE TABLE hockey_player (
position ENUM('com.example.hockey.Player.Position')
)
SQLDelight支持布尔列,将它们存储在数据库中的整数。由于它们作为整数实现时,它们可以给INT列约束:
CREATE TABLE hockey_player (
injured BOOLEAN DEFAULT 0
)
如果您想保存为自定义类型,它看起来类似于定义枚举列:
CREATE TABLE hockey_player (
birth_date CLASS('java.util.Calendar') NOT NULL
)
但是,创建一个元帅或映射器将要求您提供ColumnAdapter
它知道如何将映射光标
到你的类型和元帅你的类型变成ContentValues
:
public class HockeyPlayer implements HockeyPlayerModel {
private static final ColumnAdapter<Calendar> CALENDAR_ADAPTER = new ColumnAdapter<>() {
@Override public Calendar map(Cursor cursor, int columnIndex) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(cursor.getLong(columnIndex));
return calendar;
}
@Override public void marshal(ContentValues contentValues, String key, Calendar value) {
contentValues.put(key, value.getTimeInMillis());
}
}
public static final Mapper<HockeyPlayer> MAPPER = new Mapper<>(new Mapper.Creator<>() { },
CALENDAR_ADAPTER);
public static final class Marshal extends HockeyPlayerMarshal<Marshal> {
public Marshal() {
super(CALENDAR_ADAPTER);
}
}
}
SQL查询也可以包含参数以同样的方式SqliteDatabase
作用:
select_by_position:
SELECT *
FROM hockey_player
WHERE position = ;
Cursor centers = db.rawQuery(HockeyPlayer.SELECT_BY_POSITION, new String[] { Center.name() });
该插件的IntelliJ提供语言级特性为.sq
文件,包括:
模式
编辑后的文件对于摇篮插件:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.squareup.sqldelight:gradle-plugin:0.2.2'
}
}
apply plugin: 'com.squareup.sqldelight'
该插件的IntelliJ可以从Android的工作室通过导航安装
>首选项- - Android电子工作室>插件- >浏览资料库- >搜索SQLDelight
热门源码