Example #1
0
        public void Assigned_Modification()
        {
            _api.EnterFluidMode();
            _api.Key("foo", "pk", false);

            var bean = _api.Dispense("foo");

            bean["pk"] = 1;
            _api.Store(bean);

            bean["pk"] = 2;
            _api.Store(bean);

            Assert.Equal(2, _api.Count("foo"));
            // moral: keys should be immutable
        }
Example #2
0
        void FluidMode(BeanApi api)
        {
            /// ## Fluid Mode
            /// LimeBean is committed to mitigate the common inconvenience associated with relational databases,
            /// namely necessity to manually create tables, columns and adjust their data types.
            /// In this sense, LimeBean takes SQL databases a little closer to NoSQL ones like MongoDB.
            ///
            /// **Fluid Mode** is optional, turned off by default, and is recommended for use only during early development stages
            /// (particularly for prototyping and scaffolding).
            /// To enable it, invoke the `EnterFluidMode` method on the `BeanApi` object:
#if CODE
            api.EnterFluidMode();
#endif
            /// How does it work? When you save the next bean, LimeBean analyzes its fields and compares
            /// their names and types to the database schema.
            /// If new data cannot be stored to an existing table, schema alteration occurs.
            /// LimeBean can create new tables, add missing columns, and widen data types.
            /// It will never truncate data or delete unused columns.
            ///
            /// **NOTE:** LimeBean doesn't detect renamings.
            ///
            /// **CAUTION:** Automatically generated schema is usually sub-optimal and lacks indexes which are essential
            /// for performance. When most of planned tables are already in place,
            /// and only minor changes are expected,
            /// it is recommended to turn the Fluid Mode off, audit the database structure, add indexes, and make further schema
            /// changes with a dedicated database management tool (like HeidiSQL, SSMS, pgAdmin, etc).
            ///
        }
Example #3
0
            /// ## BeanApi Object Lifetime
            /// The `BeanApi` class implements `IDisposable` (it holds the `DbConnection`) and is not thread-safe.
            /// Care should be taken to ensure that the same `BeanApi` and `DbConnection` instance is not used from multiple threads without
            /// synchronization, and that it is properly disposed. Let's consider some common usage scenarios.
            ///
            /// ### Local Usage
            /// If LimeBean is used locally, then it should be enclosed in a `using` block:
            void LocalUsage(string connectionString, Type connectionType)
            {
#if CODE
                using (var api = new BeanApi(connectionString, connectionType)) {
                    api.EnterFluidMode();

                    // work with beans
                }
#endif
            }
Example #4
0
        public void Scenario()
        {
            using (var api = new BeanApi("data source=:memory:", SQLiteFactory.Instance))
            {
                api.EnterFluidMode();
                api.DefaultKey(false);
                api.AddObserver(new GuidKeyObserver());

                var bean = api.Dispense("foo");
                var key  = api.Store(bean);
                Console.WriteLine("Key is: " + key);
            }
        }
Example #5
0
        public void Scenario()
        {
            // Tell about your database
            var r = new BeanApi("data source=:memory:", SQLiteFactory.Instance);

            // Enable automatic schema update
            r.EnterFluidMode();

            // create a bean
            var bean = r.Dispense("person");

            // it's of kind "person"
            Console.WriteLine(bean.GetKind());

            // give me the Id of the newly stored bean
            var id = bean
                     // fill it
                     .Put("name", "Alex")
                     .Put("year", 1984)
                     .Put("smart", true)
                     // store it
                     .Store();

            // Database schema will be updated automatically for you

            // Now the bean has an id
            Console.WriteLine(bean["id"]);

            // load a bean
            bean = r.Load("person", id);

            bean
            // change it
            .Put("name", "Lexa")
            .Put("new_prop", 123)
            // commit changes
            .Store();

            // or delete it
            r.Trash(bean);

            // close the connection
            r.Dispose();
        }
