Thursday, December 29, 2016

Quick Tip - Count lines

Here's a command to get the count of js lines in a node project:

find . -name '*.js' ! -path '*node_module*' | xargs wc -l

The -path part is to exclude the node_modules folders so you don't count 3rd part libs.

Wednesday, June 8, 2016

Quick Tip - import recognized in IntelliJ Idea

If you know you have the correct gradle configuration for an library, but the import isn't recognized in IntelliJ Idea, do the following:

File menu -> Invalidate Caches/Restart...

This can take a while.  IDEA will rebuild indexes.  But sometimes, this is required!

At command-line, you may need:

./gradlew clean build --refresh-dependencies


Friday, May 20, 2016

Kickstarting a Java program

Here are steps to create a new Java command-line program.  Use a different quick start if you are making a web server as there are guides for those.

0. Prerequisite: Install Gradle.

1. Create an empty folder, we will refer to as project1.

2. Copy this build.gradle code into a new build.gradle file in your project1 folder.

(Modified from https://docs.gradle.org/current/userguide/tutorial_java_projects.html )

apply plugin: 'java'apply plugin: 'idea'
sourceCompatibility = 1.8version = '0.1'jar {
    manifest {
        attributes 'Implementation-Title': 'War Game',
                   'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections',
    name: 'commons-collections',
    version: '3.2.2'    
    testCompile group: 'junit'
    name: 'junit', version: '4.+'}

test {
    systemProperties 'property': 'value'}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'       }
    }
}


3. At terminal, run
gradle wrapper
This will create a gradlew file for you.


4. At terminal, run
gradlew idea
Assuming using idea.

5. Open in idea

6. In IDEA, create this folder structure: src->main->java->project1

7. Right-click on the "java", mark as sources root.

8. Same thing for tests: src->test->java, mark java as test sources root.






Monday, July 2, 2012

JavaScript variable definition scope

function myFunction()
{
  var a =2;
  if (a>0)
  {
    var a=4;
    a++;
  }
  alert("A="+a);
}

The above code would display a message box saying "A=5"!  This is because variables scope is either global, or at function-level.  So, having {} blocks itself does not change the variable scope, and the "a" above is always the same variable.  Since nested functions and anonymous functions are allowed, this can be one way to achieve more granularly defined variables.

Tuesday, July 27, 2010

MySQL Innodb table database backups!

It is useful to be able to do database backups and restoration at the command line. Here's my commands for doing so, no 3rd party tools needed for this, just mysql. You can restore the database to a different server entirely.

mysqldump -h 127.0.0.1 -P [my_port] -u [me] -p[password] -B my_database >the_bak

where:
[password] is the database password (no space).
[me] is the mysql admin account username
my_database is the name of your database.
[my_port] database access port, default =3306

Restoration:
create database:
mysql -u[me] -p[password] information_schema -e "create database my_database"

restore database:
mysql -u[me] -p[password] my_database < ./the_bak.sql

New MySQL 5 stored procedures

Seems the old syntax of my stored procedure creation doesn't work in MySQL 5. Don't use $$ as a delimiter anymore!

Here's my NEW sample for you. I included a cursor.

DELIMITER |
CREATE PROCEDURE sp_my_stored_proc()
BEGIN

declare done int default 0;
declare n_value bigint default 0;
declare str_value varchar(200) default '';
declare cur1 cursor for select the_id, the_val from my_table;
declare continue handler for not found set done=1;

open cur1;
repeat
fetch cur1 into n_value, str_value;

if not done then
[multiple statements can go here, each terminated by ;]
end if;

UNTIL done END REPEAT;
CLOSE cur1;
END |

Thursday, May 14, 2009

Quasitime.com

Quaisitime.com is now released! I have been working on this for a couple years now. Thanks to all my blog readers. Now you can see what I have been using my web programming for. Go to quasitime.com and get your free account (pro and business solutions available as well).

I know I haven't posted for a while as it has been crunch time at my company, but I will resume shortly. I have a number of tips I need to post. I am also considering posting an RFC that, if adopted by operating systems and browsers, will make the world wide web work better.

-Dan

Sunday, February 24, 2008

Javascript Reference Pointers

Does Javascript assign values by "pointer" (to use the C term)?
Yes, indeed. Let's take a look how this impacts code.

When dealing with Strings you generally don't need to worry about this because the String modification functions don't actually modify the original String, they just return a new one.
Try this code after guessing what the output will be:

// Test 1: Append
var str_a = "aloha";
var str_b = str_a;
alert("str_a==str_b: "+(str_a==str_b));
str_b = "aloh";
str_b += "a";
alert("str_a="+str_a);
alert("str_b="+str_b);
alert("str_a==str_b: "+(str_a==str_b));

