import { assertValidLiteColumnSpec } from "../types/lite.js";
import { id, idList } from "../types/sql.js";
function liteColumnDef(spec) {
  assertValidLiteColumnSpec(spec);
  let def = id(spec.dataType);
  if (spec.characterMaximumLength) {
    def += `(${spec.characterMaximumLength})`;
  }
  if (spec.notNull) {
    def += " NOT NULL";
  }
  if (spec.dflt) {
    def += ` DEFAULT ${spec.dflt}`;
  }
  return def;
}
function createLiteTableStatement(spec) {
  const defs = Object.entries(spec.columns).sort(([_a, { pos: a }], [_b, { pos: b }]) => a - b).map(([name, columnSpec]) => `${id(name)} ${liteColumnDef(columnSpec)}`);
  if (spec.primaryKey) {
    defs.push(`PRIMARY KEY (${idList(spec.primaryKey)})`);
  }
  const createStmt = `CREATE TABLE ${id(spec.name)} (`;
  return [createStmt, defs.join(",\n"), ");"].join("\n");
}
function createLiteIndexStatement(index) {
  const columns = Object.entries(index.columns).map(([name, dir]) => `${id(name)} ${dir}`).join(",");
  const unique = index.unique ? "UNIQUE" : "";
  return `CREATE ${unique} INDEX ${id(index.name)} ON ${id(
    index.tableName
  )} (${columns});`;
}
export {
  createLiteIndexStatement,
  createLiteTableStatement,
  liteColumnDef
};
//# sourceMappingURL=create.js.map
