I have some existing code
local.query = new Query();
local.query = setDataSource(application.DSN);
local.setSQL("
SELECT ...
");
local.result = local.query.execute(); // I am OK up to this point
local.prefix = local.result.getPrefix();
I understand that this gets the meta information for the query.
How do I convert this to
local.prefix = QueryExecute("
SELECT ...
",
[],
{datasource : application.DSN}
);
Now what? Where do I do getPrefix()?
A resultset is returned from queryExecute()
local.result = QueryExecute("
SELECT ... ", [],
{datasource : application.DSN}
);
local.prefix = result.prefix
Related
Creating a WCF in VisualStudio 2017, I need to create a WebMethod that will return multiple rows from a SQL query. Here is the code that is not working...
code
[WebMethod]
public List<TABLE_NAME> GetAllLineInfoDetailes(string OrderNumberSM)
{
string #OrdNumLG = OrderNumberSM;
List<TABLE_NAME> OrdNo = new List<TABLE_NAME>();
using (CONNECTION_NAME pubs = new CONNECTION_NAME())
{
var OrdNo_LINES = (from p in pubs.TABLE_NAME select p.OrderNumber == #OrdNumLG);
foreach (TABLE_NAME OrderLine in OrdNo_LINES)
{
TABLE_NAME a = new TABLE_NAME();
a.ItemNumber = OrderLine.ItemNumber;
a.LineNumber = OrderLine.LineNumber;
a.OrderNumber = OrderLine.OrderNumber;
OrdNo.Add(a);
}
}
return OrdNo;
}
code
The foreach is giving error "Cannot convert type 'bool' to 'CONNECTION_NAME.TABLE_NAME'"
Any help with this, or a better way to return the full result set, would be appreciated.
As the error says, it's a type conversion problem.
You need to
var OrdNo_LINES = (from p in pubs.TABLE_NAME select p.OrderNumber = #OrdNumLG);
replace
var OrdNo_LINES = (from p in pubs.TABLE_NAME select p.OrderNumber == #OrdNumLG);
Found what will work...
[WebMethod]
public List<TABLE_NAME> GetAllLineInfoDetailes(string OrderNumberSM)
{
string #OrdNumSM = OrderNumberSM;
using (CONNECTION_NAME pubs = new CONNECTION_NAME())
{
var query = (from c in pubs.TABLE_NAME
where c.OrderNumber == #OrdNumSM
orderby c.LineNumber ascending
select c).ToList();
return query;
}
}
Im trying to Right join in symfony. I tried as described here Doctrine 2 - Outer join query and here Symfony - Using Outer Joins with Doctrine ORM .
$query = $em->getRepository('AppBundle:raports')->createQueryBuilder('r')
->select('r')
->leftJoin('r.requestRaports rr WITH rr.formId = :formId', false)
->setParameter('formId', $requestId->getFormId())
->getQuery();
it gives
SELECT
r0_.id AS id_0,
r0_.adminComment AS adminComment_1,
r0_.addDate AS addDate_2,
r0_.submitDate AS submitDate_3,
r0_.statusId AS statusId_4,
r0_.userId AS userId_5,
r0_.requestId AS requestId_6,
r0_.requestRaports AS requestRaports_7
FROM
raports r0_
LEFT JOIN request_raports r1_ ON r0_.requestRaports = r1_.id
AND (r1_.formId = ?)
When i try
$query = $em->getRepository('AppBundle:raports')->createQueryBuilder('r')
->select('r')
->join('r.requestRaports rr WITH rr.formId = :formId', false)
->setParameter('formId', $requestId->getFormId())
->getQuery();
it looks like that
SELECT
r0_.id AS id_0,
r0_.adminComment AS adminComment_1,
r0_.addDate AS addDate_2,
r0_.submitDate AS submitDate_3,
r0_.statusId AS statusId_4,
r0_.userId AS userId_5,
r0_.requestId AS requestId_6,
r0_.requestRaports AS requestRaports_7
FROM
raports r0_
INNER JOIN request_raports r1_ ON r0_.requestRaports = r1_.id
AND (r1_.formId = ?)
But i want query like
SELECT * FROM raports r RIGHT JOIN request_raports rr ON
r.requestRaports = rr.id
How to make right join work in doctrine2?
You can ether use a LEFT JOIN and reversing your SQL like this
SELECT * FROM request_raports rr LEFT JOIN raports r ON r.requestRaports = rr.id
Or you can create your own "right join". If you look at leftJoin definition in doctrine, it's like :
leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null)
{
$parentAlias = substr($join, 0, strpos($join, '.'));
$rootAlias = $this->findRootAlias($alias, $parentAlias);
$join = new Expr\Join(
Expr\Join::LEFT_JOIN, $join, $alias, $conditionType, $condition, $indexBy
);
return $this->add('join', array($rootAlias => $join), true);
}
So, it may look like this :
$qb = $this->createQueryBuilder('b');
$rightJoin = new Expr\Join('RIGHT', 'r.requestRaports', 'rr', Expr\Join::WITH, 'rr.formId = :formId');
$qb
->select('r')
->add('join', ['r' => $rightJoin], true)
...
I have not tested this and don't know if its the best way to do it ...
I am trying to populate a list with maps created from objects in a query/findAllBy... I always end up with list of maps identical to the last map in the loop.
I have set break points and stepped through the method and found that 1) the data returned from the query is correct, 2) as I step through the loop the data is inserted into the map(s) correctly, 3) the failure comes when inserting the map into the list. All the methods I've used to insert elements into a list (.add, .push, <<, list[(i)] = map, etc) end up overwriting all the previous elements in the list.
Please help. I don't know why this is happening. Hopefully this is an easy one for someone out there.
def shiftRecords = Shift.findAllByUserAndStartTimeBetween( userInstance, startDate, endDate )
ArrayList allShifts = new ArrayList()
LinkedHashMap thisShift = new LinkedHashMap()
def size = shiftRecords.size()
for ( int i = 0; i < size; i++ ){
thisShift["id"] = shiftRecords[(i)].id
thisShift["user"] = shiftRecords[(i)].user
thisShift["startTime"] = shiftRecords[(i)].startTime
thisShift["posCode"] = shiftRecords[(i)].posCode
thisShift["deptCode"] = shiftRecords[(i)].deptCode
thisShift["billingIDX"] = shiftRecords[(i)].billingIDX
Position thisPos = Position.findByPositionCode( thisShift.posCode )
thisShift["posTitle"] = thisPos.shortTitle
thisShift["deptTitle"] = thisPos.departmentTitle
allShifts.add( (i), thisShift )
}
I need the results of allShifts to be a list of maps with the selected data pulled from the Shift query results. I've tried using shiftRecords.each and eachWithIndex. The problem happens with any type of loop at the point where thisShift map is inserted into allShifts. It doesnt just insert one instance of the map, but replaces all list elements with the current thisShift map.
def shiftRecords = Shift.findAllByUserAndStartTimeBetween(
userInstance, startDate, endDate )
ArrayList allShifts = new ArrayList()
def size = shiftRecords.size()
for ( int i = 0; i < size; i++ ){
LinkedHashMap thisShift = new LinkedHashMap()
thisShift["id"] = shiftRecords[(i)].id
thisShift["user"] = shiftRecords[(i)].user
thisShift["startTime"] = shiftRecords[(i)].startTime
thisShift["posCode"] = shiftRecords[(i)].posCode
thisShift["deptCode"] = shiftRecords[(i)].deptCode
thisShift["billingIDX"] = shiftRecords[(i)].billingIDX
Position thisPos = Position.findByPositionCode( thisShift.posCode )
thisShift["posTitle"] = thisPos.shortTitle
thisShift["deptTitle"] = thisPos.departmentTitle
allShifts << thisShift
}
You need to create a new map each time you iterate over shiftRecords. Although, the above code can be overly simplified in groovy as below:
def shiftRecords = Shift.findAllByUserAndStartTimeBetween(
userInstance, startDate, endDate )
def allShifts = []
shiftRecords.each{
def thisShift = [:]
thisShift.id = it.id
thisShift.user = it.user
thisShift.startTime = it.startTime
thisShift.posCode = it.posCode
thisShift.deptCode = it.deptCode
thisShift.billingIDX = it.billingIDX
Position thisPos = Position.findByPositionCode( thisShift.posCode )
thisShift.posTitle = thisPos.shortTitle
thisShift.deptTitle = thisPos.departmentTitle
allShifts << thisShift
}
Try:
def shiftRecords = Shift.findAllByUserAndStartTimeBetween( userInstance, startDate, endDate )
def allShifts = shiftRecords.collect { it ->
def pos = Position.findByPositionCode( it.posCode )
[ id : it.id,
user : it.user,
startTime : it.startTime,
posCode : it.posCode,
deptCode : it.deptCode,
billingIDX : it.billingIDX,
posTitle : pos.shortTitle,
deptTitle : pos.departmentTitle ]
}
Ok! I have a flashVar variable that is coming into Flash, its URL encoded but I have already decoded it. My problem is I want the set of variables to be pushed into an array.
Let's say the variables are
"&text0=Enter Text...&size0=18&font0=Arial&color0=0&rotation0=0&y0=360&x0=640&text1=Enter
Text...&size1=18&font1=Arial&color1=0&rotation1=0&y1=360&x1=640"
and so on...
What I want is the variables to go into an array like
myArray[0].text = Enter Text...
myArray[0].size = 18]
myArray[0].font = Arial
myArray[0].color = 0
myArray[0].rotation = 0
myArray[0].y = 360
myArray[0].x = 640
myArray[1].text = ...........
.............................
.............................
myArray[n].text = ...........
I think there must be some way to do this. Most probably I'm thinking regular expression, but I'm pretty bad at regular expression. Please some help would be very very appreciated.
Thank You!
You don't have to decode your query string, just use the URLVariables object - it will do all the decoding for you. Then iterate over its dynamic properties to create your array. Use a RegExp to find the index numbers at the end of your variable keys:
function parseURLVariables( query:String ) : Array {
var vars:URLVariables = new URLVariables (query);
var arr:Array = [];
for (var key : String in vars) {
var splitIndex : int = key.search(/[0-9]+$/);
var name:String = key.substr (0,splitIndex);
var indexNumber:int = parseInt ( key.substr(splitIndex));
arr[indexNumber] ||= {};
arr[indexNumber][name] = vars[key];
}
return arr;
}
Since your query string starts with a an ampersand, you might have to use parseURLVariables ( myString.substr(1)), otherwise the URLVariables object will throw an error, complaining that the query string is not valid (it has to be url encoded, and start with a variable key).
you may use split method of string to something like this;
var astrKeyValue: Array = url.Split( "&" );
in this way each value in astrKeyValue is string keyvalue ( for example font1=Arial )
after than you may split each item with "=" and will get pair key and value ( for key - font1 and for value - arial)
so this code maybe will work for you
var str = "text0=Enter Text...&size0=18&font0=Arial&color0=0&rotation0=0&y0=360&x0=640&text1=Enter Text...&size1=18&font1=Arial&color1=0&rotation1=0&y1=360&x1=640"
var a : Array = str.split( "&" );
var newArr: Array = new Array()
for each ( var str1 in a )
{
var t: Array = str1.split( "=" );
newArr[ t[0] ] = t[1];
}
trace( newArr.text0 ) // -> Enter Text...
Here is a solution for you from me,
//your string data should be like this, there should be a seperate seperator (i've used pipe sign |) for each element which will be converted to an object and then pushed to the array
var strData:String = "text=Enter Text...&size=18&font=Arial&color=0&rotation=0&y=360&x=640|text=Enter Text...&size=18&font=Arial&color=0&rotation=0&y=360&x=640";
var myArray:Array = new Array();
var _tmpArr:Array = strData.split("|");
//populating the array
for(var i:int=0;i<_tmpArr.length;i++)
{
myArray.push(strToObj(_tmpArr[i]));
}
trace(myArray.length);
// coverts chunk of string to object with all key and value in it
function strToObj(str:String):Object
{
var obj:Object = new Object();
var tmpArr:Array = str.split('&');
for (var i:int = 0; i < tmpArr.length; i++)
{
var _arr:Array = String(tmpArr[i]).split('=');
var key:String = String(_arr[0]);
var val:String = String(_arr[1]);
obj[key] = val;
trace(key+" = "+val);
}
trace("----");
return obj;
}
I am able to retrieve database values and insert database values, but I can't figure out what the Update() syntax should be with a where statement.
Environment -> ASP.Net, C#
Settings.ttinclude
const string Namespace = "subsonic_db.Data";
const string ConnectionStringName = "subsonic_dbConnectionString";
//This is the name of your database and is used in naming
//the repository. By default we set it to the connection string name
const string DatabaseName = "subsonic_db";
Retreive example
var product = equipment.SingleOrDefault(x => x.id == 1);
Insert Example
equipment my_equipment = new equipment();
try
{
// insert
my_equipment.parent_id = 0;
my_equipment.primary_id = 0;
my_equipment.product_code = product_code.Text;
my_equipment.product_description = product_description.Text;
my_equipment.product_type_id = Convert.ToInt32(product_type_id.SelectedItem.Value);
my_equipment.created_date = DateTime.Now;
my_equipment.serial_number = serial_number.Text;
my_equipment.Save();
}
catch (Exception err)
{
lblError.Text = err.Message;
}
Edit: Think that I was just too tired last night, it is pretty easy to update. Just use the retrieve function and use the Update() on that.
var equip = Equipment.SingleOrDefault(x => x.id == 1);
lblGeneral.Text = equip.product_description;
equip.product_description = "Test";
equip.Update();
Resolved. View answer above.