// Test 2: toUppercase
str_b = str_a;
str_b.toUpperCase();
alert("str_a="+str_a);
alert("str_b="+str_b);


In the first test appending to str_b does not affect str_a, even though they originally pointed to the same string. The appending actually sets str_b to a new string that contains the appended string. The comparison is overridden though, so your == for string comparisons still works even though the pointer values are different.

In the second test, the toUpperCase() function does not modify either string. It only returns a new string that is uppercase, which we haven't used. So, in place functions will not affect str_a, even if they are used on str_b. Obviously, if we did copy the result to str_b, then str_b would point to something else, and not affect str_a. I haven't tested with the valueOf() function, so if you are using that, you may want to test it to check the pointer behavior.

Next, let's look at the Date Object. What would you expect as a result from this code?

var jsdate_a = new Date();
var jsdate_b = jsdate_a;
alert("a: " +jsdate_a);
alert("b: " +jsdate_b);
jsdate_b.setHours(0);
alert("a: " +jsdate_a);
alert("b: " +jsdate_b);


In this case jsdate_a does get its hours reset to zero by modifying jsdate_b! That is because the Date Class has functions which modify the data in the object itself, without just creating a new instance. Since jsdate_a and jsdate_b point to the same Date Object, a modification to the data pointed to by one of them, affects the data pointed to by the other.

Thursday, October 4, 2007

Firefox object member size limit

I encountered an issue in Firefox with my data coming back from AJAX. The object members of the XML Doc were being truncated to 4096 bytes. If this happens, you can use the function normalize(); on the object resulting from the parser.parseFromString call to have Firefox correct this. Make sure to do a browser check, so you don't get a script error in other browsers that don't have teh normalize function. IE 7 does not have normalize(), but it does not need it in this case because it does not break up the nodes into 4K chunks when you create the document from teh XML stream.

To see what's going on, we can look at the W3C spec at http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html.

"When a document is first made available via the DOM, there is only one Text node for each block of text. Users may create adjacent Text nodes that represent the contents of a given element without any intervening markup, but should be aware that there is no way to represent the separations between these nodes in XML or HTML, so they will not (in general) persist between DOM editing sessions. The normalize() method on Element merges any such adjacent Text objects into a single node for each block of text; this is recommended before employing operations that depend on a particular document structure, such as navigation with XPointers."

According to the first line in the quote above, IE 7 has the correct behavior in this regard and not Firefox. When the document is first created, the browser should not be splitting the nodes into chunks. However, the data is still accessible, even when split. It is placed in child nodes. You should be able to access it via the DOM child node functions/properties. This requires extra coding, so it much easier to just use the "normalize()" function.

MySql stored proc delimiters

When creating a stored proc in MySQL (MySQL 5.0+ supports this), unfortunatly you have to workaround delimiter issues. If you just use semicolons to terminate each line as you would in MS SQL Server, it won't work. So, I'm posting a template here to help developers. I'll use $$ as the delimitor, but you can use a different symbol if you like:

DELIMITER $$;
DROP PROCEDURE IF EXISTS `my_db`.`my_proc_name`$$
CREATE PROCEDURE `my_db`.`my_proc_name` ()
BEGIN
declare my_var_A INT;
[some more statements, each terminated by ; ... ];
END$$
DELIMITER ;$$

The first line configures your new symbol ($$) to be statement terminator. This allows us to put semicolons, the ordinary statement termintor inside our stored procedure, without MySQL trying to go ahead and start executing them as statements when we run the above script to create the stored procedure. See the line "END$$". We terminate that with our new "real" delimiter to allow MySQL to execute that block, thereby creating the whole stored procedure. Then we restore the delimiter to semicolon. Now when we execute our stored procedure, we should be using the default semicolon delimiter, and so the stored procedure will run fine, having ordinary statement terminators.

Wednesday, July 11, 2007

Firefox comments issue

Here's an issue to be aware of in Firefox browsers:

If you make a comment in a .jsp file like this:
<!-- This is my comment -- no real info though -->

It will work fine in IE, but in Firefox I have found this to cause major problems, as it will interpret this as an unterminated comment:
<!-- This is my comment

So, be careful not to include -- in your Firefox comments!

Also, don't use the single tag element terminating slash for script includes:
<script type="text/javascript" src="test.js"></script> (This will break in IE7 or Firefox)
<script type="text/javascript" src="test.js"/> (This will work)

Thursday, June 21, 2007

Javascript URL encoding

If you are using AJAX or something else to communicate via url, you can use the javascript function "escape()" to encode parts of the URL. I list some of the characters that are encoded and some that are not:

Encoded: ! # $ % ^ ( ) = [ ] { } ; " \ ' ? / ,
Not encoded: underscore @ & * - + .

You can also use the javascript function "encodeURI". It has a different set of encoded characters:

