/// <summary> /// Initializes a new instance of the <see cref="OmniDB.Session"/> class. /// </summary> /// <param name="p_user_id">User ID.</param> /// <param name="p_user_name">Username.</param> /// <param name="p_database">Database that manages the application.</param> public Session(string p_user_id, string p_user_name, OmniDatabase.Generic p_database, string p_editor_theme, string p_theme_type, string p_theme_id, string p_editor_font_size, int p_enable_chat) { v_omnidb_database = p_database; v_database_index = -1; v_user_id = p_user_id; v_user_name = p_user_name; v_databases = new System.Collections.Generic.List <OmniDatabase.Generic> (); v_theme_id = p_theme_id; v_editor_theme = p_editor_theme; v_theme_type = p_theme_type; v_editor_font_size = p_editor_font_size; v_enable_omnichat = p_enable_chat; if (Environment.OSVersion.ToString().ToLower().Contains("unix")) { v_current_os = "unix"; } else { v_current_os = "windows"; } RefreshDatabaseList(); }
public static AjaxReturn GetProcedureDefinition(int p_database_index, string p_procedure) { AjaxReturn v_return = new AjaxReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session["OMNIDB_SESSION"]; if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } OmniDatabase.Generic v_database = v_session.v_databases[p_database_index]; try { string v_procedure_definition = v_database.GetProcedureDefinition(p_procedure); v_return.v_data = v_procedure_definition; } catch (Spartacus.Database.Exception e) { v_return.v_error = true; v_return.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); return(v_return); } return(v_return); }
public static AjaxReturn GetTreeInfo() { AjaxReturn v_return = new AjaxReturn(); TreeReturn v_tree_data = new TreeReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"]; if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } v_tree_data.v_mode = "database"; TreeDatabaseReturn v_database_return = new TreeDatabaseReturn(); OmniDatabase.Generic v_database = v_session.GetSelectedDatabase(); v_database_return.v_database = v_database.GetName(); v_database_return.v_has_schema = v_database.v_has_schema; if (v_database_return.v_has_schema) { v_database_return.v_schema = v_database.v_schema; } v_tree_data.v_database_return = v_database_return; v_return.v_data = v_tree_data; return(v_return); }
/// <summary> /// Add a new database to session database list. /// </summary> /// <param name="p_database">Database.</param> public void AddDatabase(OmniDatabase.Generic p_database) { if (v_databases.Count == 0) { v_database_index = 0; } v_databases.Add(p_database); }
/// <summary> /// Execute the specified SQL string. /// </summary> /// <param name="p_database_index">Database index.</param> /// <param name="p_sql">SQL string.</param> /// <param name="p_loghistory">If set to <c>true</c>, logs the command to the history.</param> /// <param name="p_logmigration">If set to <c>true</c>, logs the command to the migration.</param> public void Execute(OmniDatabase.Generic p_database, string p_sql, bool p_loghistory, bool p_logmigration) { p_database.v_connection.Execute(p_sql); if (p_loghistory) { this.LogHistory(p_sql); } if (p_logmigration) { this.LogMigration(p_database, p_sql); } }
/// <summary> /// Queries the specified SQL string, limited by a number of registers. /// </summary> /// <param name="p_database_index">Database index.</param> /// <param name="p_sql">SQL string.</param> /// <param name="p_count">Number of registers.</param> /// <param name="p_loghistory">If set to <c>true</c>, logs the command to the history.</param> /// <param name="p_logmigration">If set to <c>true</c>, logs the command to the migration.</param> public System.Data.DataTable QueryDataLimited(OmniDatabase.Generic p_database, string p_sql, int p_count, bool p_loghistory, bool p_logmigration) { System.Data.DataTable v_table = p_database.QueryDataLimited(p_sql, p_count); if (p_loghistory) { this.LogHistory(p_sql); } if (p_logmigration) { this.LogMigration(p_database, p_sql); } return(v_table); }
/// <summary> /// Queries the specified SQL string. /// </summary> /// <param name="p_database_index">Database index.</param> /// <param name="p_sql">SQL string.</param> /// <param name="p_loghistory">If set to <c>true</c>, logs the command to the history.</param> /// <param name="p_logmigration">If set to <c>true</c>, logs the command to the migration.</param> public System.Data.DataTable Query(OmniDatabase.Generic p_database, string p_sql, bool p_loghistory, bool p_logmigration) { System.Data.DataTable v_table = p_database.v_connection.Query(p_sql, "Data"); if (p_loghistory) { this.LogHistory(p_sql); } if (p_logmigration) { this.LogMigration(p_database, p_sql); } return(v_table); }
public static AjaxReturn GetFKs(int p_database_index, string p_table) { AjaxReturn v_return = new AjaxReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"]; if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } OmniDatabase.Generic v_database = v_session.v_databases[p_database_index]; System.Collections.Generic.List <System.Collections.Generic.List <string> > v_list_fks = new System.Collections.Generic.List <System.Collections.Generic.List <string> > (); try { System.Data.DataTable v_table_fks = v_database.QueryTablesForeignKeys(p_table); foreach (System.Data.DataRow v_table_fk in v_table_fks.Rows) { System.Collections.Generic.List <string> v_fk = new System.Collections.Generic.List <string>(); v_fk.Add(v_table_fk["constraint_name"].ToString()); v_fk.Add(v_table_fk["column_name"].ToString()); v_fk.Add(v_table_fk["r_table_name"].ToString()); v_fk.Add(v_table_fk["r_column_name"].ToString()); v_fk.Add(v_table_fk["delete_rule"].ToString()); v_fk.Add(v_table_fk["update_rule"].ToString()); v_list_fks.Add(v_fk); } } catch (Spartacus.Database.Exception e) { v_return.v_error = true; v_return.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); return(v_return); } v_return.v_data = v_list_fks; return(v_return); }
public static AjaxReturn GetTables(int p_database_index) { AjaxReturn v_return = new AjaxReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session["OMNIDB_SESSION"]; if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } OmniDatabase.Generic v_database = v_session.v_databases[p_database_index]; System.Collections.Generic.List <TableReturn> v_list_tables = new System.Collections.Generic.List <TableReturn>(); try { System.Data.DataTable v_tables = v_database.QueryTables(false); foreach (System.Data.DataRow v_table in v_tables.Rows) { TableReturn v_obj = new TableReturn(); v_obj.v_name = v_table["table_name"].ToString(); v_obj.v_has_primary_keys = v_database.v_has_primary_keys; v_obj.v_has_foreign_keys = v_database.v_has_foreign_keys; v_obj.v_has_uniques = v_database.v_has_uniques; v_obj.v_has_indexes = v_database.v_has_indexes; v_list_tables.Add(v_obj); } } catch (Spartacus.Database.Exception e) { v_return.v_error = true; v_return.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); return(v_return); } v_return.v_data = v_list_tables; return(v_return); }
public static AjaxReturn GetColumns(int p_database_index, string p_table) { AjaxReturn v_return = new AjaxReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"]; if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } OmniDatabase.Generic v_database = v_session.v_databases[p_database_index]; System.Collections.Generic.List <System.Collections.Generic.List <string> > v_list_columns = new System.Collections.Generic.List <System.Collections.Generic.List <string> > (); try { System.Data.DataTable v_columns = v_database.QueryTablesFields(p_table); foreach (System.Data.DataRow v_column in v_columns.Rows) { System.Collections.Generic.List <string> v_column_details = new System.Collections.Generic.List <string>(); v_column_details.Add(v_column["column_name"].ToString()); v_column_details.Add(v_column["data_type"].ToString()); v_column_details.Add(v_column["data_length"].ToString()); v_column_details.Add(v_column["nullable"].ToString()); v_list_columns.Add(v_column_details); } } catch (Spartacus.Database.Exception e) { v_return.v_error = true; v_return.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); return(v_return); } v_return.v_data = v_list_columns; return(v_return); }
private void LogMigration(OmniDatabase.Generic p_database, string p_sql) { OmniDatabase.Generic v_database = p_database; System.Data.DataTable v_command_table; string v_schema; if (v_database.v_has_schema) { v_schema = v_database.v_schema + "."; } else { v_schema = ""; } try { int v_migid = int.Parse(v_database.v_connection.ExecuteScalar("select max(mig_id) from " + v_schema + "omnidb_migrations where mig_status = 'E'")); int v_numcommands = int.Parse(v_database.v_connection.ExecuteScalar("select count(*) from " + v_schema + "omnidb_mig_commands")); if (v_numcommands > 0) { v_command_table = v_database.v_connection.Query("select max(cmd_id)+1 as next_id from " + v_schema + "omnidb_mig_commands", "Command List"); } else { v_command_table = v_database.v_connection.Query("select 1 as next_id", "Command List"); } v_database.v_connection.Execute("insert into " + v_schema + "omnidb_mig_commands values ( " + v_migid.ToString() + "," + v_command_table.Rows [0] ["next_id"].ToString() + ",'" + DateTime.Now + "','" + v_user_name + "','" + p_sql.Replace("'", "''") + "')"); } catch { } }
public static AjaxReturn GetFunctions(int p_database_index) { AjaxReturn v_return = new AjaxReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session["OMNIDB_SESSION"]; System.Collections.Generic.List <FunctionReturn> v_function_data_list = new System.Collections.Generic.List <FunctionReturn>(); if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } OmniDatabase.Generic v_database = v_session.v_databases[p_database_index]; try { System.Data.DataTable v_tables = v_database.QueryFunctions(); foreach (System.Data.DataRow v_table in v_tables.Rows) { FunctionReturn v_function_data = new FunctionReturn(); v_function_data.v_name = v_table["name"].ToString(); v_function_data.v_id = v_table["id"].ToString(); v_function_data_list.Add(v_function_data); } } catch (Spartacus.Database.Exception e) { v_return.v_error = true; v_return.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); return(v_return); } v_return.v_data = v_function_data_list; return(v_return); }
public static AjaxReturn GetProcedureFields(int p_database_index, string p_procedure) { AjaxReturn v_return = new AjaxReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"]; if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } OmniDatabase.Generic v_database = v_session.v_databases[p_database_index]; System.Collections.Generic.List <ProcedureFieldReturn> v_list_fields = new System.Collections.Generic.List <ProcedureFieldReturn> (); try { System.Data.DataTable v_fields = v_database.QueryProcedureFields(p_procedure); foreach (System.Data.DataRow v_field in v_fields.Rows) { ProcedureFieldReturn v_field_data = new ProcedureFieldReturn(); v_field_data.v_name = v_field["name"].ToString(); v_field_data.v_type = v_field["type"].ToString(); v_list_fields.Add(v_field_data); } } catch (Spartacus.Database.Exception e) { v_return.v_error = true; v_return.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); return(v_return); } v_return.v_data = v_list_fields; return(v_return); }
public static AjaxReturn GetTables() { AjaxReturn v_return = new AjaxReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"]; if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } OmniDatabase.Generic v_database = v_session.GetSelectedDatabase(); System.Collections.Generic.List <string> v_list_tables = new System.Collections.Generic.List <string> (); try { System.Data.DataTable v_tables = v_database.QueryTables(false); foreach (System.Data.DataRow v_table in v_tables.Rows) { v_list_tables.Add(v_table["table_name"].ToString()); } } catch (Spartacus.Database.Exception e) { v_return.v_error = true; v_return.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); return(v_return); } v_return.v_data = v_list_tables; return(v_return); }
public static AjaxReturn GetTreeInfo(int p_database_index) { AjaxReturn v_return = new AjaxReturn(); TreeReturn v_tree_data = new TreeReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"]; if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } v_tree_data.v_mode = "database"; TreeDatabaseReturn v_database_return = new TreeDatabaseReturn(); OmniDatabase.Generic v_database = v_session.v_databases[p_database_index]; v_database_return.v_database = v_database.GetName(); v_database_return.v_has_schema = v_database.v_has_schema; v_database_return.v_has_functions = v_database.v_has_functions; v_database_return.v_has_procedures = v_database.v_has_procedures; v_database_return.v_has_sequences = v_database.v_has_sequences; if (v_database_return.v_has_schema) { v_database_return.v_schema = v_database.v_schema; } v_tree_data.v_database_return = v_database_return; v_return.v_data = v_tree_data; return(v_return); }
public static AjaxReturn SignIn(string p_username, string p_pwd) { AjaxReturn v_return = new AjaxReturn(); Spartacus.Utils.Cryptor v_cryptor = new Spartacus.Utils.Cryptor("omnidb_spartacus"); // Instantiating tool management database. OmniDatabase.Generic v_omnidb_database = OmniDatabase.Generic.InstantiateDatabase("", "0", "sqlite", "", "", System.Web.Configuration.WebConfigurationManager.AppSettings ["OmniDB.Database"].ToString(), "", "", ""); if (p_username == "admin") { string v_encrypted_pwd = System.IO.File.ReadAllText("config/admin.txt"); string v_pwd; try { v_pwd = v_cryptor.Decrypt(v_encrypted_pwd); if (v_pwd == p_pwd) { Session v_session = new Session("-1", p_username, v_omnidb_database, "", "", "", ""); v_session.v_omnidb_version = System.Web.Configuration.WebConfigurationManager.AppSettings ["OmniDB.Version"].ToString(); System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"] = v_session; v_return.v_data = true; } else { System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"] = null; v_return.v_data = false; } } catch (Spartacus.Utils.Exception) { System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"] = null; v_return.v_data = false; } return(v_return); } else { try { // Querying user information. System.Data.DataTable v_user_data = v_omnidb_database.v_connection.Query( "select u.user_id, " + " u.password, " + " t.theme_id, " + " t.theme_name, " + " t.theme_type, " + " u.editor_font_size " + "from users u, " + " themes t " + " where u.theme_id = t.theme_id " + "and u.user_name = '" + p_username + "' ", "db_data"); // If username exists, decrypt password. if (v_user_data.Rows.Count > 0) { string v_pwd; try { v_pwd = v_cryptor.Decrypt(v_user_data.Rows [0] ["password"].ToString()); } catch (Spartacus.Utils.Exception) { v_pwd = v_user_data.Rows [0] ["password"].ToString(); } // If password is correct, set user as logged in, instantiate Session and return true. if (v_pwd == p_pwd) { Session v_session = new Session(v_user_data.Rows [0] ["user_id"].ToString(), p_username, v_omnidb_database, v_user_data.Rows[0]["theme_name"].ToString(), v_user_data.Rows[0]["theme_type"].ToString(), v_user_data.Rows[0]["theme_id"].ToString(), v_user_data.Rows [0] ["editor_font_size"].ToString()); v_session.v_omnidb_version = System.Web.Configuration.WebConfigurationManager.AppSettings ["OmniDB.Version"].ToString(); System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"] = v_session; if (!((Dictionary <string, Session>)System.Web.HttpContext.Current.Application["OMNIDB_SESSION_LIST"]).ContainsKey(v_session.v_user_id)) { ((Dictionary <string, Session>)System.Web.HttpContext.Current.Application["OMNIDB_SESSION_LIST"]).Add(v_session.v_user_id, v_session); } else { ((Dictionary <string, Session>)System.Web.HttpContext.Current.Application["OMNIDB_SESSION_LIST"])[v_session.v_user_id] = v_session; } v_return.v_data = true; } else { System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"] = null; v_return.v_data = false; } } else { System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"] = null; v_return.v_data = false; } } catch (Spartacus.Database.Exception e) { v_return.v_error = true; v_return.v_data = e.v_message; } return(v_return); } }
public static AjaxReturn CompareBases(int p_second_db) { HttpContext.Current.Server.ScriptTimeout = 86400; AjaxReturn v_g1 = new AjaxReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"]; if (v_session == null) { v_g1.v_error = true; v_g1.v_error_id = 1; return v_g1; } System.Collections.Generic.List<System.Collections.Generic.List<string>> v_list_tables = new System.Collections.Generic.List<System.Collections.Generic.List<string>> (); CompareData v_d1 = new CompareData (); CompareColumns v_compare_columns; int v_num_orange_tables = 0; int v_num_green_tables = 0; int v_num_red_tables = 0; int v_num_orange_cols = 0; int v_num_green_cols = 0; int v_num_red_cols = 0; String v_html = ""; String v_log = ""; System.Data.DataTable v_data1; System.Data.DataTable v_data2; OmniDatabase.Generic v_database1 = v_session.GetSelectedDatabase(); OmniDatabase.Generic v_database2 = v_session.v_databases[p_second_db]; try { System.Data.DataTable v_orig_data_columns1 = v_database1.QueryTablesFields(null); System.Data.DataView v_view = new System.Data.DataView(v_orig_data_columns1); v_view.Sort = "table_name asc, column_name asc"; System.Data.DataTable v_data_columns1 = v_view.ToTable(); System.Data.DataTable v_orig_data_columns2 = v_database2.QueryTablesFields(null); v_view = new System.Data.DataView(v_orig_data_columns2); v_view.Sort = "table_name asc, column_name asc"; System.Data.DataTable v_data_columns2 = v_view.ToTable(); int v_pos_counter = 0; int v_count_cols = 0; v_data1 = new System.Data.DataTable (); v_data1.Columns.Add ("table_name"); v_data1.Columns.Add ("num_rows"); v_data1.Columns.Add ("position"); string v_curr_table = v_orig_data_columns1.Rows[0]["table_name"].ToString(); foreach (System.Data.DataRow v_column in v_orig_data_columns1.Rows) { if (v_curr_table!=v_column["table_name"].ToString()) { System.Data.DataRow v_new_row = v_data1.NewRow (); v_new_row ["table_name"] = v_curr_table; v_new_row ["num_rows"] = v_count_cols; v_new_row ["position"] = v_pos_counter; v_data1.Rows.Add (v_new_row); v_pos_counter += v_count_cols; v_count_cols=0; v_curr_table=v_column["table_name"].ToString(); } v_count_cols++; } System.Data.DataRow v_last_row = v_data1.NewRow (); v_last_row ["table_name"] = v_curr_table; v_last_row ["num_rows"] = v_count_cols; v_last_row ["position"] = v_pos_counter; v_data1.Rows.Add (v_last_row); //Console.WriteLine("--------------------"); v_pos_counter = 0; v_count_cols = 0; v_data2 = new System.Data.DataTable (); v_data2.Columns.Add ("table_name"); v_data2.Columns.Add ("num_rows"); v_data2.Columns.Add ("position"); v_curr_table = v_orig_data_columns2.Rows[0]["table_name"].ToString(); foreach (System.Data.DataRow v_column in v_orig_data_columns2.Rows) { if (v_curr_table!=v_column["table_name"].ToString()) { System.Data.DataRow v_new_row = v_data2.NewRow (); v_new_row ["table_name"] = v_curr_table; v_new_row ["num_rows"] = v_count_cols; v_new_row ["position"] = v_pos_counter; v_data2.Rows.Add (v_new_row); v_pos_counter += v_count_cols; v_count_cols=0; v_curr_table=v_column["table_name"].ToString(); } v_count_cols++; } v_last_row = v_data2.NewRow (); v_last_row ["table_name"] = v_curr_table; v_last_row ["num_rows"] = v_count_cols; v_last_row ["position"] = v_pos_counter; v_data2.Rows.Add (v_last_row); int v_counter1 = 0; int v_counter2 = 0; while (v_counter1 < v_data1.Rows.Count) { if (v_counter2 < v_data2.Rows.Count) { int v_compare = String.Compare( v_data2.Rows[v_counter2]["table_name"].ToString(), v_data1.Rows[v_counter1]["table_name"].ToString(), true ); //Console.WriteLine("Compare " + v_compare); // data1[i] nao esta em data2[i] System.Collections.Generic.List<string> v_table_info = new System.Collections.Generic.List<string>(); if (v_compare == 1) { v_table_info.Add(v_data1.Rows[v_counter1]["table_name"].ToString()); v_table_info.Add("-1"); v_html += PrintTable(v_data1.Rows[v_counter1]["table_name"].ToString(),"green","red"); v_html += PrintTableColumns(v_data1.Rows[v_counter1],v_data_columns1,"green","red"); v_num_red_cols += Convert.ToInt32(v_data1.Rows[v_counter1]["num_rows"]); v_counter1++; } // data2[i] nao esta em data1[i] else if (v_compare == -1) { v_table_info.Add(v_data2.Rows[v_counter2]["table_name"].ToString()); v_table_info.Add("1"); v_html += PrintTable(v_data2.Rows[v_counter2]["table_name"].ToString(),"red","green"); v_html += PrintTableColumns(v_data2.Rows[v_counter2],v_data_columns2,"red","green"); v_num_red_cols += Convert.ToInt32(v_data2.Rows[v_counter2]["num_rows"]); v_counter2++; } else { //Console.WriteLine("iguais"); v_compare_columns = CompareTablesColumns(v_data1.Rows[v_counter1],v_data_columns1,v_data2.Rows[v_counter2],v_data_columns2); int v_total_cols = Math.Max(Convert.ToInt32(v_data1.Rows[v_counter1]["num_rows"]),Convert.ToInt32(v_data2.Rows[v_counter2]["num_rows"])); //Console.WriteLine(v_total_cols + " " + v_compare_columns.v_green_items + " " + v_compare_columns.v_orange_items); v_num_green_cols += v_compare_columns.v_green_items; v_num_orange_cols += v_compare_columns.v_orange_items; v_num_red_cols += v_total_cols - v_compare_columns.v_green_items - v_compare_columns.v_orange_items; if (v_total_cols==v_compare_columns.v_green_items) { v_table_info.Add(v_data1.Rows[v_counter1]["table_name"].ToString()); v_table_info.Add("0"); v_html += PrintTable(v_data1.Rows[v_counter1]["table_name"].ToString(),"green","green"); v_num_green_tables++; } else { v_table_info.Add(v_data1.Rows[v_counter1]["table_name"].ToString()); v_table_info.Add("2"); v_html += PrintTable(v_data1.Rows[v_counter1]["table_name"].ToString(),"orange","orange"); v_num_orange_tables++; } v_html += v_compare_columns.v_html; v_counter1++; v_counter2++; } v_list_tables.Add(v_table_info); } else { v_html += PrintTable(v_data1.Rows[v_counter1]["table_name"].ToString(),"green","red"); v_html += PrintTableColumns(v_data1.Rows[v_counter1],v_data_columns1,"green","red"); v_counter1++; } } while (v_counter2 < v_data2.Rows.Count) { System.Collections.Generic.List<string> v_table_info = new System.Collections.Generic.List<string>(); v_table_info.Add(v_data2.Rows[v_counter2]["table_name"].ToString()); v_table_info.Add("0"); v_list_tables.Add(v_table_info); v_html += PrintTable(v_data2.Rows[v_counter2]["table_name"].ToString(),"red","green"); v_html += PrintTableColumns(v_data2.Rows[v_counter2],v_data_columns2,"red","green"); v_counter2++; } int v_total_tables = Math.Max(v_data1.Rows.Count,v_data2.Rows.Count); v_num_red_tables = v_total_tables - v_num_green_tables - v_num_orange_tables; //Console.WriteLine("TABLES -> Greens: " + v_num_green_tables + " - Oranges: " + v_num_orange_tables + " - Reds: " + v_num_red_tables); //Console.WriteLine("COLUMNS -> Greens: " + v_num_green_cols + " - Oranges: " + v_num_orange_cols + " - Reds: " + v_num_red_cols); } catch (Spartacus.Database.Exception e) { System.Console.WriteLine(e.v_message); } v_d1.v_html = v_html; v_d1.v_log = v_log; v_d1.v_num_green_tables = v_num_green_tables; v_d1.v_num_orange_tables = v_num_orange_tables; v_d1.v_num_red_tables = v_num_red_tables; v_d1.v_num_green_cols = v_num_green_cols; v_d1.v_num_orange_cols = v_num_orange_cols; v_d1.v_num_red_cols = v_num_red_cols; //v_g1.v_data = v_d1; v_g1.v_data = v_list_tables; return v_g1; }
/// <summary> /// Handler called when a new message from a client arrives to the server. /// </summary> /// <param name="p_webSocketSession">The connection session.</param> /// <param name="p_message">The message send by the client session.</param> private void NewMessageReceived(WebSocketSession p_webSocketSession, string p_message) { WebSocketMessage v_request = JsonConvert.DeserializeObject <WebSocketMessage>(p_message); if (v_request.v_code == (int)request.Login) { string v_userId = (string)v_request.v_data; if (!p_webSocketSession.Cookies.ContainsKey("user_id")) { p_webSocketSession.Cookies.Add("user_id", v_userId); } } WebSocketMessage v_response = new WebSocketMessage(); if (!this.v_httpSessions.ContainsKey(p_webSocketSession.Cookies["user_id"])) { v_response.v_error = true; v_response.v_data = "Session Object was destroyed. Please, restart the application."; SendToClient(p_webSocketSession, v_response); return; } Session v_httpSession = this.v_httpSessions[p_webSocketSession.Cookies["user_id"]]; if (v_httpSession == null) { v_response.v_error = true; v_response.v_data = "Session Object was destroyed. Please, restart the application."; SendToClient(p_webSocketSession, v_response); return; } switch (v_request.v_code) { case (int)request.Login: { OmniDatabase.Generic v_database = v_httpSession.v_omnidb_database; List <ChatUser> v_userList = new List <ChatUser>(); try { string v_onlineUsers = ""; for (int i = 0; i < this.v_chatSessions.Count; i++) { if (this.v_chatSessions[i].Cookies.ContainsKey("user_id")) { v_onlineUsers += this.v_chatSessions[i].Cookies["user_id"] + ", "; } } v_onlineUsers = v_onlineUsers.Remove(v_onlineUsers.Length - 2); string v_sql = "select x.*" + "from (" + " select user_id, " + " user_name, " + " 1 as online " + " from users " + " where user_id in ( " + v_onlineUsers + ") " + " " + " union " + " " + " select user_id, " + " user_name, " + " 0 as online " + " from users " + " where user_id not in ( " + v_onlineUsers + ") " + ") x " + "order by x.online desc, x.user_name "; System.Data.DataTable v_table = v_database.v_connection.Query(v_sql, "chat_users"); if (v_table != null && v_table.Rows.Count > 0) { for (int i = 0; i < v_table.Rows.Count; i++) { ChatUser v_user = new ChatUser(); v_user.v_user_id = int.Parse(v_table.Rows[i]["user_id"].ToString()); v_user.v_user_name = v_table.Rows[i]["user_name"].ToString(); v_user.v_user_online = int.Parse(v_table.Rows[i]["online"].ToString()); v_userList.Add(v_user); } } } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } v_response.v_code = (int)response.UserList; v_response.v_data = v_userList; SendToAllClients(v_response); return; } case (int)request.GetOldMessages: { OmniDatabase.Generic v_database = v_httpSession.v_omnidb_database; List <ChatMessage> v_messageList = new List <ChatMessage>(); try { string v_sql = "select mes.mes_in_code, " + " use.user_name, " + " mes.mes_st_text, " + " mes.mes_dt_timestamp " + "from messages mes " + "inner join messages_users meu " + " on mes.mes_in_code = meu.mes_in_code " + "inner join users use " + " on mes.user_id = use.user_id " + "where meu.user_id = " + v_httpSession.v_user_id + " " + " and meu.meu_bo_viewed = 'N';"; System.Data.DataTable v_table = v_database.v_connection.Query(v_sql, "chat_messages"); if (v_table != null && v_table.Rows.Count > 0) { for (int i = 0; i < v_table.Rows.Count; i++) { ChatMessage v_message = new ChatMessage(); v_message.v_message_id = int.Parse(v_table.Rows[i]["mes_in_code"].ToString()); v_message.v_user_name = v_table.Rows[i]["user_name"].ToString(); v_message.v_text = v_table.Rows[i]["mes_st_text"].ToString(); v_message.v_timestamp = v_table.Rows[i]["mes_dt_timestamp"].ToString(); v_messageList.Add(v_message); } } } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } v_response.v_code = (int)response.OldMessages; v_response.v_data = v_messageList; SendToClient(p_webSocketSession, v_response); return; } case (int)request.ViewMessage: { OmniDatabase.Generic v_database = v_httpSession.v_omnidb_database; ChatMessage v_message = JsonConvert.DeserializeObject <ChatMessage>(v_request.v_data.ToString()); try { string v_sql = "update messages_users " + "set meu_bo_viewed = 'Y' " + "where user_id = " + v_httpSession.v_user_id + " " + " and mes_in_code = " + v_message.v_message_id; v_database.v_connection.Execute(v_sql); } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } return; } case (int)request.SendMessage: { OmniDatabase.Generic v_database = v_httpSession.v_omnidb_database; string v_text = (string)v_request.v_data; ChatMessage v_message; try { string v_sql = "insert into messages (" + " mes_st_text, " + " mes_dt_timestamp, " + " user_id " + ") values ( " + " '" + v_text + "', " + " datetime('now', 'localtime'), " + " " + v_httpSession.v_user_id + ");" + "select max(mes_in_code) " + "from messages;"; int v_messsageCode = int.Parse(v_database.v_connection.ExecuteScalar(v_sql)); v_sql = "insert into messages_users (" + " mes_in_code, " + " meu_bo_viewed, " + " user_id " + ")" + "select " + v_messsageCode + ", " + " 'N', " + " use.user_id " + "from users use "; // + //"where use.user_id <> " + v_httpSession.v_user_id + ";"; v_database.v_connection.Execute(v_sql); v_sql = "select mes_dt_timestamp " + "from messages " + "where mes_in_code = " + v_messsageCode; v_message = new ChatMessage(); v_message.v_message_id = v_messsageCode; v_message.v_user_name = v_httpSession.v_user_name; v_message.v_text = v_text; v_message.v_timestamp = v_database.v_connection.ExecuteScalar(v_sql); } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } v_response.v_code = (int)response.NewMessage; v_response.v_data = v_message; SendToAllClients(v_response); return; } default: { v_response.v_error = true; v_response.v_data = "Unrecognized request code."; SendToClient(p_webSocketSession, v_response); return; } } /*Thread v_sendResponse = new Thread(SendResponse); * v_sendResponse.Start((Object)p_webSocketSession);*/ }
/// <summary> /// Handler called when a connection is closed. /// </summary> /// <param name="p_webSocketSession">The connection session.</param> /// <param name="p_reason">The reason why connection was closed.</param> private void SessionClosed(WebSocketSession p_webSocketSession, CloseReason p_reason) { lock (v_chatSessionsSyncRoot) this.v_chatSessions.Remove(p_webSocketSession); if (p_reason == CloseReason.ServerShutdown) { return; } WebSocketMessage v_response = new WebSocketMessage(); if (!this.v_httpSessions.ContainsKey(p_webSocketSession.Cookies["user_id"])) { v_response.v_error = true; v_response.v_data = "Session Object was destroyed. Please, restart the application."; SendToClient(p_webSocketSession, v_response); return; } Session v_httpSession = this.v_httpSessions[p_webSocketSession.Cookies["user_id"]]; if (v_httpSession == null) { v_response.v_error = true; v_response.v_data = "Session Object was destroyed. Please, restart the application."; SendToClient(p_webSocketSession, v_response); return; } OmniDatabase.Generic v_database = v_httpSession.v_omnidb_database; List <ChatUser> v_userList = new List <ChatUser>(); try { string v_onlineUsers = ""; for (int i = 0; i < this.v_chatSessions.Count; i++) { if (this.v_chatSessions[i].Cookies.ContainsKey("user_id")) { v_onlineUsers += this.v_chatSessions[i].Cookies["user_id"] + ", "; } } v_onlineUsers = v_onlineUsers.Remove(v_onlineUsers.Length - 2); string v_sql = "select x.*" + "from (" + " select user_id, " + " user_name, " + " 1 as online " + " from users " + " where user_id in ( " + v_onlineUsers + ") " + " " + " union " + " " + " select user_id, " + " user_name, " + " 0 as online " + " from users " + " where user_id not in ( " + v_onlineUsers + ") " + ") x " + "order by x.online desc, x.user_name "; System.Data.DataTable v_table = v_database.v_connection.Query(v_sql, "chat_users"); if (v_table != null && v_table.Rows.Count > 0) { for (int i = 0; i < v_table.Rows.Count; i++) { ChatUser v_user = new ChatUser(); v_user.v_user_id = int.Parse(v_table.Rows[i]["user_id"].ToString()); v_user.v_user_name = v_table.Rows[i]["user_name"].ToString(); v_user.v_user_online = int.Parse(v_table.Rows[i]["online"].ToString()); v_userList.Add(v_user); } } } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } v_response.v_code = (int)response.UserList; v_response.v_data = v_userList; SendToAllClients(v_response); }
public static AjaxReturn ConversionData() { AjaxReturn v_return = new AjaxReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"]; if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } ConversionDataReturn v_conversion_data = new ConversionDataReturn(); string v_html = "<table class='conversion_table'>" + "<tr>" + "<th>Drop Existing Records</th>" + "<th>Create Table</th>" + "<th>Transfer Data</th>" + "<th>Create Primary Keys</th>" + "<th>Create Foreign Keys</th>" + "<th>Create Uniques</th>" + "<th>Create Indexes</th>" + "<th>Transfer Filter</th>" + "<th>Table</th>" + "</tr>" + "<tr>" + "<td><input id='cb_all_drop_records' onchange='changeAllCheckboxes(this,0)' type='checkbox'/></td>" + "<td><input id='cb_all_create_tables' onchange='changeAllCheckboxes(this,1)' type='checkbox'/></td>" + "<td><input id='cb_all_transfer_data' onchange='changeAllCheckboxes(this,2)' type='checkbox'/></td>" + "<td><input id='cb_all_create_pks' onchange='changeAllCheckboxes(this,3)' type='checkbox'/></td>" + "<td><input id='cb_all_create_fks' onchange='changeAllCheckboxes(this,4)' type='checkbox'/></td>" + "<td><input id='cb_all_create_uniques' onchange='changeAllCheckboxes(this,5)' type='checkbox'/></td>" + "<td><input id='cb_all_create_indexes' onchange='changeAllCheckboxes(this,6)' type='checkbox'/></td>" + "<td></td>" + "<td></td>" + "</tr>"; System.Collections.Generic.List <string> v_tables_list = new System.Collections.Generic.List <string> (); OmniDatabase.Generic v_database = v_session.GetSelectedDatabase(); try { System.Data.DataTable v_tables = v_database.QueryTables(false); int v_counter = 0; foreach (System.Data.DataRow v_table in v_tables.Rows) { string tname = v_table ["table_name"].ToString(); v_tables_list.Add(tname); string v_style = ""; if (v_counter % 2 == 0) { v_style = "style='background-color: rgb(234, 237, 249)'"; } v_html += "<tr " + v_style + ">" + "<td><input id='cb_" + tname + "_drop_records' type='checkbox'/></td>" + "<td><input id='cb_" + tname + "_create_table' type='checkbox'/></td>" + "<td><input id='cb_" + tname + "_transfer_data' type='checkbox'/></td>" + "<td><input id='cb_" + tname + "_create_pks' type='checkbox'/></td>" + "<td><input id='cb_" + tname + "_create_fks' type='checkbox'/></td>" + "<td><input id='cb_" + tname + "_create_uniques' type='checkbox'/></td>" + "<td><input id='cb_" + tname + "_create_indexes' type='checkbox'/></td>" + "<td><input id='txt_" + tname + "_transferfilter' type='text'/></td>" + "<td>" + tname + "</td>" + "</tr>"; v_counter++; } v_html += "</table>"; v_conversion_data.v_html = v_html; v_conversion_data.v_tables = v_tables_list; v_return.v_data = v_conversion_data; } catch (Spartacus.Database.Exception e) { v_return.v_error = true; v_return.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); return(v_return); } return(v_return); }
public static AjaxReturn StartConversion(int p_dst_index, System.Collections.Generic.List <ConversionTableData> p_tables_data) { AjaxReturn v_return = new AjaxReturn(); Session v_session = (Session)System.Web.HttpContext.Current.Session ["OMNIDB_SESSION"]; if (v_session == null) { v_return.v_error = true; v_return.v_error_id = 1; return(v_return); } int v_dst_index = p_dst_index; System.Data.DataTable v_max_conv_id = v_session.v_omnidb_database.v_connection.Query("select max(conv_id)+1 as next_id from conversions", "Conversions"); string v_next_id = ""; if (v_max_conv_id.Rows [0] ["next_id"].ToString() != "") { v_next_id = v_max_conv_id.Rows [0] ["next_id"].ToString(); } else { v_next_id = "0"; } OmniDatabase.Generic v_database = v_session.GetSelectedDatabase(); v_session.v_omnidb_database.v_connection.Execute("insert into conversions values ( " + v_next_id + "," + v_database.v_conn_id + "," + v_session.v_databases[v_dst_index].v_conn_id + "," + "'','','0','R','',''," + v_session.v_user_id + ",'')"); foreach (ConversionTableData v_table_data in p_tables_data) { string v_drop_records = "N"; string v_create_table = "N"; string v_create_pks = "N"; string v_create_fks = "N"; string v_create_uniques = "N"; string v_create_indexes = "N"; string v_transfer_data = "N"; bool v_create_record = false; if (v_table_data.v_drop_records) { v_drop_records = "R"; v_create_record = true; } if (v_table_data.v_create_table) { v_create_table = "R"; v_create_record = true; } if (v_table_data.v_transfer_data) { v_transfer_data = "R"; v_create_record = true; } if (v_session.v_databases [v_dst_index].v_can_add_constraint) { if (v_table_data.v_create_pks) { v_create_pks = "R"; v_create_record = true; } if (v_table_data.v_create_fks) { v_create_fks = "R"; v_create_record = true; } if (v_table_data.v_create_uniques) { v_create_uniques = "R"; v_create_record = true; } if (v_table_data.v_create_indexes) { v_create_indexes = "R"; v_create_record = true; } } if (v_create_record) { v_session.v_omnidb_database.v_connection.Execute("insert into conv_tables_data values ( " + v_next_id + ",'" + v_table_data.v_table + "','" + v_drop_records + "','" + v_create_table + "','" + v_create_pks + "','" + v_create_fks + "','" + v_create_uniques + "','" + v_create_indexes + "','" + v_transfer_data + "'," + "0,0,0,0,'','','','','','','','','','','" + v_table_data.v_transferfilter.Replace("'", "''") + "')"); } } return(v_return); }
public System.Collections.Generic.List <System.Collections.Generic.List <string> > QueryList(OmniDatabase.Generic p_database, string p_sql, bool p_loghistory, bool p_logmigration, out System.Collections.Generic.List <string> p_columns) { if (p_loghistory) { this.LogHistory(p_sql); } if (p_logmigration) { this.LogMigration(p_database, p_sql); } return(p_database.v_connection.QuerySList(p_sql, out p_columns)); }
public System.Collections.Generic.List <System.Collections.Generic.List <string> > QueryListLimited(OmniDatabase.Generic p_database, string p_sql, int p_count, bool p_loghistory, bool p_logmigration, out System.Collections.Generic.List <string> p_columns) { if (p_loghistory) { this.LogHistory(p_sql); } if (p_logmigration) { this.LogMigration(p_database, p_sql); } return(p_database.QueryDataLimitedList(p_sql, p_count, out p_columns)); }
private void ExecuteQuery(WebSocketMessage p_response, WebSocketSession p_webSocketSession, string p_query_data, Session v_httpSession) { WebSocketMessage v_response = p_response; QueryData v_query_data = JsonConvert.DeserializeObject <QueryData>(p_query_data); v_response.v_code = (int)response.QueryResult; QueryReturn v_g1 = new QueryReturn(); System.Collections.Generic.List <System.Collections.Generic.List <string> > v_table = new System.Collections.Generic.List <System.Collections.Generic.List <string> >(); OmniDatabase.Generic v_database2 = v_httpSession.v_databases[v_query_data.v_db_index]; OmniDatabase.Generic v_database = OmniDatabase.Generic.InstantiateDatabase( v_database2.v_alias, v_database2.v_conn_id, v_database2.v_db_type, v_database2.v_server, v_database2.v_port, v_database2.v_service, v_database2.v_user, v_database2.v_connection.v_password, v_database2.v_schema ); v_database.v_connection.SetTimeout(0); if (v_query_data.v_cmd_type == -2) { try { v_httpSession.Execute(v_database, v_query_data.v_sql_cmd, true, true); } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } catch (System.InvalidOperationException e) { v_response.v_error = true; v_response.v_data = e.Message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } } else if (v_query_data.v_cmd_type == -3) { string[] v_commands = v_query_data.v_sql_cmd.Split(';'); string v_return_html = ""; int v_num_success_commands = 0; int v_num_error_commands = 0; v_database.v_connection.Open(); foreach (string v_command in v_commands) { if (v_command.Trim() != "") { try { v_httpSession.Execute(v_database, v_command, true, true); v_num_success_commands++; } catch (Spartacus.Database.Exception e) { v_num_error_commands++; v_return_html += "<b>Command:</b> " + v_command + "<br/><br/><b>Message:</b> " + e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>") + "<br/><br/>"; } } } v_response.v_data = "<b>Successful commands:</b> " + v_num_success_commands + "<br/>"; v_response.v_data += "<b>Errors: </b> " + v_num_error_commands + "<br/><br/>"; if (v_num_error_commands > 0) { v_response.v_data += "<b>Errors details:</b><br/><br/>" + v_return_html; } v_database.v_connection.Close(); } else { try { System.Collections.Generic.List <string> v_columns; if (v_query_data.v_cmd_type == -1) { v_table = v_httpSession.QueryList(v_database, v_query_data.v_sql_cmd, true, true, out v_columns); } else { v_table = v_httpSession.QueryListLimited(v_database, v_query_data.v_sql_cmd, v_query_data.v_cmd_type, true, false, out v_columns); } v_g1.v_query_info = "Number of records: " + v_table.Count.ToString(); v_g1.v_data = v_table; v_g1.v_col_names = v_columns; v_response.v_data = v_g1; } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } catch (System.InvalidOperationException e) { v_response.v_error = true; v_response.v_data = e.Message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } catch (System.Data.DuplicateNameException e) { v_response.v_error = true; v_response.v_data = e.Message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } } SendToClient(p_webSocketSession, v_response); }