Compare commits
65 commits
Author | SHA1 | Date | |
---|---|---|---|
3ed554f5ea | |||
25a38efdc6 | |||
575e9c2d4f | |||
f290b61f44 | |||
c2dc88062f | |||
0a1cb7c85f | |||
3a01244199 | |||
23ebf3c75a | |||
eed8d36c0b | |||
5d97a3211f | |||
56fb927c74 | |||
21b2bec2c4 | |||
1e396b5934 | |||
57544dffd2 | |||
c9f76a0799 | |||
d647fba5a9 | |||
dc7f68054d | |||
5810a834ab | |||
92678dc4b2 | |||
5222fa22af | |||
fc9718f9a1 | |||
8210af9c6b | |||
124c129c2b | |||
6e38f4ee13 | |||
f8aa601fcf | |||
d51ad10864 | |||
ea5954a9ff | |||
cbac881a49 | |||
53cb448e36 | |||
67b10d949a | |||
a6d368eb58 | |||
e446270591 | |||
6b121937f7 | |||
09d43f8f98 | |||
a501f86ef6 | |||
e78e90d83c | |||
871c4d6be7 | |||
85921d6493 | |||
3b163c66de | |||
0dde68f0ad | |||
24cdcd5c39 | |||
155e702863 | |||
f63c0cf6d0 | |||
cba4248727 | |||
c532ebcbb7 | |||
88321c76d1 | |||
123bbeba6c | |||
bf8b9e8f06 | |||
1f0ffbeba9 | |||
9317706181 | |||
9c350a5795 | |||
c150551975 | |||
747879058b | |||
79cec9047d | |||
3bed53bedd | |||
baff76e50d | |||
d342aab539 | |||
6aa6791699 | |||
039ae26773 | |||
2f94997f4a | |||
af4208ed3a | |||
5fd514332b | |||
287de2e09f | |||
2b6e4872d5 | |||
b5e68e6640 |
16 changed files with 778 additions and 195 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -2,3 +2,5 @@ dist
|
||||||
node_modules
|
node_modules
|
||||||
ltests
|
ltests
|
||||||
sqlsave
|
sqlsave
|
||||||
|
postgresHandler.ts
|
||||||
|
.vscode
|
|
@ -1,2 +1,3 @@
|
||||||
test.ts
|
test.ts
|
||||||
ltests
|
ltests
|
||||||
|
sqlsave
|
|
@ -10,9 +10,13 @@ pipeline:
|
||||||
- npm install git+https://jusax.de/git/jusax23/gitea-release.git
|
- npm install git+https://jusax.de/git/jusax23/gitea-release.git
|
||||||
- mkdir build
|
- mkdir build
|
||||||
- mkdir dblang
|
- mkdir dblang
|
||||||
|
- mkdir dblang/dist
|
||||||
- mkdir upload
|
- mkdir upload
|
||||||
- npm run prepublish
|
- npm run prepublish
|
||||||
- cp dist/* dblang
|
- cp dist/* dblang/dist
|
||||||
|
- cp package.json dblang
|
||||||
|
- cp package-lock.json dblang
|
||||||
|
- cp readme.md dblang
|
||||||
- zip -r upload/DBlang.zip dblang/*
|
- zip -r upload/DBlang.zip dblang/*
|
||||||
- tar -czvf upload/DBlang.tar.gz dblang/*
|
- tar -czvf upload/DBlang.tar.gz dblang/*
|
||||||
- npx gitea-release "https://jusax.de/git/api/v1/" "$${GITEA_TOKEN}" "$${CI_REPO}" "$${CI_COMMIT_BRANCH}" "$${CI_COMMIT_TAG}" "upload" "$${CI_COMMIT_MESSAGE}"
|
- npx gitea-release "https://jusax.de/git/api/v1/" "$${GITEA_TOKEN}" "$${CI_REPO}" "$${CI_COMMIT_BRANCH}" "$${CI_COMMIT_TAG}" "upload" "$${CI_COMMIT_MESSAGE}"
|
||||||
|
|
159
package-lock.json
generated
159
package-lock.json
generated
|
@ -1,23 +1,25 @@
|
||||||
{
|
{
|
||||||
"name": "dblang",
|
"name": "dblang",
|
||||||
"version": "1.0.0",
|
"version": "0.9.6",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "dblang",
|
"name": "dblang",
|
||||||
"version": "1.0.0",
|
"version": "0.9.6",
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"mariadb": "^3.0.2"
|
"mariadb": "^3.0.2",
|
||||||
|
"pg": "^8.9.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
|
"@types/pg": "^8.6.6",
|
||||||
"esbuild": "^0.17.4",
|
"esbuild": "^0.17.4",
|
||||||
"typescript": "^4.9.4"
|
"typescript": "^4.9.4"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 14"
|
"node": ">= 16"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@esbuild/android-arm": {
|
"node_modules/@esbuild/android-arm": {
|
||||||
|
@ -383,6 +385,25 @@
|
||||||
"integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==",
|
"integrity": "sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/pg": {
|
||||||
|
"version": "8.6.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/pg/-/pg-8.6.6.tgz",
|
||||||
|
"integrity": "sha512-O2xNmXebtwVekJDD+02udOncjVcMZQuTEQEMpKJ0ZRf5E7/9JJX3izhKUcUifBkyKpljyUM6BTgy2trmviKlpw==",
|
||||||
|
"dev": true,
|
||||||
|
"dependencies": {
|
||||||
|
"@types/node": "*",
|
||||||
|
"pg-protocol": "*",
|
||||||
|
"pg-types": "^2.2.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/buffer-writer": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz",
|
||||||
|
"integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/denque": {
|
"node_modules/denque": {
|
||||||
"version": "2.1.0",
|
"version": "2.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz",
|
||||||
|
@ -487,11 +508,133 @@
|
||||||
"node": "*"
|
"node": "*"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/packet-reader": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz",
|
||||||
|
"integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ=="
|
||||||
|
},
|
||||||
|
"node_modules/pg": {
|
||||||
|
"version": "8.9.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg/-/pg-8.9.0.tgz",
|
||||||
|
"integrity": "sha512-ZJM+qkEbtOHRuXjmvBtOgNOXOtLSbxiMiUVMgE4rV6Zwocy03RicCVvDXgx8l4Biwo8/qORUnEqn2fdQzV7KCg==",
|
||||||
|
"dependencies": {
|
||||||
|
"buffer-writer": "2.0.0",
|
||||||
|
"packet-reader": "1.0.0",
|
||||||
|
"pg-connection-string": "^2.5.0",
|
||||||
|
"pg-pool": "^3.5.2",
|
||||||
|
"pg-protocol": "^1.6.0",
|
||||||
|
"pg-types": "^2.1.0",
|
||||||
|
"pgpass": "1.x"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 8.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"pg-native": ">=3.0.1"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"pg-native": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/pg-connection-string": {
|
||||||
|
"version": "2.5.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.5.0.tgz",
|
||||||
|
"integrity": "sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ=="
|
||||||
|
},
|
||||||
|
"node_modules/pg-int8": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/pg-pool": {
|
||||||
|
"version": "3.5.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.5.2.tgz",
|
||||||
|
"integrity": "sha512-His3Fh17Z4eg7oANLob6ZvH8xIVen3phEZh2QuyrIl4dQSDVEabNducv6ysROKpDNPSD+12tONZVWfSgMvDD9w==",
|
||||||
|
"peerDependencies": {
|
||||||
|
"pg": ">=8.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/pg-protocol": {
|
||||||
|
"version": "1.6.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.6.0.tgz",
|
||||||
|
"integrity": "sha512-M+PDm637OY5WM307051+bsDia5Xej6d9IR4GwJse1qA1DIhiKlksvrneZOYQq42OM+spubpcNYEo2FcKQrDk+Q=="
|
||||||
|
},
|
||||||
|
"node_modules/pg-types": {
|
||||||
|
"version": "2.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz",
|
||||||
|
"integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==",
|
||||||
|
"dependencies": {
|
||||||
|
"pg-int8": "1.0.1",
|
||||||
|
"postgres-array": "~2.0.0",
|
||||||
|
"postgres-bytea": "~1.0.0",
|
||||||
|
"postgres-date": "~1.0.4",
|
||||||
|
"postgres-interval": "^1.1.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/pgpass": {
|
||||||
|
"version": "1.0.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.5.tgz",
|
||||||
|
"integrity": "sha512-FdW9r/jQZhSeohs1Z3sI1yxFQNFvMcnmfuj4WBMUTxOrAyLMaTcE1aAMBiTlbMNaXvBCQuVi0R7hd8udDSP7ug==",
|
||||||
|
"dependencies": {
|
||||||
|
"split2": "^4.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/postgres-array": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz",
|
||||||
|
"integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/postgres-bytea": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz",
|
||||||
|
"integrity": "sha512-xy3pmLuQqRBZBXDULy7KbaitYqLcmxigw14Q5sj8QBVLqEwXfeybIKVWiqAXTlcvdvb0+xkOtDbfQMOf4lST1w==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/postgres-date": {
|
||||||
|
"version": "1.0.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz",
|
||||||
|
"integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/postgres-interval": {
|
||||||
|
"version": "1.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz",
|
||||||
|
"integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==",
|
||||||
|
"dependencies": {
|
||||||
|
"xtend": "^4.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/safer-buffer": {
|
"node_modules/safer-buffer": {
|
||||||
"version": "2.1.2",
|
"version": "2.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
|
||||||
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
|
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
|
||||||
},
|
},
|
||||||
|
"node_modules/split2": {
|
||||||
|
"version": "4.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/split2/-/split2-4.1.0.tgz",
|
||||||
|
"integrity": "sha512-VBiJxFkxiXRlUIeyMQi8s4hgvKCSjtknJv/LVYbrgALPwf5zSKmEwV9Lst25AkvMDnvxODugjdl6KZgwKM1WYQ==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 10.x"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/typescript": {
|
"node_modules/typescript": {
|
||||||
"version": "4.9.4",
|
"version": "4.9.4",
|
||||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz",
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.4.tgz",
|
||||||
|
@ -504,6 +647,14 @@
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=4.2.0"
|
"node": ">=4.2.0"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"node_modules/xtend": {
|
||||||
|
"version": "4.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz",
|
||||||
|
"integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.4"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
package.json
14
package.json
|
@ -1,15 +1,15 @@
|
||||||
{
|
{
|
||||||
"name": "dblang",
|
"name": "dblang",
|
||||||
"version": "1.0.0",
|
"version": "0.9.6",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/db.js",
|
"main": "dist/db.js",
|
||||||
"types": "dist/db.d.ts",
|
"types": "dist/db.d.ts",
|
||||||
"type": "commonjs",
|
"type": "module",
|
||||||
"directories": {
|
"directories": {
|
||||||
"dsit": "dist"
|
"dist": "dist"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist/*"
|
||||||
],
|
],
|
||||||
"private": false,
|
"private": false,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -18,13 +18,15 @@
|
||||||
"author": "jusax23",
|
"author": "jusax23",
|
||||||
"license": "UNLICENSED",
|
"license": "UNLICENSED",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"mariadb": "^3.0.2"
|
"mariadb": "^3.0.2",
|
||||||
|
"pg": "^8.9.0"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 14"
|
"node": ">= 16"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.11.18",
|
||||||
|
"@types/pg": "^8.6.6",
|
||||||
"esbuild": "^0.17.4",
|
"esbuild": "^0.17.4",
|
||||||
"typescript": "^4.9.4"
|
"typescript": "^4.9.4"
|
||||||
}
|
}
|
||||||
|
|
31
readme.md
31
readme.md
|
@ -12,6 +12,9 @@ Features:
|
||||||
- [x] create Schema
|
- [x] create Schema
|
||||||
- [x] remove unused Schema
|
- [x] remove unused Schema
|
||||||
- [x] joins
|
- [x] joins
|
||||||
|
- [x] table alias
|
||||||
|
- [x] general aliases
|
||||||
|
- [x] unified response
|
||||||
- [ ] change Schema
|
- [ ] change Schema
|
||||||
- [ ] Key-Value Store Shortcut
|
- [ ] Key-Value Store Shortcut
|
||||||
- [ ] Views
|
- [ ] Views
|
||||||
|
@ -27,7 +30,8 @@ Supported Databses:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
import { DB } from "dblang";
|
import { DB } from "dblang";
|
||||||
const db = new DB({
|
const db = new DB();
|
||||||
|
db.connect({
|
||||||
host: "localhost",
|
host: "localhost",
|
||||||
user: "root",
|
user: "root",
|
||||||
password: "0123456789",
|
password: "0123456789",
|
||||||
|
@ -103,7 +107,7 @@ let res = await select([Table2.AttrName2], Table1)
|
||||||
.query(db);
|
.query(db);
|
||||||
```
|
```
|
||||||
|
|
||||||
Joins:
|
_Joins_:
|
||||||
```javascript
|
```javascript
|
||||||
import { INT, onAction } from "dblang"
|
import { INT, onAction } from "dblang"
|
||||||
|
|
||||||
|
@ -124,6 +128,26 @@ let res = await select([TableA.A1, TableB.B1], innerJoinOn(eq(TableA.A1, Tableb.
|
||||||
```
|
```
|
||||||
See: [Joins](#joins)
|
See: [Joins](#joins)
|
||||||
|
|
||||||
|
_Aliases_:
|
||||||
|
```javascript
|
||||||
|
const alias1 = TableA.createAlias("alias1");
|
||||||
|
//alias1 can be used like a Table in select Statements.
|
||||||
|
const alias2 = alias(avg(TableA.A1), "someAvg");
|
||||||
|
```
|
||||||
|
|
||||||
|
Get requestet Data:
|
||||||
|
```javascript
|
||||||
|
let response = await select([TableA.A1, TableB.B1], innerJoinOn(eq(TableA.A1, Tableb.B1)))
|
||||||
|
.query(db);
|
||||||
|
|
||||||
|
response[0][TableA.A1];
|
||||||
|
response[0][TableB.B1];
|
||||||
|
|
||||||
|
response[i][TableA.A1];
|
||||||
|
response[i][TableB.B1]; // i < repsonse.length
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
#### Insert:
|
#### Insert:
|
||||||
```javascript
|
```javascript
|
||||||
import { select } from "dblang"
|
import { select } from "dblang"
|
||||||
|
@ -217,6 +241,7 @@ BooleanModifier:
|
||||||
- `le(v1, v2, ...)`
|
- `le(v1, v2, ...)`
|
||||||
- `not(v1)`
|
- `not(v1)`
|
||||||
- `like(v1, v2)`
|
- `like(v1, v2)`
|
||||||
|
- `exists(s)`
|
||||||
|
|
||||||
NumberModifier:
|
NumberModifier:
|
||||||
- `plus(v1, v2, ...)`
|
- `plus(v1, v2, ...)`
|
||||||
|
@ -227,7 +252,7 @@ NumberModifier:
|
||||||
StringModifier:
|
StringModifier:
|
||||||
- `concat(v1, v2, ...)`
|
- `concat(v1, v2, ...)`
|
||||||
|
|
||||||
(v\* = string, number, boolean, null, Modifier, Aggregation, select Query, Attribute)
|
(v\* = string, number, boolean, null, Modifier, Aggregation, select Query, Attribute; s = selectQuery)
|
||||||
|
|
||||||
#### Joins
|
#### Joins
|
||||||
|
|
||||||
|
|
90
src/alias.ts
Normal file
90
src/alias.ts
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
import { Attribute, DB, Table } from "./db.js"
|
||||||
|
import { Aggregation, Modifier } from "./dbStructure.js";
|
||||||
|
import { Handler } from "./defaultHandler.js";
|
||||||
|
import { QueryBuilder, selectQuery } from "./query.js";
|
||||||
|
import { selectElements } from "./types.js";
|
||||||
|
|
||||||
|
export class AttributeAlias {
|
||||||
|
name: string;
|
||||||
|
nameO: string;
|
||||||
|
table: TableAlias;
|
||||||
|
attr: Attribute;
|
||||||
|
constructor(attr: Attribute, table: TableAlias) {
|
||||||
|
this.name = attr.name;
|
||||||
|
this.nameO = attr.nameO;
|
||||||
|
this.attr = attr;
|
||||||
|
this.table = table;
|
||||||
|
}
|
||||||
|
serialize(handler: Handler) {
|
||||||
|
return handler.builders.escapeID(this.name);
|
||||||
|
}
|
||||||
|
getString(handler: Handler = this.table.dbLangDatabaseInstance.getHandler()) {
|
||||||
|
return this.table.serialize(handler) + "." + this.serialize(handler);
|
||||||
|
}
|
||||||
|
getStringFunc(handler: Handler) {
|
||||||
|
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
||||||
|
}
|
||||||
|
toString() {
|
||||||
|
return this.table.dbLangTableName + "_" + this.name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TableAlias {
|
||||||
|
dbLangTableName: string;
|
||||||
|
dbLangTableAttributes: { [key: string]: AttributeAlias; } = {};
|
||||||
|
dbLangDatabaseInstance: DB;
|
||||||
|
dbLangTableInstance: Table;
|
||||||
|
[key: string]: AttributeAlias | any;
|
||||||
|
constructor(name: string, table: Table) {
|
||||||
|
this.dbLangTableName = name;
|
||||||
|
this.dbLangTableInstance = table;
|
||||||
|
this.dbLangDatabaseInstance = table.dbLangDatabaseInstance;
|
||||||
|
let keys = Object.keys(table.dbLangTableAttributes);
|
||||||
|
for (let i = 0; i < keys.length; i++) {
|
||||||
|
const a = table.dbLangTableAttributes[keys[i]];
|
||||||
|
let attrAlias = new AttributeAlias(a, this);
|
||||||
|
this[a.name] = attrAlias;
|
||||||
|
this[a.nameO] = attrAlias;
|
||||||
|
this.dbLangTableAttributes[a.name] = attrAlias;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
serializeAlias(hander: Handler) {
|
||||||
|
return this.dbLangTableInstance.serialize(hander) + " " + this.toString(hander);
|
||||||
|
}
|
||||||
|
serialize(handler: Handler) {
|
||||||
|
return this.toString(handler);
|
||||||
|
}
|
||||||
|
toString(handler: Handler = this.dbLangDatabaseInstance.getHandler()) {
|
||||||
|
return handler.builders.escapeID(this.dbLangTableName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Alias {
|
||||||
|
alias: string;
|
||||||
|
a: selectElements;
|
||||||
|
|
||||||
|
constructor(a: selectElements, alias: string) {
|
||||||
|
this.alias = alias;
|
||||||
|
this.a = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
serialize(handler: Handler) {
|
||||||
|
let builder = new QueryBuilder();
|
||||||
|
if (this.a instanceof Attribute || this.a instanceof AttributeAlias)
|
||||||
|
builder.addCode(this.a.getString(handler) + " " + handler.builders.escapeID(this.a.toString()));
|
||||||
|
else if (this.a instanceof Modifier || this.a instanceof selectQuery || this.a instanceof Aggregation || this.a instanceof Alias) {
|
||||||
|
builder.addCode("(");
|
||||||
|
builder.append(this.a.serialize(handler));
|
||||||
|
builder.addCode(")");
|
||||||
|
} else {
|
||||||
|
builder.addInjection(this.a);
|
||||||
|
}
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
getString(handler: Handler) {
|
||||||
|
return this.serialize(handler);
|
||||||
|
}
|
||||||
|
toString() {
|
||||||
|
return this.alias;
|
||||||
|
}
|
||||||
|
}
|
101
src/db.ts
101
src/db.ts
|
@ -1,34 +1,63 @@
|
||||||
import mariadb from 'mariadb';
|
import mariadb from 'mariadb';
|
||||||
import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure';
|
import { checkConstraint, Constraint, Datatype, foreignConstraint, uniqueConstraint } from './dbStructure.js';
|
||||||
import { Handler } from './defaultHandler';
|
import { Handler } from './defaultHandler.js';
|
||||||
import { Query } from './query';
|
import { Query } from './query.js';
|
||||||
import { attributeSettings, extendedAttributeSettings, onAction } from './types';
|
import { attributeSettings, extendedAttributeSettings, onAction, dbType, order } from './types.js';
|
||||||
|
import { TableAlias } from "./alias.js"
|
||||||
|
import { insertResponse, readResponse, writeResponse, singleResponse } from "./responses.js"
|
||||||
|
//import { postgresHandler } from "./postgresHandler"
|
||||||
|
|
||||||
|
declare module "mariadb" {
|
||||||
|
interface Pool{
|
||||||
|
on(ev: 'error', callback: () => void): Pool;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class DB {
|
export class DB {
|
||||||
tables: Table[] = [];
|
tables: Table[] = [];
|
||||||
handler: Handler;
|
handler!: Handler;
|
||||||
name: string;
|
name!: string;
|
||||||
pool: mariadb.Pool;
|
type!: dbType;
|
||||||
constructor({ host, user, password, database, connectionLimit = 5 }: { host: string, user: string, password: string, database: string, connectionLimit: number }) {
|
mariaPool!: mariadb.Pool;
|
||||||
this.pool = mariadb.createPool({ host, user, password, database, connectionLimit, multipleStatements: true });
|
private connected = false;
|
||||||
this.handler = new Handler();
|
//pgPool!: pg.Pool;
|
||||||
|
connect({ host, user, password, database, port = 3306, connectionLimit = 5, databaseType = dbType.mariadb }: { host: string, user: string, password: string, database: string, port?: number, connectionLimit?: number, databaseType?: dbType }) {
|
||||||
|
this.connected = true;
|
||||||
|
this.type = databaseType;
|
||||||
|
if (databaseType == dbType.mariadb) {
|
||||||
|
this.mariaPool = mariadb.createPool({ host, user, password, port, database, connectionLimit, multipleStatements: true });
|
||||||
|
// catch errors
|
||||||
|
this.mariaPool.on("error",()=>{});
|
||||||
|
this.handler = new Handler(this);
|
||||||
|
}
|
||||||
|
/*else if (databaseType == dbType.postgres) {
|
||||||
|
this.pgPool = new pg.Pool({ host, user, password, max: connectionLimit, database, idleTimeoutMillis: 30000, connectionTimeoutMillis: 2000 });
|
||||||
|
this.handler = new postgresHandler(this);
|
||||||
|
}*/
|
||||||
|
else throw new Error("Unsuported Database type!");
|
||||||
|
|
||||||
this.name = database;
|
this.name = database;
|
||||||
}
|
}
|
||||||
async query(query: Query) {
|
async query(query: Query, printQuery = false) {
|
||||||
//console.log(query);
|
if (!this.connected) throw new Error("Not connected yet!");
|
||||||
return await this.pool.query(query);
|
if (printQuery) console.log(query);
|
||||||
|
if (this.type == dbType.mariadb) return await this.mariaPool.query(query);
|
||||||
|
/*else if (this.type == dbType.postgres) {
|
||||||
|
let res = await this.pgPool.query(query.sql, query.values);
|
||||||
|
console.log([...res.rows]);
|
||||||
|
return res.rows;
|
||||||
|
}*/
|
||||||
|
else throw new Error("Invalid Database Type!");
|
||||||
}
|
}
|
||||||
getHandler() {
|
getHandler() {
|
||||||
|
if (!this.connected) throw new Error("Not connected yet!");
|
||||||
return this.handler;
|
return this.handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sync(force = false) {
|
async sync(force = false) {
|
||||||
|
if (!this.connected) throw new Error("Not connected yet!");
|
||||||
let handler = this.getHandler();
|
let handler = this.getHandler();
|
||||||
await handler.syncDB(this, handler, force);
|
await handler.syncDB(this, handler, force);
|
||||||
//this.tables.forEach(t=>{
|
|
||||||
// console.log(handler.builders.query(handler.querys.create(handler,t)));
|
|
||||||
//})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
newTable(name: string) {
|
newTable(name: string) {
|
||||||
|
@ -42,17 +71,22 @@ export class DB {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
async close() {
|
async close() {
|
||||||
if (this.pool) await this.pool.end();
|
if (!this.connected) throw new Error("Not connected yet!");
|
||||||
|
if (this.type == dbType.mariadb && this.mariaPool) await this.mariaPool.end();
|
||||||
|
//if (this.type == dbType.postgres && this.pgPool) await this.pgPool.end();
|
||||||
|
this.connected = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Attribute {
|
export class Attribute {
|
||||||
name: string;
|
name: string;
|
||||||
|
nameO: string;
|
||||||
table: Table;
|
table: Table;
|
||||||
ops: attributeSettings;
|
ops: attributeSettings;
|
||||||
type: Datatype;
|
type: Datatype;
|
||||||
constructor(name: string, table: Table, type: Datatype, ops: attributeSettings) {
|
constructor(name: string, nameO: string, table: Table, type: Datatype, ops: attributeSettings) {
|
||||||
this.name = name.toLowerCase();
|
this.name = name.toLowerCase();
|
||||||
|
this.nameO = nameO;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.table = table;
|
this.table = table;
|
||||||
this.ops = ops;
|
this.ops = ops;
|
||||||
|
@ -70,13 +104,13 @@ export class Attribute {
|
||||||
[this]
|
[this]
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
if (ops.foreginKey != null) {
|
if (ops.foreignKey != null) {
|
||||||
table.addConstraint(new foreignConstraint(
|
table.addConstraint(new foreignConstraint(
|
||||||
table.dbLangDatabaseInstance.name + "_" + table.dbLangTableName + "_" + name + "_foreign_constraint_to_" + ops.foreginKey.link.name,
|
table.dbLangDatabaseInstance.name + "_" + table.dbLangTableName + "_" + name + "_foreign_constraint_to_" + ops.foreignKey.link.name,
|
||||||
[this],
|
[this],
|
||||||
[ops.foreginKey.link],
|
[ops.foreignKey.link],
|
||||||
ops.foreginKey.onDelete,
|
ops.foreignKey.onDelete,
|
||||||
ops.foreginKey.onUpdate
|
ops.foreignKey.onUpdate
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,12 +123,15 @@ export class Attribute {
|
||||||
serialize(handler: Handler) {
|
serialize(handler: Handler) {
|
||||||
return handler.builders.escapeID(this.name);
|
return handler.builders.escapeID(this.name);
|
||||||
}
|
}
|
||||||
toString(handler: Handler = this.table.dbLangDatabaseInstance.getHandler()) {
|
getString(handler: Handler = this.table.dbLangDatabaseInstance.getHandler()) {
|
||||||
return this.table.serialize(handler) + "." + this.serialize(handler);
|
return this.table.serialize(handler) + "." + this.serialize(handler);
|
||||||
}
|
}
|
||||||
toStringFunc(handler: Handler) {
|
toStringFunc(handler: Handler) {
|
||||||
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
return this.table.serialize(handler) + "(" + this.serialize(handler) + ")";
|
||||||
}
|
}
|
||||||
|
toString() {
|
||||||
|
return this.table.dbLangTableName + "_" + this.name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -117,9 +154,9 @@ export class Table {
|
||||||
addAttribute(name: string, type: Datatype, ops: attributeSettings = {}, noErrorOnNameConflict = false) {
|
addAttribute(name: string, type: Datatype, ops: attributeSettings = {}, noErrorOnNameConflict = false) {
|
||||||
let lowName = name.toLowerCase();
|
let lowName = name.toLowerCase();
|
||||||
if (this.dbLangTableAttributes[lowName] != null) throw new Error("You are tring to create an Attribute twise!");
|
if (this.dbLangTableAttributes[lowName] != null) throw new Error("You are tring to create an Attribute twise!");
|
||||||
let attr = new Attribute(lowName, this, type, ops);
|
let attr = new Attribute(lowName, name, this, type, ops);
|
||||||
this.dbLangTableAttributes[lowName] = attr;
|
this.dbLangTableAttributes[lowName] = attr;
|
||||||
if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes", "dbLangDatabaseInstance", "addConstraint", "addAttributes"].includes(lowName)) {
|
if (["serialize", "toString", "addAttribute", "dbLangTableName", "dbLangTableAttributes", "dbLangDatabaseInstance", "addConstraint", "addAttributes", "createAlias"].includes(lowName)) {
|
||||||
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!");
|
||||||
} else {
|
} else {
|
||||||
this[lowName] = attr;
|
this[lowName] = attr;
|
||||||
|
@ -133,10 +170,16 @@ export class Table {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
addConstraint(c: Constraint) {
|
addConstraint(c: Constraint) {
|
||||||
c.check(this);
|
let check = c.check(this);
|
||||||
|
if(typeof check == "string") throw new Error(check);
|
||||||
this.dbLangConstrains.push(c);
|
this.dbLangConstrains.push(c);
|
||||||
}
|
}
|
||||||
|
createAlias(name: string) {
|
||||||
|
return new TableAlias(name, this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export * from './funcs';
|
export * from './funcs.js';
|
||||||
export { onAction };
|
export { onAction, order };
|
||||||
|
export { dbType as databaseType }
|
||||||
|
export { readResponse, writeResponse, insertResponse, singleResponse }
|
|
@ -1,7 +1,8 @@
|
||||||
import { Attribute, Table } from "./db";
|
import { Attribute, Table } from "./db.js";
|
||||||
import { Handler } from "./defaultHandler";
|
import { Handler } from "./defaultHandler.js";
|
||||||
import { QueryBuilder } from "./query";
|
import { QueryBuilder } from "./query.js";
|
||||||
import { allModifierInput, joinElements, joinType, onAction, primaryData, serializeReturn } from "./types";
|
import { sha256 } from "./tools.js";
|
||||||
|
import { allModifierInput, joinElements, joinType, onAction, primaryData, serializeReturn } from "./types.js";
|
||||||
|
|
||||||
export class Datatype {
|
export class Datatype {
|
||||||
type: string;
|
type: string;
|
||||||
|
@ -102,7 +103,6 @@ export class joinCross implements Joins{
|
||||||
|
|
||||||
export interface Constraint {
|
export interface Constraint {
|
||||||
name: string;
|
name: string;
|
||||||
serialize(handler: Handler): QueryBuilder;
|
|
||||||
uses(attr: Attribute): boolean;
|
uses(attr: Attribute): boolean;
|
||||||
check(table: Table): boolean | string;
|
check(table: Table): boolean | string;
|
||||||
}
|
}
|
||||||
|
@ -111,6 +111,7 @@ export class checkConstraint implements Constraint {
|
||||||
checkQuery: BooleanModifier;
|
checkQuery: BooleanModifier;
|
||||||
name: string;
|
name: string;
|
||||||
constructor(name: string, check: BooleanModifier) {
|
constructor(name: string, check: BooleanModifier) {
|
||||||
|
if (name.length > 64) name = sha256(name);
|
||||||
this.name = name.toLowerCase();
|
this.name = name.toLowerCase();
|
||||||
this.checkQuery = check;
|
this.checkQuery = check;
|
||||||
}
|
}
|
||||||
|
@ -120,32 +121,28 @@ export class checkConstraint implements Constraint {
|
||||||
uses(attr: Attribute): boolean {
|
uses(attr: Attribute): boolean {
|
||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
serialize(handler: Handler): QueryBuilder {
|
|
||||||
throw new Error("Method not implemented.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class uniqueConstraint implements Constraint {
|
export class uniqueConstraint implements Constraint {
|
||||||
name: string;
|
name: string;
|
||||||
attrs: Attribute[];
|
attrs: Attribute[];
|
||||||
constructor(name: string, attrs: Attribute[]) {
|
constructor(name: string, attrs: Attribute[]) {
|
||||||
|
if (name.length > 64) name = sha256(name);
|
||||||
this.name = name.toLowerCase();
|
this.name = name.toLowerCase();
|
||||||
this.attrs = attrs;
|
this.attrs = attrs;
|
||||||
}
|
}
|
||||||
check(table: Table): boolean | string {
|
check(table: Table): boolean | string {
|
||||||
|
let prim = true;
|
||||||
for (let i = 0; i < this.attrs.length; i++) {
|
for (let i = 0; i < this.attrs.length; i++) {
|
||||||
if (this.attrs[i].ops.primaryKey) return "Can not combine unique Constraint and primary key";
|
if (!this.attrs[i].ops.primaryKey) prim = false;
|
||||||
if (this.attrs[i].table != table) return "Referencing Attributes must be in host Table.";
|
if (this.attrs[i].table != table) return "Referencing Attributes must be in host Table.";
|
||||||
}
|
}
|
||||||
|
if (prim) return "Can not combine unique Constraint and primary key";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
uses(attr: Attribute): boolean {
|
uses(attr: Attribute): boolean {
|
||||||
return this.attrs.includes(attr);
|
return this.attrs.includes(attr);
|
||||||
}
|
}
|
||||||
serialize(handler: Handler): QueryBuilder {
|
|
||||||
throw new Error("Method not implemented.");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class foreignConstraint implements Constraint {
|
export class foreignConstraint implements Constraint {
|
||||||
|
@ -155,15 +152,13 @@ export class foreignConstraint implements Constraint {
|
||||||
onUpdate: onAction;
|
onUpdate: onAction;
|
||||||
onDelete: onAction;
|
onDelete: onAction;
|
||||||
constructor(name: string, from: Attribute[], to: Attribute[], onDelete: onAction = onAction.nothing, onUpdate: onAction = onAction.nothing) {
|
constructor(name: string, from: Attribute[], to: Attribute[], onDelete: onAction = onAction.nothing, onUpdate: onAction = onAction.nothing) {
|
||||||
|
if (name.length > 64) name = sha256(name);
|
||||||
this.name = name.toLowerCase();
|
this.name = name.toLowerCase();
|
||||||
this.fromAttrs = from;
|
this.fromAttrs = from;
|
||||||
this.toAttrs = to;
|
this.toAttrs = to;
|
||||||
this.onUpdate = onUpdate;
|
this.onUpdate = onUpdate;
|
||||||
this.onDelete = onDelete;
|
this.onDelete = onDelete;
|
||||||
}
|
}
|
||||||
serialize(handler: Handler): QueryBuilder {
|
|
||||||
throw new Error("Method not implemented.");
|
|
||||||
}
|
|
||||||
uses(attr: Attribute): boolean {
|
uses(attr: Attribute): boolean {
|
||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
|
@ -171,7 +166,7 @@ export class foreignConstraint implements Constraint {
|
||||||
let table = this.toAttrs[0].table;
|
let table = this.toAttrs[0].table;
|
||||||
for (let i = 0; i < this.toAttrs.length; i++) {
|
for (let i = 0; i < this.toAttrs.length; i++) {
|
||||||
if (table != this.toAttrs[i].table) return "Referenced Attributes must be in one Table.";
|
if (table != this.toAttrs[i].table) return "Referenced Attributes must be in one Table.";
|
||||||
if (this.toAttrs[i].ops.primaryKey) return "Can not reference non primary keys.";
|
if (!this.toAttrs[i].ops.primaryKey) return "Can not reference non primary keys.";
|
||||||
}
|
}
|
||||||
for (let i = 0; i < this.fromAttrs.length; i++) {
|
for (let i = 0; i < this.fromAttrs.length; i++) {
|
||||||
if (this.fromAttrs[i].table != t) return "Referencing Attributes must be in host Table.";
|
if (this.fromAttrs[i].table != t) return "Referencing Attributes must be in host Table.";
|
||||||
|
|
|
@ -1,25 +1,22 @@
|
||||||
import { Attribute, DB, Table } from "./db"
|
import { Alias, AttributeAlias, TableAlias } from "./alias.js";
|
||||||
import { Aggregation, checkConstraint, joinCross, Datatype, foreignConstraint, Modifier, joinNatural, onJoin, uniqueConstraint, usingJoin } from "./dbStructure"
|
import { Attribute, DB, insertResponse, readResponse, Table, writeResponse } from "./db.js"
|
||||||
import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query"
|
import { Aggregation, checkConstraint, joinCross, Datatype, foreignConstraint, Modifier, joinNatural, onJoin, uniqueConstraint, usingJoin } from "./dbStructure.js"
|
||||||
import { allModifierInput, joinType, onAction, primaryData } from "./types"
|
import { insertQuery, Query, QueryBuilder, removeQuery, selectQuery, updateQuery } from "./query.js"
|
||||||
|
import { allModifierInput, joinType, onAction, order, primaryData } from "./types.js"
|
||||||
|
import { sha256 } from "./tools.js"
|
||||||
|
|
||||||
export class Handler {
|
export class Handler {
|
||||||
|
db: DB;
|
||||||
|
constructor(db: DB) {
|
||||||
|
this.db = db;
|
||||||
|
}
|
||||||
async syncDB(db: DB, handler: Handler, deleteInDB: boolean = false) {
|
async syncDB(db: DB, handler: Handler, deleteInDB: boolean = false) {
|
||||||
let gd = new QueryBuilder();
|
//Constraint Data
|
||||||
gd.addCode(`SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE where CONSTRAINT_SCHEMA = `);
|
let constraints = await db.query(handler.builders.query(handler.querys.listConstraints(handler, db)));
|
||||||
gd.addInjection(db.name);
|
let key = [...await db.query(handler.builders.query(handler.querys.listConstraintDetails(handler, db)))];
|
||||||
gd.addCode(` or TABLE_SCHEMA = `);
|
|
||||||
gd.addInjection(db.name);
|
|
||||||
gd.addCode(`;select * from information_schema.table_constraints where CONSTRAINT_SCHEMA = `);
|
|
||||||
gd.addInjection(db.name)
|
|
||||||
gd.addCode(` or TABLE_SCHEMA = `);
|
|
||||||
gd.addInjection(db.name);
|
|
||||||
|
|
||||||
let [key, constraints] = await db.query(handler.builders.query(gd));
|
|
||||||
|
|
||||||
//Table List
|
//Table List
|
||||||
let allTables = (await db.query(handler.builders.query(handler.querys.listTables(handler, db)))).map((d: any) => d.TABLE_NAME);
|
let allTables = (await db.query(handler.builders.query(handler.querys.listTables(handler, db)))).map((d: any) => d.TABLE_NAME ?? d.table_name);
|
||||||
|
|
||||||
// gether Table Schematics
|
// gether Table Schematics
|
||||||
let tableData: { [key: string]: any; } = {};
|
let tableData: { [key: string]: any; } = {};
|
||||||
for (let i = 0; i < db.tables.length; i++) {
|
for (let i = 0; i < db.tables.length; i++) {
|
||||||
|
@ -28,15 +25,16 @@ export class Handler {
|
||||||
const tableDataBuilder = new QueryBuilder();
|
const tableDataBuilder = new QueryBuilder();
|
||||||
tableDataBuilder.addCode("DESCRIBE ");
|
tableDataBuilder.addCode("DESCRIBE ");
|
||||||
tableDataBuilder.addCode(table.serialize(handler));
|
tableDataBuilder.addCode(table.serialize(handler));
|
||||||
tableData[table.dbLangTableName.toLowerCase()] =
|
tableData[table.dbLangTableName] =
|
||||||
Object.fromEntries((await db.query(handler.builders.query(tableDataBuilder))).map((d: any) => [d.Field.toLowerCase(), d]));
|
Object.fromEntries((await db.query(handler.builders.query(tableDataBuilder))).map((d: any) => [d.Field.toLowerCase(), d]));
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
tableData[table.dbLangTableName.toLowerCase()] = null;
|
tableData[table.dbLangTableName] = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const del = new QueryBuilder();
|
const del = new QueryBuilder();
|
||||||
const create = new QueryBuilder();
|
const create = new QueryBuilder();
|
||||||
|
const connst = new QueryBuilder();
|
||||||
|
|
||||||
function checkUniqueIsVaild(n: string, table: string) {
|
function checkUniqueIsVaild(n: string, table: string) {
|
||||||
let t = db.getTable(table);
|
let t = db.getTable(table);
|
||||||
|
@ -48,10 +46,10 @@ export class Handler {
|
||||||
let found = 0;
|
let found = 0;
|
||||||
for (let j = 0; j < key.length; j++) {
|
for (let j = 0; j < key.length; j++) {
|
||||||
if (
|
if (
|
||||||
key[j].CONSTRAINT_NAME == n
|
(key[j].CONSTRAINT_NAME ?? key[j].constraint_name) == n
|
||||||
&& key[j].TABLE_NAME == table
|
&& (key[j].TABLE_NAME ?? key[j].table_name) == table
|
||||||
) {
|
) {
|
||||||
if (attrs.includes(key[j].COLUMN_NAME.toLowerCase())) found++;
|
if (attrs.includes((key[j].COLUMN_NAME ?? key[j].column_name).toLowerCase())) found++;
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,12 +71,12 @@ export class Handler {
|
||||||
let found = 0;
|
let found = 0;
|
||||||
for (let j = 0; j < key.length; j++) {
|
for (let j = 0; j < key.length; j++) {
|
||||||
if (
|
if (
|
||||||
key[j].CONSTRAINT_NAME == n
|
(key[j].CONSTRAINT_NAME ?? key[j].constraint_name) == n
|
||||||
&& key[j].TABLE_NAME == table
|
&& (key[j].TABLE_NAME ?? key[j].table_name) == table
|
||||||
&& key[j].REFERENCED_TABLE_NAME == refTable
|
&& (key[j].REFERENCED_TABLE_NAME ?? key[j].referenced_table_name) == refTable
|
||||||
) {
|
) {
|
||||||
let inF = fromAttrs.indexOf(key[j].COLUMN_NAME.toLowerCase());
|
let inF = fromAttrs.indexOf((key[j].COLUMN_NAME ?? key[j].column_name).toLowerCase());
|
||||||
let inT = toAttrs.indexOf(key[j].REFERENCED_COLUMN_NAME.toLowerCase());
|
let inT = toAttrs.indexOf((key[j].REFERENCED_COLUMN_NAME ?? key[j].referenced_column_name).toLowerCase());
|
||||||
if (inF != -1 && inT == inF) found++;
|
if (inF != -1 && inT == inF) found++;
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
@ -94,10 +92,10 @@ export class Handler {
|
||||||
let found = 0;
|
let found = 0;
|
||||||
for (let j = 0; j < key.length; j++) {
|
for (let j = 0; j < key.length; j++) {
|
||||||
if (
|
if (
|
||||||
key[j].CONSTRAINT_NAME.toLowerCase() == c.name
|
(key[j].CONSTRAINT_NAME ?? key[j].constraint_name).toLowerCase() == c.name
|
||||||
&& key[j].TABLE_NAME == tabel.dbLangTableName
|
&& (key[j].TABLE_NAME ?? key[j].table_name) == tabel.dbLangTableName
|
||||||
) {
|
) {
|
||||||
if (attrs.includes(key[j].COLUMN_NAME.toLowerCase())) found++;
|
if (attrs.includes((key[j].COLUMN_NAME ?? key[j].column_name).toLowerCase())) found++;
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,12 +108,12 @@ export class Handler {
|
||||||
let found = 0;
|
let found = 0;
|
||||||
for (let j = 0; j < key.length; j++) {
|
for (let j = 0; j < key.length; j++) {
|
||||||
if (
|
if (
|
||||||
key[j].CONSTRAINT_NAME == c.name
|
(key[j].CONSTRAINT_NAME ?? key[j].constraint_name) == c.name
|
||||||
&& key[j].TABLE_NAME == tabel.dbLangTableName
|
&& (key[j].TABLE_NAME ?? key[j].table_name) == tabel.dbLangTableName
|
||||||
&& key[j].REFERENCED_TABLE_NAME == refTable
|
&& (key[j].REFERENCED_TABLE_NAME ?? key[j].referenced_table_name) == refTable
|
||||||
) {
|
) {
|
||||||
let inF = fromAttrs.indexOf(key[j].COLUMN_NAME.toLowerCase());
|
let inF = fromAttrs.indexOf((key[j].COLUMN_NAME ?? key[j].column_name).toLowerCase());
|
||||||
let inT = toAttrs.indexOf(key[j].REFERENCED_COLUMN_NAME.toLowerCase());
|
let inT = toAttrs.indexOf((key[j].REFERENCED_COLUMN_NAME ?? key[j].referenced_column_name).toLowerCase());
|
||||||
if (inF != -1 && inT == inF) found++;
|
if (inF != -1 && inT == inF) found++;
|
||||||
else return false;
|
else return false;
|
||||||
}
|
}
|
||||||
|
@ -123,44 +121,59 @@ export class Handler {
|
||||||
return found == fromAttrs.length && found == toAttrs.length;
|
return found == fromAttrs.length && found == toAttrs.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function freeForUpdate(attr: string, table: string, start = false, pool = del) {
|
||||||
|
for (let i = 0; i < key.length; i++) {
|
||||||
|
const k = key[i];
|
||||||
|
if (
|
||||||
|
((k.REFERENCED_TABLE_NAME ?? k.referenced_table_name) == table
|
||||||
|
&& (k.REFERENCED_COLUMN_NAME ?? k.referenced_column_name).toLowerCase() == attr.toLowerCase())
|
||||||
|
|| (start && (k.TABLE_NAME ?? k.table_name) == table
|
||||||
|
&& (k.COLUMN_NAME ?? k.column_name).toLowerCase() == attr.toLowerCase() && (k.REFERENCED_COLUMN_NAME ?? k.referenced_column_name) != null)
|
||||||
|
) {
|
||||||
|
pool.appendEnding(handler.querys.removeForeignKey(handler, (k.TABLE_NAME ?? k.table_name), (k.CONSTRAINT_NAME ?? k.constraint_name)));
|
||||||
|
key.splice(i--, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//delete check and unused/false(unique, foreign) constraints
|
//delete check and unused/false(unique, foreign) constraints
|
||||||
for (let i = 0; i < constraints.length; i++) {
|
for (let i = 0; i < constraints.length; i++) {
|
||||||
const c = constraints[i];
|
const c = constraints[i];
|
||||||
if (c.CONSTRAINT_TYPE == "CHECK") {
|
if ((c.CONSTRAINT_TYPE ?? c.constraint_type) == "CHECK") {
|
||||||
del.appendEnding(handler.querys.removeCheck(handler, c.TABLE_NAME, c.CONSTRAINT_NAME));
|
del.appendEnding(handler.querys.removeCheck(handler, (c.TABLE_NAME ?? c.table_name), (c.CONSTRAINT_NAME ?? c.constraint_name)));
|
||||||
} else if (c.CONSTRAINT_TYPE == "UNIQUE") {
|
} else if ((c.CONSTRAINT_TYPE ?? c.constraint_type) == "UNIQUE") {
|
||||||
if (
|
if (
|
||||||
(typeof tableData[c.TABLE_NAME.toLowerCase()] == "undefined" && deleteInDB)
|
(typeof tableData[c.TABLE_NAME ?? c.table_name] == "undefined" && deleteInDB)
|
||||||
|| !checkUniqueIsVaild(c.CONSTRAINT_NAME, c.TABLE_NAME)
|
|| !checkUniqueIsVaild((c.CONSTRAINT_NAME ?? c.constraint_name), (c.TABLE_NAME ?? c.table_name))
|
||||||
) del.appendEnding(handler.querys.removeUnique(handler, c.TABLE_NAME, c.CONSTRAINT_NAME));
|
) {
|
||||||
} else if (c.CONSTRAINT_TYPE == "FOREIGN KEY") {
|
const uTName = (c.TABLE_NAME ?? c.table_name);
|
||||||
|
const uCName = (c.CONSTRAINT_NAME ?? c.constraint_name);
|
||||||
|
for (let j = 0; j < key.length; j++) {
|
||||||
|
if (
|
||||||
|
(key[j].CONSTRAINT_NAME ?? key[j].constraint_name) == uCName
|
||||||
|
&& (key[j].TABLE_NAME ?? key[j].table_name) == uTName
|
||||||
|
) {
|
||||||
|
freeForUpdate((key[j].COLUMN_NAME ?? key[j].column_name), uTName, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
del.appendEnding(handler.querys.removeUnique(handler, uTName, uCName));
|
||||||
|
}
|
||||||
|
} else if ((c.CONSTRAINT_TYPE ?? c.constraint_type) == "FOREIGN KEY") {
|
||||||
if (
|
if (
|
||||||
(typeof tableData[c.TABLE_NAME.toLowerCase()] == "undefined" && deleteInDB)
|
(typeof tableData[c.TABLE_NAME ?? c.table_name] == "undefined" && deleteInDB)
|
||||||
|| !checkForeignIsVaild(c.CONSTRAINT_NAME, c.TABLE_NAME)
|
|| !checkForeignIsVaild((c.CONSTRAINT_NAME ?? c.constraint_name), (c.TABLE_NAME ?? c.table_name))
|
||||||
) del.appendEnding(handler.querys.removeForeignKey(handler, c.TABLE_NAME, c.CONSTRAINT_NAME));
|
) del.appendEnding(handler.querys.removeForeignKey(handler, (c.TABLE_NAME ?? c.table_name), (c.CONSTRAINT_NAME ?? c.constraint_name)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//delete unused Tables
|
//delete unused Tables
|
||||||
if (deleteInDB) for (let i = 0; i < allTables.length; i++) {
|
if (deleteInDB) for (let i = 0; i < allTables.length; i++) {
|
||||||
if (typeof tableData[allTables[i].toLowerCase()] == "undefined") del.appendEnding(handler.querys.deleteTable(handler, allTables[i]));
|
if (typeof tableData[allTables[i]] == "undefined") del.appendEnding(handler.querys.deleteTable(handler, allTables[i]));
|
||||||
}
|
|
||||||
|
|
||||||
function freeForUpdate(attr: string, table: string) {
|
|
||||||
for (let i = 0; i < key.length; i++) {
|
|
||||||
const k = key[i];
|
|
||||||
if (
|
|
||||||
k.REFERENCED_TABLE_NAME == table
|
|
||||||
&& k.REFERENCED_COLUMN_NAME.toLowerCase() == attr.toLowerCase()
|
|
||||||
) {
|
|
||||||
del.appendEnding(handler.querys.removeForeignKey(handler, k.TABLE_NAME, k.CONSTRAINT_NAME));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//create tables
|
//create tables
|
||||||
for (let i = 0; i < db.tables.length; i++) {
|
for (let i = 0; i < db.tables.length; i++) {
|
||||||
const table = db.tables[i];
|
const table = db.tables[i];
|
||||||
const tableD = tableData[table.dbLangTableName.toLowerCase()];
|
const tableD = tableData[table.dbLangTableName];
|
||||||
//delete unused Columns
|
//delete unused Columns
|
||||||
if (deleteInDB && tableD != null) {
|
if (deleteInDB && tableD != null) {
|
||||||
let keys = Object.keys(tableD);
|
let keys = Object.keys(tableD);
|
||||||
|
@ -175,58 +188,78 @@ export class Handler {
|
||||||
let changePrimary = false;
|
let changePrimary = false;
|
||||||
if (tableD == null) {
|
if (tableD == null) {
|
||||||
create.appendEnding(handler.querys.create(handler, table));
|
create.appendEnding(handler.querys.create(handler, table));
|
||||||
changePrimary = true;
|
//changePrimary = true;
|
||||||
} else {
|
} else {
|
||||||
let keys = Object.keys(table.dbLangTableAttributes);
|
let keys = Object.keys(table.dbLangTableAttributes);
|
||||||
for (let j = 0; j < keys.length; j++) {
|
for (let j = 0; j < keys.length; j++) {
|
||||||
const a = table.dbLangTableAttributes[keys[j]];
|
const a = table.dbLangTableAttributes[keys[j]];
|
||||||
const attrData = tableD[keys[j]];
|
const attrData = tableD[keys[j]];
|
||||||
if (attrData == null) {
|
if (attrData == null) {
|
||||||
create.appendEnding(handler.querys.addColumn(handler, a));
|
if (a.ops.autoIncrement) {
|
||||||
changePrimary = true;
|
a.ops.autoIncrement = false;
|
||||||
|
create.appendEnding(handler.querys.addColumn(handler, a));
|
||||||
|
a.ops.autoIncrement = true;
|
||||||
|
connst.appendEnding(handler.querys.addColumn(handler, a));
|
||||||
|
}
|
||||||
|
else create.appendEnding(handler.querys.addColumn(handler, a));
|
||||||
|
if (a.ops.primaryKey) changePrimary = true;
|
||||||
} else if (
|
} else if (
|
||||||
!handler.builders.compareDatatypes(handler, a.type, attrData.Type) ||
|
!handler.builders.compareDatatypes(handler, a.type, attrData.Type) ||
|
||||||
a.ops.default != attrData.Default ||
|
a.ops.default != attrData.Default ||
|
||||||
(!!a.ops.notNull || !!a.ops.autoIncrement || !!a.ops.primaryKey) != (attrData.Null == "NO") ||
|
(!!a.ops.notNull || !!a.ops.autoIncrement || !!a.ops.primaryKey) != (attrData.Null == "NO") ||
|
||||||
(!!a.ops.autoIncrement) != (attrData.Extra == "auto_increment")
|
(!!a.ops.autoIncrement) != (attrData.Extra == "auto_increment")
|
||||||
) {
|
) {
|
||||||
/*console.log(!handler.builders.compareDatatypes(handler, a.type, attrData.Type), "|",
|
|
||||||
a.ops.default, attrData.Default, "|",
|
|
||||||
(!!a.ops.notNull || !!a.ops.autoIncrement || !!a.ops.primaryKey), (attrData.Null == "NO"), "|",
|
|
||||||
(!!a.ops.autoIncrement), (attrData.Extra == "auto_increment"));*/
|
|
||||||
freeForUpdate(a.name, a.table.dbLangTableName);
|
freeForUpdate(a.name, a.table.dbLangTableName);
|
||||||
create.appendEnding(handler.querys.changeColumn(handler, a));
|
if (a.ops.autoIncrement) {
|
||||||
|
a.ops.autoIncrement = false;
|
||||||
|
create.appendEnding(handler.querys.changeColumn(handler, a));
|
||||||
|
a.ops.autoIncrement = true;
|
||||||
|
connst.appendEnding(handler.querys.changeColumn(handler, a));
|
||||||
|
}
|
||||||
|
else create.appendEnding(handler.querys.changeColumn(handler, a));
|
||||||
}
|
}
|
||||||
if (attrData == null) {
|
if (attrData != null) {
|
||||||
changePrimary = true;
|
|
||||||
} else {
|
|
||||||
if ((attrData.Key == "PRI") != (!!a.ops.primaryKey)) {
|
if ((attrData.Key == "PRI") != (!!a.ops.primaryKey)) {
|
||||||
freeForUpdate(a.name, a.table.dbLangTableName);
|
freeForUpdate(a.name, a.table.dbLangTableName);
|
||||||
changePrimary = true;
|
changePrimary = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (changePrimary) for (let j = 0; j < keys.length; j++) {
|
||||||
|
const a = table.dbLangTableAttributes[keys[j]];
|
||||||
|
if (a.ops.autoIncrement) {
|
||||||
|
a.ops.autoIncrement = false;
|
||||||
|
create.appendEnding(handler.querys.changeColumn(handler, a));
|
||||||
|
a.ops.autoIncrement = true;
|
||||||
|
connst.appendEnding(handler.querys.changeColumn(handler, a));
|
||||||
|
}
|
||||||
|
freeForUpdate(a.name, a.table.dbLangTableName, true, create);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (changePrimary) {
|
if (changePrimary) {
|
||||||
create.appendEnding(handler.querys.removePrimaryKey(handler, table.dbLangTableName));
|
create.appendEnding(handler.querys.removePrimaryKey(handler, table.dbLangTableName));
|
||||||
create.appendEnding(handler.querys.addPrimaryKey(handler, table));
|
create.appendEnding(handler.querys.addPrimaryKey(handler, table));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
for (let i = 0; i < db.tables.length; i++) {
|
||||||
|
const table = db.tables[i];
|
||||||
for (let j = 0; j < table.dbLangConstrains.length; j++) {
|
for (let j = 0; j < table.dbLangConstrains.length; j++) {
|
||||||
const c = table.dbLangConstrains[j];
|
const c = table.dbLangConstrains[j];
|
||||||
if (c instanceof checkConstraint) {
|
if (c instanceof checkConstraint) {
|
||||||
create.appendEnding(handler.querys.addCheck(handler, table, c));
|
connst.appendEnding(handler.querys.addCheck(handler, table, c));
|
||||||
} else if (c instanceof uniqueConstraint) {
|
} else if (c instanceof uniqueConstraint) {
|
||||||
if (!checkUniqueExists(table, c)) {
|
if (!checkUniqueExists(table, c)) {
|
||||||
create.appendEnding(handler.querys.addUnique(handler, table, c));
|
connst.appendEnding(handler.querys.addUnique(handler, table, c));
|
||||||
}
|
}
|
||||||
} else if (c instanceof foreignConstraint) {
|
} else if (c instanceof foreignConstraint) {
|
||||||
if (!checkForeignExists(table, c)) {
|
if (!checkForeignExists(table, c)) {
|
||||||
create.appendEnding(handler.querys.addForeignKey(handler, table, c));
|
connst.appendEnding(handler.querys.addForeignKey(handler, table, c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!create.isEmpty()) del.append(create);
|
if (!create.isEmpty()) del.append(create);
|
||||||
|
if (!connst.isEmpty()) del.append(connst);
|
||||||
if (!del.isEmpty()) await db.query(handler.builders.query(del));
|
if (!del.isEmpty()) await db.query(handler.builders.query(del));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,13 +267,32 @@ export class Handler {
|
||||||
select: (handler: Handler, q: selectQuery): QueryBuilder => {
|
select: (handler: Handler, q: selectQuery): QueryBuilder => {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
builder.addCode("select ");
|
builder.addCode("select ");
|
||||||
builder.append(joinArg(", ")(handler, q.attr));
|
let a = q.attr;
|
||||||
|
for (let i = 0; i < a.length; i++) {
|
||||||
|
const d = a[i];
|
||||||
|
if (d instanceof Attribute || d instanceof AttributeAlias)
|
||||||
|
builder.addCode(d.getString(handler) + " " + handler.builders.escapeID(d.toString()));
|
||||||
|
else if (d instanceof Alias) {
|
||||||
|
builder.append(d.serialize(handler));
|
||||||
|
builder.addCode(handler.builders.escapeID(d.toString()));
|
||||||
|
} else if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
|
||||||
|
builder.addCode("(");
|
||||||
|
builder.append(d.serialize(handler));
|
||||||
|
builder.addCode(")");
|
||||||
|
} else {
|
||||||
|
builder.addInjection(d);
|
||||||
|
}
|
||||||
|
if (i + 1 < a.length) builder.addCode(", ");
|
||||||
|
}
|
||||||
|
//builder.append(joinArg(", ")(handler, q.attr));
|
||||||
builder.addCode(` from `);
|
builder.addCode(` from `);
|
||||||
if(q.from == null){
|
if (q.from == null) {
|
||||||
builder.addCode(" DUAL");
|
builder.addCode(" DUAL");
|
||||||
}else if (q.from instanceof Table){
|
} else if (q.from instanceof Table) {
|
||||||
builder.addCode(q.from.serialize(handler));
|
builder.addCode(q.from.serialize(handler));
|
||||||
}else{
|
} else if (q.from instanceof TableAlias) {
|
||||||
|
builder.addCode(q.from.serializeAlias(handler));
|
||||||
|
} else {
|
||||||
builder.append(q.from.serialize(handler));
|
builder.append(q.from.serialize(handler));
|
||||||
}
|
}
|
||||||
if (q.whereD) {
|
if (q.whereD) {
|
||||||
|
@ -255,9 +307,35 @@ export class Handler {
|
||||||
builder.append(q.havingD.serialize(handler));
|
builder.append(q.havingD.serialize(handler));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (q.havingD) {
|
if (q.orderByD.length > 0) {
|
||||||
builder.addCode(" limit ");
|
builder.addCode(" order by ");
|
||||||
builder.addInjection(q.limitD);
|
for (let i = 0; i < q.orderByD.length; i++) {
|
||||||
|
const d = q.orderByD[i];
|
||||||
|
if (d[0] instanceof Attribute || d[0] instanceof AttributeAlias) builder.addCode(d[0].getString(handler));
|
||||||
|
else if (d[0] instanceof Alias)
|
||||||
|
builder.addCode(handler.builders.escapeID(d.toString()));
|
||||||
|
else if (d[0] instanceof Modifier || d[0] instanceof selectQuery || d[0] instanceof Aggregation) {
|
||||||
|
builder.addCode("(");
|
||||||
|
builder.append(d[0].serialize(handler));
|
||||||
|
builder.addCode(")");
|
||||||
|
} else {
|
||||||
|
builder.addInjection(d[0]);
|
||||||
|
}
|
||||||
|
if(d[1] == order.DESC) builder.addCode(" DESC");
|
||||||
|
else builder.addCode(" ASC");
|
||||||
|
if (i + 1 < q.orderByD.length) builder.addCode(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (q.limitD != null) {
|
||||||
|
if (q.limitOffsetD == null) {
|
||||||
|
builder.addCode(" limit ");
|
||||||
|
builder.addInjection(q.limitD);
|
||||||
|
} else {
|
||||||
|
builder.addCode(" limit ");
|
||||||
|
builder.addInjection(q.limitOffsetD);
|
||||||
|
builder.addCode(", ");
|
||||||
|
builder.addInjection(q.limitD);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*builder.setHandler((json)=>{
|
/*builder.setHandler((json)=>{
|
||||||
|
@ -268,7 +346,7 @@ export class Handler {
|
||||||
},
|
},
|
||||||
insert: (hander: Handler, q: insertQuery): QueryBuilder => {
|
insert: (hander: Handler, q: insertQuery): QueryBuilder => {
|
||||||
const qb = new QueryBuilder();
|
const qb = new QueryBuilder();
|
||||||
qb.addCode("INSERT INTO ");
|
qb.addCode("INSERT IGNORE INTO ");
|
||||||
qb.addCode(q.attrs[0].table.serialize(hander));
|
qb.addCode(q.attrs[0].table.serialize(hander));
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.addCodeCommaSeperated(q.attrs.map(a => a.serialize(hander)));
|
qb.addCodeCommaSeperated(q.attrs.map(a => a.serialize(hander)));
|
||||||
|
@ -295,7 +373,9 @@ export class Handler {
|
||||||
const s = q.setD[i];
|
const s = q.setD[i];
|
||||||
qb.addCode(s[0].serialize(handler));
|
qb.addCode(s[0].serialize(handler));
|
||||||
qb.addCode(" = (");
|
qb.addCode(" = (");
|
||||||
if (s[1] instanceof Attribute) qb.addCode(s[1].serialize(handler));
|
if (s[1] instanceof Attribute || s[1] instanceof AttributeAlias) qb.addCode(s[1].serialize(handler));
|
||||||
|
else if (s[1] instanceof Alias)
|
||||||
|
qb.addCode(handler.builders.escapeID(s[1].toString()));
|
||||||
else if (s[1] instanceof Modifier || s[1] instanceof selectQuery || s[1] instanceof Aggregation) {
|
else if (s[1] instanceof Modifier || s[1] instanceof selectQuery || s[1] instanceof Aggregation) {
|
||||||
qb.append(s[1].serialize(handler));
|
qb.append(s[1].serialize(handler));
|
||||||
} else {
|
} else {
|
||||||
|
@ -324,7 +404,24 @@ export class Handler {
|
||||||
const qb = new QueryBuilder();
|
const qb = new QueryBuilder();
|
||||||
qb.addCode(`SELECT * FROM information_schema.tables where TABLE_SCHEMA = `);
|
qb.addCode(`SELECT * FROM information_schema.tables where TABLE_SCHEMA = `);
|
||||||
qb.addInjection(db.name);
|
qb.addInjection(db.name);
|
||||||
qb.addCode(` and TABLE_TYPE = "BASE TABLE"`);
|
qb.addCode(` and TABLE_TYPE = `);
|
||||||
|
qb.addInjection("BASE TABLE");
|
||||||
|
return qb;
|
||||||
|
},
|
||||||
|
listConstraints: (handler: Handler, db: DB) => {
|
||||||
|
let qb = new QueryBuilder();
|
||||||
|
qb.addCode(`select * from information_schema.table_constraints where CONSTRAINT_SCHEMA = `);
|
||||||
|
qb.addInjection(db.name);
|
||||||
|
qb.addCode(` or TABLE_SCHEMA = `);
|
||||||
|
qb.addInjection(db.name);
|
||||||
|
return qb;
|
||||||
|
},
|
||||||
|
listConstraintDetails: (handler: Handler, db: DB) => {
|
||||||
|
let qb = new QueryBuilder();
|
||||||
|
qb.addCode(`SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE where CONSTRAINT_SCHEMA = `);
|
||||||
|
qb.addInjection(db.name);
|
||||||
|
qb.addCode(` or TABLE_SCHEMA = `);
|
||||||
|
qb.addInjection(db.name);
|
||||||
return qb;
|
return qb;
|
||||||
},
|
},
|
||||||
create: (handler: Handler, table: Table): QueryBuilder => {
|
create: (handler: Handler, table: Table): QueryBuilder => {
|
||||||
|
@ -337,6 +434,15 @@ export class Handler {
|
||||||
builder.append(a.serializeSettings(handler));
|
builder.append(a.serializeSettings(handler));
|
||||||
if (i + 1 < keys.length) builder.addCode(", ");
|
if (i + 1 < keys.length) builder.addCode(", ");
|
||||||
}
|
}
|
||||||
|
let primAttr = Object.entries(table.dbLangTableAttributes)
|
||||||
|
.filter(([n, attr]) => !!attr.ops.primaryKey);
|
||||||
|
if (primAttr.length > 0) {
|
||||||
|
builder.addCode(", primary key (");
|
||||||
|
builder.addCodeCommaSeperated(
|
||||||
|
primAttr.map(([n, attr]) => handler.builders.escapeID(n))
|
||||||
|
);
|
||||||
|
builder.addCode(")");
|
||||||
|
}
|
||||||
builder.addCode(")");
|
builder.addCode(")");
|
||||||
return builder;
|
return builder;
|
||||||
},
|
},
|
||||||
|
@ -413,14 +519,15 @@ export class Handler {
|
||||||
return builder;
|
return builder;
|
||||||
},
|
},
|
||||||
addPrimaryKey: (handler: Handler, table: Table) => {
|
addPrimaryKey: (handler: Handler, table: Table) => {
|
||||||
|
let primaAttr = Object.entries(table.dbLangTableAttributes)
|
||||||
|
.filter(([n, attr]) => !!attr.ops.primaryKey);
|
||||||
const qb = new QueryBuilder();
|
const qb = new QueryBuilder();
|
||||||
|
if (primaAttr.length == 0) return qb;
|
||||||
qb.addCode("ALTER TABLE ");
|
qb.addCode("ALTER TABLE ");
|
||||||
qb.addCode(table.serialize(handler));
|
qb.addCode(table.serialize(handler));
|
||||||
qb.addCode(" add PRIMARY KEY if not exists (");
|
qb.addCode(" add PRIMARY KEY if not exists (");
|
||||||
qb.addCodeCommaSeperated(
|
qb.addCodeCommaSeperated(
|
||||||
Object.entries(table.dbLangTableAttributes)
|
primaAttr.map(([n, attr]) => handler.builders.escapeID(n))
|
||||||
.filter(([n, attr]) => !!attr.ops.primaryKey)
|
|
||||||
.map(([n, attr]) => handler.builders.escapeID(n))
|
|
||||||
);
|
);
|
||||||
qb.addCode(")");
|
qb.addCode(")");
|
||||||
return qb;
|
return qb;
|
||||||
|
@ -509,27 +616,37 @@ export class Handler {
|
||||||
},
|
},
|
||||||
escapeID: (key: string): string => {
|
escapeID: (key: string): string => {
|
||||||
if (!key || key === "" || key.includes('\u0000')) throw new Error("Can not escape empty key or with null unicode!");
|
if (!key || key === "" || key.includes('\u0000')) throw new Error("Can not escape empty key or with null unicode!");
|
||||||
|
if (key.length > 64) key = sha256(key);
|
||||||
if (key.match(/^`.+`$/g)) return key;
|
if (key.match(/^`.+`$/g)) return key;
|
||||||
return `\`${key.replace(/`/g, '``')}\``;
|
return `\`${key.replace(/`/g, '``')}\``;
|
||||||
},
|
},
|
||||||
|
escapeLiteral: (data: any): string => {
|
||||||
|
return this.db.mariaPool.escape(data);
|
||||||
|
},
|
||||||
compareDatatypes: (handler: Handler, attr: Datatype, curr: String) => {
|
compareDatatypes: (handler: Handler, attr: Datatype, curr: String) => {
|
||||||
let qb = attr.serialize(handler);
|
let qb = attr.serialize(handler);
|
||||||
let sql = "";
|
let sql = "";
|
||||||
for (let i = 0; i < qb.list.length; i++) {
|
for (let i = 0; i < qb.list.length; i++) {
|
||||||
const [inject, data] = qb.list[i];
|
const [inject, data] = qb.list[i];
|
||||||
sql += data;
|
if (inject) sql += handler.builders.escapeLiteral(data);
|
||||||
|
else sql += data;
|
||||||
}
|
}
|
||||||
|
return curr.split(", ").join(",").startsWith(sql.split(", ").join(","));
|
||||||
return curr.split(" ").join("").startsWith(sql.split(" ").join(""));
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
responses = {
|
||||||
|
readResponse: (resp: any) => new readResponse(resp),
|
||||||
|
writeResponse: (resp: any) => new writeResponse(resp.affectedRows),
|
||||||
|
insertResponse: (resp: any) => new insertResponse(resp.affectedRows, resp.insertId),
|
||||||
|
}
|
||||||
|
|
||||||
aggregations = {
|
aggregations = {
|
||||||
count: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "count(" + a.toString(handler) + ")" }]),
|
count: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "count(" + a.getString(handler) + ")" }]),
|
||||||
sum: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "sum(" + a.toString(handler) + ")" }]),
|
sum: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "sum(" + a.getString(handler) + ")" }]),
|
||||||
avg: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "avg(" + a.toString(handler) + ")" }]),
|
avg: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "avg(" + a.getString(handler) + ")" }]),
|
||||||
min: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "min(" + a.toString(handler) + ")" }]),
|
min: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "min(" + a.getString(handler) + ")" }]),
|
||||||
max: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "max(" + a.toString(handler) + ")" }]),
|
max: (handler: Handler, a: Attribute): QueryBuilder => new QueryBuilder([{ data: "max(" + a.getString(handler) + ")" }]),
|
||||||
}
|
}
|
||||||
|
|
||||||
joins = {
|
joins = {
|
||||||
|
@ -538,6 +655,7 @@ export class Handler {
|
||||||
for (let i = 0; i < j.tables.length; i++) {
|
for (let i = 0; i < j.tables.length; i++) {
|
||||||
const t = j.tables[i];
|
const t = j.tables[i];
|
||||||
if (t instanceof Table) qb.addCode(t.serialize(handler));
|
if (t instanceof Table) qb.addCode(t.serialize(handler));
|
||||||
|
else if (t instanceof TableAlias) qb.addCode(t.serializeAlias(handler));
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(t.serialize(handler));
|
qb.append(t.serialize(handler));
|
||||||
|
@ -556,6 +674,7 @@ export class Handler {
|
||||||
using: (handler: Handler, j: usingJoin) => {
|
using: (handler: Handler, j: usingJoin) => {
|
||||||
const qb = new QueryBuilder();
|
const qb = new QueryBuilder();
|
||||||
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
||||||
|
else if (j.tableA instanceof TableAlias) qb.addCode(j.tableA.serializeAlias(handler));
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(j.tableA.serialize(handler));
|
qb.append(j.tableA.serialize(handler));
|
||||||
|
@ -566,10 +685,11 @@ export class Handler {
|
||||||
if (j.type == joinType.right) qb.addCode("right ");
|
if (j.type == joinType.right) qb.addCode("right ");
|
||||||
if (j.type == joinType.full) qb.addCode("full ");
|
if (j.type == joinType.full) qb.addCode("full ");
|
||||||
qb.addCode("join ");
|
qb.addCode("join ");
|
||||||
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
if (j.tableB instanceof Table) qb.addCode(j.tableB.serialize(handler));
|
||||||
|
else if (j.tableB instanceof TableAlias) qb.addCode(j.tableB.serializeAlias(handler));
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(j.tableA.serialize(handler));
|
qb.append(j.tableB.serialize(handler));
|
||||||
qb.addCode(")");
|
qb.addCode(")");
|
||||||
}
|
}
|
||||||
qb.addCode(" using (");
|
qb.addCode(" using (");
|
||||||
|
@ -580,6 +700,7 @@ export class Handler {
|
||||||
on: (handler: Handler, j: onJoin) => {
|
on: (handler: Handler, j: onJoin) => {
|
||||||
const qb = new QueryBuilder();
|
const qb = new QueryBuilder();
|
||||||
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
||||||
|
else if (j.tableA instanceof TableAlias) qb.addCode(j.tableA.serializeAlias(handler));
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(j.tableA.serialize(handler));
|
qb.append(j.tableA.serialize(handler));
|
||||||
|
@ -590,10 +711,11 @@ export class Handler {
|
||||||
if (j.type == joinType.right) qb.addCode("right ");
|
if (j.type == joinType.right) qb.addCode("right ");
|
||||||
if (j.type == joinType.full) qb.addCode("full ");
|
if (j.type == joinType.full) qb.addCode("full ");
|
||||||
qb.addCode("join ");
|
qb.addCode("join ");
|
||||||
if (j.tableA instanceof Table) qb.addCode(j.tableA.serialize(handler));
|
if (j.tableB instanceof Table) qb.addCode(j.tableB.serialize(handler));
|
||||||
|
else if (j.tableB instanceof TableAlias) qb.addCode(j.tableB.serializeAlias(handler));
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(j.tableA.serialize(handler));
|
qb.append(j.tableB.serialize(handler));
|
||||||
qb.addCode(")");
|
qb.addCode(")");
|
||||||
}
|
}
|
||||||
qb.addCode(" on (");
|
qb.addCode(" on (");
|
||||||
|
@ -606,6 +728,7 @@ export class Handler {
|
||||||
for (let i = 0; i < j.tables.length; i++) {
|
for (let i = 0; i < j.tables.length; i++) {
|
||||||
const t = j.tables[i];
|
const t = j.tables[i];
|
||||||
if (t instanceof Table) qb.addCode(t.serialize(handler));
|
if (t instanceof Table) qb.addCode(t.serialize(handler));
|
||||||
|
else if (t instanceof TableAlias) qb.addCode(t.serializeAlias(handler));
|
||||||
else {
|
else {
|
||||||
qb.addCode("(");
|
qb.addCode("(");
|
||||||
qb.append(t.serialize(handler));
|
qb.append(t.serialize(handler));
|
||||||
|
@ -631,7 +754,9 @@ export class Handler {
|
||||||
divide: joinArg("-/"),
|
divide: joinArg("-/"),
|
||||||
not: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
not: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
let e = a[0];
|
let e = a[0];
|
||||||
if (e instanceof Attribute) return new QueryBuilder([{ data: "not (" + e.toString(handler) + ")" }])
|
if (e instanceof Attribute || e instanceof AttributeAlias) return new QueryBuilder([{ data: "not (" + e.getString(handler) + ")" }])
|
||||||
|
if (e instanceof Alias)
|
||||||
|
return new QueryBuilder([{ data: handler.builders.escapeID(e.toString()) }]);
|
||||||
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
builder.addCode("not (");
|
builder.addCode("not (");
|
||||||
|
@ -647,14 +772,18 @@ export class Handler {
|
||||||
},
|
},
|
||||||
like: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
like: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
if (a[0] instanceof Attribute) builder.addCode(a[0].toString());
|
if (a[0] instanceof Attribute || a[0] instanceof AttributeAlias) builder.addCode(a[0].getString());
|
||||||
|
else if (a[0] instanceof Alias)
|
||||||
|
builder.addCode(handler.builders.escapeID(a[0].toString()));
|
||||||
else if (a[0] instanceof Modifier || a[0] instanceof selectQuery || a[0] instanceof Aggregation) {
|
else if (a[0] instanceof Modifier || a[0] instanceof selectQuery || a[0] instanceof Aggregation) {
|
||||||
builder.append(a[0].serialize(handler));
|
builder.append(a[0].serialize(handler));
|
||||||
} else {
|
} else {
|
||||||
builder.addInjection(a[0]);
|
builder.addInjection(a[0]);
|
||||||
}
|
}
|
||||||
builder.addCode(" LIKE ");
|
builder.addCode(" LIKE ");
|
||||||
if (a[1] instanceof Attribute) builder.addCode(a[1].toString());
|
if (a[1] instanceof Attribute || a[1] instanceof AttributeAlias) builder.addCode(a[1].getString());
|
||||||
|
else if (a[1] instanceof Alias)
|
||||||
|
builder.addCode(handler.builders.escapeID(a[1].toString()));
|
||||||
else if (a[1] instanceof Modifier || a[1] instanceof selectQuery || a[1] instanceof Aggregation) {
|
else if (a[1] instanceof Modifier || a[1] instanceof selectQuery || a[1] instanceof Aggregation) {
|
||||||
builder.append(a[1].serialize(handler));
|
builder.append(a[1].serialize(handler));
|
||||||
} else {
|
} else {
|
||||||
|
@ -662,12 +791,53 @@ export class Handler {
|
||||||
}
|
}
|
||||||
return builder;
|
return builder;
|
||||||
},
|
},
|
||||||
|
regexp: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
|
const builder = new QueryBuilder();
|
||||||
|
if (a[0] instanceof Attribute || a[0] instanceof AttributeAlias) builder.addCode(a[0].getString());
|
||||||
|
else if (a[0] instanceof Alias)
|
||||||
|
builder.addCode(handler.builders.escapeID(a[0].toString()));
|
||||||
|
else if (a[0] instanceof Modifier || a[0] instanceof selectQuery || a[0] instanceof Aggregation) {
|
||||||
|
builder.append(a[0].serialize(handler));
|
||||||
|
} else {
|
||||||
|
builder.addInjection(a[0]);
|
||||||
|
}
|
||||||
|
builder.addCode(" REGEXP ");
|
||||||
|
if (a[1] instanceof Attribute || a[1] instanceof AttributeAlias) builder.addCode(a[1].getString());
|
||||||
|
else if (a[1] instanceof Alias)
|
||||||
|
builder.addCode(handler.builders.escapeID(a[1].toString()));
|
||||||
|
else if (a[1] instanceof Modifier || a[1] instanceof selectQuery || a[1] instanceof Aggregation) {
|
||||||
|
builder.append(a[1].serialize(handler));
|
||||||
|
} else {
|
||||||
|
builder.addInjection(a[1]);
|
||||||
|
}
|
||||||
|
return builder;
|
||||||
|
},
|
||||||
|
exists: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
|
let e = a[0];
|
||||||
|
if (e instanceof Attribute || e instanceof AttributeAlias) return new QueryBuilder([{ data: "exists (" + e.getString(handler) + ")" }])
|
||||||
|
if (e instanceof Alias)
|
||||||
|
return new QueryBuilder([{ data: handler.builders.escapeID(e.toString()) }]);
|
||||||
|
if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
||||||
|
const builder = new QueryBuilder();
|
||||||
|
builder.addCode("exists (");
|
||||||
|
builder.append(e.serialize(handler));
|
||||||
|
builder.addCode(")");
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
return new QueryBuilder([
|
||||||
|
{ data: "exists (" },
|
||||||
|
{ inject: true, data: e },
|
||||||
|
{ data: ")" }
|
||||||
|
]);
|
||||||
|
},
|
||||||
concat: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
concat: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
builder.addCode("CONCAT(");
|
builder.addCode("CONCAT(");
|
||||||
for (let i = 0; i < a.length; i++) {
|
for (let i = 0; i < a.length; i++) {
|
||||||
const e = a[i];
|
const e = a[i];
|
||||||
if (e instanceof Attribute) builder.addCode(e.toString());
|
if (e instanceof Attribute || e instanceof AttributeAlias) builder.addCode(e.getString());
|
||||||
|
else if (e instanceof Alias)
|
||||||
|
builder.addCode(handler.builders.escapeID(e.toString()));
|
||||||
else if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
else if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
||||||
builder.append(e.serialize(handler));
|
builder.append(e.serialize(handler));
|
||||||
} else {
|
} else {
|
||||||
|
@ -677,7 +847,25 @@ export class Handler {
|
||||||
}
|
}
|
||||||
builder.addCode(")");
|
builder.addCode(")");
|
||||||
return builder;
|
return builder;
|
||||||
}
|
},
|
||||||
|
coalesce: (handler: Handler, a: allModifierInput[]): QueryBuilder => {
|
||||||
|
const builder = new QueryBuilder();
|
||||||
|
builder.addCode("COALESCE(");
|
||||||
|
for (let i = 0; i < a.length; i++) {
|
||||||
|
const e = a[i];
|
||||||
|
if (e instanceof Attribute || e instanceof AttributeAlias) builder.addCode(e.getString());
|
||||||
|
else if (e instanceof Alias)
|
||||||
|
builder.addCode(handler.builders.escapeID(e.toString()));
|
||||||
|
else if (e instanceof Modifier || e instanceof selectQuery || e instanceof Aggregation) {
|
||||||
|
builder.append(e.serialize(handler));
|
||||||
|
} else {
|
||||||
|
builder.addInjection(e);
|
||||||
|
}
|
||||||
|
if (i < a.length - 1) builder.addCode(", ");
|
||||||
|
}
|
||||||
|
builder.addCode(")");
|
||||||
|
return builder;
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
datatypes = {
|
datatypes = {
|
||||||
|
@ -709,7 +897,7 @@ export class Handler {
|
||||||
builder.addCode(")");
|
builder.addCode(")");
|
||||||
return builder;
|
return builder;
|
||||||
},
|
},
|
||||||
bool: dataTypeNoArg("bool"),
|
bool: dataTypeNoArg("tinyint"),
|
||||||
bit: dataTypeNoArg("bit"),
|
bit: dataTypeNoArg("bit"),
|
||||||
tinyint: dataTypeNoArg("tinyint"),
|
tinyint: dataTypeNoArg("tinyint"),
|
||||||
smallint: dataTypeNoArg("smallint"),
|
smallint: dataTypeNoArg("smallint"),
|
||||||
|
@ -765,7 +953,9 @@ function joinArg(type: string) {
|
||||||
const builder = new QueryBuilder();
|
const builder = new QueryBuilder();
|
||||||
for (let i = 0; i < a.length; i++) {
|
for (let i = 0; i < a.length; i++) {
|
||||||
const d = a[i];
|
const d = a[i];
|
||||||
if (d instanceof Attribute) builder.addCode(d.toString(handler));
|
if (d instanceof Attribute || d instanceof AttributeAlias) builder.addCode(d.getString(handler));
|
||||||
|
else if (d instanceof Alias)
|
||||||
|
builder.addCode(handler.builders.escapeID(d.toString()));
|
||||||
else if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
|
else if (d instanceof Modifier || d instanceof selectQuery || d instanceof Aggregation) {
|
||||||
builder.addCode("(");
|
builder.addCode("(");
|
||||||
builder.append(d.serialize(handler));
|
builder.append(d.serialize(handler));
|
||||||
|
|
16
src/funcs.ts
16
src/funcs.ts
|
@ -1,7 +1,8 @@
|
||||||
import { Attribute, Table } from "./db";
|
import { Alias } from "./alias.js";
|
||||||
import { Aggregation, BooleanModifier, checkConstraint, Datatype, foreignConstraint, joinCross, joinNatural, usingJoin, onJoin, NumberModifier, StringModifier, uniqueConstraint } from "./dbStructure";
|
import { Attribute, Table } from "./db.js";
|
||||||
import { insertQuery, removeQuery, selectQuery, updateQuery } from "./query";
|
import { Aggregation, BooleanModifier, checkConstraint, Datatype, foreignConstraint, joinCross, joinNatural, usingJoin, onJoin, NumberModifier, StringModifier, uniqueConstraint } from "./dbStructure.js";
|
||||||
import { allModifierInput, joinType, selectElements, selectFromElements } from "./types";
|
import { insertQuery, removeQuery, selectQuery, updateQuery } from "./query.js";
|
||||||
|
import { allModifierInput, joinType, selectElements, selectFromElements } from "./types.js";
|
||||||
|
|
||||||
//modifiers
|
//modifiers
|
||||||
export const and = (...args: BooleanModifier[]) => new BooleanModifier("and", args);
|
export const and = (...args: BooleanModifier[]) => new BooleanModifier("and", args);
|
||||||
|
@ -18,8 +19,12 @@ export const divide = (...args: allModifierInput[]) => new NumberModifier("divid
|
||||||
|
|
||||||
export const not = (arg: allModifierInput) => new BooleanModifier("not", [arg]);
|
export const not = (arg: allModifierInput) => new BooleanModifier("not", [arg]);
|
||||||
export const like = (a: allModifierInput, b: allModifierInput) => new BooleanModifier("like", [a, b]);
|
export const like = (a: allModifierInput, b: allModifierInput) => new BooleanModifier("like", [a, b]);
|
||||||
|
export const regexp = (a: allModifierInput, b: allModifierInput) => new BooleanModifier("regexp", [a, b]);
|
||||||
|
|
||||||
|
export const exists = (q: selectQuery) => new BooleanModifier("exists", [q]);
|
||||||
|
|
||||||
export const concat = (...args: allModifierInput[]) => new StringModifier("concat", args);
|
export const concat = (...args: allModifierInput[]) => new StringModifier("concat", args);
|
||||||
|
export const coalesce = (...args: allModifierInput[]) => new StringModifier("coalesce", args);
|
||||||
|
|
||||||
//aggregations
|
//aggregations
|
||||||
export const count = (a: Attribute) => new Aggregation("count", a);
|
export const count = (a: Attribute) => new Aggregation("count", a);
|
||||||
|
@ -53,6 +58,9 @@ export const insert = (...attrs: Attribute[]) => new insertQuery(attrs);
|
||||||
export const update = (table: Table) => new updateQuery(table);
|
export const update = (table: Table) => new updateQuery(table);
|
||||||
export const remove = (table: Table) => new removeQuery(table);
|
export const remove = (table: Table) => new removeQuery(table);
|
||||||
|
|
||||||
|
//alias
|
||||||
|
export const alias = (a: selectElements, name: string) => new Alias(a, name);
|
||||||
|
|
||||||
//datatypes
|
//datatypes
|
||||||
|
|
||||||
export const CHAR = (size: number) => new Datatype("char", [size]);
|
export const CHAR = (size: number) => new Datatype("char", [size]);
|
||||||
|
|
49
src/query.ts
49
src/query.ts
|
@ -1,7 +1,8 @@
|
||||||
import { Attribute, DB, Table } from "./db";
|
import { AttributeAlias } from "./alias.js";
|
||||||
import { BooleanModifier, Modifier } from "./dbStructure";
|
import { Attribute, DB, Table } from "./db.js";
|
||||||
import { Handler } from "./defaultHandler";
|
import { BooleanModifier, Modifier } from "./dbStructure.js";
|
||||||
import { allModifierInput, primaryData, selectElements, selectFromElements, serializeReturn } from "./types";
|
import { Handler } from "./defaultHandler.js";
|
||||||
|
import { allModifierInput, order, primaryData, selectElements, selectFromElements, serializeReturn } from "./types.js";
|
||||||
|
|
||||||
|
|
||||||
export class Query {
|
export class Query {
|
||||||
|
@ -48,6 +49,7 @@ export class QueryBuilder {
|
||||||
this.list.push(...qb.list);
|
this.list.push(...qb.list);
|
||||||
}
|
}
|
||||||
appendEnding(qb: QueryBuilder) {
|
appendEnding(qb: QueryBuilder) {
|
||||||
|
if (qb.isEmpty()) return;
|
||||||
this.append(qb);
|
this.append(qb);
|
||||||
this.list.push([false, ";"]);
|
this.list.push([false, ";"]);
|
||||||
}
|
}
|
||||||
|
@ -72,8 +74,8 @@ export class selectQuery {
|
||||||
this.whereD = m;
|
this.whereD = m;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
groupByD: Attribute[] = [];
|
groupByD: (Attribute | AttributeAlias)[] = [];
|
||||||
groupBy(a: Attribute[]) {
|
groupBy(a: (Attribute | AttributeAlias)[]) {
|
||||||
this.groupByD = a;
|
this.groupByD = a;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -82,9 +84,22 @@ export class selectQuery {
|
||||||
this.havingD = m;
|
this.havingD = m;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
orderByD: ([allModifierInput, order])[] = [];
|
||||||
|
addOrderBy(a: allModifierInput, o: order = order.ASC) {
|
||||||
|
this.orderByD.push([a, o]);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
orderBY(a: allModifierInput, o: order = order.ASC) {
|
||||||
|
this.orderByD = [[a, o]];
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
limitD: number | null = null;
|
limitD: number | null = null;
|
||||||
limit(i: number) {
|
limitOffsetD: number | null = null;
|
||||||
|
limit(i: number, offset: number = 0) {
|
||||||
this.limitD = i;
|
this.limitD = i;
|
||||||
|
this.limitOffsetD = offset;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,11 +107,12 @@ export class selectQuery {
|
||||||
return handler.querys.select(handler, this);
|
return handler.querys.select(handler, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
async query(db: DB) {
|
async query(db: DB, printQuery = false) {
|
||||||
const handler = db.getHandler();
|
const handler = db.getHandler();
|
||||||
const builder = this.serialize(handler);
|
const builder = this.serialize(handler);
|
||||||
const s = handler.builders.query(builder);
|
const s = handler.builders.query(builder);
|
||||||
return await db.query(s);
|
let readResp = await db.query(s, printQuery);
|
||||||
|
return handler.responses.readResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,11 +145,12 @@ export class insertQuery {
|
||||||
serialize(handler: Handler): QueryBuilder {
|
serialize(handler: Handler): QueryBuilder {
|
||||||
return handler.querys.insert(handler, this);
|
return handler.querys.insert(handler, this);
|
||||||
}
|
}
|
||||||
async query(db: DB) {
|
async query(db: DB, printQuery = false) {
|
||||||
const handler = db.getHandler();
|
const handler = db.getHandler();
|
||||||
const builder = this.serialize(handler);
|
const builder = this.serialize(handler);
|
||||||
const s = handler.builders.query(builder);
|
const s = handler.builders.query(builder);
|
||||||
return await db.query(s);
|
let readResp = await db.query(s, printQuery);
|
||||||
|
return handler.responses.insertResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -157,11 +174,12 @@ export class updateQuery {
|
||||||
serialize(handler: Handler): QueryBuilder {
|
serialize(handler: Handler): QueryBuilder {
|
||||||
return handler.querys.update(handler, this);
|
return handler.querys.update(handler, this);
|
||||||
}
|
}
|
||||||
async query(db: DB) {
|
async query(db: DB, printQuery = false) {
|
||||||
const handler = db.getHandler();
|
const handler = db.getHandler();
|
||||||
const builder = this.serialize(handler);
|
const builder = this.serialize(handler);
|
||||||
const s = handler.builders.query(builder);
|
const s = handler.builders.query(builder);
|
||||||
return await db.query(s);
|
let readResp = await db.query(s, printQuery);
|
||||||
|
return handler.responses.writeResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,10 +196,11 @@ export class removeQuery {
|
||||||
serialize(handler: Handler): QueryBuilder {
|
serialize(handler: Handler): QueryBuilder {
|
||||||
return handler.querys.remove(handler, this);
|
return handler.querys.remove(handler, this);
|
||||||
}
|
}
|
||||||
async query(db: DB) {
|
async query(db: DB, printQuery = false) {
|
||||||
const handler = db.getHandler();
|
const handler = db.getHandler();
|
||||||
const builder = this.serialize(handler);
|
const builder = this.serialize(handler);
|
||||||
const s = handler.builders.query(builder);
|
const s = handler.builders.query(builder);
|
||||||
return await db.query(s);
|
let readResp = await db.query(s, printQuery);
|
||||||
|
return handler.responses.writeResponse(readResp);
|
||||||
}
|
}
|
||||||
}
|
}
|
37
src/responses.ts
Normal file
37
src/responses.ts
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
import { AttributeAlias } from "./alias.js";
|
||||||
|
import { Attribute } from "./db.js";
|
||||||
|
import { primaryData } from "./types.js";
|
||||||
|
|
||||||
|
export class singleResponse {
|
||||||
|
[key: string]: primaryData | any;
|
||||||
|
constructor(r: object) {
|
||||||
|
Object.assign(this, r);
|
||||||
|
}
|
||||||
|
getVal(a: Attribute | AttributeAlias) {
|
||||||
|
return this[a.toString()];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class readResponse extends Array<singleResponse>{
|
||||||
|
constructor(rows: ({ [key: string]: primaryData })[]) {
|
||||||
|
super();
|
||||||
|
for(let i = 0; i < rows.length; i++){
|
||||||
|
this[i] = new singleResponse(rows[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class writeResponse {
|
||||||
|
affectedRows: number;
|
||||||
|
constructor(affectedRows: number) {
|
||||||
|
this.affectedRows = affectedRows;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class insertResponse extends writeResponse {
|
||||||
|
insertId: number;
|
||||||
|
constructor(affectedRows: number, insertId: number) {
|
||||||
|
super(affectedRows);
|
||||||
|
this.insertId = insertId;
|
||||||
|
}
|
||||||
|
}
|
3
src/tools.ts
Normal file
3
src/tools.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import crypto from "crypto";
|
||||||
|
|
||||||
|
export const sha256 = (d: any) => crypto.createHash('sha256').update(String(d)).digest('base64');
|
28
src/types.ts
28
src/types.ts
|
@ -1,12 +1,13 @@
|
||||||
import { Attribute, Table } from "./db";
|
import { Alias, AttributeAlias, TableAlias } from "./alias.js";
|
||||||
import { Aggregation, BooleanModifier, Datatype, Joins, Modifier } from "./dbStructure";
|
import { Attribute, Table } from "./db.js";
|
||||||
import { QueryBuilder, selectQuery } from "./query";
|
import { Aggregation, BooleanModifier, Datatype, Joins, Modifier } from "./dbStructure.js";
|
||||||
|
import { QueryBuilder, selectQuery } from "./query.js";
|
||||||
|
|
||||||
export type primaryData = string | number | boolean | null;
|
export type primaryData = string | number | boolean | null;
|
||||||
export type allModifierInput = primaryData | Modifier | selectQuery | Attribute | Aggregation;
|
export type allModifierInput = primaryData | Modifier | selectQuery | Attribute | AttributeAlias | Aggregation | Alias;
|
||||||
|
|
||||||
export type selectElements = primaryData | Attribute | Aggregation | selectQuery
|
export type selectElements = primaryData | Attribute | AttributeAlias | Aggregation | selectQuery | Alias;
|
||||||
export type selectFromElements = Table | Joins | null;
|
export type selectFromElements = TableAlias | Table | Joins | null;
|
||||||
export type joinElements = Table | Joins;
|
export type joinElements = Table | Joins;
|
||||||
|
|
||||||
export type serializeReturn = [string, primaryData[]];
|
export type serializeReturn = [string, primaryData[]];
|
||||||
|
@ -19,7 +20,7 @@ export type attributeSettings = {
|
||||||
default?: primaryData,
|
default?: primaryData,
|
||||||
notNull?: boolean
|
notNull?: boolean
|
||||||
primaryKey?: boolean,
|
primaryKey?: boolean,
|
||||||
foreginKey?: {
|
foreignKey?: {
|
||||||
link: Attribute,
|
link: Attribute,
|
||||||
onDelete?: onAction,
|
onDelete?: onAction,
|
||||||
onUpdate?: onAction
|
onUpdate?: onAction
|
||||||
|
@ -34,7 +35,7 @@ export type extendedAttributeSettings = {
|
||||||
default?: primaryData,
|
default?: primaryData,
|
||||||
notNull?: boolean
|
notNull?: boolean
|
||||||
primaryKey?: boolean,
|
primaryKey?: boolean,
|
||||||
foreginKey?: {
|
foreignKey?: {
|
||||||
link: Attribute,
|
link: Attribute,
|
||||||
onDelete?: onAction,
|
onDelete?: onAction,
|
||||||
onUpdate?: onAction
|
onUpdate?: onAction
|
||||||
|
@ -57,3 +58,14 @@ export enum joinType {
|
||||||
full,
|
full,
|
||||||
cross
|
cross
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum dbType {
|
||||||
|
mariadb,
|
||||||
|
//mysql,
|
||||||
|
postgres,
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum order {
|
||||||
|
ASC,
|
||||||
|
DESC
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "CommonJS",
|
"target": "ES6",
|
||||||
|
"module": "ES6",
|
||||||
"lib": ["es6"],
|
"lib": ["es6"],
|
||||||
"types": ["node"],
|
"types": ["node"],
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
|
|
Loading…
Reference in a new issue