Encoded: ` ^ [ ] { } " < > \
Not encoded: underscore tilde ! @ # & $ * ( ) = + , . : ; ' / ?

Wednesday, May 9, 2007

Doctype and browser compatibility

I recommend setting a doctype in your web pages. If you specify the right doctype, then you can avoid IE dropping into "quirks mode" ("quirks mode" = IE backwords compatibility mode). Thus, your pages will look almost the same in IE and Firefox.

You can get a list of doctypes here:
http://www.w3.org/QA/2002/04/valid-dtd-list.html

If you specify an "HTML" doctype, and use DHTML, then your jsp pages may still be in quirks mode. I recommend using an XHTML doctype to solve this problem, like this doctype for example:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"

Wednesday, May 2, 2007

SQL: Adding foreign key relationships

Tip on adding foreign key relationships in SQL:

You can establish a foreign key relation in your create table or with an alter table statment, like this:

ALTER TABLE [table1_name]
ADD CONSTRAINT [constraint_name]
FOREIGN KEY ([field(s) from table1])
REFERENCES [table2_name] ([field(s) from table2])
ON [DELETE CASCADE, or UPDATE, or other command];

(BTW, in the example above, for a cascade, generally table1 would be the child table, and table2 would be the parent table)

If you get an error like this:
Error Code : 1005
Can't create table '.\test\#sql-ac_195.frm' (errno: 150)

it may be the case that your referenced field is not guaranteed unique, which it must be. If you don't have a single unique field in that table, you can use multiple fields to specify the unique key.

Tuesday, May 1, 2007

Establishing primary key auto creates index

In MySQL, you can have a primary key constraint on one or more columns. Establishing a primary key causes MySQL to automatically make a unique key that consists of the columns specified in the primary key. You can verify the existence of this key by running the command:

SHOW INDEX FROM [table name];

In the resulting output, the column 'Non_unique' shows '0' if the column is unique, and '1' if the column is not unique.

SQLyog

I want to recommend the software I use for managing my MySQL databases. It is called SQLyog. You can find it online for download. It is free, although you can buy upgraded versions. It is great for running queries, manipulating data, imports, exports, and you can even manage indexes/triggers/etc.

Wednesday, April 18, 2007

Javascript and Java parseInt

Make sure to specify the radix (base) when using parseInt to convert from string to numerical values. Otherwise if your number is prefixed with zero, a non decimal base is assumed!

In Java:
n = Integer.parseInt(s,10);

In Javascript:
n = parseInt(s,10);

From w3Scholls.com Javascript reference website:

If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with "0x", the radix is 16 (hexadecimal)
  • If the string begins with "0", the radix is 8 (octal). This feature is deprecated
  • If the string begins with any other value, the radix is 10 (decimal)
So, you can see how important it is to specify the radix in the case where you are parsing date strings like 08-08-2007. Without specifying the radix, you'd end up with Oct 10, 2007!

Java string comparison tip

Today's tip is just a simple reminder to always use th "equals" function when comparting Strings in Java and not the comparison operator "==".

For example:

String a="a";

//Good
if (a.equals("a"))
fnA();

// Bad
if (a=="a") fnA();

The comparsion operator will compare the value of the String pointer which is probably not what is desired. Generally, you should use the equals function to compare the contents of teh string.

Tuesday, April 17, 2007

MySQL pagination

I'm writing some pagination code for my application. Turns out MySQL has a great method for accomplishing this.

In the select statement, just end it with
LIMIT [offset,] number_of_rows

Combine this with distinct ordering via "ORDER BY" on a unique indexed key, and you have the database doing the pagination without even having to keep open a connection.

Exceeding the 65535 bytes limit

Quite a while ago, I was working on my server-side Java & JSP application, when I encountered this error:

org.apache.jasper.JasperException: Unable to
compile class for JSP
Generated servlet error:

The code of method
_jspx_meth_html_html_0(javax.servlet.jsp.PageContext) is exceeding the 65535
bytes limit



Last time I hit a code size limit was 14 years ago when I was compiling computer programs in Borland Turbo C. Back then, I found the solution was to split the source files.

In this case, I was using the Struts framework, Eclipse/MyEclipse and trying to load the app with Apache Tomcat 5.5.9. What was happening is all the jsp files are being server-side compiled into too large a file. It won't help then to split the files into more jsp files, as these are all compiled in on the server side.

To solve the problem, I broke out all the javascript functions in the jsps into Javascript (.js) files. These cannot access server data, but they don't count towards the size limit as they are not compiled into the server. This worked; I haven't had any more such problems.

Be sure to put anything that does not have a jsp or struts tag into a JS file and not a JSP file. This will help your application performance and avoid file size caps.

You can also put the jsp content in an html file and use an html include:
<!--#include virtual="my_file.html" -->
or
<!--#include file="my_file.html" -->