Example #1
0
        /// <summary>
        /// Gets all the primary keys for tablename from values. Throws an exception if any of the keys
        /// are not supplied
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="tablename"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static Dictionary<string, object> ExtractKeys(this IAnalyzable writer, string tablename,
            Dictionary<string, object> values)
        {
            var keynames = writer.Analyzer.GetPrimaryKeys(tablename);
            // Make sure they supplied all the keys
            if (!values.Keys.ToSet().IsSupersetOf(keynames))
                throw new KeyNotSetException(tablename, keynames.Except(values.Keys));

            var keys = values.Only(keynames);
            return keys;
        }
Example #2
0
        public virtual Dictionary<string, object> Insert(string tableName, Dictionary<string, object> row)
        {
            if (StrictTables && !Analyzer.TableExists(tableName))
                throw new NotATableException(tableName);

            // Make sure all the required keys are supplied
            this.AssertInsertKeys(tableName, row);

            var sql = MakeInsertSql(tableName, row);
            var autoKey = sqlInsert(sql, row);

            // If there's an autonumber, make sure we add it to the result
            var autoNumberKeyName = Analyzer.GetAutoNumberKey(tableName);
            //if (autoNumberKeyName != null && autoKey == null)
            //    throw new ThisSadlyHappenedException("The SQL ran beautifully, but you were expecting an autogenerated number and you did not get it");
            if(autoNumberKeyName != null && autoKey.HasValue)
                row = row.Assoc(autoNumberKeyName, autoKey.Value);

            // Return the set of primary keys from the insert operation
            var primaryKeys = Analyzer.GetPrimaryKeys(tableName);
            return row.Only(primaryKeys);
        }
        public void Only()
        {
            var dict = new Dictionary<int, string>();
            dict.Add(1, "one");
            dict.Add(2, "two");
            dict.Add(3, "three");

            Assert.AreEqual("()", dict.Only(57).Print());
            Assert.AreEqual("([1,one])", dict.Only(1).Print());
            Assert.AreEqual("([1,one],[3,three])", dict.Only(1,3).Print());
        }