SAN FRANCISCO (AP) -- Matt Cain didn't have much time to react at the ball coming straight for his head. The San Francisco pitcher tried to twist out of the way but hit the dirt within seconds, clutching his right hand in pain.
It was the most damage the Arizona Diamondbacks inflicted on Cain all afternoon.
Cain stayed in the game and finished his best outing in nearly a month, Nate Schierholtz added three hits and made an outstanding defensive play and the Giants beat the Diamondbacks 3-2 Thursday to complete a perfect homestand.
"Matty's so tough," San Francisco manager Bruce Bochy said. "He could have come out of that game but he wanted to stay in. He just kept going out there throwing up zeros."
Cain (3-2) scattered seven hits over 7 2-3 innings with seven strikeouts and one walk to win for the first time since April 15. He retired 17 of 18 hitters during one stretch and left to a rousing ovation from the sold-out crowd at AT&T Park after giving up back-to-back hits with two outs in the ninth.
Cain might not have made it that far had the pitch from Arizona starter Daniel Hudson missed a little more inside.
Cain, who was trying to put down a sacrifice bunt, tried to get out of the way but was hit on the back of his hand and immediately dropped to his knees as manager Bruce Bochy and trainer Dave Groeschner rushed out from the dugout.
Plate umpire Bill Miller ruled the ball hit Cain's bat and was a foul. After getting checked out, Cain stayed in the game and got the bunt down, moving Eli Whiteside from second to third. Andres Torres followed with a double and Aubrey Huff 's RBI single made it 3-0.
"Those are the ones where you really don't have a whole lot of options to get out of the way," Cain said. "The bat and my hand probably saved my face."
Whiteside doubled twice to help the surging Giants go 6-0 at home. They swept Colorado and then Arizona to extend their season-high winning streak to six games.
Gerardo Parra , Xavier Nady and Justin Upton had two hits apiece for Arizona, which has lost four straight.
The Diamondbacks scored twice in the eighth and had the tying run at third base with one out in the ninth before Javier Lopez recorded the final two outs for his first save in nearly five years.
"I feel like we got outplayed this series, I got outmanaged and they swept us," Diamondbacks manager Kirk Gibson said. "Those guys are the world champions and they showed it this series. We've got to just pick ourselves up."
Schierholtz, making his 10th start of the season, made big plays in the field and at the plate to give the Giants an early boost. He singled in Cody Ross in the second for a 1-0 lead, then threw out Parra at second base after he singled off the wall in right the next inning.
Schierholtz, who grounded out with the bases loaded in the third, also beat out an infield single in the sixth but was stranded at third.
The Giants tacked on two more and Cain made it stand up with his best outing in nearly a month. He left after giving up back-to-back singles with two outs in the eighth.
"That's definitely the homestand we needed," Cain said. "I got those guys in a little bit of a pickle in the eighth and the bullpen really didn't worry about it."
Reliever Jeremy Affeldt got Stephen Drew to hit a sharp grounder to first, but the ball glanced off Huff's glove for a hit. Parra easily scored from third to end the shutout bid.
Upton followed with an RBI single off Ramon Ramirez to cut the gap to 3-2, but Ramirez got Russell Branyan to ground out wit the tying run at second.
Ryan Roberts singled leading off the ninth for Arizona and took third on Chris Young 's pinch-hit single. Lopez, working in the closer role while Brian Wilson rested after pitching five straight games, struck out Kelly Johnson and then got Parra on a swinging strikeout to end the game. It was Lopez's first save since Sept. 12, 2006.
NOTES: Giants C Buster Posey was held out of the starting lineup after taking two foul balls off his mask on Wednesday night. "There are no concerns or issues," Bochy said. "We're just being a little careful." ... Giants RHP Santiago Casilla , on the DL since April 6 with an inflamed right elbow, is scheduled to start for Class-A San Jose on Friday. Bochy said the plan is for Casilla to throw 20-25 pitches, then be stretched out to two innings in his next outing. ... Parra's error on Torres' fly ball in the third was his first of the season. ... The Giants have sold out each of their 15 home games. ... This is San Francisco's first undefeated home stand of five games or more since May 25-30, 2004. ... Upton leads Arizona with 10 multi-hit games. ... Drew, who had two hits and an RBI, is batting .385 with runners in scoring position.