Example #6
0
        void FluidMode(BeanApi api)
        {
            /// ## Fluid Mode
            /// LimeBean mitigates the common inconvenience associated with relational databases,
            /// namely necessity to manually create tables, columns and adjust their data types.
            /// In this sense, LimeBean takes SQL databases a little closer to NoSQL ones like MongoDB.
            ///
            /// **Fluid Mode** is optional, turned off by default, and is recommended for use only during early development stages
            /// (particularly for prototyping and scaffolding).
            /// To enable it, invoke the `EnterFluidMode` method on the `BeanApi` object:
#if CODE
            api.EnterFluidMode();

            // Make a Bean for a table which doesn't yet exist
            var bean = api.Dispense("book_types");

            // Fill it with some data
            // Limebean will automatically detect Types and create columns with the correct Type
            bean.Put("name", "War")
            .Put("fiction", true);

            // Store will automatically create any missing tables (with an auto-incrementing 'id' column) and columns,
            // then add the Bean as a new row
            var id = api.Store(bean);

            // The bean is now available in the database
            var savedBean = api.Load("book_types", id);
#endif
            /// How does this work? When you save a Bean while in Fluid Mode, LimeBean analyzes its fields and compares
            /// their names and types to the database schema.
            /// If new data cannot be stored to an existing table, schema alteration occurs.
            /// LimeBean can create new tables, add missing columns, and widen data types.
            /// It will never truncate data or delete unused columns.
            ///
            /// **NOTE:** LimeBean will not detect renamings.
            ///
            /// **CAUTION:** Automatically generated schema is usually sub-optimal and lacks indexes which are essential
            /// for performance. When most planned tables are already in place,
            /// it is recommended you turn Fluid Mode off, audit the database structure, add indexes, and make further schema
            /// changes with a dedicated database management tool (like HeidiSQL, SSMS, pgAdmin, etc).
            ///
        }
Example #7
0
        public Auditor(BeanApi api, string auditBlacklist)
        {
            var exitFluidMode = false;

            _auditBlacklist = auditBlacklist == string.Empty
                ? new List <string>()
                : auditBlacklist.ToUpper().Split(';').ToList();
            _auditBlacklist.Add("AUDIT");

            if ((api.Database == string.Empty && api.Connection.State != ConnectionState.Open) ||
                api.IsKnownKind("AUDIT"))
            {
                return;
            }

            if (!api.IsFluidMode())
            {
                api.EnterFluidMode();
                exitFluidMode = true;
            }

            var audit = api.Dispense("AUDIT");

            audit
            .Put("AuditDate", DateTime.Now)
            .Put("Action", new string('X', 16))
            .Put("User", new string('X', 64))
            .Put("Object", new string('X', 64))
            .Put("ObjectId", new string('X', 64))
            .Put("Property", new string('X', 64))
            .Put("PropertyType", new string('X', 64))
            .Put("OldValue", new string('X', 1024))
            .Put("NewValue", new string('X', 1024))
            .Put("Notes", new string('X', 4096))
            .Store();

            if (exitFluidMode)
            {
                api.ExitFluidMode();
            }
        }
Example #8
0
 protected void Application_BeginRequest(object sender, EventArgs e)
 {
     MyBeanApi = new BeanApi("connection string", SQLiteFactory.Instance);
     MyBeanApi.EnterFluidMode();
 }
Example #9
0
 static Globals()
 {
     MyBeanApi = new BeanApi("connection string", SQLiteFactory.Instance);
     MyBeanApi.EnterFluidMode();
 }
Example #10
0
        void CRUD()
        {
            /// ## CRUD
            /// (Create / Read / Update / Delete)
            ///
            /// For basic usage, LimeBean requires zero configuration and no additional code!
            /// Database schema is maintained on-the-fly:
            /// no need to create tables and columns (see [Fluid Mode](#fluid-mode)).
            ///
            /// Take a look at the following sample scenario:
            ///
#if CODE
            // Use a temporary in-memory SQLite database
            var api = new BeanApi("Data Source=:memory:", SQLiteFactory.Instance);

            // Enter the "Fluid Mode"
            api.EnterFluidMode();
#endif
            /// **Create**
#if CODE
            // Create a bean. "Bean" means "data record", "dispense" means "instantiate new".
            var bean = api.Dispense("book");

            // Each bean has a kind. "Kind" is a synonym for "table name"
            var kind = bean.GetKind();
            Console.WriteLine(kind);

            // Fill it with some data
            bean["title"]  = "Three Comrades";
            bean["rating"] = 10;

            // Store it
            // Table "book" with 2 columns, one string and one integer, will be generated automatically
            var id = api.Store(bean);

            // Each saved bean has an ID, or primary key
            Console.WriteLine(id);
#endif
            /// **Read**
#if CODE
            // Load by ID
            bean = api.Load("book", id);
#endif
            /// **Update**
#if CODE
            // Make some edits
            bean["title"]        = "Learn LimeBean";
            bean["release_date"] = new DateTime(2015, 7, 30);
            bean["rating"]       = "good";

            // Save updated bean
            // One new column ("release_date") will be added
            // The type of column "rating" will be expanded from integer to string
            api.Store(bean);
#endif
            /// **Delete**
#if CODE
            api.Trash(bean);

            // Don't forget to close the connection
            api.Dispose();
#endif
        }
