public override void Retract (Term Term, VarStack stack, Term where) { StringBuilder deleteStat = new StringBuilder ("DELETE FROM " + dbEntity); TableColumnCollection tcc = (TableColumnCollection)MetaDataCollection; ListDictionary varNrs = new ListDictionary (); bool first = true; for (int i = 0; i < Term.Arity; i++) { Term arg = Term.Arg (i); string colName = tcc [i].Name; if (arg.IsAtom || arg.IsString) deleteStat.AppendFormat ("{0} {1}={2}", Conj (ref first), colName, Utils.EnsureQuoted (arg.Functor)); else if (arg.IsNumber) deleteStat.AppendFormat ("{0} {1}={2}", Conj (ref first), colName, arg.Functor); else if (arg.IsVar) { object prevColNo; // check whether value occurred as argument before (anonymous vars always have a unique varNo) if ((prevColNo = varNrs [arg.VarNo]) != null) // ... yes deleteStat.AppendFormat ("{0} {1}={2}", Conj (ref first), tcc [(int)prevColNo].Name, colName); varNrs [arg.VarNo] = i; // save for comparison with next argument(s) } else if (!arg.IsVar) IO.Error ("Illegal argument {0} for retract of persistent clause {1}", arg, Term); } if (where != null) // add the extra where-clause (if any) { // Copy the ref-table of column numers into a ref-table of corresponding column names ListDictionary varNames = new ListDictionary (); // ... in-situ modifications of Collection-values is not possible foreach (DictionaryEntry de in varNrs) varNames [(int)de.Key] = tcc [(int)de.Value].Name; deleteStat.AppendFormat ("{0} {1}", Conj (ref first), where.ToStringSql (varNames)); } if (first) IO.Warning ("Retract -- DELETE without WHERE -- will NOT be executed !!!!!!!!"); else ExecuteSqlCommand (deleteStat.ToString ()); }
protected ClauseNode GetSelectablePersistentData (Term t, Term where) { ListDictionary varNrs = new ListDictionary (); ProcParamCollection ppc = (ProcParamCollection)MetaDataCollection; bool first; int varNo; if (ppc.Count != t.Arity) IO.Error ("Number of parameters ({0}) for stored procedure '{1}' does not match the arity of persistent predicate {2}/{3}", ppc.Count, dbEntity, name, arity); // get all arguments from t and construct the corresponding SELECT-statement. // The arguments must be either atomic or var, but may not be compound (apart from date(...)) StringBuilder fetchStat = new StringBuilder ("SELECT NULL"); for (int i = inputParamCount; i < ppc.Count; i++) fetchStat.AppendFormat (", {0}", ppc [i].Name); // output parameters only fetchStat.Append (" FROM "); int keyStart = fetchStat.Length; // Dataset key, to test whether the query result has already been executed before fetchStat.Append (dbEntity); // append stored procedure input parameters (if any) if (inputParamCount > 0) { fetchStat.Append (" ("); varNo = 0; first = true; for (int i = 0; i < inputParamCount; i++) { if (first) first = false; else fetchStat.Append (", "); Term tt = t.Arg (varNo++); if (tt.IsAtom || tt.IsString) fetchStat.Append ("'" + tt.Functor + "'"); else if (tt.IsNumber) fetchStat.Append (tt.Functor); else IO.Error ("Illegal stored procedure input parameter {0} in persistent term {1}", tt, t); } fetchStat.Append (")"); } // construct a condition in a where-clause for each instantiated (output) argument of t varNo = 0; // t.Arg (0) is first output parameter (if any) first = true; for (int colNo = inputParamCount; colNo < ppc.Count; colNo++) { string colName = ppc [colNo].Name; Term tt = t.Arg (varNo); if (tt.IsVar) { object prevColNo; // check whether value occurred as argument before (anonymous vars always have a unique varNo) if ((prevColNo = varNrs [tt.VarNo]) != null) // ... yes fetchStat.AppendFormat ("{0} {1}={2}", Conj (ref first), ppc [(int)prevColNo].Name, colName); varNrs [tt.VarNo] = colNo; // save for comparison with next argument(s) } else if (!(tt.IsAtom || tt.IsString || tt.IsNumber)) IO.Error ("Illegal argument {0} in persistent term {1}", tt, t); varNo++; } // add the extra where-clause (if any) if (where != null) { // Copy the ref-table of column numers into a ref-table of corresponding column names ListDictionary varNames = new ListDictionary (); // ... in-situ modifications of Collection-values is not possible foreach (DictionaryEntry de in varNrs) varNames [(int)de.Key] = ppc [(int)de.Value].Name; fetchStat.AppendFormat ("{0} {1}", Conj (ref first), where.ToStringSql (varNames)); } fetchStat.Append (";"); //Console.WriteLine ("ProcedurePredDescr.GetPersistentData -- fetchStat = \"{0}\"", fetchStat); DataRowCollection drc = GetResultsetForSelect (fetchStat.ToString (), keyStart); // get the data return (drc == null || drc.Count == 0) ? null : new PersistentClauseNode (drc, 0, this); }
protected override ClauseNode GetPersistentData (Term t, Term where) { query = t; ListDictionary varNrs = new ListDictionary (); TableColumnCollection tcc = (TableColumnCollection)MetaDataCollection; if (tcc.Count != t.Arity) IO.Error ("Number of columns ({0}) in table/view '{1}' does not match the arity of persistent predicate {2}/{3}", tcc.Count, dbEntity, name, arity); // get all output arguments from t and construct the corresponding SELECT-statement. StringBuilder fetchStat = new StringBuilder ("SELECT NULL"); for (int i = 0; i < tcc.Count; i++) if (t.Args [i].IsVar) fetchStat.AppendFormat (", {0}", tcc [i].Name); fetchStat.Append (" FROM "); int keyStart = fetchStat.Length; // Dataset key, to test whether the query result has already been executed before fetchStat.Append (dbEntity); int colNo = 0; bool first = true; // construct a condition in a where-clause for each instantiated argument of t foreach (Term tt in t.Args) { string colName = tcc [colNo].Name; if (tt.IsAtom || tt.IsString) fetchStat.AppendFormat ("{0} {1}={2}", Conj (ref first), colName, Utils.EnsureQuoted (tt.Functor)); else if (tt.IsNumber) fetchStat.AppendFormat ("{0} {1}={2}", Conj (ref first), colName, tt.Functor); else if (tt.IsVar) { object prevColNo; // check whether value occurred as argument before (anonymous vars always have a unique varNo) if ((prevColNo = varNrs [tt.VarNo]) != null) // ... yes fetchStat.AppendFormat ("{0} {1}={2}", Conj (ref first), tcc [(int)prevColNo].Name, colName); varNrs [tt.VarNo] = colNo; // save for comparison with next argument(s) } else IO.Error ("Illegal argument {0} in persistent term {1}", tt, t); colNo++; } if (where != null) // add the extra where-clause { // Copy the ref-table of column numers into a ref-table of corresponding column names ListDictionary varNames = new ListDictionary (); // ... in-situ modifications of Collection-values is not possible foreach (DictionaryEntry de in varNrs) varNames [(int)de.Key] = tcc [(int)de.Value].Name; fetchStat.AppendFormat ("{0} {1}", Conj (ref first), where.ToStringSql (varNames)); } fetchStat.Append (";"); //Console.WriteLine ("TablePredDescr.GetPersistentData -- fetchStat = \"{0}\"", fetchStat); DataRowCollection drc = GetResultsetForSelect (fetchStat.ToString (), keyStart); // get the data return (drc == null || drc.Count == 0) ? null : new PersistentClauseNode (drc, 0, this); }