View Single Post
  #7 (permalink)  
Old 09-26-2007, 02:23 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 442
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
Code Update

Here's the latest code. Underneath is code I'm using to test features so far.

As always, feedback is loved!!!!!!!

Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MyCustomNamespace {
    class csv_db {
        private object database_lock = new object();
        private string database_path = "";
        private string current_database = "";
        private string current_table = "";
        private ArrayList current_rows = new ArrayList();
        private ArrayList current_indexed_columns = new ArrayList();
        private csv_table_summary current_table_summary;

        public csv_db(string databasePath) {
            try {
                if (!Directory.Exists(databasePath) && databasePath.Length > 0) {
                    Directory.CreateDirectory(databasePath);
                }
                this.database_path = databasePath;
            } catch {
                //Directory creation failed, so just use the current directory.
                this.database_path = "";
            }
        }

        public bool connect(string databaseName) {
            //returns true if database exists, false otherwise
            if (Directory.Exists(this.database_path + "\\" + databaseName)) {
                this.current_database = databaseName;
                this.database_path += "\\" + databaseName;
                return true;
            }
            return false;
        }

        public uint insert(string table, Hashtable rowValues) {
            //returns autoincrement value of new row
            return 0;
        }

        public bool update(string table, uint primaryKey, Hashtable rowValues) {
            //returns true if row updated, false otherwise
            return false;
        }
        public uint update(string table, Hashtable whereValues, Hashtable rowValues) {
            //returns number of rows updated
            return 0;
        }

        public uint query(string table, Hashtable whereValues) {
            //returns number of rows matching query
            return 0;
        }

        public bool delete(string table, uint primaryKey) {
            //returns true if row deleted
            return false;
        }
        public uint delete(string table, Hashtable whereValues) {
            //returns number of rows deleted
            return 0;
        }

        public bool create_db(string databaseName) {
            //returns true if db created
            if (!Directory.Exists(this.database_path + databaseName+"\\")) {
                Directory.CreateDirectory(this.database_path + databaseName + "\\");
                Directory.CreateDirectory(this.database_path + databaseName + "\\_indexes\\");
                Directory.CreateDirectory(this.database_path + databaseName + "\\_table_summary\\");
                return true;
            }
            return false;
        }
        public bool create_db(string databaseName, string sourceDatabaseName) {
            //returns true if db created
            if (this.create_db(this.database_path + databaseName + "\\")) {
                //Now, need to copy files from sourceDatabaseName
            }
            return false;
        }

        public bool create_table(string tableName, ArrayList columns) {
            //returns true if table created
            
            return false;
        }
        public bool create_table(string tableName, ArrayList columns, uint rowLength) {
            //returns true if table created
            if (this.current_database.Length > 0) {
                if (!File.Exists(this.database_path + "\\" + this.current_database + "\\"+tableName + ".csv")) {
                    File.Create(this.database_path + "\\" + tableName + ".csv");
                    this.current_table = tableName;
                    this.current_table_summary = new csv_table_summary(this.database_path, tableName);
                    this.current_table_summary.tableHeaders = columns;
                    this.current_table_summary.rowLength = rowLength;
                    this.save_table_summary();
                    return true;
                }
                return false;
            }
            return false;
        }

        public bool index_insert(string columnName) {
            //returns true if index created
            return false;
        }

        private void load_table_summary() {
            //Loads current table summary into memory for fast access.
            this.current_table_summary = new csv_table_summary(this.database_path, this.current_table);
        }
        private void save_table_summary() {
            FileStream fs = new FileStream(this.database_path + "\\_table_summary\\" + this.current_table + ".csv", FileMode.Create, FileAccess.Write, FileShare.None);
            using (StreamWriter appDataWriter = new StreamWriter(fs)) {
                appDataWriter.WriteLine(this.current_table_summary.nextAutoIncrement);
                appDataWriter.WriteLine(this.current_table_summary.rowLength);
                appDataWriter.WriteLine(this.current_table_summary.tableHeadersString);
                appDataWriter.WriteLine(this.current_table_summary.indexedColumnsString);
            }
        }
    }
    class csv_column {
        private string name = "";
        private Type column_type;
        private uint max_length = 0;

        public csv_column(string columnDetails) {
        }

        public string columnName {
            get {
                return this.name;
            }
        }
        public Type columnType {
            get {
                return this.column_type;
            }
        }
        public uint maxLength {
            get {
                return this.max_length;
            }
        }
    }

    class csv_table_summary {
        private uint row_length = 0;
        private uint next_auto_increment = 0;
        private ArrayList headers = new ArrayList();
        private ArrayList indexed_columns = new ArrayList();

        public csv_table_summary(string database_path,string summaryFileName) {
            //Parse file and populate variables
            if (File.Exists(database_path + "\\_table_summary" + summaryFileName)) {
                using (StreamReader appDataReader = new StreamReader(database_path + "\\_table_summary\\" + summaryFileName + ".csv")) {
                    this.next_auto_increment = Convert.ToUInt32(appDataReader.ReadLine().Trim());
                    this.row_length = Convert.ToUInt32(appDataReader.ReadLine().Trim());
                    this.headers = CSVParser.Parse(appDataReader.ReadLine());
                    this.indexed_columns = CSVParser.Parse(appDataReader.ReadLine());
                }
            }
        }

        public uint rowLength {
            get {
                return this.row_length;
            }
            set {
                this.row_length = value;
            }
        }
        public uint nextAutoIncrement {
            get {
                return this.next_auto_increment;
            }
            set {
                this.next_auto_increment = value;
            }
        }
        public ArrayList tableHeaders {
            get {
                return this.headers;
            }
            set {
                this.headers = value;
            }
        }
        public string tableHeadersString {
            get {
                string rValue = ""; string prefix = "";
                foreach (csv_table_header aHeader in this.headers) {
                    rValue += prefix + aHeader.ToString();
                    prefix = ",";
                }
                return rValue;
            }
        }
        public ArrayList indexedColumns {
            get {
                return this.indexed_columns;
            }
            set {
                this.indexed_columns = value;
            }
        }
        public string indexedColumnsString {
            get {
                string rValue = ""; string prefix = "";
                foreach (string aColumn in this.indexed_columns) {
                    rValue += prefix + aColumn;
                    prefix = ",";
                }
                return rValue;
            }
        }
        public bool isIndexed(string columnName) {
            if (this.indexed_columns.Contains(columnName)) {
                return true;
            } else {
                return false;
            }
        }
    }

    class csv_table_header {
        private string name;
        private string column_type;
        private uint max_length;

        public csv_table_header(string columnName, string columnType, uint maxLength) {
            this.name = columnName;
            this.column_type = columnType;
            this.max_length = maxLength;
        }

        public string columnName {
            set {
                this.name = value;
            }
        }
        public string columnType {
            set {
                this.column_type = value;
            }
        }
        public uint maxLength {
            set {
                this.max_length = value;
            }
        }

        public override string ToString() {
            return this.name + ":" + this.column_type + ":" + this.max_length.ToString();
        }
    }
}

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote