Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SQLDelight

 SQLDelight

Presented at Android Listener on October 19th, 2016

Avatar for Bryan Sills

Bryan Sills

October 19, 2016
Tweet

More Decks by Bryan Sills

Other Decks in Technology

Transcript

  1. What is SQLDelight? • A Java library from Square •

    Generates generates Java models from your SQL files • Not an ORM
  2. SQLDelight generates generates Java models from your SQL files import

    java.util.Calendar; CREATE TABLE author ( _id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, birth_year INTEGER AS Calendar NOT NULL ); insert_author: INSERT INTO author (name, birth_year) VALUES (?, ?); select_all: select * from author; select_by_name: select * from author where author.name = ?; List<Author> authors = new ArrayList<>(); SQLiteDatabase db = DatabaseHelper.getInstance(this).getReadableDatabase(); Cursor cursor = db.rawQuery(Author.SELECT_ALL, null); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { Author author = Author.MAPPER.map(cursor); authors.add(author); } Book.Insert_book insertBook = new BookModel.Insert_book(db, Book.FACTORY); insertBook.bind("0001", "Harry Potter and the Philosopher's Stone", rowling, new GregorianCalendar(1997, 6, 26), Book.Genre.FANTASY); insertBook.program.executeInsert();
  3. SQLDelight supports SQL features • Custom classes • Enums •

    CREATE statements • SELECT statements • Views • Joins
  4. Further Reading • https://github.com/square/sqldelight • You should use https://github.com/google/auto/tree/master/value •

    http://alexsimo.com/delightful-persistence-android/ • http://ryanharter.com/blog/2016/03/22/autovalue/ • https://github.com/bryansills/McMac • https://twitter.com/bryansills