示例#1
0
        /// <summary>
        /// Gets a <see cref="Database"/> instance by name.
        /// </summary>
        /// <remarks>
        /// This class mantains a cache of already created database
        /// objects to prevent opening the same database more than once, 
        /// because database files are open exclusively.
        /// </remarks>
        /// <param name="name">Database full path.</param>
        /// <returns>A reference of a new or existing database object.</returns>
        public static Database GetDatabase(string name)
        {
            lock( SyncRoot )
            {
                try
                {
                    Database db = null;
                    if( _dbs.ContainsKey( name ) )
                    {
                        db = (Database)_dbs[name];
                    }
                    else
                    {
                        db = new Database(name);
                        _dbs.Add(name, db);
                    }
                    return db;
                }
                catch( Exception ex )
                {
                        LogHelper.Publish( String.Format("Unexpected exception on [GetDatabase] method for database {0}.", name ), ex );

                    throw;
                }
            }
        }
示例#2
0
        private ArrayList vIndex; // vIndex(0) is always the primary key index

        #endregion Fields

        #region Constructors

        public Table(Database db, bool log, string name, bool cached)
        {
            dDatabase = db;
            lLog = log ? db.Log : null;

            if (cached)
            {
                cCache = lLog.cCache;
                bCached = true;
            }

            sName = name;
            iPrimaryKey = -1;
            iIdentityColumn = -1;
            iTimestampColumn = -1;
            vColumn = new ArrayList();
            vIndex = new ArrayList();
            vConstraint = new ArrayList();
        }
示例#3
0
        public SharpHsql.Database select_db(string name)
        {
            db = new SharpHsql.Database(name);

            return(db);
        }
示例#4
0
文件: Log.cs 项目: furesoft/SharpHSQL
        /// <summary>
        /// Close the transaction log.
        /// </summary>
        /// <param name="compact"></param>
        public void Close(bool compact)
        {
            lock( SyncLock )
            {
                if (TracingHelper.TraceEnabled)
                    TracingHelper.Write();

                if (bReadOnly)
                    return;

                // no more scripting
                CloseScript();

                // create '.script.new' (for this the cache may be still required)
                WriteScript(compact);

                // flush the cache (important: after writing the script)
                cCache.Flush();

                // create '.backup.new' using the '.data'
                Backup();

                // we have the new files
                sModified = "yes-new-files";
                SaveProperties();

                // old files can be removed and new files renamed
                RenameNewToCurrent(sFileScript);
                RenameNewToCurrent(sFileBackup);

                // now its done completely
                sModified = "no";
                SaveProperties();
                CloseProperties();

                if (compact)
                {
                    // stop the runner thread of this process (just for security)
                    Stop();

                    // delete the .data so then a new file is created
                    (new FileInfo(sFileCache)).Delete();
                    (new FileInfo(sFileBackup)).Delete();

                    // all files are closed now; simply open & close this database
                    Database db = new Database(sName);

                    db.Log.Close(false);
                }
            }
        }
示例#5
0
文件: Log.cs 项目: furesoft/SharpHSQL
        /// <summary>
        /// Script the database objects to a file.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="file"></param>
        /// <param name="full"></param>
        /// <param name="channel"></param>
        public static void ScriptToFile(Database db, string file, bool full, Channel channel)
        {
            if ((new FileInfo(file)).Exists)
            {
                // there must be no such file; overwriting not allowed for security
                throw TracingHelper.Error(TracingHelper.FILE_IO_ERROR, file);
            }

            try
            {
                DateTime   time = DateTime.Now;

                // only ddl commands; needs not so much memory
                Result r;

                if (full)
                {
                    // no drop, no insert, and no positions for cached tables
                    r = db.GetScript(false, false, false, channel);
                }
                else
                {
                    // no drop, no insert, but positions for cached tables
                    r = db.GetScript(false, false, true, channel);
                }

                Record     n = r.Root;
                StreamWriter w = new StreamWriter(file);

                while (n != null)
                {
                    writeLine(w, (string) n.Data[0]);

                    n = n.Next;
                }

                // inserts are done separetely to save memory
                ArrayList tables = db.Tables;

                for (int i = 0; i < tables.Count; i++)
                {
                    Table t = (Table) tables[i];

                    // cached tables have the index roots set in the ddl script
                    if (full ||!t.IsCached)
                    {
                        Index primary = t.PrimaryIndex;
                        Node  x = primary.First();

                        while (x != null)
                        {
                            writeLine(w, t.GetInsertStatement(x.GetData()));

                            x = primary.Next(x);
                        }
                    }
                }

                w.Close();

                TimeSpan execution = DateTime.Now.Subtract(time);

                if (TracingHelper.TraceEnabled)
                    TracingHelper.Write((Int64)execution.TotalMilliseconds);
            }
            catch (IOException e)
            {
                TracingHelper.Error(TracingHelper.FILE_IO_ERROR, file + " " + e);
            }
        }
示例#6
0
文件: Log.cs 项目: furesoft/SharpHSQL
 /// <summary>
 /// Default constructor.
 /// </summary>
 /// <param name="db"></param>
 /// <param name="system"></param>
 /// <param name="name"></param>
 public Log(Database db, Channel system, string name)
 {
     dDatabase = db;
     cSystem = system;
     sName = name;
     sFileProperties = sName + ".cfg";
     sFileScript = sName + ".log";
     sFileCache = sName + ".data";
     sFileBackup = sName + ".backup";
 }
示例#7
0
 public DatabaseInformation(Database db, ArrayList tables, Access access)
 {
     dDatabase = db;
     tTable = tables;
     aAccess = access;
 }
示例#8
0
 /// <summary>
 /// Builds a channel based on an existing one.
 /// </summary>
 /// <param name="channel"></param>
 /// <param name="id"></param>
 public Channel(Channel channel, int id)
     : this()
 {
     _id = id;
     _database = channel._database;
     _user = channel._user;
     _autoCommit = true;
     _readOnly = channel._readOnly;
 }
示例#9
0
        /// <summary>
        /// Disconnects the current channel from database.
        /// </summary>
        public void Disconnect()
        {
            if (_closed)
            {
                return;
            }

            Rollback();

            _user = null;
            _database = null;
            _transaction = null;
            _closed = true;
        }
示例#10
0
 /// <summary>
 /// Builds a new channel.
 /// </summary>
 /// <param name="db"></param>
 /// <param name="user"></param>
 /// <param name="autoCommit"></param>
 /// <param name="readOnly"></param>
 /// <param name="id"></param>
 public Channel(Database db, User user, bool autoCommit, bool readOnly, int id)
     : this()
 {
     _id = id;
     _database = db;
     _user = user;
     _autoCommit = autoCommit;
     _readOnly = readOnly;
 }
示例#11
0
文件: sql.cs 项目: Myvar/Eclang
 public void select_db(string name)
 {
     this.db = new Database(name);
 }
示例#12
0
 public Parser(Database db, Tokenizer tokenizer, Channel channel)
 {
     dDatabase = db;
     tTokenizer = tokenizer;
     cChannel = channel;
 }