Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add table name to API nodeAdodb.schemaTableResult? #16

Open
el3um4s opened this issue Feb 23, 2022 · 1 comment
Open

Add table name to API nodeAdodb.schemaTableResult? #16

el3um4s opened this issue Feb 23, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@el3um4s
Copy link
Owner

el3um4s commented Feb 23, 2022

No description provided.

@el3um4s el3um4s added the enhancement New feature or request label Feb 23, 2022
@el3um4s
Copy link
Owner Author

el3um4s commented Aug 17, 2022

nodeAdodb.ts

import { IPC, SendChannels } from "@el3um4s/ipc-for-electron";
import { BrowserWindow } from "electron";

// ragiona come con l'updater (e guarda access-c3)
import path from "path";

import ADODB from "@el3um4s/node-adodb";
import { getTypeDescription } from "./NodeAdodb/listTypeFieldEnum";

const nameAPI = "nodeAdodb";

// to Main
const validSendChannel: SendChannels = {
  query: query,
  execute: execute,
  transaction: transaction,
  schema: schema,
  listTables: listTables,
  schemaTable: schemaTable,
};

// from Main
const validReceiveChannel: string[] = [
  "queryResult",
  "listTablesResult",
  "schemaResult",
  "transactionResult",
  "executeResult",
  "schemaTableResult",
];

const nodeAdodb = new IPC({
  nameAPI,
  validSendChannel,
  validReceiveChannel,
});

export default nodeAdodb;

// Enter here the functions for ElectronJS

async function query(
  mainWindow: BrowserWindow,
  event: Electron.IpcMainEvent,
  message: any
) {
  const { source, sql } = message;

  const stringConnection = getStringConnection(source);
  const result = await ADODB.open(stringConnection).query(sql);
  mainWindow.webContents.send("queryResult", result);
}

async function schema(
  mainWindow: BrowserWindow,
  event: Electron.IpcMainEvent,
  message: any
) {
  const { source, type, criteria, id } = message;
  const stringConnection = getStringConnection(source);
  const result = await ADODB.open(stringConnection).schema(type, criteria, id);
  mainWindow.webContents.send("schemaResult", result);
}

async function listTables(
  mainWindow: BrowserWindow,
  event: Electron.IpcMainEvent,
  message: any
) {
  const { source } = message;

  const stringConnection = getStringConnection(source);
  const result = await ADODB.open(stringConnection).schema(20);
  mainWindow.webContents.send("listTablesResult", result);
}

async function schemaTable(
  mainWindow: BrowserWindow,
  event: Electron.IpcMainEvent,
  message: any
) {
  const { source, tableName } = message;

  const stringConnection = getStringConnection(source);
  const columns: Array<any> = await ADODB.open(stringConnection).schema(4, [
    null,
    null,
    tableName,
  ]);
  const result = columns.map((column) => {
    const { DATA_TYPE } = column;
    const DATA_TYPE_DESCRIPTION = getTypeDescription(DATA_TYPE);
    return { ...column, DATA_TYPE_DESCRIPTION };
  });
  mainWindow.webContents.send("schemaTableResult", result);
}

async function transaction(
  mainWindow: BrowserWindow,
  event: Electron.IpcMainEvent,
  message: any
) {
  const { source, sql } = message;
  const stringConnection = getStringConnection(source);
  const result = await ADODB.open(stringConnection).transaction(sql);
  mainWindow.webContents.send("transactionResult", result);
}

async function execute(
  mainWindow: BrowserWindow,
  event: Electron.IpcMainEvent,
  message: any
) {
  const { source, sql, scalar } = message;
  const stringConnection = getStringConnection(source);
  const result = await ADODB.open(stringConnection).execute(sql, scalar);
  mainWindow.webContents.send("executeResult", result);
}

function getStringConnection(positionDB: string): string {
  const format = path.extname(positionDB);
  if (format.toLowerCase() == ".mdb") {
    return `Provider=Microsoft.Jet.OLEDB.4.0;Data Source=${positionDB};`;
  } else {
    return `Provider=Microsoft.ACE.OLEDB.12.0;Data Source=${positionDB};Persist Security Info=False;`;
  }
}

