Java developer new to Scala
Hi people.
My background is in java development so I thought scala should not be that hard to learn.
I think there isn't a better way to learn an new language than to have an actual problem to solve. So the issue I've looked into is to build a simple text parser to change data from MySQL dump file to a format that sqlite would consider correct for init of an database.
My goal of this mail isn't to ask for an solution. I just wanted to get some hints or tips on which concepts to look future into to improve my code. At the moment I've implemented code that could gather all the create statements and create the tables in sqlite.
I'll add my code and an example of an dump from mysql so you'll see what kind of data that could occur during this processing. The code is quite adhoc at the moment so I know I could do alot of simplifications. But because I don't know the language that well and don't have any scala developers closeby so I can't get hints, tips, tricks or pointers to improve either.
-------------------------------------------------------------------------
CUT
--------------------------------------------------------------------------
import scala.io._
import scala.collection.mutable.ArrayBuffer
class Statement {
var name = ""
var sqltype = ""
var notNullable = false
var default = ""
var autoincrement = false
var sqlStr = ""
var havePrimeKey = false
override def toString :String = {
if(sqlStr != "")
return sqlStr
if(autoincrement && sqltype.indexOf("int") != -1)
sqltype = "integer primary key autoincrement"
var str = name+" "+sqltype
if(notNullable)
str += " NOT NULL"
if(default != "")
str += " DEFAULT "+default
return str
}
}
object ReadFile extends Application {
// val s = Source.fromFile("dump.sql", "UTF-8")
val s = Source.fromFile("dump2.sql", "UTF-8")
var createArr = new Array[String](3)
var createStatArr = new ArrayBuffer[Statement]
var createStatement = false
val out = new java.io.FileWriter("c:/temp/sqltest/createtables.sql")
var table = ""
var havePrime = false
s.getLines.foreach( (line) => {
if(line.trim.toLowerCase.startsWith("create")) {
createStatement = true
createArr = line.split("`")
if(createArr.length > 1) {
table = createArr.apply(1)
out.write("drop table if exists '"+table+"';\n")
out.write("create table '"+table+"' (\n")
}
}
if(createStatement && line.trim.startsWith(")")) {
createStatement = false
var first = true
var havePrimeKey = false
createStatArr.foreach( (stat) => {
if(!first)
out.write(",\n")
out.write("\t"+stat.toString())
first = false
})
out.write("\n);\n\n")
createStatArr = new ArrayBuffer[Statement]
havePrime = false
}
if(createStatement) {
if(!(line.trim.toLowerCase.startsWith("create") ||
line.trim.toLowerCase.startsWith("primary key") ||
line.trim.toLowerCase.startsWith("unique key") ||
line.trim.toLowerCase.startsWith("key") ||
line.trim.toLowerCase.startsWith("constraint"))
) {
createArr = line.trim.split(' ')
var stat = new Statement
if(createArr.length > 1) {
stat.name = createArr.apply(0)
var linestr = createArr.apply(1)
if(linestr.endsWith(","))
linestr = linestr.substring(0, linestr.length()-1);
stat.sqltype = linestr
if(createArr.apply(1).startsWith("enum"))
stat.sqltype = "varchar(9)"
if(line.indexOf("NOT NULL") != -1)
stat.notNullable = true
if(line.toLowerCase().indexOf("auto_increment") != -1) {
stat.autoincrement = true
havePrime = true
}
for(i <- 0 until createArr.length) {
if(createArr.apply(i).compareToIgnoreCase("default") == 0) {
var linestr = createArr.apply(i+1)
if(linestr.endsWith(","))
linestr = linestr.substring(0, linestr.length()-1);
if(linestr.compareTo("CURRENT_TIMESTAMP") == 0)
linestr = "'0000-00-00 00:00:00'"
if(linestr.compareTo("'0000-00-00") == 0)
linestr = "'0000-00-00 00:00:00'"
stat.default = linestr
}
}
createStatArr += stat
}
}
if(
line.trim.toLowerCase.startsWith("primary key") ||
line.trim.toLowerCase.startsWith("constraint")
) {
var stat = new Statement
var linestr = line.trim()
stat.sqlStr = line.trim
if(linestr.endsWith(",")) {
stat.sqlStr = linestr.substring(0, linestr.length()-1);
}
if(!havePrime || linestr.indexOf("PRIMARY") == -1)
createStatArr += stat
}
}
})
out.close
}
------------------------------------------------------------------------- END
--------------------------------------------------------------------------
------------------------------------------------------------------------- CUT --------------------------------------------------------------------------
CREATE TABLE `table1` (
`oneId` int(11) unsigned NOT NULL AUTO_INCREMENT,
`anotherId` int(11) unsigned NOT NULL,
`creationDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`theblob` blob NOT NULL,
`hashKey` varchar(16) NOT NULL,
PRIMARY KEY (`oneId`),
UNIQUE KEY `hashKey` (`hashKey`)
) ENGINE=InnoDB AUTO_INCREMENT=240 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = <at> saved_cs_client */;
--
-- Dumping data for table `table1`
--
LOCK TABLES `table1` WRITE;
/*!40000 ALTER TABLE `table1` DISABLE KEYS */;
INSERT INTO `table1` VALUES (1,2,'2010-08-06 11:52:54',0xfaff80988e80eee7798,'fafasd'),(2,142456,'2010-07-30 11:11:29',0xfaff80988e80eee7798,'asfaew'),(3,220748,'2010-08-03 06:16:24',0xfaff80988e80eee7798,'ar5hffgadbf');
/*!40000 ALTER TABLE `table1` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `table2`
--
DROP TABLE IF EXISTS `table2`;
/*!40101 SET <at> saved_cs_client = <at> <at> character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `table2` (
`timestamp` bigint(20) NOT NULL DEFAULT '0',
`type` int(11) NOT NULL DEFAULT '0',
`id` int(11) NOT NULL DEFAULT '0',
`data1` int(11) NOT NULL DEFAULT '0',
`blob1` blob,
`blob2` blob,
`bool` tinyint(1) NOT NULL DEFAULT '0',
KEY `table2Key1` (`type`),
KEY `table2Key2` (`id`),
KEY `table2Key3` (`timestamp`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = <at> saved_cs_client */;
--------------------------------------------------------------------------END --------------------------------------------------------------------------
Best regards
Daniel