Exemplo n.º 1
0
 // All done, persist catalog if required
 public bool Finish()
 {
     Logger.Assert(_status != CatalogStatus.Finished);
     if (_sessionstate > SessionState.None)
     {
         EndSession(SessionResults.Ok);
     }
     Logger.WriteLine(2, "Catalog Finish '{0}'", this);
     SqlTarget.Close();
     _status = CatalogStatus.Finished;
     return(true);
 }
Exemplo n.º 2
0
        // Make connection to database based on available flags and current status
        // does not return on error
        void ConnectDatabase()
        {
            if (_status > CatalogStatus.Started)
            {
                return;                             // just the once
            }
            Logger.Assert(_status == CatalogStatus.Started, _status);
            Logger.WriteLine(2, $"Catalog Connect database {this}");

            // create empty catalog
            var table = DataTableLocal.Create(_catalogtableheading);

            GlobalVars.AddEntry(CatalogTableName, table.DataType, EntryKinds.Value,
                                EntryFlags.Public | EntryFlags.System, RelationValue.Create(table));
            GlobalVars.FindEntry(CatalogTableName).Flags |= EntryFlags.Database;

            // Sql or not? Open it.
            var ext = Path.GetExtension(DatabasePath);

            if (ext == "")
            {
                DatabasePath = Path.ChangeExtension(DatabasePath, (SqlFlag) ? DefaultSqlDatabaseExtension : DefaultDatabaseExtension);
            }
            SqlFlag     |= (ext == DefaultSqlDatabaseExtension || DatabaseKind != DatabaseKinds.Memory);
            DatabaseName = Path.GetFileNameWithoutExtension(DatabasePath);
            if (SqlFlag)
            {
                if (DatabaseKind == DatabaseKinds.Memory)
                {
                    DatabaseKind = DatabaseKinds.Sqlite;
                }
                Logger.WriteLine(3, "Catalog database={0} kind={1}", DatabasePath, DatabaseKind);
                if (!SqlTarget.Open(DatabasePath, DatabaseKind))
                {
                    throw ProgramError.Fatal("Catalog", "Cannot open database: {0} ({1})", DatabasePath, DatabaseKind);
                }
            }
            else
            {
                if (LoadFlag && !Directory.Exists(DatabasePath))
                {
                    throw ProgramError.Fatal("Catalog", "Database does not exist: {0}", DatabasePath);
                }
            }
            _status = CatalogStatus.Connected;
            Logger.WriteLine(3, "[CC {0}]", this);
        }
Exemplo n.º 3
0
        ///------------------------------------------------------------------------
        /// High level semantics
        ///

        // create a target instance
        public static bool Open(string name, DatabaseKinds dskind)
        {
            Logger.WriteLine(3, $">Sql Open {name} {dskind}");
            ExprDict = new Dictionary <int, ExpressionEval>();
            var sqleval = SqlEvaluator.Create();

            switch (dskind)
            {
            case DatabaseKinds.Sqlite:
                var database = Sqlite.SqliteDatabase.Create(name, sqleval);
                Current = new SqlTarget {
                    _sqlgen = new SqlGen()
                    {
                        Templater = new SqliteTemplater(),
                    },
                    _database       = database,
                    FunctionCreator = database,
                };
                break;

            case DatabaseKinds.Postgres:
                Current = new SqlTarget {
                    _sqlgen = new SqlGen()
                    {
                        Templater = new PostgresTemplater(),
                    },
                    _database = Postgres.PostgresDatabase.Create(name, sqleval),
                    // FunctionCreator set later by gateway
                };
                break;

            default:
                throw Logger.Fatal(dskind);
            }
            if (!Current._database.IsOpen)
            {
                throw Current.SqlError("database open failed");
            }
            return(true);
        }
Exemplo n.º 4
0
        //--- factories

        DataTableSql()
        {
            _database = SqlTarget.Current;
            _gen      = SqlTarget.Current.SqlGen;
        }