async function _listTables(connection: ADODB.open): Promise<any[]> {
  // TABLE, LINK, ACCESS TABLE, SYSTEM TABLE, VIEW
  const result: Array<any> = [];
  try {
    const sc: Array<any> = await connection.schema(20);
    sc.forEach((table) => {
      const {
        TABLE_CATALOG,
        TABLE_SCHEMA,
        TABLE_NAME,
        TABLE_TYPE,
        TABLE_GUID,
        DESCRIPTION,
        TABLE_PROPID,
        DATE_CREATED,
        DATE_MODIFIED,
      } = table;
      const tableInfo = { TABLE_NAME, TABLE_TYPE, DATE_CREATED, DATE_MODIFIED };
      result.push(tableInfo);
    });
  } catch (error) {
    console.log(error);
  }
  return result;
}

NodeAdodb/listTypeFieldEnum.ts

type TypeField = {
    value: number;
    constant: string;
    description: string;
}

const listTypeFieldEnum: Array<TypeField> = [
    { value: 8192, constant: "adArray", description: "Array" },
    { value: 20, constant: "adBigInt", description: "BigInt" },
    { value: 128, constant: "adBinary", description: "Binary" },
    { value: 11, constant: "adBoolean", description: "Boolean" },
    { value: 8, constant: "adBSTR", description: "BSTR" },
    { value: 136, constant: "adChapter", description: "Chapter" },
    { value: 129, constant: "adChar", description: "Char" },
    { value: 6, constant: "adCurrency", description: "Currency" },
    { value: 7, constant: "adDate", description: "Date" },
    { value: 133, constant: "adDBDate", description: "DBDate" },
    { value: 134, constant: "adDBTime", description: "DBTime" },
    { value: 135, constant: "adDBTimeStamp", description: "DBTimeStamp" },
    { value: 14, constant: "adDecimal", description: "Decimal" },
    { value: 5, constant: "adDouble", description: "Double" },
    { value: 0, constant: "adEmpty", description: "Empty" },
    { value: 10, constant: "adError", description: "Error" },
    { value: 64, constant: "adFileTime", description: "FileTime" },
    { value: 72, constant: "adGUID", description: "GUID" },
    { value: 9, constant: "adIDispatch", description: "IDispatch" },
    { value: 3, constant: "adInteger", description: "Integer" },
    { value: 13, constant: "adIUnknown", description: "IUnknown" },
    { value: 205, constant: "adLongVarBinary", description: "LongVarBinary" },
    { value: 201, constant: "adLongVarChar", description: "LongVarChar" },
    { value: 203, constant: "adLongVarWChar", description: "LongVarWChar" },
    { value: 131, constant: "adNumeric", description: "Numeric" },
    { value: 138, constant: "adPropVariant", description: "PropVariant" },
    { value: 4, constant: "adSingle", description: "Single" },
    { value: 2, constant: "adSmallInt", description: "SmallInt" },
    { value: 16, constant: "adTinyInt", description: "TinyInt" },
    { value: 21, constant: "adUnsignedBigInt", description: "UnsignedBigInt" },
    { value: 19, constant: "adUnsignedInt", description: "UnsignedInt" },
    { value: 18, constant: "adUnsignedSmallInt", description: "UnsignedSmallInt" },
    { value: 17, constant: "adUnsignedTinyInt", description: "UnsignedTinyInt" },
    { value: 132, constant: "adUserDefined", description: "UserDefined" },
    { value: 204, constant: "adVarBinary", description: "VarBinary" },
    { value: 200, constant: "adVarChar", description: "VarChar" },
    { value: 12, constant: "adVariant", description: "Variant" },
    { value: 139, constant: "adVarNumeric", description: "VarNumeric" },
    { value: 202, constant: "adVarWChar", description: "VarWChar" },
    { value: 130, constant: "adWChar", description: "WChar" },
];

export function getTypeDescription(value: number):string {
    const typeField: TypeField = listTypeFieldEnum.filter( el => el.value == value)[0];
    return typeField.description;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant