text: "use strict";var R=Object.defineProperty,p=Object.defineProperties;var O=Object.getOwnPropertyDescriptors;var q=Object.getOwnPropertySymbols;var b=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var A=(E,e,i)=>e in E?R(E,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):E[e]=i,L=(E,e)=>{for(var i in e||(e={}))b.call(e,i)&&A(E,i,e[i]);if(q)for(var i of q(e))N.call(e,i)&&A(E,i,e[i]);return E},y=(E,e)=>p(E,O(e));var S=(E,e)=>R(E,"name",{value:e,configurable:!0});const d=require("../../utils"),I=require("../../transaction"),$=require("lodash"),M=require("../mysql/query-generator"),C=require("../abstract/query-generator");class m extends M{createSchema(){return"SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';"}showSchemasQuery(){return"SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';"}versionQuery(){return"SELECT sqlite_version() as `version`"}createTableQuery(e,i,r){r=r||{};const t=[],s=Object.values(i).filter(u=>u.includes("PRIMARY KEY")).length>1,n=[];for(const u in i)if(Object.prototype.hasOwnProperty.call(i,u)){const a=i[u],f=a.includes("AUTOINCREMENT");let h=a;a.includes("PRIMARY KEY")&&(a.includes("INT")&&(h=f?"INTEGER PRIMARY KEY AUTOINCREMENT":"INTEGER PRIMARY KEY",a.includes(" REFERENCES")&&(h+=a.substr(a.indexOf(" REFERENCES")))),s&&(t.push(u),a.includes("NOT NULL")?h=a.replace(" PRIMARY KEY",""):h=a.replace("PRIMARY KEY","NOT NULL"))),n.push(`${this.quoteIdentifier(u)} ${h}`)}const o=this.quoteTable(e);let l=n.join(", ");const T=t.map(u=>this.quoteIdentifier(u)).join(", ");r.uniqueKeys&&$.each(r.uniqueKeys,u=>{u.customIndex&&(l+=`, UNIQUE (${u.fields.map(a=>this.quoteIdentifier(a)).join(", ")})`)}),T.length>0&&(l+=`, PRIMARY KEY (${T})`);const c=`CREATE TABLE IF NOT EXISTS ${o} (${l});`;return this.replaceBooleanDefaults(c)}booleanValue(e){return e?1:0}_checkValidJsonStatement(e){if(typeof e!="string")return!1;const i=/^\s*(json(?:_[a-z]+){0,2})\([^)]*\)/i,r=/^\s*((?:([`"'])(?:(?!\2).|\2{2})*\2)|[\w\d\s]+|[().,;+-])/i;let t=0,s=0,n=0,o=!1,l=!1;for(;t<e.length;){const T=e.substr(t),c=i.exec(T);if(c){t+=c[0].indexOf("("),o=!0;continue}const u=r.exec(T);if(u){const a=u[1];if(a==="(")s++;else if(a===")")n++;else if(a===";"){l=!0;break}t+=u[0].length;continue}break}if(l|=s!==n,o&&l)throw new Error(`Invalid json statement: ${e}`);return o}_toJSONValue(e){return e instanceof Date?e.toISOString():Array.isArray(e)&&e[0]instanceof Date?e.map(i=>i.toISOString()):e}handleSequelizeMethod(e,i,r,t,s){return e instanceof d.Json?super.handleSequelizeMethod(e,i,r,t,s):(e instanceof d.Cast&&/timestamp/i.test(e.type)&&(e.type="datetime"),C.prototype.handleSequelizeMethod.call(this,e,i,r,t,s))}addColumnQuery(e,i,r){const t={};t[i]=r;const s=this.attributesToSQL(t,{context:"addColumn"}),n=`${this.quoteIdentifier(i)} ${s[i]}`,o=`ALTER TABLE ${this.quoteTable(e)} ADD ${n};`;return this.replaceBooleanDefaults(o)}showTablesQuery(){return"SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';"}updateQuery(e,i,r,t,s){t=t||{},$.defaults(t,this.options),i=d.removeNullValuesFromHash(i,t.omitNull,t);const n={},o=[],l=[],T=t.bindParam||this.bindParam(l);s&&$.each(s,(a,f)=>{n[f]=a,a.field&&(n[a.field]=a)});for(const a in i){const f=i[a];f instanceof d.SequelizeMethod||t.bindParam===!1?o.push(`${this.quoteIdentifier(a)}=${this.escape(f,n&&n[a]||void 0,{context:"UPDATE"})}`):o.push(`${this.quoteIdentifier(a)}=${this.format(f,n&&n[a]||void 0,{context:"UPDATE"},T)}`)}let c;const u=y(L({},t),{bindParam:T});return t.limit?c=`UPDATE ${this.quoteTable(e)} SET ${o.join(",")} WHERE rowid IN (SELECT rowid FROM ${this.quoteTable(e)} ${this.whereQuery(r,u)} LIMIT ${this.escape(t.limit)})`:c=`UPDATE ${this.quoteTable(e)} SET ${o.join(",")} ${this.whereQuery(r,u)}`,{query:c,bind:l}}truncateTableQuery(e,i={}){return[`DELETE FROM ${this.quoteTable(e)}`,i.restartIdentity?`; DELETE FROM ${this.quoteTable("sqlite_sequence")} WHERE ${this.quoteIdentifier("name")} = ${d.addTicks(d.removeTicks(this.quoteTable(e),"`"),"'")};`:""].join("")}deleteQuery(e,i,r={},t){$.defaults(r,this.options);let s=this.getWhereConditions(i,null,t,r);return s&&(s=`WHERE ${s}`),r.limit&&(s=`WHERE rowid IN (SELECT rowid FROM ${this.quoteTable(e)} ${s} LIMIT ${this.escape(r.limit)})`),`DELETE FROM ${this.quoteTable(e)} ${s}`}attributesToSQL(e){const i={};for(const r in e){const t=e[r],s=t.field||r;if($.isObject(t)){let n=t.type.toString();if(Object.prototype.hasOwnProperty.call(t,"allowNull")&&!t.allowNull&&(n+=" NOT NULL"),d.defaultValueSchemable(t.defaultValue)&&(n+=` DEFAULT ${this.escape(t.defaultValue,t)}`),t.unique===!0&&(n+=" UNIQUE"),t.primaryKey&&(n+=" PRIMARY KEY",t.autoIncrement&&(n+=" AUTOINCREMENT")),t.references){const o=this.quoteTable(t.references.model);let l;t.references.key?l=this.quoteIdentifier(t.references.key):l=this.quoteIdentifier("id"),n+=` REFERENCES ${o} (${l})`,t.onDelete&&(n+=` ON DELETE ${t.onDelete.toUpperCase()}`),t.onUpdate&&(n+=` ON UPDATE ${t.onUpdate.toUpperCase()}`)}i[s]=n}else i[s]=t}return i}showIndexesQuery(e){return`PRAGMA INDEX_LIST(${this.quoteTable(e)})`}showConstraintsQuery(e,i){let r=`SELECT sql FROM sqlite_master WHERE tbl_name='${e}'`;return i&&(r+=` AND sql LIKE '%${i}%'`),`${r};`}removeIndexQuery(e,i){let r=i;return typeof r!="string"&&(r=d.underscore(`${e}_${i.join("_")}`)),`DROP INDEX IF EXISTS ${this.quoteIdentifier(r)}`}describeTableQuery(e,i,r){const t={_schema:i,_schemaDelimiter:r,tableName:e};return`PRAGMA TABLE_INFO(${this.quoteTable(this.addSchema(t))});`}describeCreateTableQuery(e){return`SELECT sql FROM sqlite_master WHERE tbl_name='${e}';`}removeColumnQuery(e,i){i=this.attributesToSQL(i);let r;typeof e=="object"?r={tableName:`${e.tableName}_backup`,schema:e.schema}:r=`${e}_backup`;const t=this.quoteTable(e),s=this.quoteTable(r),n=Object.keys(i).map(o=>this.quoteIdentifier(o)).join(", ");return`${this.createTableQuery(r,i)}INSERT INTO ${s} SELECT ${n} FROM ${t};DROP TABLE ${t};${this.createTableQuery(e,i)}INSERT INTO ${t} SELECT ${n} FROM ${s};DROP TABLE ${s};`}_alterConstraintQuery(e,i,r){let t;i=this.attributesToSQL(i),typeof e=="object"?t={tableName:`${e.tableName}_backup`,schema:e.schema}:t=`${e}_backup`;const s=this.quoteTable(e),n=this.quoteTable(t),o=Object.keys(i).map(l=>this.quoteIdentifier(l)).join(", ");return`${r.replace(`CREATE TABLE ${s}`,`CREATE TABLE ${n}`).replace(`CREATE TABLE ${s.replace(/`/g,'"')}`,`CREATE TABLE ${n}`)}INSERT INTO ${n} SELECT ${o} FROM ${s};DROP TABLE ${s};ALTER TABLE ${n} RENAME TO ${s};`}renameColumnQuery(e,i,r,t){let s;t=this.attributesToSQL(t),typeof e=="object"?s={tableName:`${e.tableName}_backup`,schema:e.schema}:s=`${e}_backup`;const n=this.quoteTable(e),o=this.quoteTable(s),l=Object.keys(t).map(c=>r===c?`${this.quoteIdentifier(i)} AS ${this.quoteIdentifier(c)}`:this.quoteIdentifier(c)).join(", "),T=Object.keys(t).map(c=>this.quoteIdentifier(c)).join(", ");return`${this.createTableQuery(s,t)}INSERT INTO ${o} SELECT ${l} FROM ${n};DROP TABLE ${n};${this.createTableQuery(e,t)}INSERT INTO ${n} SELECT ${T} FROM ${o};DROP TABLE ${o};`}startTransactionQuery(e){return e.parent?`SAVEPOINT ${this.quoteIdentifier(e.name)};`:`BEGIN ${e.options.type} TRANSACTION;`}setIsolationLevelQuery(e){switch(e){case I.ISOLATION_LEVELS.REPEATABLE_READ:return"-- SQLite is not able to choose the isolation level REPEATABLE READ.";case I.ISOLATION_LEVELS.READ_UNCOMMITTED:return"PRAGMA read_uncommitted = ON;";case I.ISOLATION_LEVELS.READ_COMMITTED:return"PRAGMA read_uncommitted = OFF;";case I.ISOLATION_LEVELS.SERIALIZABLE:return"-- SQLite's default isolation level is SERIALIZABLE. Nothing to do.";default:throw new Error(`Unknown isolation level: ${e}`)}}replaceBooleanDefaults(e){return e.replace(/DEFAULT '?false'?/g,"DEFAULT 0").replace(/DEFAULT '?true'?/g,"DEFAULT 1")}getForeignKeysQuery(e){return`PRAGMA foreign_key_list(${this.quoteTable(this.addSchema(e))})`}}S(m,"SQLiteQueryGenerator"),module.exports=m;
  //# sourceMappingURL=query-generator.js.map
  