static void Close(int id) { SQLQueryObj obj = GetSQLObject(id); obj.DataReader.Dispose(); obj.Command.Dispose(); obj.Connection.Dispose(); s_queries[id] = null; }
static int ExecuteQuery(string query) { SQLQueryObj newQuery = new SQLQueryObj(); newQuery.Query = GetSQLQuery(query); newQuery.Connection = new SqlConnection(CSCS_SQL.ConnectionString); newQuery.Connection.Open(); newQuery.Command = new SqlCommand(newQuery.Query, newQuery.Connection); newQuery.DataReader = newQuery.Command.ExecuteReader(); newQuery.Table = GetTableName(query); newQuery.Columns = SQLQueryFunction.GetColumnData(newQuery.Table); s_queries.Add(newQuery); return(s_queries.Count - 1);// (int)count; }
static int GetTotalRecords(int id) { SQLQueryObj obj = GetSQLObject(id); if (obj.TotalRows >= 0) { return(obj.TotalRows); } using (var sqlCon = new SqlConnection(CSCS_SQL.ConnectionString)) { sqlCon.Open(); var com = sqlCon.CreateCommand(); com.CommandText = GetCountQuery(obj.Query); var totalRow = com.ExecuteScalar(); obj.TotalRows = (int)totalRow; sqlCon.Close(); } return(obj.TotalRows); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, m_name); CSCS_SQL.CheckConnectionString(script, m_name); if (m_mode == Mode.SETUP) { var query = Utils.GetSafeString(args, 0); Variable result = new Variable(ExecuteQuery(query)); return(result); } else if (m_mode == Mode.NEXT) { var id = Utils.GetSafeInt(args, 0); return(GetNextRecord(id)); } else if (m_mode == Mode.CLOSE) { var id = Utils.GetSafeInt(args, 0); Close(id); return(Variable.EmptyInstance); } else if (m_mode == Mode.CURRENT_ROW) { var id = Utils.GetSafeInt(args, 0); SQLQueryObj obj = GetSQLObject(id); Variable result = new Variable(obj.CurrentRow); return(result); } else if (m_mode == Mode.TOTAL) { var id = Utils.GetSafeInt(args, 0); Variable result = new Variable(GetTotalRecords(id)); return(result); } return(Variable.EmptyInstance); }
static Variable GetNextRecord(int id) { SQLQueryObj obj = GetSQLObject(id); if (obj == null || !obj.DataReader.HasRows || !obj.DataReader.Read()) { return(Variable.EmptyInstance); } Variable rowVar = new Variable(Variable.VarType.ARRAY); for (int i = 0; i < obj.DataReader.FieldCount; i++) { var cell = obj.DataReader.GetValue(i); var cellType = obj.DataReader.GetDataTypeName(i); var variable = SQLQueryFunction.ConvertToVariable(cell, cellType); rowVar.AddVariable(variable); } obj.CurrentRow++; return(rowVar); }
static SQLQueryObj GetSQLObject(int id, bool throwExc = true) { if (id < 0 || id >= s_queries.Count) { if (!throwExc) { return(null); } throw new ArgumentException("Invalid handle: " + id); } SQLQueryObj obj = s_queries[id]; if (obj == null) { if (!throwExc) { return(null); } throw new ArgumentException("Object has already been recycled. Handle: " + id); } return(obj); }