Exemple #1
0
// Creates a new table.
func (db *Database) Create(name string) (*table.Table, int) {
	var newTable *table.Table
	_, exists := db.Tables[name]
	if exists {
		return nil, st.TableAlreadyExists
	}
	if len(name) > constant.MaxTableNameLength {
		return nil, st.TableNameTooLong
	}
	// Create table files and directories.
	tablefilemanager.Create(db.Path, name)
	// Open the table
	var status int
	newTable, status = table.Open(db.Path, name)
	if status == st.OK {
		// Add default columns
		for columnName, length := range constant.DatabaseColumns() {
			status = newTable.Add(columnName, length)
			if status != st.OK {
				return nil, status
			}
		}
		db.Tables[name] = newTable
	}
	return newTable, st.OK
}
Exemple #2
0
// Renames a table
func (db *Database) Rename(oldName, newName string) int {
	_, exists := db.Tables[oldName]
	if !exists {
		return st.TableNotFound
	}
	_, exists = db.Tables[newName]
	if exists {
		return st.TableAlreadyExists
	}
	db.Tables[oldName].Flush()
	// Rename table files and directories
	status := tablefilemanager.Rename(db.Path, oldName, newName)
	if status != st.OK {
		return status
	}
	db.Tables[newName], status = table.Open(db.Path, newName)
	db.Tables[oldName] = nil, false
	return status
}
Exemple #3
0
// Opens a path as database.
func Open(path string) (*Database, int) {
	var db *Database
	db = new(Database)
	db.Tables = make(map[string]*table.Table)
	// Open and read content of the path (as a directory).
	directory, err := os.Open(path)
	if err != nil {
		db = nil
		logg.Err("database", "Open", err.String())
		return db, st.CannotOpenDatabaseDirectory
	}
	defer directory.Close()
	fi, err := directory.Readdir(0)
	if err != nil {
		db = nil
		logg.Err("database", "Open", err.String())
		return db, st.CannotReadDatabaseDirectory
	}
	for _, fileInfo := range fi {
		// Extract extension of file name.
		if fileInfo.IsRegular() {
			name, ext := util.FilenameParts(fileInfo.Name)
			// If extension is .data, open the file as a Table.
			if ext == "data" {
				_, exists := db.Tables[name]
				if !exists {
					var status int
					// Open the table and put it into tables map.
					db.Tables[name], status = table.Open(path, name)
					if status != st.OK {
						return nil, status
					}
				}
			}
		}
	}
	db.Path = path
	return db, db.PrepareForTriggers(false)
}