diff --git a/Data/IDatabase.cs b/Data/IDatabase.cs index fe526f15..8817585b 100644 --- a/Data/IDatabase.cs +++ b/Data/IDatabase.cs @@ -48,6 +48,12 @@ public abstract class IDatabase public abstract int AlterTable(string query, params DbParameter[] parameters); public abstract int DeleteTable(string query, params DbParameter[] parameters); public abstract int UpdateTable(string query, params DbParameter[] parameters); + + public abstract int CreateIndex(string query, params DbParameter[] parameters); + public abstract int EditIndexName(string query, params DbParameter[] parameters); + public abstract int CreateConstraint(string query, params DbParameter[] parameters); + public abstract int CreateFunction(string query, params DbParameter[] parameters); + public abstract EbDbTypes ConvertToDbType(Type typ); public abstract ColumnColletion GetColumnSchema(string table); public virtual string DBName { get; } diff --git a/Data/MSSQLServer/MSSQLServer.cs b/Data/MSSQLServer/MSSQLServer.cs index ce363d45..6a0a98f4 100644 --- a/Data/MSSQLServer/MSSQLServer.cs +++ b/Data/MSSQLServer/MSSQLServer.cs @@ -715,6 +715,93 @@ public override int DeleteTable(string query, params DbParameter[] parameters) } } + public override int CreateIndex(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as SqlConnection) + { + try + { + con.Open(); + using (SqlCommand cmd = new SqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); + } + } + catch (Exception e) + { + throw e; + } + } + } + public override int CreateFunction(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as SqlConnection) + { + try + { + con.Open(); + using (SqlCommand cmd = new SqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); // This usually returns -1 for CREATE FUNCTION + } + } + catch (Exception e) + { + throw e; + } + } + } + + public override int EditIndexName(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as SqlConnection) + { + try + { + con.Open(); + using (SqlCommand cmd = new SqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); + } + } + catch (Exception e) + { + throw e; + } + } + } + + public override int CreateConstraint(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as SqlConnection) + { + try + { + con.Open(); + using (SqlCommand cmd = new SqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); + } + } + catch (Exception e) + { + throw e; + } + } + } + public override ColumnColletion GetColumnSchema(string table) { ColumnColletion cols = new ColumnColletion(); diff --git a/Data/MySQL/MySqlDB.cs b/Data/MySQL/MySqlDB.cs index 88017804..45f3c4b7 100644 --- a/Data/MySQL/MySqlDB.cs +++ b/Data/MySQL/MySqlDB.cs @@ -805,6 +805,132 @@ public override int DeleteTable(string query, params DbParameter[] parameters) } } + public override int CreateIndex(string query, params DbParameter[] parameters) + { + if (query.Contains(":")) + { + query = query.Replace(":", "@"); + } + + using (var con = GetNewConnection() as MySqlConnection) + { + try + { + con.Open(); + using (MySqlCommand cmd = new MySqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); + } + } + catch (MySqlException myexce) + { + throw myexce; + } + catch (SocketException scket) + { + throw scket; + } + } + } + + public override int CreateFunction(string query, params DbParameter[] parameters) + { + if (query.Contains(":")) + { + query = query.Replace(":", "@"); + } + + using (var con = GetNewConnection() as MySqlConnection) + { + try + { + con.Open(); + using (MySqlCommand cmd = new MySqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); // Usually -1 for CREATE FUNCTION + } + } + catch (MySqlException myexce) + { + throw myexce; + } + catch (SocketException scket) + { + throw scket; + } + } + } + + + public override int EditIndexName(string query, params DbParameter[] parameters) + { + if (query.Contains(":")) + { + query = query.Replace(":", "@"); + } + + using (var con = GetNewConnection() as MySqlConnection) + { + try + { + con.Open(); + using (MySqlCommand cmd = new MySqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); + } + } + catch (MySqlException myexce) + { + throw myexce; + } + catch (SocketException scket) + { + throw scket; + } + } + } + + public override int CreateConstraint(string query, params DbParameter[] parameters) + { + if (query.Contains(":")) + { + query = query.Replace(":", "@"); + } + + using (var con = GetNewConnection() as MySqlConnection) + { + try + { + con.Open(); + using (MySqlCommand cmd = new MySqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); + } + } + catch (MySqlException myexce) + { + throw myexce; + } + catch (SocketException scket) + { + throw scket; + } + } + } + + public override ColumnColletion GetColumnSchema(string table) { ColumnColletion cols = new ColumnColletion(); diff --git a/Data/OracleDB/OracleDB.cs b/Data/OracleDB/OracleDB.cs index bb0a651b..9ad14959 100644 --- a/Data/OracleDB/OracleDB.cs +++ b/Data/OracleDB/OracleDB.cs @@ -623,6 +623,132 @@ public override int DeleteTable(string query, params DbParameter[] parameters) return rslt; } + public override int CreateIndex(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as OracleConnection) + { + try + { + con.Open(); + using (OracleCommand cmd = new OracleCommand(query, con)) + { + cmd.BindByName = true; + + if (Regex.IsMatch(query, @"\:+") && parameters != null && parameters.Length > 0) + { + cmd.Parameters.AddRange(parameters); + } + + return cmd.ExecuteNonQuery(); + } + } + catch (OracleException orcl) + { + Console.WriteLine(orcl.Message); + throw orcl; + } + catch (SocketException scket) + { + throw scket; + } + } + } + + public override int CreateFunction(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as OracleConnection) + { + try + { + con.Open(); + using (OracleCommand cmd = new OracleCommand(query, con)) + { + cmd.BindByName = true; + + if (Regex.IsMatch(query, @"\:+") && parameters != null && parameters.Length > 0) + { + cmd.Parameters.AddRange(parameters); + } + + return cmd.ExecuteNonQuery(); // Usually returns -1 for CREATE FUNCTION + } + } + catch (OracleException orcl) + { + Console.WriteLine(orcl.Message); + throw orcl; + } + catch (SocketException scket) + { + throw scket; + } + } + } + + + public override int EditIndexName(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as OracleConnection) + { + try + { + con.Open(); + using (OracleCommand cmd = new OracleCommand(query, con)) + { + cmd.BindByName = true; + + if (Regex.IsMatch(query, @"\:+") && parameters != null && parameters.Length > 0) + { + cmd.Parameters.AddRange(parameters); + } + + return cmd.ExecuteNonQuery(); + } + } + catch (OracleException orcl) + { + Console.WriteLine(orcl.Message); + throw orcl; + } + catch (SocketException scket) + { + throw scket; + } + } + } + + public override int CreateConstraint(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as OracleConnection) + { + try + { + con.Open(); + using (OracleCommand cmd = new OracleCommand(query, con)) + { + cmd.BindByName = true; + + if (Regex.IsMatch(query, @"\:+") && parameters != null && parameters.Length > 0) + { + cmd.Parameters.AddRange(parameters); + } + + return cmd.ExecuteNonQuery(); + } + } + catch (OracleException orcl) + { + Console.WriteLine(orcl.Message); + throw orcl; + } + catch (SocketException scket) + { + throw scket; + } + } + } + + public override ColumnColletion GetColumnSchema(string table) { ColumnColletion cols = new ColumnColletion(); diff --git a/Data/PostgreSQL/PGSQLDatabase.cs b/Data/PostgreSQL/PGSQLDatabase.cs index 79cdad77..8f5855c1 100644 --- a/Data/PostgreSQL/PGSQLDatabase.cs +++ b/Data/PostgreSQL/PGSQLDatabase.cs @@ -9,6 +9,7 @@ using Npgsql; using Npgsql.Schema; using NpgsqlTypes; +using Oracle.ManagedDataAccess.Client; using ProtoBuf; using System; using System.Collections.Generic; @@ -16,6 +17,7 @@ using System.Data; using System.Data.Common; using System.Net.Sockets; +using System.Text.RegularExpressions; namespace ExpressBase.Common { @@ -854,6 +856,127 @@ public override int DeleteTable(string query, params DbParameter[] parameters) } } } + public override int CreateIndex(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as NpgsqlConnection) + { + try + { + con.Open(); + using (NpgsqlCommand cmd = new NpgsqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); + } + } + catch (Npgsql.NpgsqlException npgse) + { + if ((uint)npgse.ErrorCode == 0x80004005) + NpgsqlConnection.ClearPool(con); + throw npgse; + } + catch (SocketException scket) + { + throw scket; + } + } + } + + + public override int EditIndexName(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as NpgsqlConnection) + { + try + { + con.Open(); + using (NpgsqlCommand cmd = new NpgsqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); + } + } + catch (Npgsql.NpgsqlException npgse) + { + if ((uint)npgse.ErrorCode == 0x80004005) + NpgsqlConnection.ClearPool(con); + throw npgse; + } + catch (SocketException scket) + { + throw scket; + } + } + } + public override int CreateConstraint(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as NpgsqlConnection) + { + try + { + con.Open(); + using (var cmd = new NpgsqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + { + cmd.Parameters.AddRange(parameters); + } + + return cmd.ExecuteNonQuery(); + } + } + catch (NpgsqlException npgse) + { + if ((uint)npgse.ErrorCode == 0x80004005) + { + NpgsqlConnection.ClearPool(con); + } + throw new Exception("NpgsqlException occurred while creating constraint.", npgse); + } + catch (SocketException scket) + { + throw new Exception("SocketException occurred while creating constraint.", scket); + } + catch (Exception ex) + { + throw new Exception("An unexpected error occurred while creating constraint.", ex); + } + } + } + + public override int CreateFunction(string query, params DbParameter[] parameters) + { + using (var con = GetNewConnection() as NpgsqlConnection) + { + try + { + con.Open(); + using (NpgsqlCommand cmd = new NpgsqlCommand(query, con)) + { + if (parameters != null && parameters.Length > 0) + cmd.Parameters.AddRange(parameters); + + return cmd.ExecuteNonQuery(); // usually returns -1 for CREATE FUNCTION + } + } + catch (Npgsql.NpgsqlException npgse) + { + if ((uint)npgse.ErrorCode == 0x80004005) + NpgsqlConnection.ClearPool(con); + throw npgse; + } + catch (SocketException scket) + { + throw scket; + } + } + } + + public override ColumnColletion GetColumnSchema(string table) { @@ -1292,62 +1415,108 @@ public override string EB_GETDBCLIENTTTABLES get { return @" - SELECT Q1.table_name, Q1.table_schema, i.indexname FROM - (SELECT - table_name, table_schema - FROM - information_schema.tables s - WHERE - table_schema != 'pg_catalog' - AND table_schema != 'information_schema' - AND table_type='BASE TABLE' - AND table_name NOT LIKE '{0}')Q1 - LEFT JOIN - pg_indexes i - ON - Q1.table_name = i.tablename ORDER BY tablename; - - - SELECT - table_name, column_name, data_type - FROM - information_schema.columns - WHERE - table_schema != 'pg_catalog' AND - table_schema != 'information_schema' AND - table_name NOT LIKE '{0}'AND - is_updatable != 'NO' - ORDER BY table_name; - - SELECT - c.conname AS constraint_name, - c.contype AS constraint_type, - tbl.relname AS tabless, - ARRAY_AGG(col.attname - ORDER BY - u.attposition) - AS columns, - pg_get_constraintdef(c.oid) AS definition - FROM - pg_constraint c - JOIN - LATERAL UNNEST(c.conkey) WITH - ORDINALITY AS u(attnum, attposition) ON TRUE - JOIN - pg_class tbl ON tbl.oid = c.conrelid - JOIN - pg_namespace sch ON sch.oid = tbl.relnamespace - JOIN - pg_attribute col ON(col.attrelid = tbl.oid AND col.attnum = u.attnum) - WHERE - tbl.relname NOT LIKE '{0}' - GROUP BY - constraint_name, constraint_type, tabless, definition - ORDER BY - tabless;"; +-- TABLES + INDEXES +SELECT Q1.table_name, Q1.table_schema, i.indexname FROM +( + SELECT + table_name, table_schema + FROM + information_schema.tables s + WHERE + table_schema != 'pg_catalog' + AND table_schema != 'information_schema' + AND table_type='BASE TABLE' + AND ( + table_name NOT LIKE 'eb_%' + OR table_name IN ( + 'eb_browser_exceptions','eb_email_logs','eb_downloads','eb_fin_years','eb_fin_years_lines','eb_files_bytea', + 'eb_files_ref','eb_files_ref_variations','eb_role2location','eb_role2permission','eb_role2role','eb_role2user', + 'eb_roles','eb_signin_log','eb_user2usergroup','eb_usersanonymous','eb_usergroup','eb_users','eb_userstatus', + 'eb_public_holidays','eb_notifications','eb_user_types','eb_my_actions','eb_approval_lines','eb_sms_logs', + 'eb_executionlogs','eb_locations','eb_location_types' + ) + ) +) AS Q1 +LEFT JOIN pg_indexes i ON Q1.table_name = i.tablename +ORDER BY Q1.table_name; + +-- COLUMNS +SELECT + table_name, column_name, data_type +FROM + information_schema.columns +WHERE + table_schema != 'pg_catalog' + AND table_schema != 'information_schema' + AND is_updatable != 'NO' + AND ( + table_name NOT LIKE 'eb_%' + OR table_name IN ( + 'eb_browser_exceptions','eb_email_logs','eb_downloads','eb_fin_years','eb_fin_years_lines','eb_files_bytea', + 'eb_files_ref','eb_files_ref_variations','eb_role2location','eb_role2permission','eb_role2role','eb_role2user', + 'eb_roles','eb_signin_log','eb_user2usergroup','eb_usersanonymous','eb_usergroup','eb_users','eb_userstatus', + 'eb_public_holidays','eb_notifications','eb_user_types','eb_my_actions','eb_approval_lines','eb_sms_logs', + 'eb_executionlogs','eb_locations','eb_location_types' + ) + ) +ORDER BY table_name; + +-- CONSTRAINTS +SELECT + c.conname AS constraint_name, + c.contype AS constraint_type, + tbl.relname AS table_name, + ARRAY_AGG(col.attname ORDER BY u.attposition) AS columns, + pg_get_constraintdef(c.oid) AS definition +FROM + pg_constraint c +JOIN + LATERAL UNNEST(c.conkey) WITH ORDINALITY AS u(attnum, attposition) ON TRUE +JOIN + pg_class tbl ON tbl.oid = c.conrelid +JOIN + pg_namespace sch ON sch.oid = tbl.relnamespace +JOIN + pg_attribute col ON(col.attrelid = tbl.oid AND col.attnum = u.attnum) +WHERE + ( + tbl.relname NOT LIKE 'eb_%' + OR tbl.relname IN ( + 'eb_browser_exceptions','eb_email_logs','eb_downloads','eb_fin_years','eb_fin_years_lines','eb_files_bytea', + 'eb_files_ref','eb_files_ref_variations','eb_role2location','eb_role2permission','eb_role2role','eb_role2user', + 'eb_roles','eb_signin_log','eb_user2usergroup','eb_usersanonymous','eb_usergroup','eb_users','eb_userstatus', + 'eb_public_holidays','eb_notifications','eb_user_types','eb_my_actions','eb_approval_lines','eb_sms_logs', + 'eb_executionlogs','eb_locations','eb_location_types' + ) + ) +GROUP BY + constraint_name, constraint_type, table_name, definition +ORDER BY + table_name; + +-- FUNCTIONS (Optional) +SELECT + n.nspname AS function_schema, + p.proname || '(' || + regexp_replace( + COALESCE(pg_get_function_identity_arguments(p.oid), ''), + '\s+DEFAULT[^,)]*', '', 'gi' + ) || ')' AS function_name, + pg_get_functiondef(p.oid) AS definition +FROM + pg_proc p +JOIN + pg_namespace n ON n.oid = p.pronamespace +WHERE + n.nspname NOT IN ('pg_catalog', 'information_schema') +ORDER BY + function_schema, function_name; +"; } } + + //.......OBJECTS QUERIES..... public override string EB_GET_ALL_TAGS diff --git a/Enums/Enums.cs b/Enums/Enums.cs index 5c6d0999..4cd7bed4 100644 --- a/Enums/Enums.cs +++ b/Enums/Enums.cs @@ -406,14 +406,19 @@ public enum DBOperations { SELECT, CREATE, + EDIT_INDEX_NAME, + CREATE_CONSTRAINT, + CREATE_INDEX, INSERT, DELETE, ALTER, UPDATE, DROP, - TRUNCATE + TRUNCATE, + CREATE_FUNCTION } + public enum MyActionTypes { Approval = 1,