Class: Audrey::Engine::SQLite3::Importer::CSV

Inherits:
Audrey::Engine::SQLite3::Importer show all
Defined in:
lib/audrey/engine/sqlite3.rb

Overview

Audrey::Engine::SQLite3::Importer::CSV

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, fclass, path, opts = {}) ⇒ CSV


initialize



1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
# File 'lib/audrey/engine/sqlite3.rb', line 1217

def initialize(engine, fclass, path, opts={})
	# $tm.hrm
	require 'csv'
	@engine = engine
	@dbh = @engine.dbh
	@fclass = fclass.to_s
	@path = path
	@partition = 'm'
	@fields = {}
	@verbose = false
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



1211
1212
1213
# File 'lib/audrey/engine/sqlite3.rb', line 1211

def fields
  @fields
end

#verboseObject

Returns the value of attribute verbose.



1212
1213
1214
# File 'lib/audrey/engine/sqlite3.rb', line 1212

def verbose
  @verbose
end

Instance Method Details

#fclass_pksObject


fclass_pks



1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
# File 'lib/audrey/engine/sqlite3.rb', line 1412

def fclass_pks
	@bclasses = {}
	
	# object bclass primary key
	@obj_bclass_pk = set_fclass_pk('Audrey::Object::Hash')
	
	# object fclass primary key
	@obj_fclass_pk = set_fclass_pk(@fclass)
	
	# scalar base class
	@scalar_bclass_pk = set_fclass_pk('Audrey::Object::Scalar')
	
	# loop through scalar classes
	[String, Integer, Float, NilClass].each do |clss|
		@bclasses[clss] = set_fclass_pk(Audrey::RUBY_OBJECT_TO_AUDREY[clss]['fclass'].to_s)
	end
end

#prepare_statementsObject


prepare_statements



1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
# File 'lib/audrey/engine/sqlite3.rb', line 1335

def prepare_statements
	# $tm.hrm
	
	# hash
	sql = <<~'SQL'
	insert into
		base_objects(
			pk,
			partition,
			bclass_pk,
			fclass_pk
		)
		
		values(
			:pk,
			:partition,
			:bclass,
			:fclass
		);
	SQL
	
	# prepare
	@insert_hash = @dbh.prepare(sql)
	
	# scalar
	sql = <<~'SQL'
	insert into
		base_objects(
			pk,
			partition,
			bclass_pk,
			fclass_pk,
			scalar
		)
		
		values(
			:pk,
			:partition,
			:bclass,
			:fclass,
			:scalar
		);
	SQL
	
	# prepare
	@insert_scalar = @dbh.prepare(sql)
	
	# relationship
	sql = <<~'SQL'
	insert into relationships(
		pk,
		parent,
		child,
		hkey,
		ord
	)
	
	values(
		:pk,
		:parent,
		:child,
		:hkey,
		:ord
	)
	SQL
	
	# prepare
	@insert_relationship = @dbh.prepare(sql)
end

#runObject


run



1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
# File 'lib/audrey/engine/sqlite3.rb', line 1236

def run
	# $tm.hrm
	converters = {}
	count = 0
	
	# set fclass primary keys
	fclass_pks()
	
	# prepare statements
	prepare_statements()
	
	# build converters
	@fields.each do |k, dfn|
		converters[k] = Audrey::AUDREY_SCALAR_TO_RUBY[dfn.to_s]
	end
	
	# begin transaction
	@dbh.execute 'begin transaction'
	
	# TESTING
	$tm.timer('import') do
		# loop through CSV
		::CSV.foreach(@path, headers: true) do |row|
			# verbosify
			if @verbose
				count += 1
				puts count.to_s + ': ' + row.to_s
			end
			
			# init collection ord
			ord = 0
			
			# convert
			converters.each do |k, converter|
				if row.has_key?(k)
					row[k] = converter.call(row[k])
				end
			end
			
			# object pk
			obj_pk = Audrey::Util.uuid
			
			# add hash object
			@insert_hash.execute(
				'pk'=>obj_pk,
				'partition'=>@partition,
				'bclass'=>@obj_bclass_pk,
				'fclass'=>@obj_fclass_pk
			);
			
			# add scalars and relationships
			row.each do |k,v|
				ord += 1
				scalar_uuid = Audrey::Util.uuid
				
				# add scalar object
				@insert_scalar.execute(
					'pk'=>scalar_uuid,
					'partition'=>@partition,
					'bclass'=>@scalar_bclass_pk,
					'fclass'=>@bclasses[v.class],
					'scalar'=>v
				);
				
				# add relationship
				@insert_relationship.execute(
					'pk'=>Audrey::Util.uuid,
					'parent'=>obj_pk,
					'child'=>scalar_uuid,
					'hkey'=>k,
					'ord'=>ord
				);
			end
		end
	end
	
	# commit transaction
	$tm.timer('commit') do
		@dbh.execute 'commit'
	end
	
# close prepared statements
ensure
	$tm.puts 'close prepared statements'
	
	$tm.timer('close prepared statements') do
		@insert_hash and @insert_hash.close
		@insert_scalar and @insert_scalar.close
		@insert_relationship and @insert_relationship.close
	end
end

#set_fclass_pk(set_fclass) ⇒ Object


set_fclass_pk



1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
# File 'lib/audrey/engine/sqlite3.rb', line 1437

def set_fclass_pk(set_fclass)
	# $tm.hrm
	
	# ensure fclass record
	sql = <<~'SQL'
	insert or ignore into
	fclasses(name)
	values(:fclass);
	SQL
	
	# execute
	@dbh.execute sql, 'fclass'=>set_fclass
	
	# get fclass pk
	sql = 'select pk from fclasses where name=:fclass'
	return @dbh.get_first_value(sql, 'fclass'=>set_fclass)
end