예제 #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
 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));
 }