コード例 #1
0
        public static void CreateTable(this CrateConnection conn, Type t)
        {
            var tabledata = AttributeHelpers.GetTableData(t);
            var tablecols = CrateFieldCacheProvider.Instance.Get(t);

            var sb = new StringBuilder();

            sb.Append("CREATE TABLE ").Append(tabledata.Name).Append(" ( ");
            sb.Append(LoopObject(t));

            var pk = tablecols.Where(tc => tc.Value.PrimaryKey).Select(tc => tc.Value.Name).ToArray();

            if (pk.Any())
            {
                sb.Append("primary key (").Append(string.Join(" ,", pk)).Append(" )");
            }
            else
            {
                sb.Remove(sb.Length - 2, 2);
            }
            sb.Append(" ) ");

            sb.Append(string.Format("clustered into {0} shards ", tabledata.NumberOfShards));
            sb.Append(string.Format(" with (number_of_replicas = '{0}') ", EnumHelpers.Replication(tabledata.NumberOfReplicas)));

            var sql = sb.ToString();

            using (var cmd = new CrateCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }
        }
コード例 #2
0
        public static void DropTable(this CrateConnection conn, string name)
        {
            var sql = "DROP TABLE" + name;

            using (var cmd = new CrateCommand(sql, conn))
            {
                cmd.ExecuteNonQuery();
            }
        }
コード例 #3
0
        public static int Delete <T>(this CrateConnection conn, Expression <Func <T, bool> > exp)
        {
            var table = AttributeHelpers.GetTableData(typeof(T));

            var swhere = "DELETE FROM " + table.Name + " WHERE " + (new WhereTranslater()).Translate(exp);

            using (var cmd = new CrateCommand(swhere, conn))
            {
                return(cmd.ExecuteNonQuery());
            }
        }
コード例 #4
0
        public static bool CheckIfTableExists(this CrateConnection conn, string name)
        {
            var ret = new List <Table>();

            var sql = "SELECT * FROM information_schema.tables WHERE table_name= '" + name + "'";

            using (var cmd = new CrateCommand(sql, conn))
            {
                var reader = (CrateDataReader)cmd.ExecuteReader();
                ret = reader.ToList <Table>();
            }

            return(ret.Count > 0);
        }
コード例 #5
0
        public static List <T> Where <T>(this CrateConnection cnn, Expression <Func <T, bool> > exp) where T : class, new()
        {
            List <T> ret;

            var table = AttributeHelpers.GetTableData(typeof(T));

            var swhere = "SELECT * FROM " + table.Name + " WHERE " + (new WhereTranslater()).Translate(exp);

            using (var cmd = new CrateCommand(swhere, cnn))
            {
                var reader = (CrateDataReader)cmd.ExecuteReader();
                ret = reader.ToList <T>();
            }

            return(ret);
        }
コード例 #6
0
        public static List <TOut> Execute <T, TOut>(this CrateConnection cnn, Expression <Func <List <T>, IEnumerable <TOut> > > exp)
            where T : class, new()
            where TOut : class, new()
        {
            List <TOut> ret;

            var table = AttributeHelpers.GetTableData(typeof(T));

            var sql = (new Translater(table.Name)).Translate(exp);

            using (var cmd = new CrateCommand(sql, cnn))
            {
                var reader = (CrateDataReader)cmd.ExecuteReader();
                ret = reader.ToList <TOut>();
            }

            return(ret);
        }
コード例 #7
0
        public static long Count <T>(this CrateConnection cnn, Expression <Func <T, bool> > exp = null) where T : class, new()
        {
            long ret   = 0;
            var  table = AttributeHelpers.GetTableData(typeof(T));

            var swhere = "SELECT count(*) FROM " + table.Name;

            if (exp != null)
            {
                swhere += " WHERE " + (new WhereTranslater()).Translate(exp);
            }

            using (var cmd = new CrateCommand(swhere, cnn))
            {
                var reader = (CrateDataReader)cmd.ExecuteReader();
                if (reader.Read())
                {
                    ret = reader.GetLong(0);
                }
            }

            return(ret);
        }
コード例 #8
0
        public static void Update <T>(this CrateConnection conn, Expression <Func <T, bool> > condition, params IUpdateObject[] vals)
        {
            var td     = AttributeHelpers.GetTableData(typeof(T));
            var fields = CrateFieldCacheProvider.Instance.Get(typeof(T));

            /* foreach (var item in fields)
             * {
             *   var con=(new WhereTranslater()).Translate(condition);
             * }*/
            var con = (new WhereTranslater()).Translate(condition);

            foreach (var val in vals)
            {
                var t = val.ValueType;

                /* if (t == typeof(string))
                 * {
                 *   var cval = val as UpdateObject<T, string>;
                 *   var s = new SelectTranslater().Translate(cval.Set);
                 * }
                 * else if (t == typeof(bool))
                 * {
                 *   var cval = val as UpdateObject<T, bool>;
                 *   var s = new SelectTranslater().Translate(cval.Set);
                 * }
                 * else if (t == typeof(DateTime))
                 * {
                 *   var cval = val as UpdateObject<T, DateTime>;
                 *   var s = new SelectTranslater().Translate(cval.Set);
                 * }
                 * else
                 * {
                 *
                 * }*/
                var cval = val.ToSql <T>();
            }
        }
コード例 #9
0
 public static void Insert <T>(this CrateConnection conn, T data)
 {
 }
コード例 #10
0
ファイル: CrateCommand.cs プロジェクト: retek4/crate-mono
 public CrateCommand(string commandText, CrateConnection connection)
 {
     CommandText      = commandText;
     this._connection = connection;
 }
コード例 #11
0
        public static void DropTable <T>(this CrateConnection conn)
        {
            var tabledata = AttributeHelpers.GetTableData(typeof(T));

            conn.DropTable(tabledata.Name);
        }
コード例 #12
0
        public static bool CheckIfTableExists <T>(this CrateConnection conn)
        {
            var tabledata = AttributeHelpers.GetTableData(typeof(T));

            return(conn.CheckIfTableExists(tabledata.Name));
        }
コード例 #13
0
 public static void CreateTable <T>(this CrateConnection conn)
 {
     conn.CreateTable(typeof(T));
 }