Friday, 16 October 2015

SQLStatement.execute() - multiple queries in one statement

Question:
I've written a database generation script in SQL and want to execute it in my Adobe AIR application:
Create Table tRole (
      roleID integer Primary Key
      ,roleName varchar(40)
);
Create Table tFile (
    fileID integer Primary Key
    ,fileName varchar(50)
    ,fileDescription varchar(500)
    ,thumbnailID integer
    ,fileFormatID integer
    ,categoryID integer
    ,isFavorite boolean
    ,dateAdded date
    ,globalAccessCount integer
    ,lastAccessTime date
    ,downloadComplete boolean
    ,isNew boolean
    ,isSpotlight boolean
    ,duration varchar(30)
);
Create Table tCategory (
    categoryID integer Primary Key
    ,categoryName varchar(50)
    ,parent_categoryID integer
);
...
I execute this in Adobe AIR using the following methods:
public static function RunSqlFromFile(fileName:String):void {
    var file:File = File.applicationDirectory.resolvePath(fileName);
    var stream:FileStream = new FileStream();
    stream.open(file, FileMode.READ)
    var strSql:String = stream.readUTFBytes(stream.bytesAvailable);
    NonQuery(strSql);
}

public static function NonQuery(strSQL:String):void
{
    var sqlConnection:SQLConnection = new SQLConnection();
    sqlConnection.open(File.applicationStorageDirectory.resolvePath(DBPATH);
    var sqlStatement:SQLStatement = new SQLStatement();
    sqlStatement.text = strSQL;
    sqlStatement.sqlConnection = sqlConnection;
    try
    {
        sqlStatement.execute();
    }
    catch (error:SQLError)
    {
        Alert.show(error.toString());
    }
}
No errors are generated, however only tRole exists. It seems that it only looks at the first query (up to the semicolon- if I remove it, the query fails). Is there a way to call multiple queries in one statement?


Answer:
I wound up using this. It is a kind of a hack, but it actually works pretty well. The only thing is you have to be very careful with your semicolons. : D
var strSql:String = stream.readUTFBytes(stream.bytesAvailable);      
var i:Number = 0;
var strSqlSplit:Array = strSql.split(";");
for (i = 0; i < strSqlSplit.length; i++){
    NonQuery(strSqlSplit[i].toString());
}

How can you get Subclipse in Aptana to work with the newest release of Subversion?

Question:
The version of Subclipse (1.2.4) currently available through Aptana's automagic Plugins Manager does not work with the newest version of Subversion.
I see on the Subclipse website however that they have 1.4.2 out for Eclipse. So I added a new remote update site to my Update manager. When I tried to install it, it told me I needed Mylyn 3.0.0. So I after much searching I found Mylyn 3.0.0 and added another new remote update site to my update manager. Then when I tried to install that, it told me I needed org.eclipse.ui 3.3.0 or equivalent.
Looking at the configuration details for Aptana, it looks like it is built against eclipse 3.2.2.
Does anyone know if there is a way to upgrade the version of Eclipse Aptana is built against to 3.3.0? Or if there is some other way to get Subclipse to work with the very newest version of Subversion?
I know this isn't necessarily a "programming" question, but I hope it's ok since it's highly relevant to the programming experience...

Answer:

Subclipse does not require Mylyn, but the update site includes a plugin that integrates Mylyn and Subclipse. This is intended for people that use Mylyn. In your case, you would want to just de-select Mylyn in the update dialog.
Subclipse also requires Subversion 1.5 and the corresponding version of the JavaHL native libraries. I have written the start of an FAQ to help people understand JavaHL and how to get it. See: http://desktop-eclipse.open.collab.net/wiki/JavaHL

How do I add existing comments to RDoc in Ruby?

Question:
I've got all these comments that I want to make into 'RDoc comments', so they can be formatted appropriately and viewed using ri. Can anyone get me started on understanding how to use RDoc?

Answer:
RDoc uses SimpleMarkup so it's fairly simple to create lists, etc. using *, - or a number. It also treats lines that are indented at the same column number as part of the same paragraph until there is an empty line which signifies a new paragraph. Do you have a few examples of comments you want RDoc'ed so we could show you how to do them and then you could extrapolate that for the rest of your comments?

Paging a collection with LINQ

Question:
How do you page through a collection in LINQ given that you have a startIndex and a count?

Answer:
A few months back I wrote a blog post about Fluent Interfaces and LINQ which used an Extension Method on IQueryable<T> and another class to provide the following natural way of paginating a LINQ collection.
var query = from i in ideas
            select i;
var pagedCollection = query.InPagesOf(10);
var pageOfIdeas = pagedCollection.Page(2);

Thursday, 15 October 2015

How do I calculate relative time?

Question :
Given a specific DateTime value, how do I display relative time, like:
  • 2 hours ago
  • 3 days ago
  • a month ago
Answer :
Jeff, your code is nice but could be clearer with constants (as suggested in Code Complete).

const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
const int MONTH = 30 * DAY;

var ts = new TimeSpan(DateTime.UtcNow.Ticks - yourDate.Ticks);
double delta = Math.Abs(ts.TotalSeconds);

if (delta < 1 * MINUTE)
{
  return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
}
if (delta < 2 * MINUTE)
{
  return "a minute ago";
}
if (delta < 45 * MINUTE)
{
  return ts.Minutes + " minutes ago";
}
if (delta < 90 * MINUTE)
{
  return "an hour ago";
}
if (delta < 24 * HOUR)
{
  return ts.Hours + " hours ago";
}
if (delta < 48 * HOUR)
{
  return "yesterday";
}
if (delta < 30 * DAY)
{
  return ts.Days + " days ago";
}
if (delta < 12 * MONTH)
{
  int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
  return months <= 1 ? "one month ago" : months + " months ago";
}
else
{
  int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
  return years <= 1 ? "one year ago" : years + " years ago";
}

How do I calculate someone's age in C#?

Question :
Given a DateTime representing a person's birthday, how do I calculate their age?

Answer :
For some reason Jeff's code didn't seem simple enough. To me this seems simpler and easier to understand:
DateTime today = DateTime.Today;
int age = today.Year - bday.Year;
if (bday > today.AddYears(-age)) age--;

Why doesn't the percentage width child in absolutely positioned parent work?

Question :
I have an absolutely positioned div containing several children, one of which is a relatively positioned div. When I use a percentage-based width on the child div, it collapses to 0 width on IE7, but not on Firefox or Safari.
If I use pixel width, it works. If the parent is relatively positioned, the percentage width on the child works.
  1. Is there something I'm missing here?
  2. Is there an easy fix for this besides the pixel-based width on the child?
  3. Is there an area of the CSS specification that covers this

    Answer :
    Does the parent div have a defined width either pixel or percentage? Not 100% sure but I think in IE7, the parent div needs a defined width for child percentage divs to work correctly.