Example #11
0
        void CRUD()
        {
            /// ## CRUD
            /// (Create / Read / Update / Delete)
            ///
            /// For basic usage, LimeBean requires zero configuration and no additional code!
            /// Database schema is maintained on-the-fly:
            /// no need to create tables and columns (see [Fluid Mode](#fluid-mode)).
            ///
            /// Take a look at the following sample scenario:
            ///
            #if CODE
            // Use a temporary in-memory SQLite database
            var api = new BeanApi("Data Source=:memory:", SQLiteFactory.Instance);

            // Enter the "Fluid Mode"
            api.EnterFluidMode();
            #endif
            /// **Create**
            #if CODE
            // Create a bean. "Bean" means "data record", "dispense" means "instantiate new".
            var bean = api.Dispense("book");

            // Each bean has a kind. "Kind" is a synonym for "table name"
            var kind = bean.GetKind();
            Console.WriteLine(kind);

            // Fill it with some data
            bean["title"] = "Three Comrades";
            bean["rating"] = 10;

            // Store it
            // Table "book" with 2 columns, one string and one integer, will be generated automatically
            var id = api.Store(bean);

            // Each saved bean has an ID, or primary key
            Console.WriteLine(id);
            #endif
            /// **Read**
            #if CODE
            // Load by ID
            bean = api.Load("book", id);
            #endif
            /// **Update**
            #if CODE
            // Make some edits
            bean["title"] = "Learn LimeBean";
            bean["release_date"] = new DateTime(2015, 7, 30);
            bean["rating"] = "good";

            // Save updated bean
            // One new column ("release_date") will be added
            // The type of column "rating" will be expanded from integer to string
            api.Store(bean);
            #endif
            /// **Delete**
            #if CODE
            api.Trash(bean);

            // Don't forget to close the connection
            api.Dispose();
            #endif
        }
Example #12
0
 protected void Application_BeginRequest(object sender, EventArgs e)
 {
     MyBeanApi = new BeanApi("connection string", SQLiteFactory.Instance);
     MyBeanApi.EnterFluidMode();
 }
Example #13
0
 static Globals()
 {
     MyBeanApi = new BeanApi("connection string", SQLiteFactory.Instance);
     MyBeanApi.EnterFluidMode();
 }
Example #14
0
            /// ## BeanApi Object Lifetime
            /// The `BeanApi` class is `IDisposable` (it holds the `DbConnection`) and is not thread-safe.
            /// Care should be taken to ensure that the same `BeanApi` is not used from multiple threads without
            /// synchronization, and that it is properly disposed. Let's consider some common usage scenarios.
            /// 
            /// ### Local Usage
            /// If LimeBean is used locally, then it should be enclosed in a `using` block:
            void LocalUsage(string connectionString, Type connectionType)
            {
                #if CODE
                using(var api = new BeanApi(connectionString, connectionType)) {
                    api.EnterFluidMode();

                    // work with beans
                }
                #endif
            }
Example #15
0
 void FluidMode(BeanApi api)
 {
     /// ## Fluid Mode
     /// LimeBean is committed to mitigate the common inconvenience associated with relational databases,
     /// namely necessity to manually create tables, columns and adjust their data types.
     /// In this sense, LimeBean takes SQL databases a little closer to NoSQL ones like MongoDB.
     ///
     /// **Fluid Mode** is optional, turned off by default, and is recommended for use only during early development stages
     /// (particularly for prototyping and scaffolding).
     /// To enable it, invoke the `EnterFluidMode` method on the `BeanApi` object:
     #if CODE
     api.EnterFluidMode();
     #endif
     /// How does it work? When you save the next bean, LimeBean analyzes its fields and compares
     /// their names and types to the database schema.
     /// If new data cannot be stored to an existing table, schema alteration occurs.
     /// LimeBean can create new tables, add missing columns, and widen data types.
     /// It will never truncate data or delete unused columns.
     ///
     /// **NOTE:** LimeBean doesn't detect renamings.
     ///
     /// **CAUTION:** Automatically generated schema is usually sub-optimal and lacks indexes which are essential
     /// for performance. When most of planned tables are already in place,
     /// and only minor changes are expected,
     /// it is recommended to turn the Fluid Mode off, audit the database structure, add indexes, and make further schema
     /// changes with a dedicated database management tool (like HeidiSQL, SSMS, pgAdmin, etc).
     ///
 }