diff --git a/.gitignore b/.gitignore index a34ff48..a3c17fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dist node_modules -ltests \ No newline at end of file +ltests +sqlsave \ No newline at end of file diff --git a/src/db.ts b/src/db.ts index d954eb5..e83170d 100644 --- a/src/db.ts +++ b/src/db.ts @@ -1,7 +1,8 @@ import mariadb from 'mariadb'; +import { Datatype } from './dbStructure'; import { Handler } from './defaultHandler'; import { Query } from './query'; -import { onAction, primaryData } from './types'; +import { attributeSettings, onAction, primaryData } from './types'; export class DB { @@ -23,8 +24,15 @@ export class DB { export class Attribute { name: string; - constructor(name: string) { + ops: attributeSettings; + type: Datatype; + constructor(name: string, type: Datatype, ops: attributeSettings) { this.name = name; + this.ops = ops; + this.type = type; + } + serializeDatatype(){ + return this.type.serialize(); } serialize() { return this.toString(); @@ -48,19 +56,8 @@ export class Table { toString() { return this.dbLangTableName; } - addAttribute(name: string, ops: { - unique?: boolean, - A_I?: boolean, - default?: primaryData, - notNull?: boolean - primaryKey?: boolean, - foreginKey?: { - link: Attribute, - onDelete?: onAction, - onUpdate?: onAction - } - }, noErrorOnNameConflict = false) { - let attr = new Attribute(name); + addAttribute(name: string, type: Datatype, ops: attributeSettings, noErrorOnNameConflict = false) { + let attr = new Attribute(name,type,ops); this.dbLangTableAttributes[name] = attr; if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes"].includes(name)) { if (!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!"); diff --git a/src/dbStructure.ts b/src/dbStructure.ts index ac3f88b..4d63ebc 100644 --- a/src/dbStructure.ts +++ b/src/dbStructure.ts @@ -2,7 +2,17 @@ import { Attribute } from "./db"; import { Handler } from "./defaultHandler"; import { allModifierInput, primaryData, serializeReturn } from "./types"; - +export class Datatype{ + type: string; + args: primaryData[]; + constructor(type:string, args: primaryData[]){ + this.type = type; + this.args = args; + } + serialize(handler = Handler): serializeReturn{ + return handler.datatypes[this.type as keyof typeof handler.datatypes](this.args); + } +} export abstract class Modifier { t: string; diff --git a/src/defaultHandler.ts b/src/defaultHandler.ts index 11c3ff2..702f7b8 100644 --- a/src/defaultHandler.ts +++ b/src/defaultHandler.ts @@ -58,9 +58,65 @@ export class Handler { return ["not (?)", [e]]; } } + static datatypes = { + char: dataTypeSingleNum("char"), + varchar: dataTypeSingleNum("varchar"), + binary: dataTypeSingleNum("binary"), + varbinary: dataTypeSingleNum("varbinary"), + + tinyblob: dataTypeNoArg("tinyblob"), + blob: dataTypeNoArg("blob"), + mediumblob: dataTypeNoArg("mediumblob"), + longblob: dataTypeNoArg("longblob"), + tinytext: dataTypeNoArg("tinytext"), + text: dataTypeNoArg("text"), + mediumtext: dataTypeNoArg("mediumtext"), + longtext: dataTypeNoArg("longtext"), + + enum: (a: primaryData[]): serializeReturn =>{ + return ["enum("+a.map(()=>"?").join(", ")+")",a]; + }, + set: (a: primaryData[]): serializeReturn =>{ + return ["set("+a.map(()=>"?").join(", ")+")",a]; + }, + bool: dataTypeNoArg("bool"), + bit: dataTypeNoArg("bit"), + tinyint: dataTypeNoArg("tinyint"), + smallint: dataTypeNoArg("smallint"), + mediumint: dataTypeNoArg("mediumint"), + int: dataTypeNoArg("int"), + bigint: dataTypeNoArg("bigint"), + + float: dataTypeDblNum("float"), + double: dataTypeDblNum("double"), + decimal: dataTypeDblNum("decimal"), + + data: dataTypeNoArg("data"), + datatime: dataTypeNoArg("datatime"), + timestamp: dataTypeNoArg("timestamp"), + time: dataTypeNoArg("time"), + year: dataTypeNoArg("year"), + } }; +function dataTypeNoArg(type:string){ + return (a : primaryData[]): serializeReturn =>{ + return [type,[]]; + } +} +function dataTypeSingleNum(type:string){ + return (a : primaryData[]): serializeReturn =>{ + return [type+"(?)",[a[0]]]; + } +} +function dataTypeDblNum(type:string){ + return (a : primaryData[]): serializeReturn =>{ + return [type+"(?,?)",[a[0],a[1]]]; + } +} + + function joinArg(type: string, s: any) { return (a: (allModifierInput)[]): serializeReturn => { let args: primaryData[] = []; diff --git a/src/funcs.ts b/src/funcs.ts index bb61e1a..1c117cc 100644 --- a/src/funcs.ts +++ b/src/funcs.ts @@ -1,7 +1,7 @@ import { Attribute } from "./db"; -import { Aggregation, BooleanModifier, NumberModifier } from "./dbStructure"; +import { Aggregation, BooleanModifier, Datatype, NumberModifier } from "./dbStructure"; import { selectQuery } from "./query"; -import { allModifierInput, selectElements, selectFromElements } from "./types"; +import { allModifierInput, primaryData, selectElements, selectFromElements } from "./types"; //modifiers export const and = (...args: (BooleanModifier)[]) => new BooleanModifier("and", args); @@ -20,3 +20,41 @@ export const max = (a: Attribute) => new Aggregation("max", a); //query export const select = (args: selectElements[], from: selectFromElements) => new selectQuery(args, from); + +//datatypes + +export const CHAR = (size:number) => new Datatype("char",[size]); +export const VARCHAR = (size:number) => new Datatype("varchar",[size]); +export const BINARY = (size:number) => new Datatype("binary",[size]); +export const VARBINARY = (size:number) => new Datatype("varbinary",[size]); + +export const TINYBLOB = new Datatype("tinyblob",[]); +export const BLOB = new Datatype("blob",[]); +export const MEDIUMBLOB = new Datatype("mediumblob",[]); +export const LONGBLOB = new Datatype("longblob",[]); + +export const TINYTEXT = new Datatype("tinytext",[]); +export const TEXT = new Datatype("text",[]); +export const MEDIUMTEXT = new Datatype("mediumtext",[]); +export const LONGTEXT = new Datatype("longtext",[]); + +export const ENUM = (values:string[]) => new Datatype("enum",values); +export const SET = (values:string[]) => new Datatype("set",values); + +export const BOOL = new Datatype("bool",[]); +export const BIT = new Datatype("bit",[]); +export const TINYINT = new Datatype("tinyint",[]); +export const SMALLINT = new Datatype("smallint",[]); +export const MEDIUMINT = new Datatype("mediumint",[]); +export const INT = new Datatype("int",[]); +export const BIGINT = new Datatype("bigint",[]); + +export const FLOAT = (size:number,d:number) => new Datatype("float",[size,d]); +export const DOUBLE = (size:number,d:number) => new Datatype("double",[size,d]); +export const DECIMAL = (size:number,d:number) => new Datatype("decimal",[size,d]); + +export const DATA = new Datatype("data",[]); +export const DATATIME = new Datatype("datatime",[]); +export const TIMESTAMP = new Datatype("timestamp",[]); +export const TIME = new Datatype("time",[]); +export const YEAR = new Datatype("year",[]); \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index 24adafb..bab3058 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,6 +10,18 @@ export type selectFromElements = Table | Joins | null; export type serializeReturn = [string, primaryData[]]; +export type attributeSettings = { + unique?: boolean, + A_I?: boolean, + default?: primaryData, + notNull?: boolean + primaryKey?: boolean, + foreginKey?: { + link: Attribute, + onDelete?: onAction, + onUpdate?: onAction + } +}; export enum onAction { cascade,