public void xmldata_to_table(db_schema db, string table) { List <schema_field> cols = db.table_fields(table); // cols using (GotDotNet.XPath.XPathReader xr = new GotDotNet.XPath.XPathReader(data_path(table), "/root/data")) { if (xr.ReadUntilMatch()) { while (xr.MoveToNextAttribute()) { schema_field field = findField(cols, xr.Value); if (field == null) { continue; } field.AttrName = xr.Name; } } else { throw new Exception("la struttura xml del file data della tabella '" + table + "' non è corretta"); } } // insert rows bool identity = db.type == dbType.sqlserver && cols.FirstOrDefault(x => x.AutoNumber) != null; if (identity) { db.set_identity(table, true); } try { using (GotDotNet.XPath.XPathReader xr = new GotDotNet.XPath.XPathReader(data_path(table), "/root/rows/row")) { string header = string.Format("INSERT INTO {0} ({1})", table, string.Join(", ", cols.Select(x => "[" + x.Name + "]"))); while (xr.ReadUntilMatch()) { db.exec(header + " VALUES (" + string.Join(", ", cols.Select(x => db.val_toqry(xr[x.AttrName], x.TypeField, type, _nullxml))) + ")"); } } } finally { if (identity) { db.set_identity(table, false); } } }
public long exec(db_schema conn, string name_cmd = "", page_cls pg_parse = null, Dictionary <string, string> fields = null, DataRow row = null) { long result = 0; // ciclo comandi if (fields == null) { fields = new Dictionary <string, string>(); } foreach (db_script_cmd cmd in cmds(name_cmd)) { if (cmd.type == db_script_cmd.cmd_type.sql) { string sql = pg_parse == null ? cmd.text : pg_parse.page.parse(cmd.text, "", fields, row); if (cmd.attr("setkey") != "") { long key = conn.exec(sql, true); result++; if (!fields.ContainsKey(cmd.attr("setkey"))) { fields.Add(cmd.attr("setkey"), key.ToString()); } else { fields[cmd.attr("setkey")] = key.ToString(); } } else { result += conn.exec(sql); } } else if (cmd.type == db_script_cmd.cmd_type.init_tables) { conn.upgrade_data(new db_xml(new xmlDoc(conn.parse_dbexpression(conn.schema_path, conn.parse_dbexpression(cmd.attr("ver")))) , conn.parse_dbexpression(conn.meta_path, conn.parse_dbexpression(cmd.attr("ver")))), false, true, cmd.attr("notes")); } else if (cmd.type == db_script_cmd.cmd_type.init_table) { if (conn.exist_table(cmd.attr("table"))) { conn.drop_table(cmd.attr("table")); } conn.create_table(conn.schema.table_node(cmd.attr("table"))); } else if (cmd.type == db_script_cmd.cmd_type.drop_tables) { conn.dropTables(); } else if (cmd.type == db_script_cmd.cmd_type.drop_functions) { conn.dropFunctions(); } else if (cmd.type == db_script_cmd.cmd_type.drop_procedures) { conn.dropProcedures(); } else if (cmd.type == db_script_cmd.cmd_type.exec_script) { conn.exec_script(cmd.attr("name")); } else if (cmd.type == db_script_cmd.cmd_type.for_each_table) { for_each_table(conn, cmd); } else if (cmd.type == db_script_cmd.cmd_type.set_info) { conn.setInfo(cmd.attr("name"), conn.parse_dbexpression(cmd.attr("value")), 0, cmd.attr("notes")); } } return(result); }