Пример #1
0
 public CurrentInformation(bool interactive)
 {
     information["interactive"] = new Precomputation(interactive);
     information["login"]       = new Precomputation(new SimpleStringish(Environment.UserName));
     information["directory"]   = new Precomputation(new SimpleStringish(Environment.CurrentDirectory));
     information["version"]     = new Precomputation(Configuration.Version);
     information["vm/name"]     = new Precomputation(new SimpleStringish("CLR"));
     information["vm/vendor"]   = new Precomputation(new SimpleStringish(Type.GetType("Mono.Runtime") != null ? "Mono" : "Microsoft"));
     information["vm/version"]  = new Precomputation(new SimpleStringish(Environment.Version.ToString()));
 }
Пример #2
0
        protected override bool Run()
        {
            if (connection == null)
            {
                new Lookup(task_master, source_ref, new [] { "connection" },
                           context).Notify(return_value => {
                    if (return_value is ReflectedFrame)
                    {
                        Object backing = ((ReflectedFrame)return_value).Backing;
                        if (backing is DbConnection)
                        {
                            connection = (DbConnection)backing;
                            if (Interlocked.Decrement(ref interlock) == 0)
                            {
                                task_master.Slot(this);
                            }
                            return;
                        }
                    }
                    task_master
                    .ReportOtherError(source_ref,
                                      "Expected “connection” to come from “sql:” import.");
                });
                new Lookup(task_master, source_ref, new [] { "sql_query" },
                           context).Notify(return_value => {
                    if (return_value is Stringish)
                    {
                        query = return_value.ToString();
                        if (Interlocked.Decrement(ref interlock) == 0)
                        {
                            task_master.Slot(this);
                        }
                        return;
                    }
                    task_master.ReportOtherError(source_ref, string.Format(
                                                     "Expected type Str for “sql_query”, but got {0}.",
                                                     Stringish.NameForType(return_value.GetType())));
                });
                new Lookup(task_master, source_ref, new [] { "sql_row_tmpl" },
                           context).Notify(return_value => {
                    if (return_value is Template)
                    {
                        row_tmpl = (Template)return_value;
                        if (Interlocked.Decrement(ref interlock) == 0)
                        {
                            task_master.Slot(this);
                        }
                        return;
                    }
                    task_master.ReportOtherError(source_ref, string.Format(
                                                     "Expected type Template for “sql_row_tmpl”, but got {0}.",
                                                     Stringish.NameForType(return_value.GetType())));
                });
                if (Interlocked.Decrement(ref interlock) > 0)
                {
                    return(false);
                }
            }
            try {
                var command = connection.CreateCommand();
                command.CommandType = System.Data.CommandType.Text;
                command.CommandText = query;
                var reader = command.ExecuteReader();
                if (debug)
                {
                    Console.WriteLine("SQL Query to {0}: {1}", connection, query);
                }
                NameChooser name_chooser = (rs, it) => TaskMaster.OrdinalNameStr(it);
                var         retrievers   = new List <Retriever>();
                for (int col = 0; col < reader.FieldCount; col++)
                {
                    var column = col;
                    if (reader.GetName(col) == "ATTRNAME")
                    {
                        name_chooser = (rs, it) => rs.GetString(column);
                        continue;
                    }
                    if (reader.GetName(col).StartsWith("$"))
                    {
                        var attr_name = reader.GetName(col).Substring(1);
                        if (!task_master.VerifySymbol(source_ref, attr_name))
                        {
                            return(false);
                        }
                        retrievers.Add((rs, frame, _task_master) => frame.Set(attr_name, rs.IsDBNull(column) ? Precomputation.Capture(Unit.NULL) : Lookup.Do(rs.GetString(column).Split('.'))));
                        continue;
                    }
                    Unpacker unpacker;
                    if (!unpackers.TryGetValue(reader.GetFieldType(col), out unpacker))
                    {
                        task_master
                        .ReportOtherError(
                            source_ref,
                            string.Format(
                                "Cannot convert SQL type “{0}” for column “{1}” into Flabbergast type.",
                                reader.GetFieldType(col),
                                reader.GetName(col)));
                    }
                    if (!task_master.VerifySymbol(source_ref,
                                                  reader.GetName(col)))
                    {
                        return(false);
                    }
                    retrievers.Add(Bind(reader.GetName(col), col, unpacker));
                }

                var list = new MutableFrame(task_master, source_ref, context, self);
                for (int it = 1; reader.Read(); it++)
                {
                    var frame = new MutableFrame(task_master, new JunctionReference(string.Format("SQL template instantiation row {0}", it), "<sql>", 0, 0, 0, 0, source_ref, row_tmpl.SourceReference), Context.Append(list.Context, row_tmpl.Context), list);
                    foreach (var r in retrievers)
                    {
                        r(reader, frame, task_master);
                    }
                    foreach (var name in row_tmpl.GetAttributeNames())
                    {
                        if (!frame.Has(name))
                        {
                            frame.Set(name, row_tmpl[name]);
                        }
                    }
                    list.Set(name_chooser(reader, it), frame);
                }
                list.Slot();
                result = list;
                return(true);
            } catch (DataException e) {
                task_master.ReportOtherError(source_ref, e.Message);
                return(false);
            }
        }