import 'data_classes.dart';
import 'converter.dart';

CREATE TABLE no_ids (
    payload BLOB NOT NULL PRIMARY KEY
) WITHOUT ROWID WITH NoIdRow;

CREATE TABLE with_defaults (
    a TEXT JSON KEY customJsonName DEFAULT 'something',
    b INT UNIQUE
);

CREATE TABLE with_constraints (
    a TEXT,
    b INT NOT NULL,
    c FLOAT(10, 2),

    FOREIGN KEY (a, b) REFERENCES with_defaults (a, b)
);

create table config (
    config_key TEXT not null primary key,
    -- The current value associated with the [configKey]
    config_value ANY,
    sync_state INTEGER MAPPED BY `const SyncTypeConverter()`,
    sync_state_implicit ENUM(SyncType)
) STRICT AS "Config";

CREATE INDEX IF NOT EXISTS value_idx ON config (config_value);

CREATE TABLE mytable (
    someid INTEGER NOT NULL,
    sometext TEXT,
    is_inserting BOOLEAN,
    somedate DATETIME,
    PRIMARY KEY (someid DESC),
    UNIQUE (sometext, is_inserting)
);

CREATE VIRTUAL TABLE email USING fts5(sender, title, body) AS EMail;

-- Weird table, repro for https://github.com/simolus3/drift/issues/932
CREATE TABLE "Expression" (
    class INTEGER NOT NULL AS "sqlClass",
    "text" TEXT NOT NULL AS "textColumn"
) AS "WeirdData/WeirdTable";

CREATE TRIGGER my_trigger AFTER INSERT ON config BEGIN
  INSERT INTO with_defaults VALUES (new.config_key, LENGTH(new.config_value));
END;

CREATE VIEW my_view AS SELECT * FROM config WHERE sync_state = 2;

readConfig: SELECT
  config_key AS ck,
  config_value as cf,
  sync_state AS cs1,
  sync_state_implicit AS cs2
FROM config WHERE config_key = ?;
readMultiple: SELECT * FROM config WHERE config_key IN ? ORDER BY $clause;
readDynamic ($predicate = TRUE): SELECT * FROM config WHERE $predicate;

typeConverterVar($pred = TRUE): SELECT config_key FROM config WHERE $pred AND
        (sync_state = ? OR sync_state_implicit IN ?);

tableValued AS JsonResult:
  SELECT "key", "value"
  FROM config, json_each(config.config_value)
  WHERE json_valid(config_value);

-- using NULLIF to make the second parameter nullable and allow compatibility
another AS JsonResult: SELECT 'one' AS "key", NULLIF('two', 'another') AS "value";

@create: INSERT INTO config (config_key, config_value) VALUES ('key', 'values');

multiple: SELECT d.*, c.** FROM with_defaults d
 LEFT OUTER JOIN with_constraints c
   ON d.a = c.a AND d.b = c.b
 WHERE $predicate;

searchEmails(REQUIRED :term AS TEXT OR NULL): SELECT * FROM email WHERE email MATCH :term ORDER BY rank;

readRowId: SELECT oid, * FROM config WHERE _rowid_ = $expr;

readView($where = TRUE): SELECT * FROM my_view WHERE $where;

cfeTest: WITH RECURSIVE
  cnt(x) AS (
    SELECT 1
      UNION ALL
      SELECT x+1 FROM cnt
      LIMIT 1000000
    )
  SELECT x FROM cnt;

nullableQuery: SELECT MAX(oid) FROM config;
addConfig: INSERT INTO config $value RETURNING *;

nested: SELECT defaults.**, LIST(SELECT * FROM with_constraints c WHERE c.b = defaults.b)
  FROM with_defaults defaults
  WHERE a = ?;

customResult WITH MyCustomResultClass:
  SELECT
    with_constraints.b,
    config.sync_state,
    config.**,
    no_ids.**,
    LIST(SELECT * FROM no_ids) AS nested
  FROM with_constraints
    INNER JOIN config ON config_key = with_constraints.a
    CROSS JOIN no_ids;
