First Working Version #1
6 changed files with 133 additions and 19 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
dist
|
dist
|
||||||
node_modules
|
node_modules
|
||||||
ltests
|
ltests
|
||||||
|
sqlsave
|
27
src/db.ts
27
src/db.ts
|
@ -1,7 +1,8 @@
|
||||||
import mariadb from 'mariadb';
|
import mariadb from 'mariadb';
|
||||||
|
import { Datatype } from './dbStructure';
|
||||||
import { Handler } from './defaultHandler';
|
import { Handler } from './defaultHandler';
|
||||||
import { Query } from './query';
|
import { Query } from './query';
|
||||||
import { onAction, primaryData } from './types';
|
import { attributeSettings, onAction, primaryData } from './types';
|
||||||
|
|
||||||
|
|
||||||
export class DB {
|
export class DB {
|
||||||
|
@ -23,8 +24,15 @@ export class DB {
|
||||||
|
|
||||||
export class Attribute {
|
export class Attribute {
|
||||||
name: string;
|
name: string;
|
||||||
constructor(name: string) {
|
ops: attributeSettings;
|
||||||
|
type: Datatype;
|
||||||
|
constructor(name: string, type: Datatype, ops: attributeSettings) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.ops = ops;
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
serializeDatatype(){
|
||||||
|
return this.type.serialize();
|
||||||
}
|
}
|
||||||
serialize() {
|
serialize() {
|
||||||
return this.toString();
|
return this.toString();
|
||||||
|
@ -48,19 +56,8 @@ export class Table {
|
||||||
toString() {
|
toString() {
|
||||||
return this.dbLangTableName;
|
return this.dbLangTableName;
|
||||||
}
|
}
|
||||||
addAttribute(name: string, ops: {
|
addAttribute(name: string, type: Datatype, ops: attributeSettings, noErrorOnNameConflict = false) {
|
||||||
unique?: boolean,
|
let attr = new Attribute(name,type,ops);
|
||||||
A_I?: boolean,
|
|
||||||
default?: primaryData,
|
|
||||||
notNull?: boolean
|
|
||||||
primaryKey?: boolean,
|
|
||||||
foreginKey?: {
|
|
||||||
link: Attribute,
|
|
||||||
onDelete?: onAction,
|
|
||||||
onUpdate?: onAction
|
|
||||||
}
|
|
||||||
}, noErrorOnNameConflict = false) {
|
|
||||||
let attr = new Attribute(name);
|
|
||||||
this.dbLangTableAttributes[name] = attr;
|
this.dbLangTableAttributes[name] = attr;
|
||||||
if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes"].includes(name)) {
|
if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes"].includes(name)) {
|
||||||
if (!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!");
|
if (!noErrorOnNameConflict) throw new Error("You cannot name Attribute like Methode of this Table!");
|
||||||
|
|
|
@ -2,7 +2,17 @@ import { Attribute } from "./db";
|
||||||
import { Handler } from "./defaultHandler";
|
import { Handler } from "./defaultHandler";
|
||||||
import { allModifierInput, primaryData, serializeReturn } from "./types";
|
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 {
|
export abstract class Modifier {
|
||||||
t: string;
|
t: string;
|
||||||
|
|
|
@ -58,9 +58,65 @@ export class Handler {
|
||||||
return ["not (?)", [e]];
|
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) {
|
function joinArg(type: string, s: any) {
|
||||||
return (a: (allModifierInput)[]): serializeReturn => {
|
return (a: (allModifierInput)[]): serializeReturn => {
|
||||||
let args: primaryData[] = [];
|
let args: primaryData[] = [];
|
||||||
|
|
42
src/funcs.ts
42
src/funcs.ts
|
@ -1,7 +1,7 @@
|
||||||
import { Attribute } from "./db";
|
import { Attribute } from "./db";
|
||||||
import { Aggregation, BooleanModifier, NumberModifier } from "./dbStructure";
|
import { Aggregation, BooleanModifier, Datatype, NumberModifier } from "./dbStructure";
|
||||||
import { selectQuery } from "./query";
|
import { selectQuery } from "./query";
|
||||||
import { allModifierInput, selectElements, selectFromElements } from "./types";
|
import { allModifierInput, primaryData, selectElements, selectFromElements } from "./types";
|
||||||
|
|
||||||
//modifiers
|
//modifiers
|
||||||
export const and = (...args: (BooleanModifier)[]) => new BooleanModifier("and", args);
|
export const and = (...args: (BooleanModifier)[]) => new BooleanModifier("and", args);
|
||||||
|
@ -20,3 +20,41 @@ export const max = (a: Attribute) => new Aggregation("max", a);
|
||||||
|
|
||||||
//query
|
//query
|
||||||
export const select = (args: selectElements[], from: selectFromElements) => new selectQuery(args, from);
|
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",[]);
|
12
src/types.ts
12
src/types.ts
|
@ -10,6 +10,18 @@ export type selectFromElements = Table | Joins | null;
|
||||||
|
|
||||||
export type serializeReturn = [string, primaryData[]];
|
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 {
|
export enum onAction {
|
||||||
cascade,
|
cascade,
|
||||||
|
|
Loading…
Reference in a new issue